Created
August 16, 2016 08:32
-
-
Save compilerexe/ee2fbe5de450cd303cf6c2bbb2668bc0 to your computer and use it in GitHub Desktop.
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 characters
| #include <ESP8266WiFi.h> | |
| #include <MicroGear.h> | |
| const char* ssid = "HomeModel"; | |
| const char* password = "SmartHome"; | |
| #define APPID "HomeModel" | |
| #define KEY "D4jjs3sj7iktGgP" | |
| #define SECRET "h1F2WCI6t45pXQOj64lNMTGzw" | |
| #define ALIAS "NodeMCU" | |
| #define ROOM1 D1 | |
| #define ROOM2 D2 | |
| #define ROOM3 D3 | |
| #define ROOM4 D4 | |
| WiFiClient client; | |
| int timer = 0, state_connect = 0; | |
| MicroGear microgear(client); | |
| void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) { | |
| // Serial.print("Incoming message --> "); | |
| msg[msglen] = '\0'; | |
| // Serial.println((char *)msg); | |
| char read_msg[3]; | |
| for (int i=0; i<msglen; i++) { | |
| read_msg[i] = (char)msg[i]; | |
| } | |
| // int a = (int)read_msg[0] - 48; | |
| // int b = (int)read_msg[1] - 48; | |
| // int c = (a * 10) + b; | |
| // Serial.println(c); | |
| if (read_msg[0] == 'R') { | |
| switch (read_msg[1]) { | |
| case '1' : if (read_msg[2] == '1') { Serial.println("ROOM 1 : ON"); digitalWrite(ROOM1, 0); } else { Serial.println("ROOM 1 : OFF"); digitalWrite(ROOM1, 1); } break; | |
| case '2' : if (read_msg[2] == '1') { Serial.println("ROOM 2 : ON"); digitalWrite(ROOM2, 0); } else { Serial.println("ROOM 2 : OFF"); digitalWrite(ROOM2, 1); } break; | |
| case '3' : if (read_msg[2] == '1') { Serial.println("ROOM 3 : ON"); digitalWrite(ROOM3, 0); } else { Serial.println("ROOM 3 : OFF"); digitalWrite(ROOM3, 1); } break; | |
| case '4' : if (read_msg[2] == '1') { Serial.println("ROOM 4 : ON"); digitalWrite(ROOM4, 0); } else { Serial.println("ROOM 4 : OFF"); digitalWrite(ROOM4, 1); } break; | |
| } | |
| } | |
| } | |
| 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(); | |
| } | |
| void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) { | |
| Serial.println("Connected to NETPIE..."); | |
| microgear.setAlias(ALIAS); | |
| } | |
| void setup() { | |
| pinMode(ROOM1, OUTPUT); | |
| pinMode(ROOM2, OUTPUT); | |
| pinMode(ROOM3, OUTPUT); | |
| pinMode(ROOM4, OUTPUT); | |
| digitalWrite(ROOM1, 1); | |
| digitalWrite(ROOM2, 1); | |
| digitalWrite(ROOM3, 1); | |
| digitalWrite(ROOM4, 1); | |
| microgear.on(MESSAGE,onMsghandler); | |
| microgear.on(PRESENT,onFoundgear); | |
| microgear.on(ABSENT,onLostgear); | |
| microgear.on(CONNECTED,onConnected); | |
| Serial.begin(115200); | |
| Serial.println("Starting..."); | |
| if (WiFi.begin(ssid, password)) { | |
| while (WiFi.status() != WL_CONNECTED) { | |
| delay(500); | |
| Serial.print("."); | |
| } | |
| } | |
| Serial.println(); | |
| Serial.println("WiFi connected"); | |
| Serial.println("IP address: "); | |
| Serial.println(WiFi.localIP()); | |
| microgear.init(KEY,SECRET,ALIAS); | |
| microgear.connect(APPID); | |
| } | |
| void loop() { | |
| if (microgear.connected()) { | |
| if (state_connect == 0) { | |
| Serial.println("connected"); | |
| state_connect = 1; | |
| } | |
| microgear.loop(); | |
| if (timer >= 1000) { | |
| // Serial.println("Publish..."); | |
| // microgear.chat(ALIAS,"Hello"); | |
| timer = 0; | |
| } | |
| else timer += 100; | |
| } | |
| else { | |
| Serial.println("connection lost, reconnect..."); | |
| if (timer >= 5000) { | |
| microgear.connect(APPID); | |
| timer = 0; | |
| state_connect = 0; | |
| } | |
| else timer += 100; | |
| } | |
| delay(100); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment