Skip to content

Instantly share code, notes, and snippets.

@ssshake
Last active December 24, 2017 03:01
Show Gist options
  • Save ssshake/5375190b5fd36e8647f28be712726c15 to your computer and use it in GitHub Desktop.
Save ssshake/5375190b5fd36e8647f28be712726c15 to your computer and use it in GitHub Desktop.
Youve Got Mail - Home Mailbox Notifier using ESP12E
/**
* YouveGotMail.ino
* Check your home mailbox for new mail and notify some service
* Created by https://github.com/ssshake
*
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <DNSServer.h> //Local DNS Server used for redirecting all requests to the configuration portal
#include <ESP8266WebServer.h> //Local WebServer used to serve the configuration portal
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager WiFi Configuration Magic
#define USE_SERIAL Serial
String url = "http://10.0.0.10/youvegotmail.html";
ESP8266WiFiMulti WiFiMulti;
int sensorState = 0;
int sensor = D1;
int analogSensor = A0;
int emitter = D7;
int receiverGND = D2;
String mailboxstatus = "EMPTY";
//Unused for now
const char* NTPHost = "time.nist.gov"; // Round-robin DAYTIME protocol
int ln = 0;
String TimeDate = "";
void configModeCallback (WiFiManager *myWiFiManager) {
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
//if you used auto generated SSID, print it
Serial.println(myWiFiManager->getConfigPortalSSID());
}
void setup() {
USE_SERIAL.begin(74880);
// USE_SERIAL.setDebugOutput(true);
USE_SERIAL.println("Awake");
WiFiManager wifiManager;
//reset settings - for testing
//wifiManager.resetSettings();
//set callback that gets called when connecting to previous WiFi fails, and enters Access Point mode
wifiManager.setAPCallback(configModeCallback);
if(!wifiManager.autoConnect("YouveGotMail")) {
Serial.println("failed to connect and hit timeout");
//reset and try again, or maybe put it to deep sleep
ESP.reset();
delay(1000);
}
pinMode(emitter, OUTPUT);
pinMode(receiverGND, OUTPUT);
pinMode(sensor, INPUT);
digitalWrite(sensor, HIGH);
}
void loop() {
// digitalWrite(LED_BUILTIN, LOW);
// wait for WiFi connection
digitalWrite(emitter, LOW);
digitalWrite(receiverGND, LOW);
if((WiFiMulti.run() == WL_CONNECTED)) {
USE_SERIAL.println("Checking For Mail");
// sensorState = digitalRead(sensor);
sensorState = analogRead(analogSensor);
USE_SERIAL.println("SENSOR STATE:");
USE_SERIAL.println(sensorState);
//if (sensorState == LOW) {
if (sensorState > 900) {
USE_SERIAL.println("You've Got Mail");
mailboxstatus = "FULL";
}
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
http.begin(url + "?MAILBOXSTATUS=" + mailboxstatus + "&UID=" + WiFi.macAddress() + "&SIGNAL=" + sensorState); //HTTP
USE_SERIAL.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if(httpCode > 0) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode);
// file found at server
if(httpCode == HTTP_CODE_OK) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
}
http.end();
USE_SERIAL.println("Sleep");
ESP.deepSleep(15e6, WAKE_RF_DEFAULT);
}
// digitalWrite(emitter, LOW);
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment