Advertisement
Guest User

Untitled

a guest
Sep 11th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <PubSubClient.h>
  4. #include <RCSwitch.h>
  5.  
  6. /*
  7. * ===============================
  8. * RC-SWITCH SETTINGS
  9. * ===============================
  10. */
  11. RCSwitch mySwitch = RCSwitch();
  12.  
  13. /*
  14. * ===============================
  15. * MQTT SETTINGS
  16. * ===============================
  17. */
  18. #define MQTT_VERSION MQTT_VERSION_3_1_1
  19.  
  20. // MQTT: ID, server IP, port
  21. const PROGMEM char* MQTT_CLIENT_ID = "home-assistant-1";
  22. const PROGMEM char* MQTT_SERVER_IP = "192.168.1.xx";
  23. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  24. const PROGMEM char* MQTT_USER = "myuser";
  25. const PROGMEM char* MQTT_PASSWORD = "abcdefg";
  26.  
  27.  
  28. const char* MQTT_LIGHT_STATE_TOPIC = "main/desk/status";
  29. const char* MQTT_LIGHT_COMMAND_TOPIC = "main/desk/switch";
  30.  
  31. const char* LIGHT_ON = "ON";
  32. const char* LIGHT_OFF = "OFF";
  33. boolean m_light_state = false; // light is turned off by default
  34.  
  35. PubSubClient client;
  36.  
  37. void statusLight() {
  38. if (m_light_state) {
  39. client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
  40. } else {
  41. client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
  42. }
  43. }
  44.  
  45. // function called to turn on/off the light
  46. void setLight() {
  47. if (m_light_state) {
  48. mySwitch.send("00010101010100010101000");
  49. Serial.println("INFO: Turn light on...");
  50. } else {
  51. mySwitch.send("00010101010100010101000");
  52. Serial.println("INFO: Turn light off...");
  53. }
  54. }
  55.  
  56. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  57. // concat the payload into a string
  58. String payload;
  59. for (uint8_t i = 0; i < p_length; i++) {
  60. payload.concat((char)p_payload[i]);
  61. }
  62.  
  63. Serial.println("This is the payload");
  64. Serial.print(payload);
  65. // handle message topic
  66. if (String(MQTT_LIGHT_COMMAND_TOPIC).equals(p_topic)) {
  67. // test if the payload is equal to "ON" or "OFF"
  68. if (payload.equals(String(LIGHT_ON))) {
  69. if (m_light_state != true) {
  70. m_light_state = true;
  71. setLight();
  72. statusLight();
  73. }
  74. } else if (payload.equals(String(LIGHT_OFF))) {
  75. if (m_light_state != false) {
  76. m_light_state = false;
  77. setLight();
  78. statusLight();
  79. }
  80. }
  81. }
  82. }
  83.  
  84. void reconnect() {
  85. // Loop until we're reconnected
  86. while (!client.connected()) {
  87. Serial.print("INFO: Attempting MQTT connection...");
  88. // Attempt to connect
  89. if (client.connect(MQTT_CLIENT_ID,MQTT_USER,MQTT_PASSWORD)) {
  90. Serial.println("INFO: connected");
  91. // Once connected, publish an announcement...
  92. statusLight();
  93. // ... and resubscribe
  94. client.subscribe(MQTT_LIGHT_COMMAND_TOPIC);
  95. } else {
  96. Serial.print("ERROR: failed, rc=");
  97. Serial.print(client.state());
  98. Serial.println("DEBUG: try again in 5 seconds");
  99. // Wait 5 seconds before retrying
  100. delay(5000);
  101. }
  102. }
  103. }
  104.  
  105. /*
  106. * ===============================
  107. * ETHERNET SETTINGS
  108. * ===============================
  109. */
  110. byte MAC_ADDRESS[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  111.  
  112.  
  113. void setup() {
  114. // init the serial
  115. Serial.begin(115200);
  116.  
  117. //RC-switch initialization
  118. mySwitch.enableTransmit(24);
  119. mySwitch.setPulseLength(296);
  120. mySwitch.setProtocol(1);
  121. mySwitch.setRepeatTransmit(15);
  122.  
  123. setLight();
  124.  
  125. if (Ethernet.begin(MAC_ADDRESS) == 0)
  126. {
  127. Serial.println("Failed to configure Ethernet using DHCP");
  128. return;
  129. }
  130.  
  131. // init the MQTT connection
  132. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  133. client.setCallback(callback);
  134. }
  135.  
  136. void loop() {
  137. if (!client.connected()) {
  138. reconnect();
  139. }
  140. client.loop();
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement