Advertisement
DevUModerator

Untitled

Dec 9th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include "Gsender.h"
  3. #include "EmonLib.h" // Include Emon Library
  4.  
  5. #pragma region Globals
  6. const char* ssid = "YourNetwor'sName"; // WIFI network name
  7. const char* password = "YourWiFiPasword"; // WIFI network password
  8. uint8_t connection_state = 0; // Connected to WIFI or not
  9. uint16_t reconnect_interval = 10000; // If not connected wait time to try again
  10. #pragma endregion Globals
  11.  
  12. EnergyMonitor emon1; // Create an instance
  13.  
  14. uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
  15. {
  16. static uint16_t attempt = 0;
  17. Serial.print("Connecting to ");
  18. if(nSSID) {
  19. WiFi.begin(nSSID, nPassword);
  20. Serial.println(nSSID);
  21. } else {
  22. WiFi.begin(ssid, password);
  23. Serial.println(ssid);
  24. }
  25.  
  26. uint8_t i = 0;
  27. while(WiFi.status()!= WL_CONNECTED && i++ < 50)
  28. {
  29. delay(200);
  30. Serial.print(".");
  31. }
  32. ++attempt;
  33. Serial.println("");
  34. if(i == 51) {
  35. Serial.print("Connection: TIMEOUT on attempt: ");
  36. Serial.println(attempt);
  37. if(attempt % 2 == 0)
  38. Serial.println("Check if access point available or SSID and Password\r\n");
  39. return false;
  40. }
  41. Serial.println("Connection: ESTABLISHED");
  42. Serial.print("Got IP address: ");
  43. Serial.println(WiFi.localIP());
  44. return true;
  45. }
  46.  
  47. void Awaits()
  48. {
  49. uint32_t ts = millis();
  50. while(!connection_state)
  51. {
  52. delay(50);
  53. if(millis() > (ts + reconnect_interval) && !connection_state){
  54. connection_state = WiFiConnect();
  55. ts = millis();
  56. }
  57. }
  58. }
  59.  
  60. void sendEmail()
  61. {
  62. connection_state = WiFiConnect();
  63. if(!connection_state) // if not connected to WIFI
  64. Awaits(); // constantly trying to connect
  65.  
  66. Gsender *gsender = Gsender::Instance(); // Getting pointer to class instance
  67. String subject = "Washer is OFF."; // Subject line of email
  68. if(gsender->Subject(subject)->Send("AddressToSendTo.com", "Setup test")) {
  69. Serial.println("Message send.");
  70. } else {
  71. Serial.print("Error sending message: ");
  72. Serial.println(gsender->getError());
  73. }
  74.  
  75. }
  76.  
  77. void setup()
  78. {
  79. Serial.begin(115200);
  80. delay(2000);
  81. emon1.current(A0, 111.1);// Current: input pin, cal
  82.  
  83. }
  84.  
  85. double Irms = emon1.calcIrms(1480); // Calculate Irms only
  86. //int ON = 0;
  87. int washer=0;
  88.  
  89. void loop(){
  90.  
  91. delay(2000);
  92. Irms = emon1.calcIrms(1480); // Calculate Irms only
  93. // This first if statement allows the current to
  94. // settle as there is some garbage displayed
  95. // in the com port serial monitor port.
  96. if (Irms > 3)
  97. {
  98. Serial.println("Current settling");
  99. }
  100. // This statement is executed only if the conditions
  101. // are met. With washer = 0 it will be bypassed even
  102. // if the current conditions are met.
  103. if (Irms > 2.4 && Irms <=2.5 && washer==1)
  104. {
  105. Serial.println("Contents Washer ON");
  106. washer = 0;
  107. Serial.println(washer);
  108. Serial.println(Irms);
  109.  
  110. }
  111. // Conditions zero current and washer equaling zer0
  112. // is the condition that must be met to carry out
  113. // the push statement(which has not been written
  114. // yet and help is needed to get this working
  115. // I would like the IFTTT code to live in the if statement
  116. // below and call the IFTTT.
  117. if (Irms < 0.5 && washer==0)
  118.  
  119. {
  120. Serial.println("Contents Washer OFF");
  121. washer = 1;
  122. Serial.println(washer);
  123. Serial.println(Irms);
  124. sendEmail();
  125. }
  126. delay(1000);
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement