Hi, I am new to arduino and I am trying to switch on an LED from an HTML page, then the arduino checks the URL and switch LED on or off.
Till here everything is fine.
What I cannot do is when the LED switches on, send a GET to PHP which saves in the database.
If I try the URL from the browser it works, so the problem is not from the PHP side, the problem is from the arduino code.
I dont know what to use exactly 2 clients or 1 client for everything ?
Arduino Code
#include <SPI.h>
#include <Ethernet.h>
int LED = 9;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(80); //server port
String readString;
boolean lastLEDState = false;
EthernetClient client;
void setup(){
pinMode(LED, OUTPUT); //pin selected to control
//start Ethernet
Ethernet.begin(mac );
server.begin();
//enable serial data print
Serial.begin(9600);
Serial.println("server LED test 1.0");
}
void html()
{
client= server.available();
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read char by char HTTP request
if (readString.length() < 100) {
//store characters to string
readString += c;
//Serial.print(c);
}
//if HTTP request has ended
if (c == '\n') {
///////////////
Serial.println(readString); //print to serial monitor for debuging
client.println("HTTP/1.1 200 OK"); //send new page
client.println("Content-Type: text/html");
client.println();
client.println("<HTML>");
client.println("<HEAD>");
client.println("<meta name='apple-mobile-web-app-capable' content='yes' />");
client.println("<meta name='apple-mobile-web-app-status-bar-style' content='black-translucent' />");
client.println("<link rel='stylesheet' type='text/css' href='http://homeautocss.net84.net/a.css' />");
client.println("<TITLE>Home Automation</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>Home Automation</H1>");
client.println("<hr />");
client.println("
");
if(lastLEDState != false)
{
client.println("<h3 style='color:green'>LED is ON</h3>");
}
else
{
client.println("<h3 style='color:red'>LED is OFF</h3>");
}
client.println("
");
client.println("<a href=\"/?lighton\"\">Turn On Light</a>");
client.println("<a href=\"/?lightoff\"\">Turn Off Light</a>
");
client.println("
");
client.println("<h3>Buzzer</h3>");
client.println("<a href=\"/?buzon\"\">Beep</a>");
client.println("</BODY>");
client.println("</HTML>");
delay(1);
//stopping client
}
}
}
}
}
char serverr[] = "192.168.1.3";
void loop(){
// Create a client connection
EthernetClient client2;
if (client2.connect(serverr, 80)) {
html();
///////////////////// control arduino pin
if(readString.indexOf("?lighton") >0)//checks for on
{
digitalWrite(LED, HIGH); // set pin 4 high
Serial.println("Led On");
Serial.println(lastLEDState);
client2.print( "GET /arduinotest/add_data.php?state=1");
lastLEDState = true;
}
else if(readString.indexOf("?lightoff") >0)//checks for off
{
digitalWrite(LED, LOW); // set pin 4 low
Serial.println("Led Off");
Serial.println(lastLEDState);
lastLEDState = false;
}
else if(readString.indexOf("?buzon") >0)//checks for off
{
analogWrite(Buzzer, 20);
delay(100);
analogWrite(Buzzer, 0); // set pin 4 low
Serial.println("Beep");
}
//clearing string for next read
html();
readString="";
client2.stop();
}
}