Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include "Gsender.h"
- #include "EmonLib.h" // Include Emon Library
- #pragma region Globals
- const char* ssid = "YourNetwor'sName"; // WIFI network name
- const char* password = "YourWiFiPasword"; // WIFI network password
- uint8_t connection_state = 0; // Connected to WIFI or not
- uint16_t reconnect_interval = 10000; // If not connected wait time to try again
- #pragma endregion Globals
- EnergyMonitor emon1; // Create an instance
- uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
- {
- static uint16_t attempt = 0;
- Serial.print("Connecting to ");
- if(nSSID) {
- WiFi.begin(nSSID, nPassword);
- Serial.println(nSSID);
- } else {
- WiFi.begin(ssid, password);
- Serial.println(ssid);
- }
- uint8_t i = 0;
- while(WiFi.status()!= WL_CONNECTED && i++ < 50)
- {
- delay(200);
- Serial.print(".");
- }
- ++attempt;
- Serial.println("");
- if(i == 51) {
- Serial.print("Connection: TIMEOUT on attempt: ");
- Serial.println(attempt);
- if(attempt % 2 == 0)
- Serial.println("Check if access point available or SSID and Password\r\n");
- return false;
- }
- Serial.println("Connection: ESTABLISHED");
- Serial.print("Got IP address: ");
- Serial.println(WiFi.localIP());
- return true;
- }
- void Awaits()
- {
- uint32_t ts = millis();
- while(!connection_state)
- {
- delay(50);
- if(millis() > (ts + reconnect_interval) && !connection_state){
- connection_state = WiFiConnect();
- ts = millis();
- }
- }
- }
- void sendEmail()
- {
- connection_state = WiFiConnect();
- if(!connection_state) // if not connected to WIFI
- Awaits(); // constantly trying to connect
- Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance
- String subject = "Washer is OFF."; // Subject line of email
- if(gsender->Subject(subject)->Send("AddressToSendTo.com", "Setup test")) {
- Serial.println("Message send.");
- } else {
- Serial.print("Error sending message: ");
- Serial.println(gsender->getError());
- }
- }
- void setup()
- {
- Serial.begin(115200);
- delay(2000);
- emon1.current(A0, 111.1);// Current: input pin, cal
- }
- double Irms = emon1.calcIrms(1480); // Calculate Irms only
- //int ON = 0;
- int washer=0;
- void loop(){
- delay(2000);
- Irms = emon1.calcIrms(1480); // Calculate Irms only
- // This first if statement allows the current to
- // settle as there is some garbage displayed
- // in the com port serial monitor port.
- if (Irms > 3)
- {
- Serial.println("Current settling");
- }
- // This statement is executed only if the conditions
- // are met. With washer = 0 it will be bypassed even
- // if the current conditions are met.
- if (Irms > 2.4 && Irms <=2.5 && washer==1)
- {
- Serial.println("Contents Washer ON");
- washer = 0;
- Serial.println(washer);
- Serial.println(Irms);
- }
- // Conditions zero current and washer equaling zer0
- // is the condition that must be met to carry out
- // the push statement(which has not been written
- // yet and help is needed to get this working
- // I would like the IFTTT code to live in the if statement
- // below and call the IFTTT.
- if (Irms < 0.5 && washer==0)
- {
- Serial.println("Contents Washer OFF");
- washer = 1;
- Serial.println(washer);
- Serial.println(Irms);
- sendEmail();
- }
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement