Advertisement
Guest User

Untitled

a guest
Feb 13th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. //include all libraries
  2. #include <ESP8266WiFi.h>
  3. #include <ESP8266mDNS.h>
  4. #include <PubSubClient.h>
  5. #include <RCSwitch.h>
  6. #include <fauxmoESP.h>
  7. #include <WiFiUdp.h>
  8. #include <ArduinoOTA.h>
  9.  
  10. //basic defines
  11. #define MQTT_VERSION MQTT_VERSION_3_1_1
  12. #define IRB 14
  13. #define RCPIN 2
  14.  
  15. //wifi credentials
  16. const char* WIFI_SSID = "!";
  17. const char* WIFI_PASSWORD = "@";
  18.  
  19. //mqtt variables
  20. const PROGMEM char* MQTT_CLIENT_ID = "livingroom_rc";
  21. const PROGMEM char* MQTT_SERVER_IP = "192.168.2.125";
  22. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  23. const PROGMEM char* MQTT_USER = "";
  24. const PROGMEM char* MQTT_PASSWORD = "";
  25. const PROGMEM char* MQTT_SENSOR_TOPIC = "/house/livingroom/sensor_dht";
  26. const PROGMEM char* MQTT_LIGHT_STATE_TOPIC[] = { "/house/livingroom/light_left/status", "/house/livingroom/light_right/status", "/house/livingroom/light_center/status" };
  27. const PROGMEM char* MQTT_LIGHT_COMMAND_TOPIC[] = { "/house/livingroom/light_left/switch", "/house/livingroom/light_right/switch", "/house/livingroom/light_center/switch" };
  28. const PROGMEM char* MQTT_MOVEMENT_STATE_TOPIC = "/house/livingroom/movement/status";
  29.  
  30. boolean m_light_state[] = { false, false, false }; //variable to store light states
  31. const PROGMEM uint16_t SLEEPING_TIME_IN_SECONDS = 30; //sleep timer in case i want to implement deep sleep later
  32. const char* LIGHT_ON = "ON";
  33. const char* LIGHT_OFF = "OFF";
  34.  
  35. //variables for rc-switch functionality
  36. const char* housecode = "11010"; //first 5 dip switches on rc switches
  37. const char* socketcodes[] = { "00010", "00001", "10000" }; //last 5 dip switches on rc switches
  38. const char* socketnames[] = { "Steckdose Wohnzimmer Links", "Steckdose Wohnzimmer Rechts", "Steckdose_Wohnzimmer_Eingang" }; //in case you like to name your rc switches, put them in here.
  39.  
  40. unsigned long wait; //wait time
  41. unsigned long last_millis = 0; //now() initialize
  42.  
  43. //initialize classes
  44. WiFiClient wifiClient;
  45. PubSubClient client(wifiClient);
  46. RCSwitch mySwitch = RCSwitch();
  47. fauxmoESP fauxmo;
  48.  
  49. void callback(char* p_topic, byte* p_payload, unsigned int p_length) { //handle mqtt callbacks
  50. String payload;
  51. for (uint8_t i = 0; i < p_length; i++) { //concatenate payload
  52. payload.concat((char)p_payload[i]);
  53. }
  54. Serial.println(p_topic);
  55. for(int i=0; i<3; i++) { //for loop to run though the MQTT_LIGHT_COMMAND_TOPIC array. if the topic equals, switch the corresponding RC
  56. if (String(MQTT_LIGHT_COMMAND_TOPIC[i]).equals(p_topic)) { //if topic matches
  57. if (payload.equals(String(LIGHT_ON))) {
  58. m_light_state[i] = true; //set state for the corresponding light
  59. mySwitch.switchOn(housecode, socketcodes[i]);
  60. Serial.print("INFO: Turn Light: ");
  61. Serial.print(MQTT_LIGHT_COMMAND_TOPIC[i]);
  62. Serial.println(" on!");
  63. client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_ON, true); //publish the state to mqtt
  64. }
  65. else if (payload.equals(String(LIGHT_OFF))) {
  66. m_light_state[i] = false;
  67. mySwitch.switchOff(housecode, socketcodes[i]);
  68. Serial.print("INFO: Turn Light: ");
  69. Serial.print(MQTT_LIGHT_COMMAND_TOPIC[i]);
  70. Serial.println(" off!");
  71. client.publish(MQTT_LIGHT_STATE_TOPIC[i], LIGHT_OFF, true); //publish state to mqtt
  72. }
  73. }
  74. }
  75. }
  76.  
  77. void movement() {
  78. unsigned long now = millis();
  79. if(digitalRead(IRB) == HIGH) {
  80. if(now - last_millis >= wait) { //only publish no movement every 20secs
  81. last_millis = now;
  82. wait = 20000;
  83. Serial.println("INFO: Movement detected! ");
  84. client.publish(MQTT_MOVEMENT_STATE_TOPIC, "ON", true); //publish the state to mqtt
  85. }
  86. }
  87. if(digitalRead(IRB) == LOW) {
  88. if(now - last_millis >= wait) { //only publish no movement every 20secs
  89. last_millis = now;
  90. wait = 5000;
  91. Serial.println("INFO: No Movement detected! ");
  92. client.publish(MQTT_MOVEMENT_STATE_TOPIC, "OFF", true); //publish the state to mqtt
  93. }
  94. }
  95. }
  96.  
  97. void reconnect() {
  98. while (!client.connected()) { //loop until we're reconnected
  99. Serial.println("INFO: Attempting MQTT connection...");
  100. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  101. Serial.println("INFO: connected");
  102. for(int i=0; i<3; i++) {
  103. client.subscribe(MQTT_LIGHT_COMMAND_TOPIC[i]); //subscribe to all light topics
  104. }
  105. } else {
  106. Serial.print("ERROR: failed, rc=");
  107. Serial.print(client.state());
  108. Serial.println("DEBUG: try again in 5 seconds");
  109. delay(5000); //wait 5 seconds before retrying
  110. }
  111. }
  112. }
  113.  
  114. void setup() {
  115. Serial.begin(115200); //init the serial
  116. mySwitch.enableTransmit(RCPIN); //enable transmit on RCPIN
  117. Serial.println();
  118. Serial.print("INFO: Connecting to ");
  119. WiFi.mode(WIFI_STA);
  120. Serial.println(WIFI_SSID);
  121. WiFi.begin(WIFI_SSID, WIFI_PASSWORD); //connect to wifi
  122.  
  123. while (WiFi.status() != WL_CONNECTED) {
  124. delay(500);
  125. Serial.print(".");
  126. }
  127.  
  128. Serial.println("");
  129. Serial.println("INFO: WiFi connected");
  130. Serial.println("INFO: IP address: ");
  131. Serial.println(WiFi.localIP());
  132.  
  133. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  134. client.setCallback(callback);
  135.  
  136. fauxmo.addDevice("Licht eins");
  137. fauxmo.addDevice("Licht zwei");
  138. fauxmo.addDevice("Licht drei");
  139. fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
  140. Serial.printf("[MAIN] Device #%d (%s) state: %s\n", device_id, device_name, state ? "ON" : "OFF");
  141. client.publish(MQTT_LIGHT_COMMAND_TOPIC[device_id], state ? "ON" : "OFF", true);
  142. });
  143. }
  144.  
  145. void loop() {
  146. if (!client.connected()) {
  147. reconnect();
  148. }
  149. client.loop();
  150. fauxmo.handle();
  151. movement();
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement