TolentinoCotesta

MQTT deep sleep

Jan 1st, 2021 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. WiFiClient espClient;
  5. PubSubClient client(espClient);
  6.  
  7. // Create an object of the class Bsec
  8. //Bsec iaqSensor;
  9.  
  10. #define BUILTIN_LED 4
  11.  
  12. #define SSID "xxxxxx"
  13. #define PWD "xxxxxx"
  14.  
  15. #define mqtt_user "user"
  16. #define mqtt_pwd "password"
  17.  
  18.  
  19. // env variable
  20. String temperature;
  21. String humidity;
  22. String pressure;
  23. String CO2;
  24. String gasResistance;
  25.  
  26. bool can_sleep = false;
  27.  
  28. void WiFiStationGotIP(WiFiEvent_t event, WiFiEventInfo_t info){
  29.   Serial.print("WiFi connected. IP: ");
  30.   Serial.println(WiFi.localIP());
  31.  
  32.   // Connect to MQTT broker (with timeout)
  33.   bool connected = connectToBroker();
  34.   unsigned long timeout = millis();
  35.   while ( !connected ) {  
  36.     delay(1000);    
  37.     connected = connectToBroker();
  38.     if(millis() - timeout > 5000){
  39.       connected = false;
  40.       break;
  41.     }
  42.   }
  43.  
  44.   // Publish data
  45.   if(connected) {
  46.     publishData();
  47.     // Once connected at least one time, eSP32 can sleep
  48.     can_sleep = true;
  49.   }
  50. }
  51.  
  52.  
  53. bool connectToBroker(){  
  54.   Serial.print("Attempting MQTT connection...");
  55.   // Create a random client ID
  56.   String clientId = "ESP8266Client-";
  57.   clientId += String(random(0xffff), HEX);
  58.  
  59.   // Attempt to connect
  60.   if (client.connect(clientId.c_str(), mqtt_user, mqtt_pwd)) {
  61.     Serial.println("connected");
  62.     return true;
  63.   }
  64.   else {
  65.     // Error on connection (host unreachable)
  66.     Serial.print("failed, rc=");
  67.     Serial.print(client.state());    
  68.     delay(100);
  69.   }
  70.   return false;
  71. }
  72.  
  73.  
  74. void publishData(){
  75.  
  76.   #define MSG_BUFFER_SIZE  (50)
  77.   char msg[MSG_BUFFER_SIZE];
  78.   static int value = 0;
  79.   snprintf (msg, MSG_BUFFER_SIZE, "hello world #%ld", ++value);
  80.   Serial.print("Publish message: ");
  81.   Serial.println(msg);
  82.  
  83.   temperature = 25.6 + random(5);
  84.   humidity = 45 + random(10);
  85.   Serial.println("Umidità:"+(String)temperature);
  86.   Serial.println("Temperatura:"+(String)humidity);
  87.  
  88.   client.publish("esp32/humidity", humidity.c_str());
  89.   client.publish("esp32/temperature", temperature.c_str());
  90.  
  91.   //wait a little before sleep, so data can be sent to broker
  92.   delay(200);
  93. }
  94.  
  95.  
  96. void setup() {
  97.   Serial.begin(115200);
  98.  
  99.   Serial.print("Connecting to ");
  100.   Serial.println(SSID);
  101.  
  102.   WiFi.onEvent(WiFiStationGotIP, SYSTEM_EVENT_STA_GOT_IP);
  103.   WiFi.begin(SSID, PWD);  
  104.  
  105.   // Test on local LAN mosquitto broker
  106.   IPAddress ip;
  107.   ip.fromString("192.168.2.81");
  108.   client.setServer(ip, 1883);
  109. }
  110.  
  111.  
  112.  
  113. void loop() {
  114.  
  115.   if(can_sleep){
  116.     // ESP32 wakes up every 30 seconds
  117.     esp_sleep_enable_timer_wakeup(10000000);
  118.     Serial.println("Going to deep-sleep now");
  119.     Serial.flush();    
  120.  
  121.     esp_deep_sleep_start();
  122.     while (WiFi.status() != WL_CONNECTED) {;}
  123.  
  124.   }
  125.  
  126. }
Add Comment
Please, Sign In to add comment