yotanutmaliwan

PieRelay Online version

Aug 15th, 2018
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /* PieRelay Online version */
  2.  
  3. #include "config.h" // load a NETPIE credential from file
  4. #include "PieRelay.h"
  5.  
  6. #include <ESP8266WiFi.h> // include a wifi library
  7. #include <MicroGear.h> // include a NETPIE library
  8.  
  9. #define RELAYPIN1 12
  10. #define RELAYPIN2 13
  11. #define BUTTONPIN1 2
  12. #define BUTTONPIN2 0
  13.  
  14. //NETPIE
  15. #define APPID ""
  16. #define KEY ""
  17. #define SECRET ""
  18. #define ALIAS ""
  19.  
  20. const char* ssid = "";
  21. const char* password = "";
  22.  
  23. WiFiClient client; // declare a wificlient
  24. MicroGear microgear(client); // declare microgear object
  25.  
  26. PieRelay *sw1, *sw2;
  27.  
  28. bool toPublishSW1State, toPublishSW2State;
  29.  
  30. void stateChange(int relayID, int type, int state) {
  31. Serial.print("relay "); Serial.print(relayID); Serial.print(" --> ");
  32. Serial.println(state?"ON":"OFF");
  33. if (relayID==1) toPublishSW1State = true;
  34. if (relayID==2) toPublishSW2State = true;
  35. }
  36.  
  37. void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
  38. char m = *(char *)msg;
  39. msg[msglen] = '\0';
  40. Serial.print("incoming message: "); Serial.print((char *)topic);
  41. Serial.print(" --> "); Serial.println((char*)msg);
  42.  
  43. if (msg[0]=='1' && msg[1]=='0') sw1->setState(0);
  44. if (msg[0]=='1' && msg[1]=='1') sw1->setState(1);
  45. if (msg[0]=='2' && msg[1]=='0') sw2->setState(0);
  46. if (msg[0]=='2' && msg[1]=='1') sw2->setState(1);
  47.  
  48. if (msg[0]=='1' && msg[1]=='?') toPublishSW1State = true;
  49. if (msg[0]=='2' && msg[1]=='?') toPublishSW2State = true;
  50. }
  51.  
  52. void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
  53. Serial.println("Connected to NETPIE...");
  54. microgear.setAlias(ALIAS);
  55. toPublishSW1State = true;
  56. toPublishSW2State = true;
  57. }
  58.  
  59. /* A function to init WIFI
  60. * This is just a basic method to configure WIFI on ESP8266.
  61. * There are other methods that provide better user experience,
  62. * but a little bit more complicated.
  63. */
  64. void initWiFi(){
  65. if (WiFi.begin(ssid, password)) {
  66. while (WiFi.status() != WL_CONNECTED) {
  67. delay(500);
  68. Serial.print(".");
  69. }
  70. }
  71. Serial.println("WiFi connected");
  72. Serial.println("IP address: ");
  73. Serial.println(WiFi.localIP());
  74. }
  75.  
  76. void setup() {
  77. Serial.begin(115200);
  78.  
  79. sw1 = new PieRelay(1, RELAYPIN1, BUTTONPIN1);
  80. sw2 = new PieRelay(2, RELAYPIN2, BUTTONPIN2);
  81.  
  82. sw1->setCallback(STATECHANGE, stateChange);
  83. sw2->setCallback(STATECHANGE, stateChange);
  84.  
  85. initWiFi(); // setup and connect to WiFi
  86.  
  87. // add event listeners
  88. microgear.on(MESSAGE,onMsghandler); // when new message arrives
  89. microgear.on(CONNECTED,onConnected); // when connected to NETPIE
  90. microgear.init(KEY,SECRET); // initial a micrigear variable
  91. microgear.connect(APPID); // let a device connects to NETPIE
  92. }
  93.  
  94. void loop() {
  95. if(WiFi.status()!=WL_CONNECTED){
  96. WiFi.disconnect();
  97. initWiFi();
  98. } else {
  99. if (microgear.connected()) {
  100.  
  101. // to check if switch 1 need to update its status
  102. if (toPublishSW1State) {
  103. // check the status and send message to the topic “/state1”
  104. microgear.publish("/state1",sw1->getState()?"1":"0");
  105. toPublishSW1State = false;
  106. }
  107. if (toPublishSW2State) {
  108. microgear.publish("/state2",sw2->getState()?"1":"0");
  109. toPublishSW2State = false;
  110. }
  111. microgear.loop();
  112. }
  113. else {
  114. microgear.connect(APPID);
  115. delay(500);
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment