Guest User

Touch Sonoff NETPIE

a guest
Aug 7th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.86 KB | None | 0 0
  1. #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
  2. #include <ESP8266WebServer.h>
  3. #include <MicroGear.h> // include a NETPIE library
  4.  
  5. //NETPIE
  6. #define APPID "Your application ID"
  7. #define KEY "Your device key"
  8. #define SECRET "Your device secret key"
  9. #define ALIAS "name to make you remember"
  10.  
  11. const char* ssid = "your wifi network name";
  12. const char* password = "your wifi password";
  13.  
  14. //GPIO Pin definitions
  15. #define WiFiLED 13
  16. #define TouchLED 12 //Also operates Relay coil
  17. #define TouchInput 0
  18.  
  19. //Only output info to serial if this is HIGH
  20. #define fDebug true
  21.  
  22. //Set our inital states and values
  23. boolean ledState = false;
  24. boolean lastButtonState = false;
  25. boolean buttonState = false;
  26. unsigned long lastDebounceTime = 0;
  27. // the last time the output pin was toggled
  28. unsigned long debounceDelay = 50;
  29. // the debounce time; increase if the output flickers
  30. int reading = digitalRead(TouchInput);
  31.  
  32. const char* LIGHT_ON = "1";
  33. const char* LIGHT_OFF = "0";
  34. bool toPublishSWState;
  35.  
  36. WiFiClient client; // declare a wificlient
  37. MicroGear microgear(client); // declare microgear object
  38.  
  39. void stateChange(int relayID, int type, int state) {
  40. Serial.print("relay ");
  41. Serial.print(relayID);
  42. Serial.print(" --> ");
  43. Serial.println(state?"ON":"OFF");
  44. if (relayID==1) toPublishSWState = true;
  45. }
  46.  
  47. void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
  48. char m = *(char *)msg;
  49. msg[msglen] = '\0';
  50. Serial.print("incoming message: ");
  51. Serial.print((char *)topic);
  52. Serial.print(" --> ");
  53. Serial.println((char*)msg);
  54.  
  55. if (msg[0]=='1' && msg[1]=='0')
  56. {
  57. buttonState = reading;
  58.  
  59. ledState = false;
  60. // set the LED:
  61. setLightState();
  62. microgear.publish("/state",ledState);
  63. }
  64. if (msg[0]=='1' && msg[1]=='1')
  65. {
  66. buttonState = reading;
  67.  
  68. ledState = true;
  69. // set the LED:
  70. setLightState();
  71. microgear.publish("/state",ledState);
  72. }
  73. if (msg[0]=='1' && msg[1]=='?')
  74. {
  75. toPublishSWState = true;
  76. }
  77. }
  78.  
  79. void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
  80. Serial.println("Connected to NETPIE...");
  81. microgear.setAlias(ALIAS);
  82. toPublishSWState = true;
  83. }
  84.  
  85. void initWiFi(){
  86. if (WiFi.begin(ssid, password)) {
  87. while (WiFi.status() != WL_CONNECTED) {
  88. delay(500);
  89. Serial.print(".");
  90. }
  91. }
  92. Serial.println("WiFi connected");
  93. Serial.println("IP address: ");
  94. Serial.println(WiFi.localIP());
  95. }
  96.  
  97. void setup() {
  98.  
  99. //Setup our GPIO pins for input/output as needed
  100. pinMode(WiFiLED, OUTPUT);
  101. pinMode(TouchLED, OUTPUT);
  102. pinMode(TouchInput, INPUT);
  103.  
  104. //Turn those LEDS's off :)
  105. digitalWrite(WiFiLED, HIGH);
  106. digitalWrite(TouchLED, ledState);
  107.  
  108. initWiFi();
  109.  
  110. // add event listeners
  111. microgear.on(MESSAGE, onMsghandler); // when new message arrives
  112. microgear.on(CONNECTED, onConnected); // when connected to NETPIE
  113. microgear.init(KEY, SECRET); // initial a micrigear variable
  114. microgear.connect(APPID); // let a device connects to NETPIE
  115.  
  116. }
  117. // function called to publish the state of the light (on/off)
  118. void publishLightState() {
  119. if (fDebug) {
  120. Serial.println("PublishLightState Called");
  121. Serial.print("Light State: ");
  122. Serial.println(ledState);
  123. }
  124. if (ledState == HIGH) {
  125. if (fDebug) {
  126. Serial.print("Light state high ");
  127. Serial.println(ledState);
  128. }
  129. // client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
  130. } else {
  131. if (fDebug) {
  132. Serial.print("Light state low ");
  133. Serial.println(ledState);
  134. }
  135. // client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
  136. }
  137. }
  138.  
  139. // function called to turn on/off the light
  140. void setLightState() {
  141. //Set our light state, ie turn the touch LED on/off
  142. //which is the same GPIO pin as the relay.
  143. digitalWrite(TouchLED, ledState);
  144. //Feedback to HA our new state
  145. publishLightState();
  146. }
  147.  
  148.  
  149. void loop() {
  150. /**
  151. * THis section is to let the NETPIE control the system
  152. */
  153.  
  154. if(WiFi.status()!=WL_CONNECTED){
  155. WiFi.disconnect();
  156. initWiFi();
  157. } else {
  158. if (microgear.connected()) {
  159.  
  160. // to check if switch 1 need to update its status
  161. if (toPublishSWState) {
  162.  
  163. // check the status and send message to the topic “/state”
  164. // if (ledState) microgear.publish("/state","1");
  165. // if (!ledState) microgear.publish("/state","0");
  166. //
  167. microgear.publish("/state",ledState);
  168. toPublishSWState = false;
  169. }
  170. microgear.loop();
  171. }
  172. else {
  173. microgear.connect(APPID);
  174. delay(500);
  175. }
  176. }
  177.  
  178. /**
  179. * This section is the physical touch condition
  180. */
  181. //Get the current button state
  182. reading = digitalRead(TouchInput);
  183. //De-bounce the button
  184. if (reading != lastButtonState) {
  185. lastDebounceTime = millis();
  186. }
  187. if ((millis() - lastDebounceTime) > debounceDelay) {
  188. // whatever the reading is at, it's been there for longer
  189. // than the debounce delay, so take it as the actual current state:
  190.  
  191. // if the button state has changed:
  192. if (reading != buttonState) {
  193. buttonState = reading;
  194.  
  195. // only toggle the LED if the new button state is HIGH
  196. if (buttonState == HIGH) {
  197. ledState = !ledState;
  198. }
  199. // set the LED:
  200. setLightState();
  201. microgear.publish("/state",ledState);
  202. }
  203. }
  204. // save the reading. Next time through the loop,
  205. // it'll be the lastButtonState:
  206. lastButtonState = reading;
  207. delay(64);
  208. // client.loop();
  209. }
Advertisement
Add Comment
Please, Sign In to add comment