09-08-2022, 02:15 AM
(This post was last modified: 09-08-2022, 02:29 AM by KinCony Support.)
[Arduino IDE demo source code for KC868-AI]--#07-AI_Digital input_Ethernet_Web_server_code
Arduino source code and bin file
KC868-AI_Web_Server-ETH.zip (Size: 470.08 KB / Downloads: 336)
PC and Phone webpage display
Arduino source code and bin file
KC868-AI_Web_Server-ETH.zip (Size: 470.08 KB / Downloads: 336)
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("");
}
}