Guest User

Untitled

a guest
Apr 17th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <iSYNC.h>
  3.  
  4. WiFiClient client;
  5. iSYNC iSYNC(client);
  6.  
  7. String ssid = "HONEYLab";
  8. String pass = "@HONEYLab";
  9.  
  10. String iSYNC_USERNAME = "admin";
  11. String iSYNC_KEY = "---";
  12. String iSYNC_AUTH = "---"; //auth project
  13.  
  14. void callback(char* topic, byte* payload, unsigned int length) {
  15. String msg = "";
  16. Serial.print("[iSYNC]-> ");
  17. for (int i = 0; i < length; i++){
  18. msg+=(char)payload[i];
  19. Serial.print((char)payload[i]);
  20. }
  21. Serial.println();
  22. if(msg.startsWith("LINE:"))msg = msg.substring(5);
  23.  
  24. /* command control */
  25. if(msg.equals("เปิดไฟ")){
  26. digitalWrite(2,LOW);
  27. iSYNC.mqPub(iSYNC_KEY,"ได้เลยจ๊ะพี่จ๋า"); //Publish
  28. }else if(msg.equals("ปิดไฟ")){
  29. digitalWrite(2,HIGH);
  30. iSYNC.mqPub(iSYNC_KEY,"ได้เลยจ๊ะพี่จ๋า"); //Publish
  31. }
  32. }
  33.  
  34. void connectMQTT(){
  35. while(!iSYNC.mqConnect()){
  36. Serial.println("Reconnect MQTT...");
  37. delay(3000);
  38. }
  39. iSYNC.mqPub(iSYNC_KEY,"พร้อมรับคำสั่งแล้วจ๊ะพี่จ๋า"); //Publish on Connect
  40. // iSYNC.mqSubProject(); //subscribe all key in your project
  41. iSYNC.mqSub(iSYNC_KEY); //subscribe key
  42. }
  43.  
  44. void setup() {
  45. Serial.begin(115200);
  46. Serial.println(iSYNC.getVersion());
  47.  
  48. pinMode(2,OUTPUT); // led on/off set output
  49.  
  50. iSYNC.begin(ssid,pass);
  51. iSYNC.mqInit(iSYNC_USERNAME,iSYNC_AUTH);
  52. iSYNC.mqCallback(callback);
  53. connectMQTT();
  54. }
  55.  
  56. void loop() {
  57. if (!iSYNC.mqConnected())connectMQTT();
  58. iSYNC.mqLoop();
  59. }
Add Comment
Please, Sign In to add comment