Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <DHT.h>
  4. #include <ArduinoJson.h>
  5. #include <RCSwitch.h>
  6.  
  7. #define MQTT_VERSION MQTT_VERSION_3_1_1
  8. #define DHTPIN 13
  9. #define DHTTYPE DHT11
  10.  
  11. const char* WIFI_SSID = "Drueck mich!";
  12. const char* WIFI_PASSWORD = "Key01@BM";
  13. const PROGMEM char* MQTT_CLIENT_ID = "bedroom_dht11";
  14. const PROGMEM char* MQTT_SERVER_IP = "beutler.nl";
  15. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  16. const PROGMEM char* MQTT_USER = "";
  17. const PROGMEM char* MQTT_PASSWORD = "";
  18. const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/bedroom/sensor_dht";
  19. const PROGMEM char* MQTT_LIGHT_STATE_TOPIC = "/house/livingroom/light_left/status";
  20. const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC = "/house/livingroom/light_left/switch";
  21. const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30;
  22.  
  23. const char* LIGHT_ON = "ON";
  24. const char* LIGHT_OFF = "OFF";
  25. boolean m_light_state = false;
  26. const char* housecode = "11010";
  27. const char* socketcodes[] = {"00010", "00001", "10000"};
  28. const char* socketnames[] = {"Steckdose_Wohnzimmer_Links", "Steckdose_Wohnzimmer_rechts", "Steckdose_Wohnzimmer_Eingang"};
  29. const long wait = 20000;
  30. unsigned long last_millis = 0;
  31.  
  32. DHT dht(DHTPIN, DHTTYPE);
  33. WiFiClient wifiClient;
  34. PubSubClient client(wifiClient);
  35. RCSwitch mySwitch = RCSwitch();
  36.  
  37. // function called to publish the state of the light (on/off)
  38. void publishLightState() {
  39. if (m_light_state) {
  40. client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
  41. } else {
  42. client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
  43. }
  44. }
  45.  
  46. // function called to turn on/off the light
  47. void setLightState() {
  48. if (m_light_state) {
  49. mySwitch.switchOn(housecode, socketcodes[0]);
  50. Serial.println("INFO: Turn light on...");
  51. } else {
  52. mySwitch.switchOff(housecode, socketcodes[0]);
  53. Serial.println("INFO: Turn light off...");
  54. }
  55. }
  56.  
  57. void publishData(float p_temperature, float p_humidity) {
  58. StaticJsonBuffer<200> jsonBuffer;
  59. JsonObject& root = jsonBuffer.createObject();
  60.  
  61. root["temperature"] = (String)p_temperature;
  62. root["humidity"] = (String)p_humidity;
  63.  
  64. root.prettyPrintTo(Serial);
  65. Serial.println("");
  66.  
  67. char data[200];
  68. root.printTo(data, root.measureLength() + 1);
  69. client.publish(MQTT_SENSOR_TOPIC, data, true);
  70. }
  71.  
  72. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  73. String payload;
  74. for (uint8_t i = 0; i < p_length; i++) {
  75. payload.concat((char)p_payload[i]);
  76. }
  77. // handle message topic
  78. if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
  79. // test if the payload is equal to "ON" or "OFF"
  80. if (payload.equals(String(LIGHT_ON))) {
  81. if (m_light_state != true) {
  82. m_light_state = true;
  83. setLightState();
  84. publishLightState();
  85. }
  86. } else if (payload.equals(String(LIGHT_OFF))) {
  87. if (m_light_state != false) {
  88. m_light_state = false;
  89. setLightState();
  90. publishLightState();
  91. }
  92. }
  93. }
  94. }
  95.  
  96. void reconnect() {
  97. // Loop until we're reconnected
  98. while (!client.connected()) {
  99. Serial.println("INFO: Attempting MQTT connection...");
  100. // Attempt to connect
  101. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  102. Serial.println("INFO: connected");
  103. publishLightState();
  104. client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
  105. } else {
  106. Serial.print("ERROR: failed, rc=");
  107. Serial.print(client.state());
  108. Serial.println("DEBUG: try again in 5 seconds");
  109. // Wait 5 seconds before retrying
  110. delay(5000);
  111. }
  112. }
  113. }
  114.  
  115. void setup() {
  116. // init the serial
  117. Serial.begin(115200);
  118. mySwitch.enableTransmit(2);
  119. dht.begin();
  120. setLightState();
  121. // init the WiFi connection
  122. Serial.println();
  123. Serial.println();
  124. Serial.print("INFO: Connecting to ");
  125. WiFi.mode(WIFI_STA);
  126. Serial.println(WIFI_SSID);
  127. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  128.  
  129. while (WiFi.status() != WL_CONNECTED) {
  130. delay(500);
  131. Serial.print(".");
  132. }
  133.  
  134. Serial.println("");
  135. Serial.println("INFO: WiFi connected");
  136. Serial.println("INFO: IP address: ");
  137. Serial.println(WiFi.localIP());
  138.  
  139. // init the MQTT connection
  140. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  141. client.setCallback(callback);
  142. }
  143.  
  144. void loop() {
  145. if (!client.connected()) {
  146. reconnect();
  147. }
  148. client.loop();
  149.  
  150. unsigned long now = millis();
  151.  
  152. if(now - last_millis >= wait) {
  153. delay(500);
  154. last_millis = now;
  155. float h = dht.readHumidity();
  156. float t = dht.readTemperature();
  157.  
  158. if (isnan(h) || isnan(t)) {
  159. Serial.println("ERROR: Failed to read from DHT sensor!");
  160. return;
  161. } else {
  162. //Serial.println(h);
  163. //Serial.println(t);
  164. publishData(t, h);
  165. }
  166. }
  167.  
  168. /*Serial.println("INFO: Closing the MQTT connection");
  169. client.disconnect();
  170.  
  171. Serial.println("INFO: Closing the Wifi connection");
  172. WiFi.disconnect();
  173.  
  174. ESP.deepSleep(SLEEPING_TIME_IN_SECONDS * 1000000, WAKE_RF_DEFAULT);
  175. delay(500); // wait for deep sleep to happen*/
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement