Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
- #include <ESP8266WebServer.h>
- #include <MicroGear.h> // include a NETPIE library
- //NETPIE
- #define APPID "Your application ID"
- #define KEY "Your device key"
- #define SECRET "Your device secret key"
- #define ALIAS "name to make you remember"
- const char* ssid = "your wifi network name";
- const char* password = "your wifi password";
- //GPIO Pin definitions
- #define WiFiLED 13
- #define TouchLED 12 //Also operates Relay coil
- #define TouchInput 0
- //Only output info to serial if this is HIGH
- #define fDebug true
- //Set our inital states and values
- boolean ledState = false;
- boolean lastButtonState = false;
- boolean buttonState = false;
- unsigned long lastDebounceTime = 0;
- // the last time the output pin was toggled
- unsigned long debounceDelay = 50;
- // the debounce time; increase if the output flickers
- int reading = digitalRead(TouchInput);
- const char* LIGHT_ON = "1";
- const char* LIGHT_OFF = "0";
- bool toPublishSWState;
- WiFiClient client; // declare a wificlient
- MicroGear microgear(client); // declare microgear object
- void stateChange(int relayID, int type, int state) {
- Serial.print("relay ");
- Serial.print(relayID);
- Serial.print(" --> ");
- Serial.println(state?"ON":"OFF");
- if (relayID==1) toPublishSWState = true;
- }
- void onMsghandler(char *topic, uint8_t* msg, unsigned int msglen) {
- char m = *(char *)msg;
- msg[msglen] = '\0';
- Serial.print("incoming message: ");
- Serial.print((char *)topic);
- Serial.print(" --> ");
- Serial.println((char*)msg);
- if (msg[0]=='1' && msg[1]=='0')
- {
- buttonState = reading;
- ledState = false;
- // set the LED:
- setLightState();
- microgear.publish("/state",ledState);
- }
- if (msg[0]=='1' && msg[1]=='1')
- {
- buttonState = reading;
- ledState = true;
- // set the LED:
- setLightState();
- microgear.publish("/state",ledState);
- }
- if (msg[0]=='1' && msg[1]=='?')
- {
- toPublishSWState = true;
- }
- }
- void onConnected(char *attribute, uint8_t* msg, unsigned int msglen) {
- Serial.println("Connected to NETPIE...");
- microgear.setAlias(ALIAS);
- toPublishSWState = true;
- }
- void initWiFi(){
- if (WiFi.begin(ssid, password)) {
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- }
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void setup() {
- //Setup our GPIO pins for input/output as needed
- pinMode(WiFiLED, OUTPUT);
- pinMode(TouchLED, OUTPUT);
- pinMode(TouchInput, INPUT);
- //Turn those LEDS's off :)
- digitalWrite(WiFiLED, HIGH);
- digitalWrite(TouchLED, ledState);
- initWiFi();
- // add event listeners
- microgear.on(MESSAGE, onMsghandler); // when new message arrives
- microgear.on(CONNECTED, onConnected); // when connected to NETPIE
- microgear.init(KEY, SECRET); // initial a micrigear variable
- microgear.connect(APPID); // let a device connects to NETPIE
- }
- // function called to publish the state of the light (on/off)
- void publishLightState() {
- if (fDebug) {
- Serial.println("PublishLightState Called");
- Serial.print("Light State: ");
- Serial.println(ledState);
- }
- if (ledState == HIGH) {
- if (fDebug) {
- Serial.print("Light state high ");
- Serial.println(ledState);
- }
- // client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_ON, true);
- } else {
- if (fDebug) {
- Serial.print("Light state low ");
- Serial.println(ledState);
- }
- // client.publish(MQTT_LIGHT_STATE_TOPIC, LIGHT_OFF, true);
- }
- }
- // function called to turn on/off the light
- void setLightState() {
- //Set our light state, ie turn the touch LED on/off
- //which is the same GPIO pin as the relay.
- digitalWrite(TouchLED, ledState);
- //Feedback to HA our new state
- publishLightState();
- }
- void loop() {
- /**
- * THis section is to let the NETPIE control the system
- */
- if(WiFi.status()!=WL_CONNECTED){
- WiFi.disconnect();
- initWiFi();
- } else {
- if (microgear.connected()) {
- // to check if switch 1 need to update its status
- if (toPublishSWState) {
- // check the status and send message to the topic “/state”
- // if (ledState) microgear.publish("/state","1");
- // if (!ledState) microgear.publish("/state","0");
- //
- microgear.publish("/state",ledState);
- toPublishSWState = false;
- }
- microgear.loop();
- }
- else {
- microgear.connect(APPID);
- delay(500);
- }
- }
- /**
- * This section is the physical touch condition
- */
- //Get the current button state
- reading = digitalRead(TouchInput);
- //De-bounce the button
- if (reading != lastButtonState) {
- lastDebounceTime = millis();
- }
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // whatever the reading is at, it's been there for longer
- // than the debounce delay, so take it as the actual current state:
- // if the button state has changed:
- if (reading != buttonState) {
- buttonState = reading;
- // only toggle the LED if the new button state is HIGH
- if (buttonState == HIGH) {
- ledState = !ledState;
- }
- // set the LED:
- setLightState();
- microgear.publish("/state",ledState);
- }
- }
- // save the reading. Next time through the loop,
- // it'll be the lastButtonState:
- lastButtonState = reading;
- delay(64);
- // client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment