help for rebuilding sketch (esp8266) for esp32 ?

feredy2011
Posts: 10
Joined: Mon Aug 14, 2017 8:47 pm

help for rebuilding sketch (esp8266) for esp32 ?

Postby feredy2011 » Mon Aug 14, 2017 8:55 pm

hi friends.
thank you for esp32 . this is a interesting module.
i am beginner in esp32 so please help me to rebuilding my sketch with esp32. thank you
this is my sketch that i use for esp8266, can you change this for esp32 ?

Code: Select all

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <Bounce2.h>

IPAddress ip(192, 168, 1, 241); 
IPAddress gateway(192, 168, 1, 1); 
IPAddress subnet(255, 255, 255, 0); 
IPAddress DNS(192, 168, 1, 1); 
const char* ssid = "XXX";
const char* password = "XXXX";

const int buttonPinA = D5;
const int relayPinA = D1;
Bounce debouncerA = Bounce();
int trigger = 0;
int oldValueA;
WiFiClient client;

WiFiServer server(80);

void setup() {
  pinMode(buttonPinA, INPUT_PULLUP);
  debouncerA.attach(buttonPinA);
  debouncerA.interval(50);
  pinMode(relayPinA, OUTPUT);
  digitalWrite(relayPinA, HIGH);

  Serial.begin(115200);
  delay(10);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.config(ip, gateway, subnet, DNS);//**********************
  delay(100);//**********************************
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  /* while (WiFi.status() != WL_CONNECTED) {
     delay(500);
     Serial.print(".");
    }*/
  /*  if (WiFi.status() != WL_CONNECTED) {
      Serial.println("Error in connection");
    }*/
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

  debouncerA.update();
  oldValueA = debouncerA.read();
}


void loop() {
  {
    if (trigger == 0) {
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=55&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      trigger = 1;
    }
  }
  {
    debouncerA.update();
    int valueA = debouncerA.read();
    if (valueA != oldValueA) {
      int state = digitalRead(relayPinA);
      digitalWrite(relayPinA, !state);
      if ( state == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=55&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( state == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=55&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueA = valueA;
    delay(50);
  }
  {
    WiFiClient client = server.available();
    if (!client) {
      return;
    }
    Serial.println("new client");
    while (!client.available()) {
      delay(1);
    }
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();
    if (request.indexOf("/relayon") != -1) {
      digitalWrite(relayPinA, LOW);
    }
    if (request.indexOf("/relayoff") != -1) {
      digitalWrite(relayPinA, HIGH);
    }


    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println(""); //  do not forget this one
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
  }
}
please help me to upload this code to esp32 .thank you

gdsports
Posts: 15
Joined: Wed Aug 02, 2017 12:17 am

Re: help for rebuilding sketch (esp8266) for esp32 ?

Postby gdsports » Wed Aug 16, 2017 5:50 am

I suggest looking at the examples to see what has changed.

One big change is the include file names have changed. Try changing ESP8266WiFi.h to WiFi.h and ESP8266mDNS.h to ESPmDNS.h.

feredy2011
Posts: 10
Joined: Mon Aug 14, 2017 8:47 pm

Re: help for rebuilding sketch (esp8266) for esp32 ?

Postby feredy2011 » Wed Aug 16, 2017 3:14 pm

gdsports wrote:I suggest looking at the examples to see what has changed.

One big change is the include file names have changed. Try changing ESP8266WiFi.h to WiFi.h and ESP8266mDNS.h to ESPmDNS.h.
thank you friend.for answer. where is examples?
i change this and code was compile seccess and in serial monitor i see connect to network but there is not in mynetwork

feredy2011
Posts: 10
Joined: Mon Aug 14, 2017 8:47 pm

Re: help for rebuilding sketch (esp8266) for esp32 ?

Postby feredy2011 » Thu Aug 17, 2017 6:01 am

thank you for answer. i change my code and compile is done, but after upload dont work well. some relay will on in startup(i set all relay to 0)but some relay turn on auto. also after connect to wifi,some key dont work,some code send with a delay....
this is my code for 10 channel relay, do you think this code have a problem ?please help me thank you

Code: Select all

#include "WiFi.h"
#include <Bounce2.h>

/*IPAddress ip(192, 168, 1, 90);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress DNS(192, 168, 1, 1);*/
const char* ssid = "Reza";
const char* password = "rayan1369";

const int buttonPinA = 22;
const int buttonPinB = 23;
const int buttonPinC = 19;
const int buttonPinD = 4;
const int buttonPinE = 26;
const int buttonPinF = 25;
const int buttonPinG = 14;
const int buttonPinH = 27;
const int buttonPinI = 33;
const int buttonPinJ = 32;

const int relayPinA = 0;
const int relayPinB = 12;
const int relayPinC = 15;
const int relayPinD = 21;
const int relayPinE = 2;
const int relayPinF = 13;
const int relayPinG = 18;
const int relayPinH = 5;
const int relayPinI = 17;
const int relayPinJ = 16;

Bounce debouncerA = Bounce();
Bounce debouncerB = Bounce();
Bounce debouncerC = Bounce();
Bounce debouncerD = Bounce();
Bounce debouncerE = Bounce();
Bounce debouncerF = Bounce();
Bounce debouncerG = Bounce();
Bounce debouncerH = Bounce();
Bounce debouncerI = Bounce();
Bounce debouncerJ = Bounce();

int trigger = 0;

int oldValueA;
int oldValueB;
int oldValueC;
int oldValueD;
int oldValueE;
int oldValueF;
int oldValueG;
int oldValueH;
int oldValueI;
int oldValueJ;

WiFiClient client;

WiFiServer server(80);

void setup() {
  pinMode(buttonPinA, INPUT_PULLUP);
  debouncerA.attach(buttonPinA);
  debouncerA.interval(100);
  pinMode(buttonPinB, INPUT_PULLUP);
  debouncerB.attach(buttonPinB);
  debouncerB.interval(100);
  pinMode(buttonPinC, INPUT_PULLUP);
  debouncerC.attach(buttonPinC);
  debouncerC.interval(100);
  pinMode(buttonPinD, INPUT_PULLUP);
  debouncerD.attach(buttonPinD);
  debouncerD.interval(100);
  pinMode(buttonPinE, INPUT_PULLUP);
  debouncerE.attach(buttonPinE);
  debouncerE.interval(100);
  pinMode(buttonPinF, INPUT_PULLUP);
  debouncerF.attach(buttonPinF);
  debouncerF.interval(100);
  pinMode(buttonPinG, INPUT_PULLUP);
  debouncerG.attach(buttonPinG);
  debouncerG.interval(100);
  pinMode(buttonPinH, INPUT_PULLUP);
  debouncerH.attach(buttonPinH);
  debouncerH.interval(100);
  pinMode(buttonPinI, INPUT_PULLUP);
  debouncerI.attach(buttonPinI);
  debouncerI.interval(100);
  pinMode(buttonPinJ, INPUT_PULLUP);
  debouncerJ.attach(buttonPinJ);
  debouncerJ.interval(100);

  pinMode(relayPinA, OUTPUT);
  digitalWrite(relayPinA, HIGH);
  pinMode(relayPinB, OUTPUT);
  digitalWrite(relayPinB, HIGH);
  pinMode(relayPinC, OUTPUT);
  digitalWrite(relayPinC, HIGH);
  pinMode(relayPinD, OUTPUT);
  digitalWrite(relayPinD, HIGH);
  pinMode(relayPinE, OUTPUT);
  digitalWrite(relayPinE, HIGH);
  pinMode(relayPinF, OUTPUT);
  digitalWrite(relayPinF, HIGH);
  pinMode(relayPinG, OUTPUT);
  digitalWrite(relayPinG, HIGH);
  pinMode(relayPinH, OUTPUT);
  digitalWrite(relayPinH, HIGH);
  pinMode(relayPinI, OUTPUT);
  digitalWrite(relayPinI, HIGH);
  pinMode(relayPinJ, OUTPUT);
  digitalWrite(relayPinJ, HIGH);


  Serial.begin(115200);
  delay(10);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 /* WiFi.config(ip, gateway, subnet, DNS);//**********************
  delay(100);//**********************************/
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  if (WiFi.status() != WL_CONNECTED) {
    for (int i = 0; i <= 20; i++) {
      delay(500);
      Serial.print(".");
    }
  }
  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("");
    Serial.println("WiFi connected");
  }

  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.print("Use this URL : ");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.println("/");

  debouncerA.update();
  oldValueA = debouncerA.read();
  debouncerB.update();
  oldValueB = debouncerB.read();
  debouncerC.update();
  oldValueC = debouncerC.read();
  debouncerD.update();
  oldValueD = debouncerD.read();
  debouncerE.update();
  oldValueE = debouncerE.read();
  debouncerF.update();
  oldValueF = debouncerF.read();
  debouncerG.update();
  oldValueG = debouncerG.read();
  debouncerH.update();
  oldValueH = debouncerH.read();
  debouncerI.update();
  oldValueI = debouncerI.read();
  debouncerJ.update();
  oldValueJ = debouncerJ.read();
}


void loop() {
  {
    if (WiFi.status() != WL_CONNECTED) {
      Serial.println();
      Serial.println();
      Serial.print("Connecting to ");
      Serial.println(ssid);
      //WiFi.config(ip, gateway, subnet, DNS);
      delay(100);
      WiFi.mode(WIFI_STA);
      WiFi.begin(ssid, password);
      Serial.println("");
      Serial.println("WiFi connected");
      delay(100);
      // Start the server
      server.begin();
      Serial.println("Server started");

      // Print the IP address
      Serial.print("Use this URL : ");
      Serial.print("http://");
      Serial.print(WiFi.localIP());
      Serial.println("/");
      delay(1000);
    }
  }
  
  {
    if (trigger == 0) {
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=62&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=63&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=64&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=65&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=66&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=67&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=68&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=69&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=70&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=udevice&idx=71&nvalue=0");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }
      delay(20);
      if (client.connect("192.168.1.201", 8080)) {
        client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
        client.println(" HTTP/1.1");
        client.print("Host: ");
        client.print("192.168.1.201");
        client.print(":");
        client.println(8080);
        client.println();
        client.stop();
      }

      trigger = 1;
    }
  }
  {
    debouncerA.update();
    int valueA = debouncerA.read();
    if (valueA != oldValueA) {
      int stateA = digitalRead(relayPinA);
      digitalWrite(relayPinA, !stateA);
      if ( stateA == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=62&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateA == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=62&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueA = valueA;
  }
  {
    debouncerB.update();
    int valueB = debouncerB.read();
    if (valueB != oldValueB) {
      int stateB = digitalRead(relayPinB);
      digitalWrite(relayPinB, !stateB);
      if ( stateB == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=63&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateB == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=63&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueB = valueB;
  }
  {
    debouncerC.update();
    int valueC = debouncerC.read();
    if (valueC != oldValueC) {
      int stateC = digitalRead(relayPinC);
      digitalWrite(relayPinC, !stateC);
      if ( stateC == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=64&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateC == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=64&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueC = valueC;
  }
  {
    debouncerD.update();
    int valueD = debouncerD.read();
    if (valueD != oldValueD) {
      int stateD = digitalRead(relayPinD);
      digitalWrite(relayPinD, !stateD);
      if ( stateD == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=65&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateD == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=65&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueD = valueD;
  }
  {
    debouncerE.update();
    int valueE = debouncerE.read();
    if (valueE != oldValueE) {
      int stateE = digitalRead(relayPinE);
      digitalWrite(relayPinE, !stateE);
      if ( stateE == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=66&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateE == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=66&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueE = valueE;
  }
  {
    debouncerF.update();
    int valueF = debouncerF.read();
    if (valueF != oldValueF) {
      int stateF = digitalRead(relayPinF);
      digitalWrite(relayPinF, !stateF);
      if ( stateF == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=67&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateF == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=67&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueF = valueF;
  }
  {
    debouncerG.update();
    int valueG = debouncerG.read();
    if (valueG != oldValueG) {
      int stateG = digitalRead(relayPinG);
      digitalWrite(relayPinG, !stateG);
      if ( stateG == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=68&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateG == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=68&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueG = valueG;
  }
  {
    debouncerH.update();
    int valueH = debouncerH.read();
    if (valueH != oldValueH) {
      int stateH = digitalRead(relayPinH);
      digitalWrite(relayPinH, !stateH);
      if ( stateH == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=69&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateH == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=69&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueH = valueH;
  }
  {
    debouncerI.update();
    int valueI = debouncerI.read();
    if (valueI != oldValueI) {
      int stateI = digitalRead(relayPinI);
      digitalWrite(relayPinI, !stateI);
      if ( stateI == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=70&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateI == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=70&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueI = valueI;
  }
  {
    debouncerJ.update();
    int valueJ = debouncerJ.read();
    if (valueJ != oldValueJ) {
      int stateJ = digitalRead(relayPinJ);
      digitalWrite(relayPinJ, !stateJ);
      if ( stateJ == 0) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=71&nvalue=0");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=OFF");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
      if ( stateJ == 1) {
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=udevice&idx=71&nvalue=1");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
        if (client.connect("192.168.1.201", 8080)) {
          client.print("GET /json.htm?type=command&param=addlogmessage&message=ON");
          client.println(" HTTP/1.1");
          client.print("Host: ");
          client.print("192.168.1.201");
          client.print(":");
          client.println(8080);
          client.println();
          client.stop();
        }
      }
    }
    oldValueJ = valueJ;
  }

  {
    WiFiClient client = server.available();
    if (!client) {
      return;
    }
    Serial.println("new client");
    while (!client.available()) {
      delay(1);
    }
    String request = client.readStringUntil('\r');
    Serial.println(request);
    client.flush();
    if (request.indexOf("/relay1on") != -1) {
      digitalWrite(relayPinA, LOW);
    }
    if (request.indexOf("/relay1off") != -1) {
      digitalWrite(relayPinA, HIGH);
    }
    if (request.indexOf("/relay2on") != -1) {
      digitalWrite(relayPinB, LOW);
    }
    if (request.indexOf("/relay2off") != -1) {
      digitalWrite(relayPinB, HIGH);
    }
    if (request.indexOf("/relay3on") != -1) {
      digitalWrite(relayPinC, LOW);
    }
    if (request.indexOf("/relay3off") != -1) {
      digitalWrite(relayPinC, HIGH);
    }
    if (request.indexOf("/relay4on") != -1) {
      digitalWrite(relayPinD, LOW);
    }
    if (request.indexOf("/relay4off") != -1) {
      digitalWrite(relayPinD, HIGH);
    }
    if (request.indexOf("/relay5on") != -1) {
      digitalWrite(relayPinE, LOW);
    }
    if (request.indexOf("/relay5off") != -1) {
      digitalWrite(relayPinE, HIGH);
    }
    if (request.indexOf("/relay6on") != -1) {
      digitalWrite(relayPinF, LOW);
    }
    if (request.indexOf("/relay6off") != -1) {
      digitalWrite(relayPinF, HIGH);
    }
    if (request.indexOf("/relay7on") != -1) {
      digitalWrite(relayPinG, LOW);
    }
    if (request.indexOf("/relay7off") != -1) {
      digitalWrite(relayPinG, HIGH);
    }
    if (request.indexOf("/relay8on") != -1) {
      digitalWrite(relayPinH, LOW);
    }
    if (request.indexOf("/relay8off") != -1) {
      digitalWrite(relayPinH, HIGH);
    }
    if (request.indexOf("/relay9on") != -1) {
      digitalWrite(relayPinI, LOW);
    }
    if (request.indexOf("/relay9off") != -1) {
      digitalWrite(relayPinI, HIGH);
    }
    if (request.indexOf("/relay10on") != -1) {
      digitalWrite(relayPinJ, LOW);
    }
    if (request.indexOf("/relay10off") != -1) {
      digitalWrite(relayPinJ, HIGH);
    }



    client.println("HTTP/1.1 200 OK");
    client.println("Content-Type: text/html");
    client.println(""); //  do not forget this one
    client.println("<!DOCTYPE HTML>");
    client.println("<html>");
  }
}

Who is online

Users browsing this forum: No registered users and 122 guests