Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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.mqCallback(callback);
- connectMQTT();
- }
- void loop() {
- if (!iSYNC.mqConnected())connectMQTT();
- iSYNC.mqLoop();
- }
Add Comment
Please, Sign In to add comment