Advertisement
Guest User

TANK LEVEL ESP-01

a guest
Jul 12th, 2019
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // . 13 July 2019
  2. // . Dev: @crmrmn / carmineromano2000@gmail.com
  3. // . Level Tank Monitor v0.1
  4.  
  5. /*
  6. Per ridurre ulteriormente il consumo energetico,
  7. si suggerisce di rissaldare il led power [RED] (circa 8mA assorbiti).
  8. E' inoltre suggerito di disabilitare il "led" usato nello sketch
  9. come debugger in quanto non รจ usata la com UART.
  10. Si sono scelti i GPIO della USART in quanto rilevato problema
  11. con il DEEP SLEEP usando GPIO2.
  12. */
  13. #include <ESP8266WiFi.h>
  14. #include <PubSubClient.h>
  15.  
  16. #define sensor 3 //Collegare sensore a GPIO3
  17. #define led    1 //Led integrato       GPIO1
  18.  
  19. const char* ssid = "ssid";                 //Nome rete wifi
  20. const char* password =  "password";        //Password wifi
  21. const char* mqttServer = "urlserver";      //Url server
  22. const int mqttPort = 11964;                //Porta mqtt
  23. const char* mqttUser = "user";             //Utente mqtt
  24. const char* mqttPassword = "psswd";        //Password mqtt
  25.  
  26. const int sleepTimeS = 10; //Ogni quanto inviare la lettura [sec]
  27.  
  28. WiFiClient espClient;
  29. PubSubClient client(espClient);
  30.  
  31. void setup() {
  32.  
  33.   pinMode(sensor, INPUT_PULLUP);  
  34.   pinMode(led, OUTPUT);
  35.  
  36.   WiFi.begin(ssid, password);
  37.   while (WiFi.status() != WL_CONNECTED) {
  38.     digitalWrite(led,  !digitalRead(led));
  39.     delay(200);
  40.   }
  41.  
  42.   client.setServer(mqttServer, mqttPort);
  43.  
  44.   while (!client.connected()) {
  45.     digitalWrite(led, LOW);
  46.     if (client.connect("ESP8266Client", mqttUser, mqttPassword )) digitalWrite(led, HIGH);
  47.     else delay(2000);
  48.    
  49.   }
  50. }
  51.  
  52. void loop() {
  53.   client.loop();
  54.  
  55.   if(!digitalRead(sensor)) {
  56.     client.publish("esp/tanklevel", "full");
  57.     client.subscribe("esp/tanklevel");
  58.     }
  59.    else{
  60.     client.publish("esp/tanklevel", "empty");
  61.     client.subscribe("esp/tanklevel");
  62.     }
  63.  
  64.   delay(1000);
  65.   ESP.deepSleep(sleepTimeS * 1000000);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement