// ================================================================ // MAILBOX MONITOR // // ================================================================ #include #include //https://github.com/esp8266/Arduino #include //needed for library #include #include #include //https://github.com/tzapu/WiFiManager // Define deep sleep #define uS_TO_m_Factor 60000000 const int sleepTimeM=5; // 5 minutes // Prox Sensor Input GPIO5 (pin D1) #define prox 5 // Test LED GPIO4 (pin D2) #define testled 4 const String mailin = "http://www.yoursite.com/mail_alert/?mail=yes"; const String mailout = "http://www.yoursite.com/mail_alert/?mail=no"; void setup() { // put your setup code here, to run once: Serial.begin(115200); pinMode(prox, INPUT_PULLUP); pinMode(testled, OUTPUT); //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around WiFiManager wifiManager; //reset saved settings //wifiManager.resetSettings(); //set custom ip for portal //wifiManager.setAPStaticIPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0)); //fetches ssid and pass from eeprom and tries to connect //if it does not connect it starts an access point with the specified name //here "AutoConnectAP" //and goes into a blocking loop awaiting configuration wifiManager.autoConnect("AutoConnectAP"); //or use this for auto generated name ESP + ChipID //wifiManager.autoConnect(); delay(1000); if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status Serial.println("connected...yeey :)"); } } void loop() { // put your main code here, to run repeatedly: delay(1000); // Read the prox switch. if(digitalRead(prox) == LOW){ if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status HTTPClient http; http.setTimeout(3000); http.begin(mailin); //Specify the URL int httpCode = http.GET(); //Make the request if (httpCode > 0) { //Check for the returning code String payload = http.getString(); StaticJsonBuffer<100> jsonBuffer; JsonObject& object = jsonBuffer.parseObject(payload); String stat = object["STATUS"]; Serial.println("Mail Inserted Into Mailbox"); digitalWrite(testled, HIGH); delay(250); digitalWrite(testled, LOW); delay(250); digitalWrite(testled, HIGH); delay(250); digitalWrite(testled, LOW); delay(250); ESP.deepSleep(sleepTimeM * uS_TO_m_Factor, WAKE_RF_DEFAULT); } http.end(); //Free the resources } } if(digitalRead(prox) == HIGH){ if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status HTTPClient http; http.setTimeout(3000); http.begin(mailout); //Specify the URL int httpCode = http.GET(); //Make the request if (httpCode > 0) { //Check for the returning code String payload = http.getString(); StaticJsonBuffer<100> jsonBuffer; JsonObject& object = jsonBuffer.parseObject(payload); String stat = object["STATUS"]; Serial.println("Mail Removed From Mailbox"); digitalWrite(testled, HIGH); delay(500); digitalWrite(testled, LOW); delay(500); ESP.deepSleep(sleepTimeM * uS_TO_m_Factor, WAKE_RF_DEFAULT); } http.end(); //Free the resources } } }