Hi all,
I am having some issue with the bellow code, I try to combine ethernet client and ethernet server at the same time.
I am using arduino mega + ethernet shild
The code should do the following
- Read the status of 29 pins
Ethernet client should - When status change should perform a client.print("get /Stare…
Ethernet server should display the status of each pin.
The problem is that after couple of status change it frozen…. Ping is working in the ip but no page is loading and no get message sent.
Can you please have a look and help me identify where the problem is, any improvement is welcome.
Note. I had to delete the repeating code with different pin because I was exceeding 9000 characters.
Thank you and much appreciate your help.
#include <Ethernet.h>
byte mac[] = {
0x90, 0xAA, 0xDA, 0x26, 0x26, 0x36
}; //MAC address found on the back of your ethernet shield.
IPAddress ip(192, 168, 2, 50); // IP address dependent upon your network addresses.
EthernetClient client;
EthernetServer server(80);
String readString = String(30); //string
const int PLCSP1 = 22;
const int PLCSP2 = 23;
const int PLBP = 24;
const int PLHP1 = 25;
const int PLHP2 = 26;
const int PLCLP1 = 27;
const int PLCLP2 = 28;
const int PLCLP3 = 29;
const int PLCSE1 = 30;
const int PLCSE2 = 31;
const int PLBE = 32;
const int PLHE1 = 33;
const int PLHE2 = 34;
const int PLCME1 = 35;
const int PLCME2 = 36;
const int PLCGE1 = 37;
const int PLCGE2 = 38;
const int PLB1 = 39;
const int PLB2 = 40;
const int PLCC = 41;
const int PLCP = 6;
const int PLCSP = 43;
const int PLC = 44;
const int PLUP = 45;
const int PLT = 46;
const int PLBSE = 47;
const int PLBGE = 48;
const int PLDP = 49;
const int PLTP = 5;
int LCSP1;
//rest of 37 pin
int LTP;
int LASTLCSP1;
//rest of 27 pins
int LASTLTP;
void setup() {
Serial.begin(9600);
// put your setup code here, to run once:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
LASTLCSP1=digitalRead(PLCSP1);
//rest of 27 pins
LASTLTP=digitalRead(PLTP);
}
void loop() {
LCSP1=digitalRead(PLCSP1);
//rest of 37 pins
LTP=digitalRead(PLTP);
if (LCSP1!=LASTLCSP1)
{
EthernetClient client;
if (client.connect("192.168.1.25", 80))
{
client.print("get /Status/LCSP1.php?LCSP1=");
client.print(LCSP1);
client.println();
}
LASTLCSP1=LCSP1;
}
//rest of 27
if (LTP!=LASTLTP)
{
EthernetClient client;
if (client.connect("192.168.1.25", 80))
{
client.print("get /Status/LTP.php?LTP=");
client.print(LTP);
client.println();
}
LASTLTP=LTP;
}
// Create a client connection
EthernetClient client = server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 50)
{
//store characters to string
readString += c; //replaces readString.append(c);
}
//output chars to serial port
Serial.print(c);
//if HTTP request has ended
if (c == '\n') {
// now output HTML data starting with standart header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("<HEAD>");
client.print("<meta http-equiv=\"refresh\" content=\"1\">");
client.print("<TITLE />Lumina</title>");
client.print("</head>");
client.print("<table>");
client.print("<tr>");
client.print("<td>");
if (digitalRead(22)) client.print("Lumina Camera Parter Strada 1 este <font color='red'>Pornita</font>");
else client.print("Lumina Camera Parter Strada 1 este <font color='green'>Inchisa</font>");
client.print("</td>");
client.print("</tr>");
client.print("<td>");
if (digitalRead(5)) client.print("Lumina Tablou este <font color='red'>Pornita</font>");
else client.print("Lumina Tablou este <font color='green'>Inchisa</font>");
client.print("</td>");
client.print("</tr>");
client.print("</table>");
client.println("</html>");
//clearing string for next read
readString="";
//stopping client
delay(1);
client.stop();
}
}
}
}
}