Last active
September 8, 2019 18:07
-
-
Save jackrobotics/5674884cb7b6646de4c6d1dadafce000 to your computer and use it in GitHub Desktop.
This file contains 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 <WiFi.h> | |
#include <iSYNC.h> | |
WiFiClient client; | |
iSYNC iSYNC(client); | |
String ssid = "HONEYLab"; | |
String pass = "@HONEYLab"; | |
String iSYNC_USERNAME = "admin"; | |
String iSYNC_KEY = "---"; | |
String iSYNC_AUTH = "---"; //auth project | |
void callback(char* topic, byte* payload, unsigned int length) { | |
String msg = ""; | |
Serial.print("[iSYNC]-> "); | |
for (int i = 0; i < length; i++){ | |
msg+=(char)payload[i]; | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
if(msg.startsWith("LINE:"))msg = msg.substring(5); | |
/* command control */ | |
if(msg.equals("เปิดไฟ")){ | |
digitalWrite(2,LOW); | |
iSYNC.mqPub(iSYNC_KEY,"ได้เลยจ๊ะพี่จ๋า"); //Publish | |
}else if(msg.equals("ปิดไฟ")){ | |
digitalWrite(2,HIGH); | |
iSYNC.mqPub(iSYNC_KEY,"ได้เลยจ๊ะพี่จ๋า"); //Publish | |
} | |
} | |
void connectMQTT(){ | |
while(!iSYNC.mqConnect()){ | |
Serial.println("Reconnect MQTT..."); | |
delay(3000); | |
} | |
iSYNC.mqPub(iSYNC_KEY,"พร้อมรับคำสั่งแล้วจ๊ะพี่จ๋า"); //Publish on Connect | |
// iSYNC.mqSubProject(); //subscribe all key in your project | |
iSYNC.mqSub(iSYNC_KEY); //subscribe key | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(iSYNC.getVersion()); | |
pinMode(2,OUTPUT); // led on/off set output | |
iSYNC.begin(ssid,pass); | |
iSYNC.mqInit(iSYNC_USERNAME,iSYNC_AUTH); | |
iSYNC.MQTT->setCallback(callback); | |
connectMQTT(); | |
} | |
void loop() { | |
if (!iSYNC.mqConnected())connectMQTT(); | |
iSYNC.mqLoop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment