Skip to content

Instantly share code, notes, and snippets.

@pReya
Last active August 11, 2018 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pReya/576fb65aea87042cae615beedb4c2ec2 to your computer and use it in GitHub Desktop.
Save pReya/576fb65aea87042cae615beedb4c2ec2 to your computer and use it in GitHub Desktop.
#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);
// 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);
long lastMsg = 0;
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) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
digitalWrite(LED_BUILTIN, HIGH);
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;
}
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();
}
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(mqtt_resource, "hello world");
// ... and resubscribe
client.subscribe(mqtt_resource);
} 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(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);
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);
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment