Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.06 KB | None | 0 0
  1. /* Project ESP32, DHT, MQTT and Deepsleep */
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4. #include "DHT.h"          // Library for DHT sensors
  5.  
  6. #define wifi_ssid "Unifi"         //wifi ssid
  7. #define wifi_password "geheimnis01!$"     //wifi password
  8.  
  9. #define mqtt_server "192.168.0.63"  // server name or IP
  10. #define mqtt_user "openhabian"      // username
  11. #define mqtt_password "password"   // password
  12.  
  13. #define temperature_topic "topic/temp2"       //Topic temperature
  14. #define humidity_topic "topic/humid2"         //Topic humidity
  15. #define debug_topic "debug2"                   //Topic for debugging
  16.  
  17. /* definitions for deepsleep */
  18. #define uS_TO_S_FACTOR 1000000        /* Conversion factor for micro seconds to seconds */
  19. #define TIME_TO_SLEEP 60              /* Time ESP32 will go to sleep for 15 minutes (in seconds) */
  20. #define TIME_TO_SLEEP_ERROR 3600       /* Time to sleep in case of error (1 hour) */
  21.  
  22. bool debug = true;             //Display log message if True
  23.  
  24. #define DHTPIN 14               // DHT Pin
  25. // Uncomment depending on your sensor type:
  26. //#define DHTTYPE DHT11           // DHT 11
  27. #define DHTTYPE DHT22         // DHT 22  (AM2302)
  28.  
  29. // Create objects
  30. DHT dht(DHTPIN, DHTTYPE);    
  31. WiFiClient espClient;
  32. PubSubClient client(espClient);
  33.  
  34. void setup() {
  35.   Serial.begin(115200);    
  36.   setup_wifi();                           //Connect to Wifi network
  37.    
  38.   client.setServer(mqtt_server, 1883);    // Configure MQTT connection, change port if needed.
  39.  
  40.   if (!client.connected()) {
  41.     reconnect();
  42.   }
  43.   dht.begin();
  44.  
  45.   // Read temperature in Celcius
  46.     float t = dht.readTemperature();
  47.   // Read humidity
  48.     float h = dht.readHumidity();
  49.  
  50.  
  51.   // Nothing to send. Warn on MQTT debug_topic and then go to sleep for longer period.
  52.     if ( isnan(t) || isnan(h)) {
  53.       Serial.println("[ERROR] Please check the DHT sensor !");
  54.       client.publish(debug_topic, "[ERROR] Please check the DHT sensor !", true);      // Publish humidity on mosohomes/humid1
  55.       esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep
  56.       Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
  57.       Serial.println("Going to sleep now because of ERROR");
  58.       esp_deep_sleep_start();
  59.       return;
  60.     }
  61.  
  62.     if ( debug ) {
  63.       Serial.print("Temperature : ");
  64.       Serial.print(t);
  65.       Serial.print(" | Humidity : ");
  66.       Serial.println(h);
  67.     }
  68.     // Publish values to MQTT topics
  69.     client.publish(temperature_topic, String(t).c_str(), true);   // Publish temperature on mosohomes/temp1
  70.     if ( debug ) {    
  71.       Serial.println("Temperature sent to MQTT.");
  72.     }
  73.     delay(300); //some delay is needed for the mqtt server to accept the message
  74.     client.publish(humidity_topic, String(h).c_str(), true);      // Publish humidity on mosohomes/humid1
  75.     if ( debug ) {
  76.     Serial.println("Humidity sent to MQTT.");
  77.     }  
  78.  
  79.   esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //go to sleep
  80.   Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) + " Seconds");
  81.   Serial.println("Going to sleep as normal now.");
  82.   esp_deep_sleep_start();
  83. }
  84.  
  85. //Setup connection to wifi
  86. void setup_wifi() {
  87.   delay(20);
  88.   Serial.println();
  89.   Serial.print("Connecting to ");
  90.   Serial.println(wifi_ssid);
  91.  
  92.   WiFi.begin(wifi_ssid, wifi_password);
  93.  
  94.   while (WiFi.status() != WL_CONNECTED) {
  95.     delay(100);
  96.     Serial.print(".");
  97.   }
  98.  
  99.  Serial.println("");
  100.  Serial.println("WiFi is OK ");
  101.  Serial.print("=> ESP32 new IP address is: ");
  102.  Serial.print(WiFi.localIP());
  103.  Serial.println("");
  104. }
  105.  
  106. //Reconnect to wifi if connection is lost
  107. void reconnect() {
  108.  
  109.   while (!client.connected()) {
  110.     Serial.print("Connecting to MQTT broker ...");
  111.     if (client.connect("ESP32Client", mqtt_user, mqtt_password)) {
  112.       Serial.println("OK");
  113.     } else {
  114.       Serial.print("[Error] Not connected: ");
  115.       Serial.print(client.state());
  116.       Serial.println("Wait 5 seconds before retry.");
  117.       delay(5000);
  118.     }
  119.   }
  120. }
  121.  
  122. void loop() {
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement