#include <ESP8266WiFi.h>
#include <MicroGear.h>
const char* ssid= "wifi_name";
const char* password = "wifi_password";
#define APPID "Application_ID"
#define KEY "Application_ KEY"
#define SECRET "Application_SECRET"
#define ALIAS "Board1"
#define TARGET "LivingRoom"
#define LED1 D1
#define LED2 D2
#define LED3 D3
#define LED4 D4
WiFiClient client;
int timer = 0;
MicroGear microgear(client);
void onMsghandler(char* topic, uint8_t* msg, unsigned int msglen) {
Serial.print("Incoming message --> ");
Serial.print(topic);
Serial.print(" : ");
char strState[msglen];
for (int i = 0; i < msglen; i++) {
strState[i] = (char)msg[i];
Serial.print((char)msg[i]);
}
String stateStr = String(strState).substring(0, msglen);
Serial.println(stateStr);
if (stateStr == "LED1ON") digitalWrite(LED1, HIGH);
else if (stateStr == "LED1OFF") digitalWrite(LED1, LOW);
else if (stateStr == "LED2ON") digitalWrite(LED2, HIGH);
else if (stateStr == "LED2OFF") digitalWrite(LED2, LOW);
else if (stateStr == "LED3ON") digitalWrite(LED3, HIGH);
else if (stateStr == "LED3OFF") digitalWrite(LED3, LOW);
else if (stateStr == "LED4ON") digitalWrite(LED4, HIGH);
else if (stateStr == "LED4OFF") digitalWrite(LED4, LOW);
}
void onFoundgear(char* attribute, uint8_t* msg, unsigned int msglen) {
Serial.print("Found new member --> ");
for (int i = 0; i < msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
void onLostgear(char* attribute, uint8_t* msg, unsigned int msglen) {
Serial.print("Lost member --> ");
for (int i = 0; i < msglen; i++)
Serial.print((char)msg[i]);
Serial.println();
}
/*When a microgear is connected, dothis*/
void onConnected(char* attribute, uint8_t* msg, unsigned int msglen) {
Serial.println("Connected to NETPIE...");
/* Set the alias of this microgearALIAS */
microgear.setAlias(ALIAS);
}
void setup()
{
microgear.on(MESSAGE, onMsghandler);
microgear.on(PRESENT, onFoundgear);
microgear.on(ABSENT, onLostgear);
microgear.on(CONNECTED, onConnected);
Serial.begin(115200);
Serial.println("Starting...");
pinMode(LED1, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(LED3, OUTPUT);
pinMode(LED4, OUTPUT);
digitalWrite(LED1, LOW);
digitalWrite(LED2, LOW);
digitalWrite(LED3, LOW);
digitalWrite(LED4, LOW);
if (WiFi.begin(ssid, password))
{
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
}
Serial.println("WiFiconnected");
microgear.init(KEY, SECRET, ALIAS);
microgear.connect(APPID);
}
void loop()
{
if (microgear.connected()) {
microgear.loop();
String status_LED1,status_LED2,status_LED3,status_LED4,data2freeboard;
status_LED1= digitalRead(LED1)? "1,":"0,";
status_LED2= digitalRead(LED2)? "1,":"0,";
status_LED3= digitalRead(LED3)? "1,":"0,";
status_LED4= digitalRead(LED4)? "1,":"0,";
data2freeboard = status_LED1+status_LED2+status_LED3+status_LED4 ;
microgear.chat(TARGET, data2freeboard);
Serial.print("Send message to NetPie: ");
Serial.println(data2freeboard);
}
else {
Serial.println("connection lost, reconnect...");
if (timer >= 5000) {
microgear.connect(APPID);
timer = 0;
}
else timer += 100;
}
delay(1000);
}