Home automation with ESP8266


hi, doing project google home, ifttt , nodemcu esp8266 board. although i'm not using arduino board, i'm using arduino ide. in ifttt, webhooks template, json script is,
 
code: [select]
{"location":"bedroom","device":"computer", "state":"off","query":"cmd"}
in ide script line, change "relaypin"(signal pin relay) control different relays based on "device".
code: [select]
if(device=="lights"){
              int relaypin = 16;
            }else if(device=="computer"){
              int relaypin = 5;
              pinmode(relaypin, output);
            }

although 1 of applets, "turn computer off" (json code seen above) turns on both relays (pin 16, 5) instead of turning off pin 5. if sees problem may causing please tell me.

full ide code,
code: [select]

#include <arduinojson.h>
#include <esp8266wifi.h>
#include <esp8266wifimulti.h>
#include <websocketsclient.h>
#include <hash.h>


// @@@@@@@@@@@@@@@ need midify modify wi-fi , domain info @@@@@@@@@@@@@@@@@@@@
const char* ssid     = "netgear56-5g"; //enter ssid/ wi-fi(case sensitive) router name - 2.4 ghz only
const char* password = "hungrysheep940";     // enter ssid password (case sensitive)
char host[] = "googlehome1.herokuapp.com"; //192.168.0.100 -enter heroku domain name like  "alexagoogleifttt.herokuapp.com"
// @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

int port = 80;
char path[] = "/ws";
esp8266wifimulti wifimulti;
websocketsclient websocket;
 int relaypin = 16;
dynamicjsonbuffer jsonbuffer;
string currstate;
int pingcount = 0;
void websocketevent(wstype_t type, uint8_t * payload, size_t length) { //uint8_t *


    switch(type) {
        case wstype_disconnected:
           serial.println("disconnected! ");
           serial.println("connecting...");
               websocket.begin(host, port, path);
               websocket.onevent(websocketevent);
            break;
           
        case wstype_connected:
            {
             serial.println("connected! ");
          // send message server when connected
            websocket.sendtxt("connected");
            }
            break;
           
        case wstype_text:
            serial.println("got data");
              //data = (char*)payload;
           processwebscoketrequest((char*)payload);
            break;
           
        case wstype_bin:

            hexdump(payload, length);
            serial.print("got bin");
            // send data server
            websocket.sendbin(payload, length);
            break;
    }

}

void setup() {
    serial.begin(115200);
    serial.setdebugoutput(true);
   
    pinmode(relaypin, output);
   
      for(uint8_t t = 4; t > 0; t--) {
          delay(1000);
      }
    serial.println();
    serial.println();
    serial.print("connecting ");
   
    //serial.println(ssid);
    wifimulti.addap(ssid, password);

    //wifi.disconnect();
    while(wifimulti.run() != wl_connected) {
      serial.print(".");
        delay(100);
    }
    serial.println("connected wi-fi");
    websocket.begin(host, port, path);
    websocket.onevent(websocketevent);

}

void loop() {
    websocket.loop();
//if make change delay make sure adjust ping
    delay(2000);
// make sure after every 40 seconds send ping heroku
//so not terminate websocket connection
//this keep conncetion alive between esp , heroku
if (pingcount > 20) {
pingcount = 0;
websocket.sendtxt("\"heartbeat\":\"keepalive\"");
}
else {
pingcount += 1;
}
}

void processwebscoketrequest(string data){

            jsonobject& root = jsonbuffer.parseobject(data);
            string device = (const char*)root["device"];
            string location = (const char*)root["location"];
            string state = (const char*)root["state"];
            string query = (const char*)root["query"];
            string message="";

            serial.println(data);
            serial.println(state);
            if(device=="lights"){
              int relaypin = 16;
            }else if(device=="computer"){
              int relaypin = 5;
              pinmode(relaypin, output);
            }
           
            if(query == "cmd"){ //if query check state
              serial.println("recieved command!");
                    if(state=="on"){
                      digitalwrite(relaypin, high);
                      message = "{\"state\":\"on\"}";
                      currstate = "on";
                    }else{
                      digitalwrite(relaypin, low);
                      message = "{\"state\":\"off\"}";
                      currstate = "off";
                    }
                 
            }else if(query == "?"){ //if command execute   
              serial.println("recieved query!");
              int state = digitalread(relaypin);
                 if(currstate=="on"){
                      message = "{\"state\":\"on\"}";
                    }else{
                      message = "{\"state\":\"off\"}";
                    }
            }else{//can not recognized command
              serial.println("command not recognized!");
            }
            serial.print("sending response back");
            serial.println(message);
                  // send message server
                  websocket.sendtxt(message);
                  if(query == "cmd" || query == "?"){websocket.sendtxt(message);}
}

skimming code found many problems. first of indentation: press ctrl-t in ide , fixes you. makes code readable, it's mess now.

then found this:
code: [select]

            serial.println(state);
            if(device=="lights"){
              int relaypin = 16;
            }else if(device=="computer"){
              int relaypin = 5;
              pinmode(relaypin, output);
            }
           
            if(query == "cmd"){ //if query check state
              serial.println("recieved command!");
                    if(state=="on"){
                      digitalwrite(relaypin, high);
                      message = "{\"state\":\"on\"}";
                      currstate = "on";
                    }else{
                      digitalwrite(relaypin, low);
                      message = "{\"state\":\"off\"}";
                      currstate = "off";
                    }


that's worst possible way of implementing this.

first of all, define pins @ start of code, e.g.:
code: [select]

#define computer_relay 3
#define lights_relay 16


then later refer relays through names. more readable, less error.

setting relaypin somewhere 1 of relays , using later bad practice - hard figure out "what relay now?". on top of that, declare variable "int relaypin" inside curly-braces block, it's local block , block only. moment leave block it's gone, , may revert value had before.

pinmode set in setup() section of code, pins output or input throughout whole program that's set them , don't have touch again.

so you'll have lot of rewriting do. i'm expecting see few lines such as:
code: [select]

if (query == "cmd" && device == "computer") {
  digitalwrite(computer_relay, high);


can done bunch of chained "if" , "if else" statements.


Arduino Forum > Using Arduino > Project Guidance > Home automation with ESP8266


arduino

Comments

Popular posts from this blog

Error compiling for board Arduino/Genuino Uno.

Installation database is corrupt

esp8266 (nodemcu 0.9) client.write très lent ???