Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <DHT.h>
  4. #include <ArduinoJson.h>
  5.  
  6. #define MQTT_VERSION MQTT_VERSION_3_1_1
  7. #define DHTPIN 13
  8. #define DHTTYPE DHT11
  9.  
  10. const char* WIFI_SSID = "Drueck mich!";
  11. const char* WIFI_PASSWORD = "Key01@BM";
  12. const PROGMEM char* MQTT_CLIENT_ID = "bedroom_dht11";
  13. const PROGMEM char* MQTT_SERVER_IP = "";
  14. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  15. const PROGMEM char* MQTT_USER = "";
  16. const PROGMEM char* MQTT_PASSWORD = "";
  17. const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/bedroom/sensor_dht";
  18. const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30;
  19.  
  20. DHT dht(DHTPIN, DHTTYPE);
  21. WiFiClient wifiClient;
  22. PubSubClient client(wifiClient);
  23.  
  24. void publishData(float p_temperature, float p_humidity) {
  25. StaticJsonBuffer<200> jsonBuffer;
  26. JsonObject& root = jsonBuffer.createObject();
  27.  
  28. root["temperature"] = (String)p_temperature;
  29. root["humidity"] = (String)p_humidity;
  30.  
  31. root.prettyPrintTo(Serial);
  32. Serial.println("");
  33.  
  34. char data[200];
  35. root.printTo(data, root.measureLength() + 1);
  36. client.publish(MQTT_SENSOR_TOPIC, data, true);
  37. }
  38.  
  39. // function called when a MQTT message arrived
  40. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  41. }
  42.  
  43. void reconnect() {
  44. // Loop until we're reconnected
  45. while (!client.connected()) {
  46. Serial.print("INFO: Attempting MQTT connection...");
  47. // Attempt to connect
  48. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  49. Serial.println("INFO: connected");
  50. } else {
  51. Serial.print("ERROR: failed, rc=");
  52. Serial.print(client.state());
  53. Serial.println("DEBUG: try again in 5 seconds");
  54. // Wait 5 seconds before retrying
  55. delay(5000);
  56. }
  57. }
  58. }
  59.  
  60. void setup() {
  61. // init the serial
  62. Serial.begin(9600);
  63.  
  64. dht.begin();
  65.  
  66. // init the WiFi connection
  67. Serial.println();
  68. Serial.println();
  69. Serial.print("INFO: Connecting to ");
  70. WiFi.mode(WIFI_STA);
  71. Serial.println(WIFI_SSID);
  72. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  73.  
  74. while (WiFi.status() != WL_CONNECTED) {
  75. delay(500);
  76. Serial.print(".");
  77. }
  78.  
  79. Serial.println("");
  80. Serial.println("INFO: WiFi connected");
  81. Serial.println("INFO: IP address: ");
  82. Serial.println(WiFi.localIP());
  83.  
  84. // init the MQTT connection
  85. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  86. client.setCallback(callback);
  87. }
  88.  
  89. void loop() {
  90. if (!client.connected()) {
  91. reconnect();
  92. }
  93. client.loop();
  94.  
  95. float h = dht.readHumidity();
  96. float t = dht.readTemperature();
  97.  
  98. if (isnan(h) || isnan(t)) {
  99. Serial.println("ERROR: Failed to read from DHT sensor!");
  100. return;
  101. } else {
  102. //Serial.println(h);
  103. //Serial.println(t);
  104. publishData(t, h);
  105. }
  106.  
  107. Serial.println("INFO: Closing the MQTT connection");
  108. client.disconnect();
  109.  
  110. Serial.println("INFO: Closing the Wifi connection");
  111. WiFi.disconnect();
  112.  
  113. ESP.deepSleep(SLEEPING_TIME_IN_SECONDS, WAKE_RF_DEFAULT);
  114. delay(500); // wait for deep sleep to happen
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement