Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Simple program to publish and subscribe to a topic
- * from an MQTT server using the PubSub.
- * Use as a base for more interesting code.
- */
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- // Override the maximum message size
- // Client and Topic setups
- #define MQTT_CLIENT_ID "Development_Strip"
- #define MQTT_COLOR_TOPIC "smartthings/Development Strip/color/state"
- #define MQTT_BRIGHTNESS_TOPIC "smartthings/Development Strip/level/state"
- #define MQTT_SWITCH_TOPIC "smartthings/Development Strip/switch/state"
- #define MQTT_MODE_TOPIC "smartthings/Development Strip/button/state"
- #define MQTT_STATUS_TOPIC "smartthings/Development Strip/status/cmd"
- // LED setups
- #define LED_PIN 2 // On-board LED
- #define RED_PIN D6 // Pin for controlling the RED channel.
- #define GREEN_PIN D5 // Pin for controlling the GREEN channel.
- #define BLUE_PIN D8 // Pin for controlling the BLUE channel.
- // Network setup
- #define WIFI_SSID "Green Bowser"
- #define WIFI_PASSWORD "SpandexSkank"
- #define MQTT_SERVER "192.168.1.45"
- #define MQTT_PORT 1883
- #define MQTT_USERNAME "Epidurality"
- #define MQTT_PASSWORD "mqttPass"
- // MQTT client
- WiFiClient wifi_client;
- PubSubClient mqtt_client(wifi_client);
- // Holders for last events
- char* state_switch = "off";
- char* state_brightness = "100";
- char* state_color = "#ffffff";
- char* state_mode = "err";
- bool mqtt_resp_new = false;
- // Uses the preset parameters to setup a wifi connection.
- void wifiConnect() {
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to WiFi");
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println(" Connected!");
- Serial.print("IP Address: ");
- Serial.println(WiFi.localIP());
- }
- // Attempts connection to MQTT server passed to it.
- void mqttConnect(PubSubClient &c) {
- while (!c.connected()) {
- Serial.print("Attempting MQTT connection...");
- if (c.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) {
- Serial.println(" Connected!");
- } else {
- Serial.print(" Failed, rc=");
- Serial.print(c.state());
- Serial.println(". Trying again in 5 seconds...");
- delay(5000);
- }
- }
- }
- void mqttCallback(char* topic, byte* payload, unsigned int length) {
- char* message = (char*)payload;
- message[length] = '\0';
- Serial.print("Message received on topic ");
- Serial.println(topic);
- Serial.print("Payload:");
- Serial.println(message);
- if (strcmp(topic, MQTT_SWITCH_TOPIC)==0) {
- strcpy(state_switch, message);
- mqtt_resp_new = true;
- } else if (strcmp(topic, MQTT_BRIGHTNESS_TOPIC)==0) {
- strcpy(state_brightness,message);
- mqtt_resp_new = true;
- } else if (strcmp(topic, MQTT_COLOR_TOPIC)==0) {
- char* excerpt = strstr(message, "#");
- for (int i=0; i<7; i++) {
- state_color[i] = excerpt[i];
- }
- mqtt_resp_new = true;
- } else if (strcmp(topic, MQTT_MODE_TOPIC)==0) {
- strcpy(state_mode,message);
- mqtt_resp_new = true;
- } else {
- mqtt_client.publish(MQTT_STATUS_TOPIC, payload, true);
- }
- }
- void mqttSubscribeAll() {
- mqtt_client.subscribe(MQTT_SWITCH_TOPIC);
- mqtt_client.subscribe(MQTT_BRIGHTNESS_TOPIC);
- mqtt_client.subscribe(MQTT_COLOR_TOPIC);
- mqtt_client.subscribe(MQTT_MODE_TOPIC);
- }
- void setLights(int r, int g, int b) {
- analogWrite(RED_PIN, r*4);
- analogWrite(GREEN_PIN, g*4);
- analogWrite(BLUE_PIN, b*4);
- }
- void setLights(char* hex_col) {
- Serial.println(hex_col);
- long hex_as_long = strtol(&hex_col[1], NULL, 16);
- long r = hex_as_long >> 16;
- long g = hex_as_long >> 8 & 0xFF;
- long b = hex_as_long & 0xFF;
- setLights(r, g, b);
- }
- void logStates() {
- char state_log[256];
- sprintf(state_log, "Switch: %s | Brightness: %s | Color: %s | Mode: %s", state_switch, state_brightness, state_color, state_mode);
- Serial.println(state_log);
- }
- void setup() {
- Serial.begin(9600);
- Serial.println();
- pinMode(LED_PIN, OUTPUT);
- pinMode(RED_PIN, OUTPUT);
- pinMode(GREEN_PIN, OUTPUT);
- pinMode(BLUE_PIN, OUTPUT);
- analogWrite(RED_PIN, 0);
- analogWrite(GREEN_PIN, 0);
- analogWrite(BLUE_PIN, 0);
- wifiConnect();
- mqtt_client.setServer(MQTT_SERVER, MQTT_PORT);
- mqtt_client.setCallback(mqttCallback);
- mqttConnect(mqtt_client);
- mqttSubscribeAll();
- }
- void loop() {
- // Check connection to WiFi
- if (WiFi.status() != WL_CONNECTED) {
- Serial.println("WiFi connection lost.");
- wifiConnect();
- }
- // Check connection to MQTT
- if (!mqtt_client.connected()) {
- Serial.println("MQTT connection lost.");
- mqttConnect(mqtt_client);
- mqttSubscribeAll();
- }
- // Listen to our subscriptions
- mqtt_client.loop();
- if (mqtt_resp_new) {
- if (strcmp(state_switch, "off")==0) {
- setLights(0,0,0);
- }
- else if (strcmp(state_switch, "on")==0) {
- setLights(state_color);
- }
- mqtt_resp_new = false;
- logStates();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement