Welcome, Guest
You have to register before you can post on our site.

Username
  

Password
  





Search Forums

(Advanced Search)

Forum Statistics
» Members: 6,366
» Latest member: Aubergine
» Forum threads: 2,844
» Forum posts: 15,031

Full Statistics

Online Users
There are currently 74 online users.
» 2 Member(s) | 56 Guest(s)
Amazonbot, Bing, Bytespider, Crawl, Google, PetalBot, WordPress/, Yandex, bot, AshS, lusant

Latest Threads
KinCony ESP32-S3 Core Boa...
Forum: Extender module
Last Post: AshS
3 minutes ago
» Replies: 36
» Views: 1,388
A16: IFTTT Time based act...
Forum: KC868-A16
Last Post: sebfromgermany
1 hour ago
» Replies: 17
» Views: 157
analog input resolution 3...
Forum: KC868-A16
Last Post: Yosemite
2 hours ago
» Replies: 15
» Views: 384
[Arduino IDE demo source ...
Forum: KC868-A16
Last Post: wolli
3 hours ago
» Replies: 7
» Views: 2,576
KC868-HA-V21 serial port ...
Forum: KC868-HA /HA v2
Last Post: admin
5 hours ago
» Replies: 8
» Views: 92
Using B16M for driving LE...
Forum: B16M
Last Post: admin
5 hours ago
» Replies: 3
» Views: 11
KC868-M30 - how to get it...
Forum: KC868-M16 / M1 / MB / M30
Last Post: msmrz
6 hours ago
» Replies: 4
» Views: 43
KC868-HA RS485 INPUT & OU...
Forum: KC868-HA /HA v2
Last Post: admin
Yesterday, 10:57 PM
» Replies: 29
» Views: 6,584
MQTT Connection issues
Forum: KC868-A16
Last Post: admin
Yesterday, 10:55 PM
» Replies: 1
» Views: 9
KCS868-A6 not found
Forum: KC868-A series and Uair Smart Controller
Last Post: admin
Yesterday, 10:50 PM
» Replies: 12
» Views: 55

  Poor IR transmitter range
Posted by: onlyoneme - 09-08-2022, 11:13 AM - Forum: KC868-AG / AG Pro / AG8 - Replies (3)

Hello, I've just bought this device and after some preliminary tests I've noticed that IR range is terribly poor comparing to my another transmitter, which is a cheap Tuya based device.

Is it a matter of weak IR LEDs or reason is something else?

Print this item

  KC868-A16 Ethernet ESP32
Posted by: davesmithr1 - 09-08-2022, 09:23 AM - Forum: KC868-A series and Uair Smart Controller - Replies (1)

KC868-A16 Ethernet ESP32 Development Board

does anyone know how to make alexa work using this through the LAN? how to would be awesome i have NO IDEA!!! PLEASE HELP!!! 
Also i want the inputs (dry contact)  to trigger the outputs, 

if someone wants to message me and spend some time to teach/ show me how im willing to pay,

thanks again!

Print this item

  proplem mqtt sensor configration
Posted by: engmohades - 09-08-2022, 06:26 AM - Forum: KC868-HxB series Smart Controller - Replies (1)

hello every one
i have h32bs integrated in home assistant by mqtt  . please see pictures
how can solve it



Attached Files Thumbnail(s)
           
Print this item

  [Arduino IDE demo source code for KC868-A8S]--#17-A8S_Digital input_Ethernet_Web_serv
Posted by: KinCony Support - 09-08-2022, 03:17 AM - Forum: KC868-A8S - Replies (7)

[Arduino IDE demo source code for KC868-A8S]--#17-A8S_Digital input_Ethernet_Web_server_code

.zip   KC868-A8S_Digital_input_Web_Server-ETH.zip (Size: 468.1 KB / Downloads: 362)


PC and phone webpage display
   

Code:
  /***********************************************************
* Create Your own WebServer for KC868-A8s Smart Controller*
* https://www.kincony.com                                **
***********************************************************/

#include "PCF8574.h"
#define DEBUG_ETHERNET_WEBSERVER_PORT       Serial

// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_       3

#include <WebServer_WT32_ETH01.h>

WiFiServer server(80);

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

// Variable to store the HTTP request
String header;
PCF8574 pcf8574_1(0x22,4,5);


void setup() {

  Serial.begin(115200);

 
  for(int i=0;i<=7;i++)
  {
     pcf8574_1.pinMode(i, INPUT);
  }

    pcf8574_1.begin();

     
   while (!Serial);

  Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  // To be called before ETH.begin()
  WT32_ETH01_onEvent();
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
  server.begin();

}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
            if (currentLine.length() == 0) {
           client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "Refresh: 2\r\n"        // refresh the page automatically every 3 sec
            "\r\n");
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; border: 3px solid green;margin: 0px auto;text-align: center;font-size: 10px;}");
            client.println(".button { background-color: MediumSeaGreen; border-radius: 10px;color: white; padding: 10px 10px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");

            client.println(".button1 { background-color: Tomato; border-radius: 10px;color: white; padding: 10px 10px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");
            client.println(".button2 {background-color: Tomato;}</style></head>");
            client.println("<body><h1>KC868-A8S Web Server</h1>");
            if (pcf8574_1.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT01</button></a>"); else client.println("<a><button class=\"button\">INPUT01</button></a>");
            if (pcf8574_1.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT02</button></a>"); else client.println("<a><button class=\"button\">INPUT02</button></a>");                                 
            if (pcf8574_1.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT03</button></a>"); else client.println("<a><button class=\"button\">INPUT03</button></a>");
            if (pcf8574_1.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT04</button></a>"); else client.println("<a><button class=\"button\">INPUT04</button></a>");
            if (pcf8574_1.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT05</button></a>"); else client.println("<a><button class=\"button\">INPUT05</button></a>");
            if (pcf8574_1.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT06</button></a>"); else client.println("<a><button class=\"button\">INPUT06</button></a>");                                 
            if (pcf8574_1.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT07</button></a>"); else client.println("<a><button class=\"button\">INPUT07</button></a>");
            if (pcf8574_1.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT08</button></a>"); else client.println("<a><button class=\"button\">INPUT08</button></a>");
           
            client.println("</body></html>");
           
            // The HTTP response ends with another blank line
            client.println();
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }

}

Print this item

  [Arduino IDE demo source code for KC868-AI]--#07-AI_Digital input_Ethernet_Web_server
Posted by: KinCony Support - 09-08-2022, 02:15 AM - Forum: KC868-AI - Replies (4)

[Arduino IDE demo source code for KC868-AI]--#07-AI_Digital input_Ethernet_Web_server_code
Arduino source code and bin file 

.zip   KC868-AI_Web_Server-ETH.zip (Size: 470.08 KB / Downloads: 414)


PC and Phone webpage display
   



Code:
  /***********************************************************
* Create Your own WebServer for KC868-AI Smart Controller*
* https://www.kincony.com                                **
***********************************************************/

#include "PCF8574.h"
#define DEBUG_ETHERNET_WEBSERVER_PORT       Serial

// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_       3

#include <WebServer_WT32_ETH01.h>

WiFiServer server(80);

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

// Variable to store the HTTP request
String header;
// Set i2c address  
PCF8574 pcf8574_1(0x24,4,5);
PCF8574 pcf8574_2(0x25,4,5);
PCF8574 pcf8574_3(0x21,4,5);
PCF8574 pcf8574_4(0x22,4,5);
PCF8574 pcf8574_5(0x26,4,5);
PCF8574 pcf8574_6(0x23,4,5);


void setup() {

  Serial.begin(115200);

 
  for(int i=0;i<=7;i++)
  {
     pcf8574_1.pinMode(i, INPUT);
     pcf8574_2.pinMode(i, INPUT);
     pcf8574_3.pinMode(i, INPUT);
     pcf8574_4.pinMode(i, INPUT);
     pcf8574_5.pinMode(i, INPUT);
     pcf8574_6.pinMode(i, INPUT);
  }

    pcf8574_1.begin();
    pcf8574_2.begin();
    pcf8574_3.begin();
    pcf8574_4.begin();
    pcf8574_5.begin();
    pcf8574_6.begin();

     
   while (!Serial);

  Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  // To be called before ETH.begin()
  WT32_ETH01_onEvent();
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
  server.begin();

}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();        
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
            if (currentLine.length() == 0) {
             client.print(
            "HTTP/1.1 200 OK\r\n"
            "Content-Type: text/html\r\n"
            "Connection: close\r\n"  // the connection will be closed after completion of the response
            "Refresh: 2\r\n"        // refresh the page automatically every 2 sec
            "\r\n");
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; border: 3px solid green;margin: 0px auto;text-align: center;font-size: 10px;}");
            client.println(".button { background-color: MediumSeaGreen; border-radius: 10px;color: white; padding: 10px 10px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");

            client.println(".button1 { background-color: Tomato; border-radius: 10px;color: white; padding: 10px 10px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");
            client.println(".button2 {background-color: Tomato;}</style></head>");
            client.println("<body><h1>KC868-AI Web Server</h1>");
            if (pcf8574_1.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT01</button></a>"); else client.println("<a><button class=\"button\">INPUT01</button></a>");
            if (pcf8574_1.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT02</button></a>"); else client.println("<a><button class=\"button\">INPUT02</button></a>");                                
            if (pcf8574_1.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT03</button></a>"); else client.println("<a><button class=\"button\">INPUT03</button></a>");
            if (pcf8574_1.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT04</button></a>"); else client.println("<a><button class=\"button\">INPUT04</button></a>");
            if (pcf8574_1.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT05</button></a>"); else client.println("<a><button class=\"button\">INPUT05</button></a>");
            if (pcf8574_1.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT06</button></a>"); else client.println("<a><button class=\"button\">INPUT06</button></a>");                                
            if (pcf8574_1.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT07</button></a>"); else client.println("<a><button class=\"button\">INPUT07</button></a>");
            if (pcf8574_1.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT08</button></a>"); else client.println("<a><button class=\"button\">INPUT08</button></a>");

            if (pcf8574_2.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT09</button></a>"); else client.println("<a><button class=\"button\">INPUT09</button></a>");
            if (pcf8574_2.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT10</button></a>"); else client.println("<a><button class=\"button\">INPUT10</button></a>");                                
            if (pcf8574_2.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT11</button></a>"); else client.println("<a><button class=\"button\">INPUT11</button></a>");
            if (pcf8574_2.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT12</button></a>"); else client.println("<a><button class=\"button\">INPUT12</button></a>");
            if (pcf8574_2.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT13</button></a>"); else client.println("<a><button class=\"button\">INPUT13</button></a>");
            if (pcf8574_2.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT14</button></a>"); else client.println("<a><button class=\"button\">INPUT14</button></a>");                                
            if (pcf8574_2.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT15</button></a>"); else client.println("<a><button class=\"button\">INPUT15</button></a>");
            if (pcf8574_2.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT16</button></a>"); else client.println("<a><button class=\"button\">INPUT16</button></a>");


            if (pcf8574_3.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT17</button></a>"); else client.println("<a><button class=\"button\">INPUT17</button></a>");
            if (pcf8574_3.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT18</button></a>"); else client.println("<a><button class=\"button\">INPUT18</button></a>");                                
            if (pcf8574_3.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT19</button></a>"); else client.println("<a><button class=\"button\">INPUT19</button></a>");
            if (pcf8574_3.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT20</button></a>"); else client.println("<a><button class=\"button\">INPUT20</button></a>");
            if (pcf8574_3.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT21</button></a>"); else client.println("<a><button class=\"button\">INPUT21</button></a>");
            if (pcf8574_3.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT22</button></a>"); else client.println("<a><button class=\"button\">INPUT22</button></a>");                                
            if (pcf8574_3.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT23</button></a>"); else client.println("<a><button class=\"button\">INPUT23</button></a>");
            if (pcf8574_3.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT24</button></a>"); else client.println("<a><button class=\"button\">INPUT24</button></a>");

            if (pcf8574_4.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT25</button></a>"); else client.println("<a><button class=\"button\">INPUT25</button></a>");
            if (pcf8574_4.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT26</button></a>"); else client.println("<a><button class=\"button\">INPUT26</button></a>");                                
            if (pcf8574_4.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT27</button></a>"); else client.println("<a><button class=\"button\">INPUT27</button></a>");
            if (pcf8574_4.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT28</button></a>"); else client.println("<a><button class=\"button\">INPUT28</button></a>");
            if (pcf8574_4.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT29</button></a>"); else client.println("<a><button class=\"button\">INPUT29</button></a>");
            if (pcf8574_4.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT30</button></a>"); else client.println("<a><button class=\"button\">INPUT30</button></a>");                                
            if (pcf8574_4.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT31</button></a>"); else client.println("<a><button class=\"button\">INPUT31</button></a>");
            if (pcf8574_4.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT32</button></a>"); else client.println("<a><button class=\"button\">INPUT32</button></a>");

            if (pcf8574_5.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT33</button></a>"); else client.println("<a><button class=\"button\">INPUT33</button></a>");
            if (pcf8574_5.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT34</button></a>"); else client.println("<a><button class=\"button\">INPUT34</button></a>");                                
            if (pcf8574_5.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT35</button></a>"); else client.println("<a><button class=\"button\">INPUT35</button></a>");
            if (pcf8574_5.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT36</button></a>"); else client.println("<a><button class=\"button\">INPUT36</button></a>");
            if (pcf8574_5.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT37</button></a>"); else client.println("<a><button class=\"button\">INPUT37</button></a>");
            if (pcf8574_5.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT38</button></a>"); else client.println("<a><button class=\"button\">INPUT38</button></a>");                                
            if (pcf8574_5.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT39</button></a>"); else client.println("<a><button class=\"button\">INPUT39</button></a>");
            if (pcf8574_5.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT40</button></a>"); else client.println("<a><button class=\"button\">INPUT40</button></a>");

            if (pcf8574_6.digitalRead(0)==0) client.println("<a><button class=\"button1\">INPUT41</button></a>"); else client.println("<a><button class=\"button\">INPUT41</button></a>");
            if (pcf8574_6.digitalRead(1)==0) client.println("<a><button class=\"button1\">INPUT42</button></a>"); else client.println("<a><button class=\"button\">INPUT42</button></a>");                                
            if (pcf8574_6.digitalRead(2)==0) client.println("<a><button class=\"button1\">INPUT43</button></a>"); else client.println("<a><button class=\"button\">INPUT43</button></a>");
            if (pcf8574_6.digitalRead(3)==0) client.println("<a><button class=\"button1\">INPUT44</button></a>"); else client.println("<a><button class=\"button\">INPUT44</button></a>");
            if (pcf8574_6.digitalRead(4)==0) client.println("<a><button class=\"button1\">INPUT45</button></a>"); else client.println("<a><button class=\"button\">INPUT45</button></a>");
            if (pcf8574_6.digitalRead(5)==0) client.println("<a><button class=\"button1\">INPUT46</button></a>"); else client.println("<a><button class=\"button\">INPUT46</button></a>");                                
            if (pcf8574_6.digitalRead(6)==0) client.println("<a><button class=\"button1\">INPUT47</button></a>"); else client.println("<a><button class=\"button\">INPUT47</button></a>");
            if (pcf8574_6.digitalRead(7)==0) client.println("<a><button class=\"button1\">INPUT48</button></a>"); else client.println("<a><button class=\"button\">INPUT48</button></a>");
          
            client.println("</body></html>");
           
            // The HTTP response ends with another blank line
            client.println();
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
  }

}

Print this item

Lightbulb Fireplace stove combustion automation
Posted by: SvetZitrka - 09-06-2022, 10:40 AM - Forum: DIY Project - Replies (3)

Hi, I would like to equip my fireplace insert with an automation that will measure the temperature of the flue gas in the chimney and control the air supply damper based on that.


Hi, I would like to equip my fireplace insert with an automation that will measure the temperature of the flue gas in the chimney and control the air supply damper based on that.

Systems like this can make the whole burning process much more efficient, using up to 30% less wood. Which is great. Plus, they can be very user-friendly and alert the user to add wood to the fireplace.

All you have to do is measure the temperature with a ceramic sensor that can withstand high temperatures and a servo drive to control the air supply damper.

All the logic should be able to be set up through Home Assistant.

  • Which HW Kincony would handle this and how?
  • Can you give me any advice?

I believe that similar automation would save many users a lot of stress, especially now that winter is coming and there is an energy crisis.



Attached Files Thumbnail(s)
   
Print this item

  KC868-A8S - Trying to connect with GSM
Posted by: AbrahamAV97 - 09-05-2022, 05:08 PM - Forum: Development - Replies (1)

Hi!  Big Grin

I have recently purchased a KC868-A8S.

My goal is to get a GSM web server, from which I can activate the relays.

I have managed to create a web server by creating my own AP, taking a tutorial from the forum as an example.


But this, although it is something similar, does not serve my purpose.

I have worked before with ESP32 and GSM (I used to work with TinyGSM), and when I wanted to connect it was necessary to define the MODEM_TX and MODEM_RX pins in code.

In this case I cannot find anywhere, neither in the documentation nor in the forum, how to connect the ESP to the GSM network, nor to which pins the modem corresponds.

I have a SIM800L module.

Is there an example of code (.ino) in which the configuration and connection of the GSM can be seen logically?

Any help is appreciated.

Thank you very much in advance and sorry for my level of English.

Print this item

  [Arduino IDE demo source code for KC868-E16S]--#08-E16S-Ethernet_Web_server_code
Posted by: KinCony Support - 09-05-2022, 08:15 AM - Forum: KC868-E16S/E16P - Replies (10)

[Arduino IDE demo source code for KC868-E16S]--#08-E16S-Ethernet_Web_server_code
Refer to code #11 of KC868-A8 for download methods


.zip   KC868-E16S_Web_Server-ETH.zip (Size: 474.02 KB / Downloads: 460)

Code:
/***********************************************************
* Create Your own WebServer for KC868-E16S Smart Controller*
* https://www.kincony.com                                **
***********************************************************/
#include "PCF8574.h"
#define DEBUG_ETHERNET_WEBSERVER_PORT       Serial

// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_       3

#include <WebServer_WT32_ETH01.h>

WiFiServer server(80);
// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

int flag_read_di=0;

// Variable to store the HTTP request
String header;

TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);


// Set digital input i2c address
PCF8574 pcf8574_I1(&I2Cone, 0x22, 16, 15);
PCF8574 pcf8574_I2(&I2Cone, 0x24, 16, 15);
PCF8574 pcf8574_R1(&I2Cone, 0x21, 16, 15);
PCF8574 pcf8574_R2(&I2Cone, 0x25, 16, 15);

String relaystate[16]={"relay1state","relay2state","relay3state","relay4state","relay5state","relay6state","relay7state","relay8state",
                       "relay9state","relay10state","relay11state","relay12state","relay13state","relay14state","relay15state","relay16state",};

                      
String relay1state = "off";// state of relay1
String relay2state = "off";// state of relay2
String relay3state = "off";// state of relay3
String relay4state = "off";// state of relay4
String relay5state = "off";// state of relay5
String relay6state = "off";// state of relay6
String relay7state = "off";// state of relay7
String relay8state = "off";// state of relay8

String relay9state = "off";// state of relay9
String relay10state = "off";// state of relay10
String relay11state = "off";// state of relay11
String relay12state = "off";// state of relay12
String relay13state = "off";// state of relay13
String relay14state = "off";// state of relay14
String relay15state = "off";// state of relay15
String relay16state = "off";// state of relay16


void setup() {

 
  Serial.begin(115200);

  // Set PCF8574 pinMode to OUTPUT
  for(int i=0;i<=7;i++)
  {
     pcf8574_R1.pinMode(i, OUTPUT);
     pcf8574_R2.pinMode(i, OUTPUT);
  }
  pcf8574_R1.begin();
  pcf8574_R2.begin();
 
 
  for(int i=0;i<=7;i++)
  {
     pcf8574_I1.pinMode(i, INPUT);
     pcf8574_I2.pinMode(i, INPUT);
  }
  pcf8574_I1.begin();
  pcf8574_I2.begin();

  for(int i=0;i<=7;i++)
  {
     pcf8574_R1.digitalWrite(i, HIGH);
     pcf8574_R2.digitalWrite(i, HIGH);
  }

// Connect to Wi-Fi network with SSID and password
// Serial.print("Connecting to ");
// Serial.println(ssid);
while (!Serial);

  // Using this if Serial debugging is not necessary or not using Serial port
  //while (!Serial && (millis() < 3000));

  Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  // To be called before ETH.begin()
  WT32_ETH01_onEvent();

  //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
  //           eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
  //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
// ETH.config(myIP, myGW, mySN, myDNS);
  //WT32_ETH01_waitForConnect();
  server.begin();

}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();        
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
           
            // turns 16 relays on and off
            if (header.indexOf("GET /1/on") >= 0) {relay1state = "on";pcf8574_R1.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /1/off") >= 0) {relay1state = "off";pcf8574_R1.digitalWrite(P0, HIGH);}
            else if (header.indexOf("GET /2/on") >= 0)  {relay2state = "on";pcf8574_R1.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /2/off") >= 0) {relay2state = "off";pcf8574_R1.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /3/on") >= 0)  {relay3state = "on";pcf8574_R1.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /3/off") >= 0) {relay3state = "off";pcf8574_R1.digitalWrite(P2, HIGH);}        
            else if (header.indexOf("GET /4/on") >= 0)  {relay4state = "on";pcf8574_R1.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /4/off") >= 0) {relay4state = "off";pcf8574_R1.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /5/on") >= 0)  {relay5state = "on";pcf8574_R1.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /5/off") >= 0) {relay5state = "off";pcf8574_R1.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /6/on") >= 0)  {relay6state = "on";pcf8574_R1.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /6/off") >= 0) {relay6state = "off";pcf8574_R1.digitalWrite(P5, HIGH);}         
            else if (header.indexOf("GET /7/on") >= 0)  {relay7state = "on";pcf8574_R1.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /7/off") >= 0) {relay7state = "off";pcf8574_R1.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /8/on") >= 0)  {relay8state = "on";pcf8574_R1.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /8/off") >= 0) {relay8state = "off";pcf8574_R1.digitalWrite(P7, HIGH);}
            else if (header.indexOf("GET /9/on") >= 0)  {relay9state = "on";pcf8574_R2.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /9/off") >= 0) {relay9state = "off";pcf8574_R2.digitalWrite(P0, HIGH);}        
            else if (header.indexOf("GET /10/on") >= 0)  {relay10state = "on";pcf8574_R2.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /10/off") >= 0) {relay10state = "off";pcf8574_R2.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /11/on") >= 0)  {relay11state = "on";pcf8574_R2.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /11/off") >= 0) {relay11state = "off";pcf8574_R2.digitalWrite(P2, HIGH);}
            else if (header.indexOf("GET /12/on") >= 0)  {relay12state = "on";pcf8574_R2.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /12/off") >= 0) {relay12state = "off";pcf8574_R2.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /13/on") >= 0)  {relay13state = "on";pcf8574_R2.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /13/off") >= 0) {relay13state = "off";pcf8574_R2.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /14/on") >= 0)  {relay14state = "on";pcf8574_R2.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /14/off") >= 0) {relay14state = "off";pcf8574_R2.digitalWrite(P5, HIGH);}
            else if (header.indexOf("GET /15/on") >= 0)  {relay15state = "on";pcf8574_R2.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /15/off") >= 0) {relay15state = "off";pcf8574_R2.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /16/on") >= 0)  {relay16state = "on";pcf8574_R2.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /16/off") >= 0) {relay16state = "off";pcf8574_R2.digitalWrite(P7, HIGH);}
      
            else if (header.indexOf("GET /read") >= 0) flag_read_di=1;
            else if (header.indexOf("GET /all/on") >= 0)
            {
              relay1state = "on";// state of relay1
              relay2state = "on";// state of relay2
              relay3state = "on";// state of relay3
              relay4state = "on";// state of relay4
              relay5state = "on";// state of relay5
              relay6state = "on";// state of relay6
              relay7state = "on";// state of relay7
              relay8state = "on";// state of relay8

              relay9state = "on";// state of relay9
              relay10state = "on";// state of relay10
              relay11state = "on";// state of relay11
              relay12state = "on";// state of relay12
              relay13state = "on";// state of relay13
              relay14state = "on";// state of relay14
              relay15state = "on";// state of relay15
              relay16state = "on";// state of relay16
              for(int i=0;i<=7;i++)
              {
                 pcf8574_R1.digitalWrite(i, LOW);
                 pcf8574_R2.digitalWrite(i, LOW);
              }
             
            }
            else if (header.indexOf("GET /all/off") >= 0)
            {
              relay1state = "off";// state of relay1
              relay2state = "off";// state of relay2
              relay3state = "off";// state of relay3
              relay4state = "off";// state of relay4
              relay5state = "off";// state of relay5
              relay6state = "off";// state of relay6
              relay7state = "off";// state of relay7
              relay8state = "off";// state of relay8

              relay9state = "off";// state of relay9
              relay10state = "off";// state of relay10
              relay11state = "off";// state of relay11
              relay12state = "off";// state of relay12
              relay13state = "off";// state of relay13
              relay14state = "off";// state of relay14
              relay15state = "off";// state of relay15
              relay16state = "off";// state of relay16


              for(int i=0;i<=7;i++)
              {
                 pcf8574_R1.digitalWrite(i, HIGH);
                 pcf8574_R2.digitalWrite(i, HIGH);
              }
              
            }
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; border: 3px solid green;margin: 0px 15px;text-align: center;font-size: 10px;}");
            client.println(".button { background-color: #195B00; border-radius: 10px;color: white; padding: 10px 40px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
           
            if (flag_read_di==1)
            {
              client.println(" relay1:" + relay1state + " relay2:" + relay2state + " relay3:" + relay3state + " relay4:" + relay4state + " relay5:" + relay5state + " relay6:" + relay6state + " relay7:" + relay7state + " relay8:" + relay8state + " relay9:" + relay9state + " relay10:" + relay10state + " relay11:" + relay11state + " relay12:" + relay12state + " relay13:" + relay13state + " relay14:" + relay14state + " relay15:" + relay15state + " relay16:" + relay16state);
            }
            else
            {
            // Web Page Heading
            client.println("<body><h1>KC868-E16S Web Server</h1>");


            // Display current state, and ON/OFF buttons for relay
            //client.println("<p>relay1 - State " + relay1state + "</p>");
            // If the relay is off, it displays the ON button     
            if (relay1state == "off") client.println("<a href=\"/1/on\"><button class=\"button\">01-OFF</button></a>"); else client.println("<a href=\"/1/off\"><button class=\"button\">01--ON </button></a>");
            if (relay2state == "off") client.println("<a href=\"/2/on\"><button class=\"button\">02-OFF</button></a>"); else client.println("<a href=\"/2/off\"><button class=\"button\">02--ON </button></a>");                                
            if (relay3state == "off") client.println("<a href=\"/3/on\"><button class=\"button\">03-OFF</button></a>"); else client.println("<a href=\"/3/off\"><button class=\"button\">03--ON </button></a>");
            if (relay4state == "off") client.println("<a href=\"/4/on\"><button class=\"button\">04-OFF</button></a>"); else client.println("<a href=\"/4/off\"><button class=\"button\">04--ON </button></a>");
            if (relay5state == "off") client.println("<a href=\"/5/on\"><button class=\"button\">05-OFF</button></a>"); else client.println("<a href=\"/5/off\"><button class=\"button\">05--ON </button></a>");
            if (relay6state == "off") client.println("<a href=\"/6/on\"><button class=\"button\">06-OFF</button></a>"); else client.println("<a href=\"/6/off\"><button class=\"button\">06--ON </button></a>");                                
            if (relay7state == "off") client.println("<a href=\"/7/on\"><button class=\"button\">07-OFF</button></a>"); else client.println("<a href=\"/7/off\"><button class=\"button\">07--ON </button></a>");
            if (relay8state == "off") client.println("<a href=\"/8/on\"><button class=\"button\">08-OFF</button></a>"); else client.println("<a href=\"/8/off\"><button class=\"button\">08--ON </button></a>");
            if (relay9state == "off") client.println("<a href=\"/9/on\"><button class=\"button\">09-OFF</button></a>"); else client.println("<a href=\"/9/off\"><button class=\"button\">09--ON </button></a>");
            if (relay10state == "off") client.println("<a href=\"/10/on\"><button class=\"button\">10-OFF</button></a>"); else client.println("<a href=\"/10/off\"><button class=\"button\">10--ON</button></a>");                                
            if (relay11state == "off") client.println("<a href=\"/11/on\"><button class=\"button\">11-OFF</button></a>"); else client.println("<a href=\"/11/off\"><button class=\"button\">11--ON</button></a>");
            if (relay12state == "off") client.println("<a href=\"/12/on\"><button class=\"button\">12-OFF</button></a>"); else client.println("<a href=\"/12/off\"><button class=\"button\">12--ON</button></a>");
            if (relay13state == "off") client.println("<a href=\"/13/on\"><button class=\"button\">13-OFF</button></a>"); else client.println("<a href=\"/13/off\"><button class=\"button\">13--ON</button></a>");
            if (relay14state == "off") client.println("<a href=\"/14/on\"><button class=\"button\">14-OFF</button></a>"); else client.println("<a href=\"/14/off\"><button class=\"button\">14--ON</button></a>");                                
            if (relay15state == "off") client.println("<a href=\"/15/on\"><button class=\"button\">15-OFF</button></a>"); else client.println("<a href=\"/15/off\"><button class=\"button\">15--ON</button></a>");
            if (relay16state == "off") client.println("<a href=\"/16/on\"><button class=\"button\">16-OFF</button></a>"); else client.println("<a href=\"/16/off\"><button class=\"button\">16--ON</button></a>");

              client.println("<a href=\"/all/on\"><button class=\"button\">All--ON</button></a>");
              client.println("<a href=\"/all/off\"><button class=\"button\">All-OFF</button></a>");
            } 
            client.println("</body></html>");
           
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
    flag_read_di=0;
  }

  ScanKey();            //scan key
}

void ScanKey()           
{
  for(int i=0;i<=7;i++)
  {
    if(pcf8574_I1.digitalRead(i) == LOW)         //key pressed
      {
        delay(20);                       
        if(pcf8574_I1.digitalRead(i) == LOW)       //key pressed again
        {
          pcf8574_R1.digitalWrite(i,!pcf8574_R1.digitalRead(i));    // toggle relay1 state    
          while(pcf8574_I1.digitalRead(i) == LOW); //wait remove hand
          if (pcf8574_R1.digitalRead(i) == LOW) relaystate[i+1]="on";else relaystate[i+1]="off";
        }
      }
  }

    for(int j=0;j<=7;j++)
  {
    if(pcf8574_I2.digitalRead(j) == LOW)         //key pressed
      {
        delay(20);                      
        if(pcf8574_I2.digitalRead(j) == LOW)       //key pressed again
        {
          pcf8574_R2.digitalWrite(j,!pcf8574_R2.digitalRead(j));    // toggle relay1 state    
          while(pcf8574_I2.digitalRead(j) == LOW); //wait remove hand
          if (pcf8574_R2.digitalRead(j) == LOW) relaystate[j+8]="on";else relaystate[j+8]="off";
       
        }
      }
  }

}

Print this item

  [Arduino IDE demo source code for KC868-A16]--#11-KC868-A16_Ethernet_Web_server_code
Posted by: KinCony Support - 09-05-2022, 03:35 AM - Forum: KC868-A16 - Replies (12)

[Arduino IDE demo source code for KC868-A16]--#11-KC868-A16_Ethernet_Web_server_code
                      Refer to code #11 of A8 for download methods

.zip   KC868-A16_Web_Server_Eth.zip (Size: 413.26 KB / Downloads: 894)

Code:
/********************************************************************
* Create Your own Ethernet-WebServer for KC868-A32 Smart Controller*
* https://www.kincony.com                                ***********
*********************************************************************/

#include "PCF8574.h"
#define DEBUG_ETHERNET_WEBSERVER_PORT       Serial

// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_       3

#include <WebServer_WT32_ETH01.h>

WiFiServer server(80);


// Select the IP address according to your local network
IPAddress myIP(192, 168, 1, 232);
IPAddress myGW(192, 168, 1, 1);
IPAddress mySN(255, 255, 255, 0);

// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

int flag_read_di=0;

// Variable to store the HTTP request
String header;

TwoWire I2Cone = TwoWire(0);
//TwoWire I2Ctwo = TwoWire(1);

// Set relay output i2c address
PCF8574 pcf8574_R1(&I2Cone, 0x3C, 4, 5);
PCF8574 pcf8574_R2(&I2Cone, 0x3D, 4, 5);
//PCF8574 pcf8574_R3(&I2Ctwo, 0x21, 15, 13);
//PCF8574 pcf8574_R4(&I2Ctwo, 0x22, 15, 13);

// Set digital input i2c address
PCF8574 pcf8574_I1(&I2Cone, 0x3A, 4, 5);
PCF8574 pcf8574_I2(&I2Cone, 0x39, 4, 5);
//PCF8574 pcf8574_I3(&I2Cone, 0x21, 4, 5);
//PCF8574 pcf8574_I4(&I2Cone, 0x22, 4, 5);


String relay1state = "off";// state of relay1
String relay2state = "off";// state of relay2
String relay3state = "off";// state of relay3
String relay4state = "off";// state of relay4
String relay5state = "off";// state of relay5
String relay6state = "off";// state of relay6
String relay7state = "off";// state of relay7
String relay8state = "off";// state of relay8

String relay9state = "off";// state of relay9
String relay10state = "off";// state of relay10
String relay11state = "off";// state of relay11
String relay12state = "off";// state of relay12
String relay13state = "off";// state of relay13
String relay14state = "off";// state of relay14
String relay15state = "off";// state of relay15
String relay16state = "off";// state of relay16
/*
String relay17state = "off";// state of relay17
String relay18state = "off";// state of relay18
String relay19state = "off";// state of relay19
String relay20state = "off";// state of relay20
String relay21state = "off";// state of relay21
String relay22state = "off";// state of relay22
String relay23state = "off";// state of relay23
String relay24state = "off";// state of relay24

String relay25state = "off";// state of relay25
String relay26state = "off";// state of relay26
String relay27state = "off";// state of relay27
String relay28state = "off";// state of relay28
String relay29state = "off";// state of relay29
String relay30state = "off";// state of relay30
String relay31state = "off";// state of relay31
String relay32state = "off";// state of relay32*/


void setup()
{
  Serial.begin(115200);

  // Set PCF8574 pinMode to OUTPUT
pcf8574_R1.pinMode(P0, OUTPUT);
pcf8574_R1.pinMode(P1, OUTPUT);
pcf8574_R1.pinMode(P2, OUTPUT);
pcf8574_R1.pinMode(P3, OUTPUT);
pcf8574_R1.pinMode(P4, OUTPUT);
pcf8574_R1.pinMode(P5, OUTPUT);
pcf8574_R1.pinMode(P6, OUTPUT);
pcf8574_R1.pinMode(P7, OUTPUT);

pcf8574_R2.pinMode(P0, OUTPUT);
pcf8574_R2.pinMode(P1, OUTPUT);
pcf8574_R2.pinMode(P2, OUTPUT);
pcf8574_R2.pinMode(P3, OUTPUT);
pcf8574_R2.pinMode(P4, OUTPUT);
pcf8574_R2.pinMode(P5, OUTPUT);
pcf8574_R2.pinMode(P6, OUTPUT);
pcf8574_R2.pinMode(P7, OUTPUT);
/*
pcf8574_R3.pinMode(P0, OUTPUT);
pcf8574_R3.pinMode(P1, OUTPUT);
pcf8574_R3.pinMode(P2, OUTPUT);
pcf8574_R3.pinMode(P3, OUTPUT);
pcf8574_R3.pinMode(P4, OUTPUT);
pcf8574_R3.pinMode(P5, OUTPUT);
pcf8574_R3.pinMode(P6, OUTPUT);
pcf8574_R3.pinMode(P7, OUTPUT);

pcf8574_R4.pinMode(P0, OUTPUT);
pcf8574_R4.pinMode(P1, OUTPUT);
pcf8574_R4.pinMode(P2, OUTPUT);
pcf8574_R4.pinMode(P3, OUTPUT);
pcf8574_R4.pinMode(P4, OUTPUT);
pcf8574_R4.pinMode(P5, OUTPUT);
pcf8574_R4.pinMode(P6, OUTPUT);
pcf8574_R4.pinMode(P7, OUTPUT);*/

  Serial.print("Init pcf8574_R1...");
  if (pcf8574_R1.begin()){
    Serial.println("PCF8574_R1_OK");
  }else{
    Serial.println("PCF8574_R1_KO");
  }

  Serial.print("Init pcf8574_R2...");
  if (pcf8574_R2.begin()){
    Serial.println("PCF8574_R2_OK");
  }else{
    Serial.println("PCF8574_R2_KO");
  }
/*
  Serial.print("Init pcf8574_R3...");
  if (pcf8574_R3.begin()){
    Serial.println("PCF8574_R3_OK");
  }else{
    Serial.println("PCF8574_R3_KO");
  }

  Serial.print("Init pcf8574_R4...");
  if (pcf8574_R4.begin()){
    Serial.println("PCF8574_R4_OK");
  }else{
    Serial.println("PCF8574_R4_KO");
  }*/
//--------------------------------------
pcf8574_I1.pinMode(P0, INPUT);
pcf8574_I1.pinMode(P1, INPUT);
pcf8574_I1.pinMode(P2, INPUT);
pcf8574_I1.pinMode(P3, INPUT);
pcf8574_I1.pinMode(P4, INPUT);
pcf8574_I1.pinMode(P5, INPUT);
pcf8574_I1.pinMode(P6, INPUT);
pcf8574_I1.pinMode(P7, INPUT);

pcf8574_I2.pinMode(P0, INPUT);
pcf8574_I2.pinMode(P1, INPUT);
pcf8574_I2.pinMode(P2, INPUT);
pcf8574_I2.pinMode(P3, INPUT);
pcf8574_I2.pinMode(P4, INPUT);
pcf8574_I2.pinMode(P5, INPUT);
pcf8574_I2.pinMode(P6, INPUT);
pcf8574_I2.pinMode(P7, INPUT);
/*
pcf8574_I3.pinMode(P0, INPUT);
pcf8574_I3.pinMode(P1, INPUT);
pcf8574_I3.pinMode(P2, INPUT);
pcf8574_I3.pinMode(P3, INPUT);
pcf8574_I3.pinMode(P4, INPUT);
pcf8574_I3.pinMode(P5, INPUT);
pcf8574_I3.pinMode(P6, INPUT);
pcf8574_I3.pinMode(P7, INPUT);

pcf8574_I4.pinMode(P0, INPUT);
pcf8574_I4.pinMode(P1, INPUT);
pcf8574_I4.pinMode(P2, INPUT);
pcf8574_I4.pinMode(P3, INPUT);
pcf8574_I4.pinMode(P4, INPUT);
pcf8574_I4.pinMode(P5, INPUT);
pcf8574_I4.pinMode(P6, INPUT);
pcf8574_I4.pinMode(P7, INPUT);*/

  Serial.print("Init pcf8574...");
  if (pcf8574_I1.begin()){
    Serial.println("pcf8574_I1_OK");
  }else{
    Serial.println("pcf8574_I1_KO");
  }

  Serial.print("Init pcf8574...");
  if (pcf8574_I2.begin()){
    Serial.println("pcf8574_I2_OK");
  }else{
    Serial.println("pcf8574_I2_KO");
  }
/*
  Serial.print("Init pcf8574_I3...");
  if (pcf8574_I3.begin()){
    Serial.println("pcf8574_I3_OK");
  }else{
    Serial.println("pcf8574_I3_KO");
  }

  Serial.print("Init pcf8574_I4...");
  if (pcf8574_I4.begin()){
    Serial.println("pcf8574_I4_OK");
  }else{
    Serial.println("pcf8574_I4_KO");
  }*/

 
//-------------------------------------
  pcf8574_R1.digitalWrite(P0, HIGH);
  pcf8574_R1.digitalWrite(P1, HIGH);
  pcf8574_R1.digitalWrite(P2, HIGH);
  pcf8574_R1.digitalWrite(P3, HIGH);
  pcf8574_R1.digitalWrite(P4, HIGH);
  pcf8574_R1.digitalWrite(P5, HIGH);
  pcf8574_R1.digitalWrite(P6, HIGH);
  pcf8574_R1.digitalWrite(P7, HIGH);

  pcf8574_R2.digitalWrite(P0, HIGH);
  pcf8574_R2.digitalWrite(P1, HIGH);
  pcf8574_R2.digitalWrite(P2, HIGH);
  pcf8574_R2.digitalWrite(P3, HIGH);
  pcf8574_R2.digitalWrite(P4, HIGH);
  pcf8574_R2.digitalWrite(P5, HIGH);
  pcf8574_R2.digitalWrite(P6, HIGH);
  pcf8574_R2.digitalWrite(P7, HIGH);
/*
  pcf8574_R3.digitalWrite(P0, HIGH);
  pcf8574_R3.digitalWrite(P1, HIGH);
  pcf8574_R3.digitalWrite(P2, HIGH);
  pcf8574_R3.digitalWrite(P3, HIGH);
  pcf8574_R3.digitalWrite(P4, HIGH);
  pcf8574_R3.digitalWrite(P5, HIGH);
  pcf8574_R3.digitalWrite(P6, HIGH);
  pcf8574_R3.digitalWrite(P7, HIGH);

  pcf8574_R4.digitalWrite(P0, HIGH);
  pcf8574_R4.digitalWrite(P1, HIGH);
  pcf8574_R4.digitalWrite(P2, HIGH);
  pcf8574_R4.digitalWrite(P3, HIGH);
  pcf8574_R4.digitalWrite(P4, HIGH);
  pcf8574_R4.digitalWrite(P5, HIGH);
  pcf8574_R4.digitalWrite(P6, HIGH);
  pcf8574_R4.digitalWrite(P7, HIGH);*/

 
  while (!Serial);

  // Using this if Serial debugging is not necessary or not using Serial port
  //while (!Serial && (millis() < 3000));

  Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  // To be called before ETH.begin()
  WT32_ETH01_onEvent();

  //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
  //           eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
  //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
// ETH.config(myIP, myGW, mySN, myDNS);

  WT32_ETH01_waitForConnect();

  // start the web server on port 80
  server.begin();
}

void loop()
{
  // listen for incoming clients
  WiFiClient client = server.available();

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();         
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();

         

           
            // turns 32 relays on and off
            if (header.indexOf("GET /1/on") >= 0) {relay1state = "on";pcf8574_R1.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /1/off") >= 0) {relay1state = "off";pcf8574_R1.digitalWrite(P0, HIGH);}
            else if (header.indexOf("GET /2/on") >= 0)  {relay2state = "on";pcf8574_R1.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /2/off") >= 0) {relay2state = "off";pcf8574_R1.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /3/on") >= 0)  {relay3state = "on";pcf8574_R1.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /3/off") >= 0) {relay3state = "off";pcf8574_R1.digitalWrite(P2, HIGH);}         
            else if (header.indexOf("GET /4/on") >= 0)  {relay4state = "on";pcf8574_R1.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /4/off") >= 0) {relay4state = "off";pcf8574_R1.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /5/on") >= 0)  {relay5state = "on";pcf8574_R1.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /5/off") >= 0) {relay5state = "off";pcf8574_R1.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /6/on") >= 0)  {relay6state = "on";pcf8574_R1.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /6/off") >= 0) {relay6state = "off";pcf8574_R1.digitalWrite(P5, HIGH);}         
            else if (header.indexOf("GET /7/on") >= 0)  {relay7state = "on";pcf8574_R1.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /7/off") >= 0) {relay7state = "off";pcf8574_R1.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /8/on") >= 0)  {relay8state = "on";pcf8574_R1.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /8/off") >= 0) {relay8state = "off";pcf8574_R1.digitalWrite(P7, HIGH);}
            else if (header.indexOf("GET /9/on") >= 0)  {relay9state = "on";pcf8574_R2.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /9/off") >= 0) {relay9state = "off";pcf8574_R2.digitalWrite(P0, HIGH);}         
            else if (header.indexOf("GET /10/on") >= 0)  {relay10state = "on";pcf8574_R2.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /10/off") >= 0) {relay10state = "off";pcf8574_R2.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /11/on") >= 0)  {relay11state = "on";pcf8574_R2.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /11/off") >= 0) {relay11state = "off";pcf8574_R2.digitalWrite(P2, HIGH);}
            else if (header.indexOf("GET /12/on") >= 0)  {relay12state = "on";pcf8574_R2.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /12/off") >= 0) {relay12state = "off";pcf8574_R2.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /13/on") >= 0)  {relay13state = "on";pcf8574_R2.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /13/off") >= 0) {relay13state = "off";pcf8574_R2.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /14/on") >= 0)  {relay14state = "on";pcf8574_R2.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /14/off") >= 0) {relay14state = "off";pcf8574_R2.digitalWrite(P5, HIGH);}
            else if (header.indexOf("GET /15/on") >= 0)  {relay15state = "on";pcf8574_R2.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /15/off") >= 0) {relay15state = "off";pcf8574_R2.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /16/on") >= 0)  {relay16state = "on";pcf8574_R2.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /16/off") >= 0) {relay16state = "off";pcf8574_R2.digitalWrite(P7, HIGH);}
     /*       else if (header.indexOf("GET /17/on") >= 0)  {relay17state = "on";pcf8574_R3.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /17/off") >= 0) {relay17state = "off";pcf8574_R3.digitalWrite(P0, HIGH);}
            else if (header.indexOf("GET /18/on") >= 0)  {relay18state = "on";pcf8574_R3.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /18/off") >= 0) {relay18state = "off";pcf8574_R3.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /19/on") >= 0)  {relay19state = "on";pcf8574_R3.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /19/off") >= 0) {relay19state = "off";pcf8574_R3.digitalWrite(P2, HIGH);}
            else if (header.indexOf("GET /20/on") >= 0)  {relay20state = "on";pcf8574_R3.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /20/off") >= 0) {relay20state = "off";pcf8574_R3.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /21/on") >= 0)  {relay21state = "on";pcf8574_R3.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /21/off") >= 0) {relay21state = "off";pcf8574_R3.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /22/on") >= 0)  {relay22state = "on";pcf8574_R3.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /22/off") >= 0) {relay22state = "off";pcf8574_R3.digitalWrite(P5, HIGH);}
            else if (header.indexOf("GET /23/on") >= 0)  {relay23state = "on";pcf8574_R3.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /23/off") >= 0) {relay23state = "off";pcf8574_R3.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /24/on") >= 0)  {relay24state = "on";pcf8574_R3.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /24/off") >= 0) {relay24state = "off";pcf8574_R3.digitalWrite(P7, HIGH);}
            else if (header.indexOf("GET /25/on") >= 0)  {relay25state = "on";pcf8574_R4.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /25/off") >= 0) {relay25state = "off";pcf8574_R4.digitalWrite(P0, HIGH);}
            else if (header.indexOf("GET /26/on") >= 0)  {relay26state = "on";pcf8574_R4.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /26/off") >= 0) {relay26state = "off";pcf8574_R4.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /27/on") >= 0)  {relay27state = "on";pcf8574_R4.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /27/off") >= 0) {relay27state = "off";pcf8574_R4.digitalWrite(P2, HIGH);}
            else if (header.indexOf("GET /28/on") >= 0)  {relay28state = "on";pcf8574_R4.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /28/off") >= 0) {relay28state = "off";pcf8574_R4.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /29/on") >= 0)  {relay29state = "on";pcf8574_R4.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /29/off") >= 0) {relay29state = "off";pcf8574_R4.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /30/on") >= 0)  {relay30state = "on";pcf8574_R4.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /30/off") >= 0) {relay30state = "off";pcf8574_R4.digitalWrite(P5, HIGH);}
            else if (header.indexOf("GET /31/on") >= 0)  {relay31state = "on";pcf8574_R4.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /31/off") >= 0) {relay31state = "off";pcf8574_R4.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /32/on") >= 0)  {relay32state = "on";pcf8574_R4.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /32/off") >= 0) {relay32state = "off";pcf8574_R4.digitalWrite(P7, HIGH);}*/
            else if (header.indexOf("GET /read") >= 0) flag_read_di=1;
            else if (header.indexOf("GET /all/on") >= 0)
            {
              relay1state = "on";// state of relay1
              relay2state = "on";// state of relay2
              relay3state = "on";// state of relay3
              relay4state = "on";// state of relay4
              relay5state = "on";// state of relay5
              relay6state = "on";// state of relay6
              relay7state = "on";// state of relay7
              relay8state = "on";// state of relay8

              relay9state = "on";// state of relay9
              relay10state = "on";// state of relay10
              relay11state = "on";// state of relay11
              relay12state = "on";// state of relay12
              relay13state = "on";// state of relay13
              relay14state = "on";// state of relay14
              relay15state = "on";// state of relay15
              relay16state = "on";// state of relay16
/*
              relay17state = "on";// state of relay17
              relay18state = "on";// state of relay18
              relay19state = "on";// state of relay19
              relay20state = "on";// state of relay20
              relay21state = "on";// state of relay21
              relay22state = "on";// state of relay22
              relay23state = "on";// state of relay23
              relay24state = "on";// state of relay24

              relay25state = "on";// state of relay25
              relay26state = "on";// state of relay26
              relay27state = "on";// state of relay27
              relay28state = "on";// state of relay28
              relay29state = "on";// state of relay29
              relay30state = "on";// state of relay30
              relay31state = "on";// state of relay31
              relay32state = "on";// state of relay32*/

              pcf8574_R1.digitalWrite(P0, LOW);
              pcf8574_R1.digitalWrite(P1, LOW);
              pcf8574_R1.digitalWrite(P2, LOW);
              pcf8574_R1.digitalWrite(P3, LOW);
              pcf8574_R1.digitalWrite(P4, LOW);
              pcf8574_R1.digitalWrite(P5, LOW);
              pcf8574_R1.digitalWrite(P6, LOW);
              pcf8574_R1.digitalWrite(P7, LOW);

              pcf8574_R2.digitalWrite(P0, LOW);
              pcf8574_R2.digitalWrite(P1, LOW);
              pcf8574_R2.digitalWrite(P2, LOW);
              pcf8574_R2.digitalWrite(P3, LOW);
              pcf8574_R2.digitalWrite(P4, LOW);
              pcf8574_R2.digitalWrite(P5, LOW);
              pcf8574_R2.digitalWrite(P6, LOW);
              pcf8574_R2.digitalWrite(P7, LOW);
/*
              pcf8574_R3.digitalWrite(P0, LOW);
              pcf8574_R3.digitalWrite(P1, LOW);
              pcf8574_R3.digitalWrite(P2, LOW);
              pcf8574_R3.digitalWrite(P3, LOW);
              pcf8574_R3.digitalWrite(P4, LOW);
              pcf8574_R3.digitalWrite(P5, LOW);
              pcf8574_R3.digitalWrite(P6, LOW);
              pcf8574_R3.digitalWrite(P7, LOW);

              pcf8574_R4.digitalWrite(P0, LOW);
              pcf8574_R4.digitalWrite(P1, LOW);
              pcf8574_R4.digitalWrite(P2, LOW);
              pcf8574_R4.digitalWrite(P3, LOW);
              pcf8574_R4.digitalWrite(P4, LOW);
              pcf8574_R4.digitalWrite(P5, LOW);
              pcf8574_R4.digitalWrite(P6, LOW);
              pcf8574_R4.digitalWrite(P7, LOW);*/

             
            }
            else if (header.indexOf("GET /all/off") >= 0)
            {
              relay1state = "off";// state of relay1
              relay2state = "off";// state of relay2
              relay3state = "off";// state of relay3
              relay4state = "off";// state of relay4
              relay5state = "off";// state of relay5
              relay6state = "off";// state of relay6
              relay7state = "off";// state of relay7
              relay8state = "off";// state of relay8

              relay9state = "off";// state of relay9
              relay10state = "off";// state of relay10
              relay11state = "off";// state of relay11
              relay12state = "off";// state of relay12
              relay13state = "off";// state of relay13
              relay14state = "off";// state of relay14
              relay15state = "off";// state of relay15
              relay16state = "off";// state of relay16
/*
              relay17state = "off";// state of relay17
              relay18state = "off";// state of relay18
              relay19state = "off";// state of relay19
              relay20state = "off";// state of relay20
              relay21state = "off";// state of relay21
              relay22state = "off";// state of relay22
              relay23state = "off";// state of relay23
              relay24state = "off";// state of relay24

              relay25state = "off";// state of relay25
              relay26state = "off";// state of relay26
              relay27state = "off";// state of relay27
              relay28state = "off";// state of relay28
              relay29state = "off";// state of relay29
              relay30state = "off";// state of relay30
              relay31state = "off";// state of relay31
              relay32state = "off";// state of relay32*/

              pcf8574_R1.digitalWrite(P0, HIGH);
              pcf8574_R1.digitalWrite(P1, HIGH);
              pcf8574_R1.digitalWrite(P2, HIGH);
              pcf8574_R1.digitalWrite(P3, HIGH);
              pcf8574_R1.digitalWrite(P4, HIGH);
              pcf8574_R1.digitalWrite(P5, HIGH);
              pcf8574_R1.digitalWrite(P6, HIGH);
              pcf8574_R1.digitalWrite(P7, HIGH);

              pcf8574_R2.digitalWrite(P0, HIGH);
              pcf8574_R2.digitalWrite(P1, HIGH);
              pcf8574_R2.digitalWrite(P2, HIGH);
              pcf8574_R2.digitalWrite(P3, HIGH);
              pcf8574_R2.digitalWrite(P4, HIGH);
              pcf8574_R2.digitalWrite(P5, HIGH);
              pcf8574_R2.digitalWrite(P6, HIGH);
              pcf8574_R2.digitalWrite(P7, HIGH);
/*
              pcf8574_R3.digitalWrite(P0, HIGH);
              pcf8574_R3.digitalWrite(P1, HIGH);
              pcf8574_R3.digitalWrite(P2, HIGH);
              pcf8574_R3.digitalWrite(P3, HIGH);
              pcf8574_R3.digitalWrite(P4, HIGH);
              pcf8574_R3.digitalWrite(P5, HIGH);
              pcf8574_R3.digitalWrite(P6, HIGH);
              pcf8574_R3.digitalWrite(P7, HIGH);

              pcf8574_R4.digitalWrite(P0, HIGH);
              pcf8574_R4.digitalWrite(P1, HIGH);
              pcf8574_R4.digitalWrite(P2, HIGH);
              pcf8574_R4.digitalWrite(P3, HIGH);
              pcf8574_R4.digitalWrite(P4, HIGH);
              pcf8574_R4.digitalWrite(P5, HIGH);
              pcf8574_R4.digitalWrite(P6, HIGH);
              pcf8574_R4.digitalWrite(P7, HIGH);   */                   
            }         
         
         
         
          client.print("<!DOCTYPE HTML>\r\n");
          client.print("<html>\r\n");

            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
            client.println(".button { background-color: #195B6A; border: none; color: white; padding: 16px 40px;");
            client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
           
            if (flag_read_di==1)
            {
              client.println(" relay1:" + relay1state + " relay2:" + relay2state + " relay3:" + relay3state + " relay4:" + relay4state + " relay5:" + relay5state + " relay6:" + relay6state + " relay7:" + relay7state + " relay8:" + relay8state + " relay9:" + relay9state + " relay10:" + relay10state + " relay11:" + relay11state + " relay12:" + relay12state + " relay13:" + relay13state + " relay14:" + relay14state + " relay15:" + relay15state + " relay16:" + relay16state );
            }
            else
            {
            // Web Page Heading
            client.println("<body><h1>KC868-A16 Ethernet Web Server</h1>");


            // Display current state, and ON/OFF buttons for relay
            //client.println("<p>relay1 - State " + relay1state + "</p>");
            // If the relay is off, it displays the ON button     
            if (relay1state == "off") client.println("<a href=\"/1/on\"><button class=\"button\">1-ON</button></a>"); else client.println("<a href=\"/1/off\"><button class=\"button\">1-OFF</button></a>");
            if (relay2state == "off") client.println("<a href=\"/2/on\"><button class=\"button\">2-ON</button></a>"); else client.println("<a href=\"/2/off\"><button class=\"button\">2-OFF</button></a>");                                 
            if (relay3state == "off") client.println("<a href=\"/3/on\"><button class=\"button\">3-ON</button></a>"); else client.println("<a href=\"/3/off\"><button class=\"button\">3-OFF</button></a>");
            if (relay4state == "off") client.println("<a href=\"/4/on\"><button class=\"button\">4-ON</button></a>"); else client.println("<a href=\"/4/off\"><button class=\"button\">4-OFF</button></a>");
            if (relay5state == "off") client.println("<a href=\"/5/on\"><button class=\"button\">5-ON</button></a>"); else client.println("<a href=\"/5/off\"><button class=\"button\">5-OFF</button></a>");
            if (relay6state == "off") client.println("<a href=\"/6/on\"><button class=\"button\">6-ON</button></a>"); else client.println("<a href=\"/6/off\"><button class=\"button\">6-OFF</button></a>");                                 
            if (relay7state == "off") client.println("<a href=\"/7/on\"><button class=\"button\">7-ON</button></a>"); else client.println("<a href=\"/7/off\"><button class=\"button\">7-OFF</button></a>");
            if (relay8state == "off") client.println("<a href=\"/8/on\"><button class=\"button\">8-ON</button></a>"); else client.println("<a href=\"/8/off\"><button class=\"button\">8-OFF</button></a>");
            if (relay9state == "off") client.println("<a href=\"/9/on\"><button class=\"button\">9-ON</button></a>"); else client.println("<a href=\"/9/off\"><button class=\"button\">9-OFF</button></a>");
            if (relay10state == "off") client.println("<a href=\"/10/on\"><button class=\"button\">10-ON</button></a>"); else client.println("<a href=\"/10/off\"><button class=\"button\">10-OFF</button></a>");                                 
            if (relay11state == "off") client.println("<a href=\"/11/on\"><button class=\"button\">11-ON</button></a>"); else client.println("<a href=\"/11/off\"><button class=\"button\">11-OFF</button></a>");
            if (relay12state == "off") client.println("<a href=\"/12/on\"><button class=\"button\">12-ON</button></a>"); else client.println("<a href=\"/12/off\"><button class=\"button\">12-OFF</button></a>");
            if (relay13state == "off") client.println("<a href=\"/13/on\"><button class=\"button\">13-ON</button></a>"); else client.println("<a href=\"/13/off\"><button class=\"button\">13-OFF</button></a>");
            if (relay14state == "off") client.println("<a href=\"/14/on\"><button class=\"button\">14-ON</button></a>"); else client.println("<a href=\"/14/off\"><button class=\"button\">14-OFF</button></a>");                                 
            if (relay15state == "off") client.println("<a href=\"/15/on\"><button class=\"button\">15-ON</button></a>"); else client.println("<a href=\"/15/off\"><button class=\"button\">15-OFF</button></a>");
            if (relay16state == "off") client.println("<a href=\"/16/on\"><button class=\"button\">16-ON</button></a>"); else client.println("<a href=\"/16/off\"><button class=\"button\">16-OFF</button></a>");
        /*    if (relay17state == "off") client.println("<a href=\"/17/on\"><button class=\"button\">17-ON</button></a>"); else client.println("<a href=\"/17/off\"><button class=\"button\">17-OFF</button></a>");
            if (relay18state == "off") client.println("<a href=\"/18/on\"><button class=\"button\">18-ON</button></a>"); else client.println("<a href=\"/18/off\"><button class=\"button\">18-OFF</button></a>");                                 
            if (relay19state == "off") client.println("<a href=\"/19/on\"><button class=\"button\">19-ON</button></a>"); else client.println("<a href=\"/19/off\"><button class=\"button\">19-OFF</button></a>");
            if (relay20state == "off") client.println("<a href=\"/20/on\"><button class=\"button\">20-ON</button></a>"); else client.println("<a href=\"/20/off\"><button class=\"button\">20-OFF</button></a>");
            if (relay21state == "off") client.println("<a href=\"/21/on\"><button class=\"button\">21-ON</button></a>"); else client.println("<a href=\"/21/off\"><button class=\"button\">21-OFF</button></a>");
            if (relay22state == "off") client.println("<a href=\"/22/on\"><button class=\"button\">22-ON</button></a>"); else client.println("<a href=\"/22/off\"><button class=\"button\">22-OFF</button></a>");                                 
            if (relay23state == "off") client.println("<a href=\"/23/on\"><button class=\"button\">23-ON</button></a>"); else client.println("<a href=\"/23/off\"><button class=\"button\">23-OFF</button></a>");
            if (relay24state == "off") client.println("<a href=\"/24/on\"><button class=\"button\">24-ON</button></a>"); else client.println("<a href=\"/24/off\"><button class=\"button\">24-OFF</button></a>");
            if (relay25state == "off") client.println("<a href=\"/25/on\"><button class=\"button\">25-ON</button></a>"); else client.println("<a href=\"/25/off\"><button class=\"button\">25-OFF</button></a>");
            if (relay26state == "off") client.println("<a href=\"/26/on\"><button class=\"button\">26-ON</button></a>"); else client.println("<a href=\"/26/off\"><button class=\"button\">26-OFF</button></a>");                                 
            if (relay27state == "off") client.println("<a href=\"/27/on\"><button class=\"button\">27-ON</button></a>"); else client.println("<a href=\"/27/off\"><button class=\"button\">27-OFF</button></a>");
            if (relay28state == "off") client.println("<a href=\"/28/on\"><button class=\"button\">28-ON</button></a>"); else client.println("<a href=\"/28/off\"><button class=\"button\">28-OFF</button></a>");
            if (relay29state == "off") client.println("<a href=\"/29/on\"><button class=\"button\">29-ON</button></a>"); else client.println("<a href=\"/29/off\"><button class=\"button\">29-OFF</button></a>");
            if (relay30state == "off") client.println("<a href=\"/30/on\"><button class=\"button\">30-ON</button></a>"); else client.println("<a href=\"/30/off\"><button class=\"button\">30-OFF</button></a>");                                 
            if (relay31state == "off") client.println("<a href=\"/31/on\"><button class=\"button\">31-ON</button></a>"); else client.println("<a href=\"/31/off\"><button class=\"button\">31-OFF</button></a>");
            if (relay32state == "off") client.println("<a href=\"/32/on\"><button class=\"button\">32-ON</button></a>"); else client.println("<a href=\"/32/off\"><button class=\"button\">32-OFF</button></a>");
*/

              client.println("<a href=\"/all/on\"><button class=\"button\">all ON</button></a>");
              client.println("<a href=\"/all/off\"><button class=\"button\">all OFF</button></a>");
            } 
            client.println("</body></html>");
           
            // The HTTP response ends with another blank line
            client.println();
            // Break out of the while loop
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
    flag_read_di=0;
  }

  ScanKey();            //scan key
}

void ScanKey()           
{
                   
  if(pcf8574_I1.digitalRead(P0) == LOW)         //key1 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P0) == LOW)       //key1 pressed again
    {
      pcf8574_R1.digitalWrite(P0,!pcf8574_R1.digitalRead(P0));    // toggle relay1 state     
      while(pcf8574_I1.digitalRead(P0) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P0) == LOW) relay1state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P1) == LOW)         //key2 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P1) == LOW)       //key2 pressed again
    {
      pcf8574_R1.digitalWrite(P1,!pcf8574_R1.digitalRead(P1));    // toggle relay2 state     
      while(pcf8574_I1.digitalRead(P1) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P1) == LOW) relay2state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P2) == LOW)         //key3 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P2) == LOW)       //key3 pressed again
    {
      pcf8574_R1.digitalWrite(P2,!pcf8574_R1.digitalRead(P2));    // toggle relay3 state     
      while(pcf8574_I1.digitalRead(P2) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P2) == LOW) relay3state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P3) == LOW)         //key4 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P3) == LOW)       //key4 pressed again
    {
      pcf8574_R1.digitalWrite(P3,!pcf8574_R1.digitalRead(P3));    // toggle relay4 state     
      while(pcf8574_I1.digitalRead(P3) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P3) == LOW) relay4state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P4) == LOW)         //key5 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P4) == LOW)       //key5 pressed again
    {
      pcf8574_R1.digitalWrite(P4,!pcf8574_R1.digitalRead(P4));    // toggle relay5 state     
      while(pcf8574_I1.digitalRead(P4) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P4) == LOW) relay5state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P5) == LOW)         //key6 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P5) == LOW)       //key6 pressed again
    {
      pcf8574_R1.digitalWrite(P5,!pcf8574_R1.digitalRead(P5));    // toggle relay6 state     
      while(pcf8574_I1.digitalRead(P5) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P5) == LOW) relay6state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P6) == LOW)         //key7 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P6) == LOW)       //key7 pressed again
    {
      pcf8574_R1.digitalWrite(P6,!pcf8574_R1.digitalRead(P6));    // toggle relay7 state     
      while(pcf8574_I1.digitalRead(P6) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P6) == LOW) relay7state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I1.digitalRead(P7) == LOW)         //key8 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I1.digitalRead(P7) == LOW)       //key8 pressed again
    {
      pcf8574_R1.digitalWrite(P7,!pcf8574_R1.digitalRead(P7));    // toggle relay8 state     
      while(pcf8574_I1.digitalRead(P7) == LOW); //wait remove hand
      if (pcf8574_R1.digitalRead(P7) == LOW) relay8state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P0) == LOW)         //key9 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P0) == LOW)       //key9 pressed again
    {
      pcf8574_R2.digitalWrite(P0,!pcf8574_R2.digitalRead(P0));    // toggle relay9 state     
      while(pcf8574_I2.digitalRead(P0) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P0) == LOW) relay9state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P1) == LOW)         //key10 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P1) == LOW)       //key10 pressed again
    {
      pcf8574_R2.digitalWrite(P1,!pcf8574_R2.digitalRead(P1));    // toggle relay10 state     
      while(pcf8574_I2.digitalRead(P1) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P1) == LOW) relay10state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P2) == LOW)         //key11 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P2) == LOW)       //key11 pressed again
    {
      pcf8574_R2.digitalWrite(P2,!pcf8574_R2.digitalRead(P2));    // toggle relay11 state     
      while(pcf8574_I2.digitalRead(P2) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P2) == LOW) relay11state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P3) == LOW)         //key12 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P3) == LOW)       //key12 pressed again
    {
      pcf8574_R2.digitalWrite(P3,!pcf8574_R2.digitalRead(P3));    // toggle relay12 state     
      while(pcf8574_I2.digitalRead(P3) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P3) == LOW) relay12state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P4) == LOW)         //key13 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P4) == LOW)       //key13 pressed again
    {
      pcf8574_R2.digitalWrite(P4,!pcf8574_R2.digitalRead(P4));    // toggle relay13 state     
      while(pcf8574_I2.digitalRead(P4) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P4) == LOW) relay13state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P5) == LOW)         //key14 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P5) == LOW)       //key14 pressed again
    {
      pcf8574_R2.digitalWrite(P5,!pcf8574_R2.digitalRead(P5));    // toggle relay14 state     
      while(pcf8574_I2.digitalRead(P5) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P5) == LOW) relay14state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P6) == LOW)         //key15 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P6) == LOW)       //key15 pressed again
    {
      pcf8574_R2.digitalWrite(P6,!pcf8574_R2.digitalRead(P6));    // toggle relay15 state     
      while(pcf8574_I2.digitalRead(P6) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P6) == LOW) relay15state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I2.digitalRead(P7) == LOW)         //key16 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I2.digitalRead(P7) == LOW)       //key16 pressed again
    {
      pcf8574_R2.digitalWrite(P7,!pcf8574_R2.digitalRead(P7));    // toggle relay16 state     
      while(pcf8574_I2.digitalRead(P7) == LOW); //wait remove hand
      if (pcf8574_R2.digitalRead(P7) == LOW) relay16state= "on"; else relay1state= "off";
    }
  }

/*
  if(pcf8574_I3.digitalRead(P0) == LOW)         //key17 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P0) == LOW)       //key17 pressed again
    {
      pcf8574_R3.digitalWrite(P0,!pcf8574_R3.digitalRead(P0));    // toggle relay17 state     
      while(pcf8574_I3.digitalRead(P0) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P0) == LOW) relay17state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P1) == LOW)         //key18 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P1) == LOW)       //key18 pressed again
    {
      pcf8574_R3.digitalWrite(P1,!pcf8574_R3.digitalRead(P1));    // toggle relay18 state     
      while(pcf8574_I3.digitalRead(P1) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P1) == LOW) relay18state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P2) == LOW)         //key19 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P2) == LOW)       //key19 pressed again
    {
      pcf8574_R3.digitalWrite(P2,!pcf8574_R3.digitalRead(P2));    // toggle relay19 state     
      while(pcf8574_I3.digitalRead(P2) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P2) == LOW) relay19state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P3) == LOW)         //key20 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P3) == LOW)       //key20 pressed again
    {
      pcf8574_R3.digitalWrite(P3,!pcf8574_R3.digitalRead(P3));    // toggle relay20 state     
      while(pcf8574_I3.digitalRead(P3) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P3) == LOW) relay20state= "on"; else relay1state= "off";
    }
  }

  if(pcf8574_I3.digitalRead(P4) == LOW)         //key21 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P4) == LOW)       //key21 pressed again
    {
      pcf8574_R3.digitalWrite(P4,!pcf8574_R3.digitalRead(P4));    // toggle relay21 state     
      while(pcf8574_I3.digitalRead(P4) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P4) == LOW) relay21state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P5) == LOW)         //key22 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P5) == LOW)       //key22 pressed again
    {
      pcf8574_R3.digitalWrite(P5,!pcf8574_R3.digitalRead(P5));    // toggle relay22 state     
      while(pcf8574_I3.digitalRead(P5) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P5) == LOW) relay22state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P6) == LOW)         //key23 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P6) == LOW)       //key23 pressed again
    {
      pcf8574_R3.digitalWrite(P6,!pcf8574_R3.digitalRead(P6));    // toggle relay23 state     
      while(pcf8574_I3.digitalRead(P6) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P6) == LOW) relay23state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I3.digitalRead(P7) == LOW)         //key24 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I3.digitalRead(P7) == LOW)       //key24 pressed again
    {
      pcf8574_R3.digitalWrite(P7,!pcf8574_R3.digitalRead(P7));    // toggle relay24 state     
      while(pcf8574_I3.digitalRead(P7) == LOW); //wait remove hand
      if (pcf8574_R3.digitalRead(P7) == LOW) relay24state= "on"; else relay1state= "off";
    }
  }

  if(pcf8574_I4.digitalRead(P0) == LOW)         //key25 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P0) == LOW)       //key25 pressed again
    {
      pcf8574_R4.digitalWrite(P0,!pcf8574_R4.digitalRead(P0));    // toggle relay25 state     
      while(pcf8574_I4.digitalRead(P0) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P0) == LOW) relay25state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P1) == LOW)         //key26 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P1) == LOW)       //key26 pressed again
    {
      pcf8574_R4.digitalWrite(P1,!pcf8574_R4.digitalRead(P1));    // toggle relay26 state     
      while(pcf8574_I4.digitalRead(P1) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P1) == LOW) relay26state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P2) == LOW)         //key27 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P2) == LOW)       //key27 pressed again
    {
      pcf8574_R4.digitalWrite(P2,!pcf8574_R4.digitalRead(P2));    // toggle relay27 state     
      while(pcf8574_I4.digitalRead(P2) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P2) == LOW) relay27state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P3) == LOW)         //key28 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P3) == LOW)       //key28 pressed again
    {
      pcf8574_R4.digitalWrite(P3,!pcf8574_R4.digitalRead(P3));    // toggle relay28 state     
      while(pcf8574_I4.digitalRead(P3) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P3) == LOW) relay28state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P4) == LOW)         //key29 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P4) == LOW)       //key29 pressed again
    {
      pcf8574_R4.digitalWrite(P4,!pcf8574_R4.digitalRead(P4));    // toggle relay29 state     
      while(pcf8574_I4.digitalRead(P4) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P4) == LOW) relay29state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P5) == LOW)         //key30 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P5) == LOW)       //key30 pressed again
    {
      pcf8574_R4.digitalWrite(P5,!pcf8574_R4.digitalRead(P5));    // toggle relay30 state     
      while(pcf8574_I4.digitalRead(P5) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P5) == LOW) relay30state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P6) == LOW)         //key31 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P6) == LOW)       //key31 pressed again
    {
      pcf8574_R4.digitalWrite(P6,!pcf8574_R4.digitalRead(P6));    // toggle relay31 state     
      while(pcf8574_I4.digitalRead(P6) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P6) == LOW) relay31state= "on"; else relay1state= "off";
    }
  }


  if(pcf8574_I4.digitalRead(P7) == LOW)         //key32 pressed
  {
    delay(20);                        //anti-interference
    if(pcf8574_I4.digitalRead(P7) == LOW)       //key32 pressed again
    {
      pcf8574_R4.digitalWrite(P7,!pcf8574_R4.digitalRead(P7));    // toggle relay32 state     
      while(pcf8574_I4.digitalRead(P7) == LOW); //wait remove hand
      if (pcf8574_R4.digitalRead(P7) == LOW) relay32state= "on"; else relay1state= "off";
    }
  }
*/

}

Print this item

  [Arduino IDE demo source code for KC868-A8S]--#16-KC868-A8S_Ethernet_Web_server_code
Posted by: KinCony Support - 09-05-2022, 03:30 AM - Forum: KC868-A8S - No Replies

[Arduino IDE demo source code for KC868-A8S]--#16-KC868-A8S_Ethernet_Web_server_code

                                             Refer to code #11 of KC868-A8 for download methods

.zip   KC868-A8S_Web_Server-ETH.zip (Size: 471.57 KB / Downloads: 304)

Code:
/***********************************************************
* Create Your own WebServer for KC868-A8S Smart Controller*
* https://www.kincony.com                                **
***********************************************************/

#include "PCF8574.h"
#define DEBUG_ETHERNET_WEBSERVER_PORT       Serial

// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_       3

#include <WebServer_WT32_ETH01.h>

WiFiServer server(80);

// Current time
unsigned long currentTime = millis();
// Previous time
unsigned long previousTime = 0;
// Define timeout time in milliseconds (example: 2000ms = 2s)
const long timeoutTime = 2000;

int flag_read_di=0;

// Variable to store the HTTP request
String header;
TwoWire I2Cone = TwoWire(0);
TwoWire I2Ctwo = TwoWire(1);
//set I2C of A8S
PCF8574 pcf8574_I1(&I2Cone,0x22,4,5);
PCF8574 pcf8574_R1(&I2Cone,0x24,4,5);


String relaystate[8]={"relay1state","relay2state","relay3state","relay4state",
                       "relay5state","relay6state","relay7state","relay8state",
                     };
String relay1state = "off";// state of relay1
String relay2state = "off";// state of relay2
String relay3state = "off";// state of relay3
String relay4state = "off";// state of relay4
String relay5state = "off";// state of relay5
String relay6state = "off";// state of relay6
String relay7state = "off";// state of relay7
String relay8state = "off";// state of relay8
void ScanKey()           
{
  for(int i=0;i<=7;i++)
  {
    if(pcf8574_I1.digitalRead(i) == LOW)         //key pressed
      {
        delay(20);                       
        if(pcf8574_I1.digitalRead(i) == LOW)       //key pressed again
        {
          pcf8574_R1.digitalWrite(i,!pcf8574_R1.digitalRead(i));    // toggle relay1 state    
          while(pcf8574_I1.digitalRead(i) == LOW); //wait remove hand
          if (pcf8574_R1.digitalRead(i) == LOW) relaystate[i+1]="on";else relaystate[i+1]="off";
        }
      }
  }

}

void setup() {

  Serial.begin(115200);

  // Set PCF8574 pinMode to OUTPUT
  for(int i=0;i<=7;i++)
  {
     pcf8574_R1.pinMode(i, OUTPUT);
  }
 
  pcf8574_R1.begin();
 
  for(int i=0;i<=7;i++)
  {
     pcf8574_I1.pinMode(i, INPUT);
  }

    pcf8574_I1.begin();
     

  for(int i=0;i<=7;i++)
  {
     pcf8574_R1.digitalWrite(i, HIGH);
  }
   while (!Serial);

  // Using this if Serial debugging is not necessary or not using Serial port
  //while (!Serial && (millis() < 3000));

  Serial.print("\nStarting WebServer on " + String(ARDUINO_BOARD));
  Serial.println(" with " + String(SHIELD_TYPE));
  Serial.println(WEBSERVER_WT32_ETH01_VERSION);

  // To be called before ETH.begin()
  WT32_ETH01_onEvent();

  //bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
  //           eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
  //ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
  ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);

  // Static IP, leave without this line to get IP via DHCP
  //bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
// ETH.config(myIP, myGW, mySN, myDNS);

  //WT32_ETH01_waitForConnect();

  // start the web server on port 80
  server.begin();

}

void loop(){
  WiFiClient client = server.available();   // Listen for incoming clients

  if (client) {                             // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    currentTime = millis();
    previousTime = currentTime;
    while (client.connected() && currentTime - previousTime <= timeoutTime) { // loop while the client's connected
      currentTime = millis();        
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        header += c;
        if (c == '\n') {                    // if the byte is a newline character
          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println("Connection: close");
            client.println();
           
            // turns 16 relays on and off
            if (header.indexOf("GET /1/on") >= 0) {relay1state = "on";pcf8574_R1.digitalWrite(P0, LOW);}
            else if (header.indexOf("GET /1/off") >= 0) {relay1state = "off";pcf8574_R1.digitalWrite(P0, HIGH);}
            else if (header.indexOf("GET /2/on") >= 0)  {relay2state = "on";pcf8574_R1.digitalWrite(P1, LOW);}
            else if (header.indexOf("GET /2/off") >= 0) {relay2state = "off";pcf8574_R1.digitalWrite(P1, HIGH);}
            else if (header.indexOf("GET /3/on") >= 0)  {relay3state = "on";pcf8574_R1.digitalWrite(P2, LOW);}
            else if (header.indexOf("GET /3/off") >= 0) {relay3state = "off";pcf8574_R1.digitalWrite(P2, HIGH);}        
            else if (header.indexOf("GET /4/on") >= 0)  {relay4state = "on";pcf8574_R1.digitalWrite(P3, LOW);}
            else if (header.indexOf("GET /4/off") >= 0) {relay4state = "off";pcf8574_R1.digitalWrite(P3, HIGH);}
            else if (header.indexOf("GET /5/on") >= 0)  {relay5state = "on";pcf8574_R1.digitalWrite(P4, LOW);}
            else if (header.indexOf("GET /5/off") >= 0) {relay5state = "off";pcf8574_R1.digitalWrite(P4, HIGH);}
            else if (header.indexOf("GET /6/on") >= 0)  {relay6state = "on";pcf8574_R1.digitalWrite(P5, LOW);}
            else if (header.indexOf("GET /6/off") >= 0) {relay6state = "off";pcf8574_R1.digitalWrite(P5, HIGH);}         
            else if (header.indexOf("GET /7/on") >= 0)  {relay7state = "on";pcf8574_R1.digitalWrite(P6, LOW);}
            else if (header.indexOf("GET /7/off") >= 0) {relay7state = "off";pcf8574_R1.digitalWrite(P6, HIGH);}
            else if (header.indexOf("GET /8/on") >= 0)  {relay8state = "on";pcf8574_R1.digitalWrite(P7, LOW);}
            else if (header.indexOf("GET /8/off") >= 0) {relay8state = "off";pcf8574_R1.digitalWrite(P7, HIGH);}
                 
            else if (header.indexOf("GET /read") >= 0) flag_read_di=1;
            else if (header.indexOf("GET /all/on") >= 0)
            {
              relay1state = "on";// state of relay1
              relay2state = "on";// state of relay2
              relay3state = "on";// state of relay3
              relay4state = "on";// state of relay4
              relay5state = "on";// state of relay5
              relay6state = "on";// state of relay6
              relay7state = "on";// state of relay7
              relay8state = "on";// state of relay8

              for(int i=0;i<=7;i++)
              {
                 pcf8574_R1.digitalWrite(i, LOW);
              }
             
            }
            else if (header.indexOf("GET /all/off") >= 0)
            {
              relay1state = "off";// state of relay1
              relay2state = "off";// state of relay2
              relay3state = "off";// state of relay3
              relay4state = "off";// state of relay4
              relay5state = "off";// state of relay5
              relay6state = "off";// state of relay6
              relay7state = "off";// state of relay7
              relay8state = "off";// state of relay8

              for(int i=0;i<=7;i++)
              {
                 pcf8574_R1.digitalWrite(i, HIGH);
              }
              
            }
           
            // Display the HTML web page
            client.println("<!DOCTYPE html><html>");
            client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
            client.println("<link rel=\"icon\" href=\"data:,\">");
            // CSS to style the on/off buttons
            // Feel free to change the background-color and font-size attributes to fit your preferences
            client.println("<style>html { font-family: Helvetica; display: inline-block; border: 3px solid green;margin: 0px auto;text-align: center;font-size: 10px;}");
            client.println(".button { background-color: #195B00; border-radius: 10px;color: white; padding: 10px 40px;");
            client.println("text-decoration: none; font-size: 20px; margin: 2px;text-align: center; cursor: pointer;}");
            client.println(".button2 {background-color: #77878A;}</style></head>");
           
            if (flag_read_di==1)
            {
              client.println(" relay1:" + relay1state + " relay2:" + relay2state + " relay3:" + relay3state + " relay4:" + relay4state + " relay5:" + relay5state + " relay6:" + relay6state + " relay7:" + relay7state + " relay8:" + relay8state);
            }
            else
            {
            // Web Page Heading
            client.println("<body><h1>KC868-A8S Web Server</h1>");


            // Display current state, and ON/OFF buttons for relay
            //client.println("<p>relay1 - State " + relay1state + "</p>");
            // If the relay is off, it displays the ON button     
            if (relay1state == "off") client.println("<a href=\"/1/on\"><button class=\"button\">01-OFF</button></a>"); else client.println("<a href=\"/1/off\"><button class=\"button\">01--ON </button></a>");
            if (relay2state == "off") client.println("<a href=\"/2/on\"><button class=\"button\">02-OFF</button></a>"); else client.println("<a href=\"/2/off\"><button class=\"button\">02--ON </button></a>");                                
            if (relay3state == "off") client.println("<a href=\"/3/on\"><button class=\"button\">03-OFF</button></a>"); else client.println("<a href=\"/3/off\"><button class=\"button\">03--ON </button></a>");
            if (relay4state == "off") client.println("<a href=\"/4/on\"><button class=\"button\">04-OFF</button></a>"); else client.println("<a href=\"/4/off\"><button class=\"button\">04--ON </button></a>");
            if (relay5state == "off") client.println("<a href=\"/5/on\"><button class=\"button\">05-OFF</button></a>"); else client.println("<a href=\"/5/off\"><button class=\"button\">05--ON </button></a>");
            if (relay6state == "off") client.println("<a href=\"/6/on\"><button class=\"button\">06-OFF</button></a>"); else client.println("<a href=\"/6/off\"><button class=\"button\">06--ON </button></a>");                                
            if (relay7state == "off") client.println("<a href=\"/7/on\"><button class=\"button\">07-OFF</button></a>"); else client.println("<a href=\"/7/off\"><button class=\"button\">07--ON </button></a>");
            if (relay8state == "off") client.println("<a href=\"/8/on\"><button class=\"button\">08-OFF</button></a>"); else client.println("<a href=\"/8/off\"><button class=\"button\">08--ON </button></a>");
           
              client.println("<a href=\"/all/on\"><button class=\"button\">All--ON</button></a>");
              client.println("<a href=\"/all/off\"><button class=\"button\">All-OFF</button></a>");
            } 
            client.println("</body></html>");
           
            // The HTTP response ends with another blank line
            client.println();
            break;
          } else { // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
      }
    }
    // Clear the header variable
    header = "";
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
    Serial.println("");
    flag_read_di=0;
  }

  ScanKey();            //scan key
}

Print this item