Last active
August 11, 2018 14:02
-
-
Save pReya/576fb65aea87042cae615beedb4c2ec2 to your computer and use it in GitHub Desktop.
Revisions
-
pReya revised this gist
Aug 11, 2018 . 1 changed file with 35 additions and 19 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,12 +8,17 @@ Adafruit_NeoPixel led = Adafruit_NeoPixel(NUMBER_PIXELS, LEDPIN, NEO_RGB + NEO_KHZ800); // Wifi Credentials const char* ssid = "WeWork"; const char* password = "p@ssw0rd"; const char* mqtt_server = "mqtt.beebotte.com"; // For Beebotte, the MQTT token must be your "secret token", not the channel token const char* mqtt_token = "abc123"; // For Beebotte, this should be "channel/resource" const char* mqtt_resource="ESP8266/led"; WiFiClient espClient; PubSubClient client(espClient); @@ -22,14 +27,11 @@ char msg[50]; int value = 0; void setup_wifi() { delay(10); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { @@ -38,7 +40,7 @@ void setup_wifi() { } randomSeed(micros()); digitalWrite(LED_BUILTIN, HIGH); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); @@ -56,24 +58,29 @@ void onMessage(char* topic, byte* payload, unsigned int length) { return; } uint8_t red = root["red"]; uint8_t green = root["green"]; uint8_t blue = root["blue"]; // Print the received value to serial monitor for debugging Serial.println(); Serial.println("------------------------------------"); Serial.print("Received new message of length "); Serial.print(length); Serial.println(); Serial.println("Raw message: "); root.prettyPrintTo(Serial); Serial.println(); Serial.print("Red: "); Serial.print(red); Serial.print(" Green: "); Serial.print(green); Serial.print(" Blue: "); Serial.println(blue); Serial.println("------------------------------------"); Serial.println(); led.setPixelColor(NUMBER_PIXELS-1, red, green, blue); led.show(); } @@ -89,9 +96,9 @@ void reconnect() { if (client.connect(clientId.c_str(),mqtt_token,"")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish(mqtt_resource, "hello world"); // ... and resubscribe client.subscribe(mqtt_resource); } else { Serial.print("failed, rc="); Serial.print(client.state()); @@ -103,7 +110,16 @@ void reconnect() { } void setup() { pinMode(LED_BUILTIN, OUTPUT); // Turn off builtin led digitalWrite(LED_BUILTIN, LOW); // Initialize Neopixel led.begin(); led.setPixelColor(NUMBER_PIXELS-1, 0, 0, 0); led.show(); Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); -
pReya created this gist
Aug 10, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,129 @@ #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <ArduinoJson.h> #include <Adafruit_NeoPixel.h> #define LEDPIN D4 #define NUMBER_PIXELS 1 Adafruit_NeoPixel led = Adafruit_NeoPixel(NUMBER_PIXELS, LEDPIN, NEO_RGB + NEO_KHZ800); // Update these with values suitable for your network. const char* ssid = "WeWork"; const char* password = "P@ssw0rd"; const char* mqtt_server = "mqtt.beebotte.com"; const char* mqtt_token = "USERTOKENHERE"; WiFiClient espClient; PubSubClient client(espClient); long lastMsg = 0; char msg[50]; int value = 0; void setup_wifi() { delay(10); // We start by connecting to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); led.begin(); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void onMessage(char* topic, byte* payload, unsigned int length) { // decode the JSON payload StaticJsonBuffer<128> jsonInBuffer; JsonObject& root = jsonInBuffer.parseObject(payload); // Test if parsing succeeds. if (!root.success()) { Serial.println("parseObject() failed"); return; } // led resource is a boolean read it accordingly byte red = root["red"]; byte green = root["green"]; byte blue = root["blue"]; // Print the received value to serial monitor for debugging Serial.print("Received message of length "); Serial.print(length); Serial.println(); Serial.print("Red: "); Serial.print(red); Serial.print(" Green: "); Serial.print(green); Serial.print(" Blue: "); Serial.print(blue); Serial.println(); led.setPixelColor(NUMBER_PIXELS,led.Color(red, green, blue)); led.show(); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str(),mqtt_token,"")) { Serial.println("connected"); // Once connected, publish an announcement... client.publish("ESP8266/led", "hello world"); // ... and resubscribe client.subscribe("ESP8266/led"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output Serial.begin(115200); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(onMessage); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); // long now = millis(); // if (now - lastMsg > 2000) { // lastMsg = now; // ++value; // snprintf (msg, 75, "hello world #%ld", value); // Serial.print("Publish message: "); // Serial.println(msg); // client.publish("ESP8266/led", msg); // } }