safwan092

Untitled

Dec 18th, 2021 (edited)
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include "Adafruit_MQTT.h"
  3. #include "Adafruit_MQTT_Client.h"
  4. #include "DHT.h"
  5.  
  6. #define water A0
  7. #define buzzer D6
  8. #define DHTPIN D7
  9. #define DHTTYPE DHT11
  10.  
  11. DHT dht(DHTPIN, DHTTYPE);
  12. /************************* WiFi Access Point *********************************/
  13. #define WLAN_SSID "network"
  14. #define WLAN_PASS "123456789"
  15. /************************* Adafruit.io Setup *********************************/
  16. #define AIO_SERVER "io.adafruit.com"
  17. #define AIO_SERVERPORT 1883 // use 8883 for SSL
  18. #define AIO_USERNAME "Tezkar786"
  19. #define AIO_KEY "aio_OkNq45ypm1PcwDoUuLAtrA4Oor7H"
  20. /************ Global State (you don't need to change this!) ******************/
  21.  
  22. WiFiClient client;
  23. Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
  24.  
  25. /****************************** Feeds ***************************************/
  26.  
  27. // Setup a feed called 'photocell' for publishing.
  28. // Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
  29. Adafruit_MQTT_Publish temp = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temp");
  30. Adafruit_MQTT_Publish hum = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/hum");
  31. Adafruit_MQTT_Publish wL = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/wL");
  32. Adafruit_MQTT_Publish inputonoff = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/of");
  33.  
  34. // Setup a feed called 'onoff' for subscribing to changes.
  35. Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
  36.  
  37. /*************************** Sketch Code ************************************/
  38.  
  39. // Bug workaround for Arduino 1.6.6, it seems to need a function declaration
  40. // for some reason (only affects ESP8266, likely an arduino-builder bug).
  41. void MQTT_connect();
  42.  
  43. void setup() {
  44. Serial.begin(115200);
  45. pinMode(water, INPUT);
  46. pinMode(buzzer, OUTPUT);
  47. dht.begin();
  48. delay(10);
  49. Serial.println(F("Adafruit MQTT demo"));
  50. // Connect to WiFi access point.
  51. Serial.println(); Serial.println();
  52. Serial.print("Connecting to ");
  53. Serial.println(WLAN_SSID);
  54.  
  55. WiFi.begin(WLAN_SSID, WLAN_PASS);
  56. while (WiFi.status() != WL_CONNECTED) {
  57. delay(500);
  58. Serial.print(".");
  59. }
  60. Serial.println();
  61.  
  62. Serial.println("WiFi connected");
  63. Serial.println("IP address: "); Serial.println(WiFi.localIP());
  64.  
  65. // Setup MQTT subscription for onoff feed.
  66. mqtt.subscribe(&onoffbutton);
  67. }
  68.  
  69. uint32_t x = 0;
  70.  
  71. void loop() {
  72. MQTT_connect();
  73. Adafruit_MQTT_Subscribe *subscription;
  74. while ((subscription = mqtt.readSubscription(5000))) {
  75. if (subscription == &onoffbutton) {
  76. Serial.print(F("Got: "));
  77. Serial.println((char *)onoffbutton.lastread);
  78. }
  79. }
  80.  
  81. // Now we can publish stuff!
  82. //****************************************************
  83. float t = dht.readTemperature();
  84. Serial.print(F("\nSending temp val "));
  85. Serial.print(t);
  86. Serial.print("...");
  87. if (! temp.publish(t)) {
  88. Serial.println(F("Failed"));
  89. } else {
  90. Serial.println(F("OK!"));
  91. }
  92. //****************************************************
  93. //****************************************************
  94. float h = dht.readHumidity();
  95. Serial.print(F("\nSending hum val "));
  96. Serial.print(h);
  97. Serial.print("...");
  98. if (! hum.publish(h)) {
  99. Serial.println(F("Failed"));
  100. } else {
  101. Serial.println(F("OK!"));
  102. }
  103. //****************************************************
  104. //****************************************************
  105.  
  106. int waterLevel = analogRead(water);
  107. if(waterLevel<20){waterLevel=0;}
  108. waterLevel = map(waterLevel,0,1023,0,100);
  109. Serial.print(F("\nSending water val "));
  110. Serial.print(waterLevel);
  111. Serial.print("...");
  112. if (! wL.publish(waterLevel)) {
  113. Serial.println(F("Failed"));
  114. } else {
  115. Serial.println(F("OK!"));
  116. }
  117.  
  118. if (waterLevel > 0 && waterLevel <= 100) {
  119. int onStatus = 10;
  120. soundAlarm();
  121. if (! inputonoff.publish(onStatus)) {
  122. Serial.println(F("Failed"));
  123. } else {
  124. Serial.println(F("OK!"));
  125. }
  126. } else {
  127. int onStatus = 0;
  128. if (! inputonoff.publish(onStatus)) {
  129. Serial.println(F("Failed"));
  130. } else {
  131. Serial.println(F("OK!"));
  132. }
  133. }
  134. //****************************************************
  135. delay(5000);
  136. }//end of Loop
  137.  
  138. // Function to connect and reconnect as necessary to the MQTT server.
  139. // Should be called in the loop function and it will take care if connecting.
  140. void MQTT_connect() {
  141. int8_t ret;
  142.  
  143. // Stop if already connected.
  144. if (mqtt.connected()) {
  145. return;
  146. }
  147.  
  148. Serial.print("Connecting to MQTT... ");
  149.  
  150. uint8_t retries = 3;
  151. while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
  152. Serial.println(mqtt.connectErrorString(ret));
  153. Serial.println("Retrying MQTT connection in 5 seconds...");
  154. mqtt.disconnect();
  155. delay(5000); // wait 5 seconds
  156. retries--;
  157. if (retries == 0) {
  158. // basically die and wait for WDT to reset me
  159. while (1);
  160. }
  161. }
  162. Serial.println("MQTT Connected!");
  163. }
  164.  
  165. void soundAlarm() {
  166. digitalWrite(buzzer, HIGH); //Set PIN 8 feet as HIG67890]\H = 5 v
  167. delay(2000); //Set the delay time2000ms
  168. digitalWrite(buzzer, LOW); //Set PIN 8 feet for LOW = 0 v
  169. delay(2000); //Set the delay time2000ms
  170. }
Add Comment
Please, Sign In to add comment