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 4.54 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 = "asdf";
  15. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  16. const PROGMEM char* MQTT_USER = "asdf";
  17. const PROGMEM char* MQTT_PASSWORD = "asdf";
  18. const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/bedroom/sensor_dht";
  19. const PROGMEM char* MQTT_LIGHT_STATE_TOPIC[] = { "/house/livingroom/light_left/status", "/house/livingroom/light_right/status", "/house/livingroom/light_center/status" };
  20. const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC[] = { "/house/livingroom/light_left/switch", "/house/livingroom/light_right/switch", "/house/livingroom/light_center/switch" };
  21. const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30;
  22. const char* LIGHT_ON = "ON";
  23. const char* LIGHT_OFF = "OFF";
  24. boolean m_light_state = false;
  25. const char* housecode = "11010";
  26. const char* socketcodes[] = { "00010", "00001", "10000" };
  27. const char* socketnames[] = { "Steckdose Wohnzimmer Links", "Steckdose Wohnzimmer Rechts", "Steckdose_Wohnzimmer_Eingang" };
  28. const long wait = 20000;
  29. unsigned long last_millis = 0;
  30.  
  31. DHT dht(DHTPIN, DHTTYPE);
  32. WiFiClient wifiClient;
  33. PubSubClient client(wifiClient);
  34. RCSwitch mySwitch = RCSwitch();
  35.  
  36. void publishData(float p_temperature, float p_humidity) {
  37. StaticJsonBuffer<200> jsonBuffer;
  38. JsonObject& root = jsonBuffer.createObject();
  39.  
  40. root["temperature"] = (String)p_temperature;
  41. root["humidity"] = (String)p_humidity;
  42.  
  43. root.prettyPrintTo(Serial);
  44. Serial.println("");
  45.  
  46. char data[200];
  47. root.printTo(data, root.measureLength() + 1);
  48. client.publish(MQTT_SENSOR_TOPIC, data, true);
  49. }
  50.  
  51. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  52. String payload;
  53. for (uint8_t i = 0; i < p_length; i++) {
  54. payload.concat((char)p_payload[i]);
  55. }
  56. Serial.println(p_topic);// handle message topic
  57. for(int i=0; i<3; i++) {
  58. if (String(MQTT_LIGHT_COMMAND_TOPIC[i]).equals(p_topic)) {
  59. // test if the payload is equal to "ON" or "OFF"
  60. if (payload.equals(String(LIGHT_ON))) {
  61. if (m_light_state != true) {
  62. m_light_state = true;
  63. mySwitch.switchOn(housecode, socketcodes[i]);
  64. Serial.println("INFO: Turn light on...");
  65. client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_ON, true);
  66. }
  67. }
  68. else if (payload.equals(String(LIGHT_OFF))) {
  69. if (m_light_state != false) {
  70. m_light_state = false;
  71. mySwitch.switchOff(housecode, socketcodes[i]);
  72. Serial.println("INFO: Turn light off...");
  73. client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_OFF, true);
  74. }
  75. }
  76. }
  77. }
  78. }
  79.  
  80. void reconnect() {
  81. // Loop until we're reconnected
  82. while (!client.connected()) {
  83. Serial.println("INFO: Attempting MQTT connection...");
  84. // Attempt to connect
  85. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  86. Serial.println("INFO: connected");
  87. // publishLightState();
  88. for(int i=0; i<3; i++) {
  89. client.subscribe(MQTT_LIGHT_COMMAND_TOPIC[i]);
  90. }
  91. } else {
  92. Serial.print("ERROR: failed, rc=");
  93. Serial.print(client.state());
  94. Serial.println("DEBUG: try again in 5 seconds");
  95. // Wait 5 seconds before retrying
  96. delay(5000);
  97. }
  98. }
  99. }
  100.  
  101. void setup() {
  102. // init the serial
  103. Serial.begin(115200);
  104. mySwitch.enableTransmit(2);
  105. dht.begin();
  106. //setLightState();
  107. // init the WiFi connection
  108. Serial.println();
  109. Serial.println();
  110. Serial.print("INFO: Connecting to ");
  111. WiFi.mode(WIFI_STA);
  112. Serial.println(WIFI_SSID);
  113. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  114.  
  115. while (WiFi.status() != WL_CONNECTED) {
  116. delay(500);
  117. Serial.print(".");
  118. }
  119.  
  120. Serial.println("");
  121. Serial.println("INFO: WiFi connected");
  122. Serial.println("INFO: IP address: ");
  123. Serial.println(WiFi.localIP());
  124.  
  125. // init the MQTT connection
  126. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  127. client.setCallback(callback);
  128. delay(1000);
  129. }
  130.  
  131. void loop() {
  132. if (!client.connected()) {
  133. reconnect();
  134. }
  135. client.loop();
  136.  
  137. unsigned long now = millis();
  138.  
  139. if(now - last_millis >= wait) {
  140. last_millis = now;
  141. float h = dht.readHumidity();
  142. float t = dht.readTemperature();
  143.  
  144. if (isnan(h) || isnan(t)) {
  145. Serial.println("ERROR: Failed to read from DHT sensor!");
  146. return;
  147. } else {
  148. //Serial.println(h);
  149. //Serial.println(t);
  150. publishData(t, h);
  151. }
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement