Advertisement
lgarner972

smarkleak_1_0_0

Jan 27th, 2025 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.85 KB | None | 0 0
  1. /*
  2. SmartLeak
  3.  
  4. ESP8266 based leak sensor and relay control.
  5.  
  6. Feature list:
  7. - Wifi network connectivity
  8. - MQTT publish-subscribe
  9. - 110V AC relay control
  10.  
  11. created Sept 22, 2019
  12. by xxx
  13.  
  14. Revision history
  15. 1.0.0 - Initial release
  16. 1.0.1 - Relay control
  17. 1.0.2 - Variable hostname
  18. 1.1.0 - OTA firmware update
  19. */
  20.  
  21. #include <ArduinoOTA.h>
  22. #include <ESP8266WiFi.h>
  23. #include <PubSubClient.h>
  24.  
  25. //
  26. #define BUTTON_PIN 0 //GPIO 0
  27. #define SENSOR_LEAK_PIN 3 //GPIO 3 (RX)
  28. #define RELAY_PIN 12 //GPIO 12
  29. #define LED_PIN 13 //GPIO 13
  30.  
  31. // Update these with values suitable for your network.
  32. const char* ssid = "xxx";
  33. const char* password = "xxx";
  34. const char* mqtt_server = "xxx";
  35. const char* charHostname = "xxx";
  36.  
  37. WiFiClient espClient;
  38. PubSubClient client(espClient);
  39.  
  40. // Timer intervals
  41. #define sysInterval 60000
  42. #define leakInterval 30000
  43. #define buttonInterval 500
  44. #define buttonDelay 2000
  45.  
  46. //Timer variable definitions
  47. long currentMillis;
  48. long leakMillis = 0;
  49. long sysMillis = 0;
  50. long buttonMillis = 0;
  51.  
  52. // Global variables
  53. long lastMsg = 0;
  54. bool leakState = false;
  55. String strTopic;
  56. String strPayload;
  57. char charPayload[10];
  58. char charTopic[50];
  59. float sysUptime = 0;
  60. bool relayState = false;
  61. bool relayPrevious = relayState;
  62. bool buttonState = false;
  63. String strHostname = String((char*)charHostname);
  64.  
  65. void setup_wifi() {
  66. delay(100);
  67. // We start by connecting to a WiFi network
  68. WiFi.mode(WIFI_STA);
  69. WiFi.hostname(charHostname);
  70. WiFi.begin(ssid, password);
  71. while (WiFi.status() != WL_CONNECTED)
  72. {
  73. digitalWrite(LED_PIN, HIGH);
  74. delay(500);
  75. digitalWrite(LED_PIN, LOW);
  76. }
  77. randomSeed(micros());
  78. }
  79.  
  80. void callback(char* topic, byte* payload, unsigned int length)
  81. {
  82. payload[length] = '\0';
  83. strTopic = String((char*)topic);
  84. strPayload = String((char*)payload);
  85.  
  86. if (strTopic == "openhab/" + strHostname + "/relay") {
  87. if (strPayload == "OFF") {
  88. relayState = false;
  89. }
  90. if (strPayload == "ON") {
  91. relayState = true;
  92. }
  93. }
  94.  
  95. if (strTopic == "openhab/" + strHostname + "/restart") {
  96. ESP.restart();
  97. }
  98. } //end callback
  99.  
  100. void reconnect() {
  101. // Loop until we're reconnected
  102. while (!client.connected())
  103. {
  104. // Create a random client ID
  105. String clientId = "ESP8266Client-";
  106. clientId += String(random(0xffff), HEX);
  107.  
  108. if (client.connect(clientId.c_str()))
  109. {
  110. //once connected to MQTT broker, subscribe command if any
  111. client.subscribe("openhab/#");
  112. } else {
  113. // Wait 6 seconds before retrying
  114. delay(6000);
  115. }
  116. }
  117. } //end reconnect()
  118.  
  119. void setup() {
  120. pinMode(LED_PIN, OUTPUT);
  121. pinMode(SENSOR_LEAK_PIN, INPUT);
  122. pinMode(BUTTON_PIN, INPUT);
  123. pinMode(RELAY_PIN, OUTPUT);
  124.  
  125. setup_wifi();
  126. client.setServer(mqtt_server, 1883);
  127. client.setCallback(callback);
  128.  
  129. ArduinoOTA.setHostname(charHostname);
  130. ArduinoOTA.begin();
  131. }
  132.  
  133. void loop() {
  134. if (!client.connected()) {
  135. reconnect();
  136. }
  137.  
  138. client.loop();
  139.  
  140. ArduinoOTA.handle();
  141.  
  142. currentMillis = millis();
  143.  
  144. if (currentMillis - sysMillis > sysInterval) {
  145. sysMillis = currentMillis;
  146.  
  147. strTopic = strHostname + "/relay/state";
  148. strTopic.toCharArray(charTopic, 50);
  149. if (relayState) {
  150. client.publish(charTopic, "ON");
  151. } else {
  152. client.publish(charTopic, "OFF");
  153. }
  154.  
  155. sysUptime += 0.000694444;
  156. strTopic = strHostname + "/sysinfo/uptime";
  157. strTopic.toCharArray(charTopic, 50);
  158. strPayload = String(sysUptime, 1);
  159. strPayload.toCharArray(charPayload, 10);
  160. client.publish(charTopic, charPayload);
  161.  
  162. int sysRssi = WiFi.RSSI();
  163. strTopic = strHostname + "/sysinfo/rssi";
  164. strTopic.toCharArray(charTopic, 50);
  165. strPayload = String(sysRssi);
  166. strPayload.toCharArray(charPayload, 10);
  167. client.publish(charTopic, charPayload);
  168. }
  169.  
  170. if (currentMillis - leakMillis > leakInterval) {
  171. leakMillis = currentMillis;
  172.  
  173. strTopic = strHostname + "/sensor/leak";
  174. strTopic.toCharArray(charTopic, 50);
  175. leakState = digitalRead(SENSOR_LEAK_PIN);
  176. if (leakState) {
  177. client.publish(charTopic, "ALERT");
  178. } else {
  179. client.publish(charTopic, "CLEAR");
  180. }
  181. }
  182.  
  183. if (currentMillis - buttonMillis > buttonInterval) {
  184. buttonMillis = currentMillis;
  185.  
  186. buttonState = !digitalRead(BUTTON_PIN);
  187. if (buttonState) {
  188. buttonMillis = currentMillis + buttonDelay;
  189. relayState = !relayPrevious;
  190. }
  191.  
  192. if (relayState != relayPrevious) {
  193. strTopic = strHostname + "/relay/state";
  194. strTopic.toCharArray(charTopic, 50);
  195.  
  196. if (relayState) {
  197. digitalWrite(RELAY_PIN, HIGH);
  198. client.publish(charTopic, "ON");
  199. } else {
  200. digitalWrite(RELAY_PIN, LOW);
  201. client.publish(charTopic, "OFF");
  202. }
  203. relayPrevious = relayState;
  204. }
  205. }
  206. }
  207.  
  208.  
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement