Advertisement
Guest User

Untitled

a guest
Apr 3rd, 2020
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.85 KB | None | 0 0
  1. /*
  2.  * Simple program to publish and subscribe to a topic
  3.  * from an MQTT server using the PubSub.
  4.  * Use as a base for more interesting code.
  5.  */
  6. #include <ESP8266WiFi.h>
  7. #include <PubSubClient.h>
  8.  
  9. // Override the maximum message size
  10. // Client and Topic setups
  11. #define MQTT_CLIENT_ID "Development_Strip"
  12. #define MQTT_COLOR_TOPIC "smartthings/Development Strip/color/state"
  13. #define MQTT_BRIGHTNESS_TOPIC "smartthings/Development Strip/level/state"
  14. #define MQTT_SWITCH_TOPIC "smartthings/Development Strip/switch/state"
  15. #define MQTT_MODE_TOPIC "smartthings/Development Strip/button/state"
  16. #define MQTT_STATUS_TOPIC "smartthings/Development Strip/status/cmd"
  17.  
  18. // LED setups
  19. #define LED_PIN 2 // On-board LED
  20. #define RED_PIN D6 // Pin for controlling the RED channel.
  21. #define GREEN_PIN D5 //  Pin for controlling the GREEN channel.
  22. #define BLUE_PIN D8 // Pin for controlling the BLUE channel.
  23.  
  24. // Network setup
  25. #define WIFI_SSID "Green Bowser"
  26. #define WIFI_PASSWORD "SpandexSkank"
  27. #define MQTT_SERVER "192.168.1.45"
  28. #define MQTT_PORT 1883
  29. #define MQTT_USERNAME "Epidurality"
  30. #define MQTT_PASSWORD "mqttPass"
  31.  
  32. // MQTT client
  33. WiFiClient wifi_client;
  34. PubSubClient mqtt_client(wifi_client);
  35.  
  36. // Holders for last events
  37. char* state_switch = "off";
  38. char* state_brightness = "100";
  39. char* state_color = "#ffffff";
  40. char* state_mode = "err";
  41. bool mqtt_resp_new = false;
  42.  
  43. // Uses the preset parameters to setup a wifi connection.
  44. void wifiConnect() {
  45.   WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  46.   Serial.print("Connecting to WiFi");
  47.   while (WiFi.status() != WL_CONNECTED) {
  48.     delay(500);
  49.     Serial.print(".");
  50.   }
  51.   Serial.println(" Connected!");
  52.   Serial.print("IP Address: ");
  53.   Serial.println(WiFi.localIP());
  54. }
  55.  
  56. // Attempts connection to MQTT server passed to it.
  57. void mqttConnect(PubSubClient &c) {
  58.   while (!c.connected()) {
  59.     Serial.print("Attempting MQTT connection...");
  60.     if (c.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
  61.       Serial.println(" Connected!");
  62.     } else {
  63.       Serial.print(" Failed, rc=");
  64.       Serial.print(c.state());
  65.       Serial.println(". Trying again in 5 seconds...");
  66.       delay(5000);
  67.     }
  68.   }
  69. }
  70.  
  71. void mqttCallback(char* topic, byte* payload, unsigned int length) {
  72.   char* message = (char*)payload;
  73.   message[length] = '\0';
  74.   Serial.print("Message received on topic ");
  75.   Serial.println(topic);
  76.   Serial.print("Payload:");
  77.   Serial.println(message);
  78.   if (strcmp(topic, MQTT_SWITCH_TOPIC)==0) {
  79.     strcpy(state_switch, message);
  80.     mqtt_resp_new = true;
  81.   } else if (strcmp(topic, MQTT_BRIGHTNESS_TOPIC)==0) {
  82.     strcpy(state_brightness,message);
  83.     mqtt_resp_new = true;
  84.   } else if (strcmp(topic, MQTT_COLOR_TOPIC)==0) {
  85.     char* excerpt = strstr(message, "#");
  86.     for (int i=0; i<7; i++) {
  87.       state_color[i] = excerpt[i];
  88.     }
  89.     mqtt_resp_new = true;
  90.   } else if (strcmp(topic, MQTT_MODE_TOPIC)==0) {
  91.     strcpy(state_mode,message);
  92.     mqtt_resp_new = true;
  93.   } else {
  94.     mqtt_client.publish(MQTT_STATUS_TOPIC, payload, true);
  95.   }
  96. }
  97.  
  98. void mqttSubscribeAll() {
  99.   mqtt_client.subscribe(MQTT_SWITCH_TOPIC);
  100.   mqtt_client.subscribe(MQTT_BRIGHTNESS_TOPIC);
  101.   mqtt_client.subscribe(MQTT_COLOR_TOPIC);
  102.   mqtt_client.subscribe(MQTT_MODE_TOPIC);
  103. }
  104.  
  105. void setLights(int r, int g, int b) {
  106.   analogWrite(RED_PIN, r*4);
  107.   analogWrite(GREEN_PIN, g*4);
  108.   analogWrite(BLUE_PIN, b*4);
  109. }
  110.  
  111. void setLights(char* hex_col) {
  112.   Serial.println(hex_col);
  113.   long hex_as_long = strtol(&hex_col[1], NULL, 16);
  114.   long r = hex_as_long >> 16;
  115.   long g = hex_as_long >> 8 & 0xFF;
  116.   long b = hex_as_long & 0xFF;
  117.   setLights(r, g, b);
  118. }
  119.  
  120. void logStates() {
  121.   char state_log[256];
  122.   sprintf(state_log, "Switch: %s | Brightness: %s | Color: %s | Mode: %s", state_switch, state_brightness, state_color, state_mode);
  123.   Serial.println(state_log);
  124. }
  125.  
  126. void setup() {
  127.   Serial.begin(9600);
  128.   Serial.println();
  129.   pinMode(LED_PIN, OUTPUT);
  130.   pinMode(RED_PIN, OUTPUT);
  131.   pinMode(GREEN_PIN, OUTPUT);
  132.   pinMode(BLUE_PIN, OUTPUT);
  133.   analogWrite(RED_PIN, 0);
  134.   analogWrite(GREEN_PIN, 0);
  135.   analogWrite(BLUE_PIN, 0);
  136.   wifiConnect();
  137.   mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
  138.   mqtt_client.setCallback(mqttCallback);
  139.   mqttConnect(mqtt_client);
  140.   mqttSubscribeAll();
  141. }
  142.  
  143. void loop() {
  144.   // Check connection to WiFi
  145.   if (WiFi.status() != WL_CONNECTED) {
  146.     Serial.println("WiFi connection lost.");
  147.     wifiConnect();
  148.   }
  149.  
  150.   // Check connection to MQTT
  151.   if (!mqtt_client.connected()) {
  152.     Serial.println("MQTT connection lost.");
  153.     mqttConnect(mqtt_client);
  154.     mqttSubscribeAll();
  155.   }
  156.  
  157.   // Listen to our subscriptions
  158.   mqtt_client.loop();
  159.  
  160.   if (mqtt_resp_new) {
  161.     if (strcmp(state_switch, "off")==0) {
  162.       setLights(0,0,0);
  163.     }
  164.     else if (strcmp(state_switch, "on")==0) {
  165.       setLights(state_color);
  166.     }
  167.     mqtt_resp_new = false;
  168.     logStates();
  169.   }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement