Rickth

MQTT & SLEEP

Apr 6th, 2021
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3. #include <Adafruit_Sensor.h>
  4. #include <Adafruit_BME280.h>
  5. #include <ESP8266WiFi.h>
  6. #include <Ticker.h>
  7. #include <AsyncMqttClient.h>
  8.  
  9. #define SEALEVELPRESSURE_HPA (1013.25)
  10. #define WIFI_SSID "??????????"
  11. #define WIFI_PASSWORD "????????????"
  12.  
  13. // Raspberri Pi Mosquitto MQTT Broker
  14. #define MQTT_HOST IPAddress(192, 168, 42,200)
  15. #define MQTT_PORT 1883
  16.  
  17. // Temperature MQTT Topics
  18. #define MQTT_PUB_TEMP "esp/bme280/temperature"
  19. #define MQTT_PUB_HUM "esp/bme280/humidity"
  20. #define MQTT_PUB_PRES "esp/bme280/pressure"
  21.  
  22. // BME280 I2C
  23. Adafruit_BME280 bme;
  24. // Variables to hold sensor readings
  25. float temp;
  26. float hum;
  27. float pres;
  28.  
  29. AsyncMqttClient mqttClient;
  30. Ticker mqttReconnectTimer;
  31. WiFiEventHandler wifiConnectHandler;
  32. WiFiEventHandler wifiDisconnectHandler;
  33. Ticker wifiReconnectTimer;
  34.  
  35.  
  36. void connectToWifi() {
  37.   Serial.println("Connecting to Wi-Fi...");
  38.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  39. }
  40. void connectToMqtt() {
  41.   Serial.println("Connecting to MQTT...");
  42.   mqttClient.connect();
  43. }
  44.  
  45. void onWifiConnect(const WiFiEventStationModeGotIP& event) {
  46.   Serial.println("Connected to Wi-Fi.");
  47.   connectToMqtt();
  48. }
  49.  
  50. void onWifiDisconnect(const WiFiEventStationModeDisconnected& event) {
  51.   Serial.println("Disconnected from Wi-Fi.");
  52.   mqttReconnectTimer.detach(); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
  53.   wifiReconnectTimer.once(2, connectToWifi);
  54. }
  55.  
  56. void onMqttConnect(bool sessionPresent) {
  57.   Serial.println("Connected to MQTT.");
  58.   Serial.print("Session present: ");
  59.   Serial.println(sessionPresent);
  60. }
  61.  
  62. void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
  63.   Serial.println("Disconnected from MQTT.");
  64.   if (WiFi.isConnected()) {
  65.     mqttReconnectTimer.once(2, connectToMqtt);
  66.   }
  67. }
  68. void onMqttPublish(uint16_t packetId) {
  69.   Serial.print("Publish acknowledged.");
  70.   Serial.print("  packetId: ");
  71.   Serial.println(packetId);
  72. }
  73.  
  74. void setup() {
  75.  
  76.   Serial.begin(115200);
  77.   Serial.println();
  78.  
  79.   // Initialize BME280 sensor
  80.   if (!bme.begin(0x76)) {
  81.     Serial.println("Could not find a valid BME280 sensor, check wiring!");
  82.     while (1);
  83.   }
  84.  
  85.   wifiConnectHandler = WiFi.onStationModeGotIP(onWifiConnect);
  86.   wifiDisconnectHandler = WiFi.onStationModeDisconnected(onWifiDisconnect);
  87.  
  88.   mqttClient.onConnect(onMqttConnect);
  89.   mqttClient.onDisconnect(onMqttDisconnect);
  90.  
  91.   mqttClient.onPublish(onMqttPublish);
  92.   mqttClient.setServer(MQTT_HOST, MQTT_PORT);
  93.  
  94.   connectToWifi();
  95.   delay(500);
  96.   connectToMqtt();
  97.  
  98.  
  99.     temp = 1.8*bme.readTemperature() + 32;
  100.     hum = bme.readHumidity();
  101.     pres = bme.readPressure()/100.0F;
  102.    
  103.     // Publish an MQTT message on topic esp/bme280/temperature
  104.     uint16_t packetIdPub1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(temp).c_str());                            
  105.     Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_TEMP, packetIdPub1);
  106.     Serial.printf("Message: %.2f \n", temp);
  107.  
  108.     // Publish an MQTT message on topic esp/bme280/humidity
  109.     uint16_t packetIdPub2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(hum).c_str());                            
  110.     Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_HUM, packetIdPub2);
  111.     Serial.printf("Message: %.2f \n", hum);
  112.  
  113.     // Publish an MQTT message on topic esp/bme280/pressure
  114.     uint16_t packetIdPub3 = mqttClient.publish(MQTT_PUB_PRES, 1, true, String(pres).c_str());                            
  115.     Serial.printf("Publishing on topic %s at QoS 1, packetId: %i ", MQTT_PUB_PRES, packetIdPub3);
  116.     Serial.printf("Message: %.3f \n", pres);
  117.  
  118.     Serial.println("I'm awake, but I'm going into deep sleep mode for 30 seconds");
  119.   ESP.deepSleep(30e6);
  120.   }
  121.  
  122. void loop(){
  123. }
  124.  
RAW Paste Data