Bonjour,
Me revoilà !
J'ai réussi à connecter mon arduino Méga avec le shield Ethernet pour allumer ou éteindre ma led à partir de la page web chargée dans le code ci-dessous !
La livebox5 ne m'affichait pas toujours mon module nommé "PC-59". Mais cela a fonctionné un moment, jusqu'à ce que je ne puisse plus avoir accès à la page.
Messages d'erreurs "ERR_CONNECTION_TIMED_OUT" ou "unreachable".
Où sont mes erreurs ? Que faudrait-il faire ? Rediriger vers l'ip fixe au niveau de la box ? Problème de pare-feu ???
Merci pour votre aide.
Voici le sketch :
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html
//address will look like http://192.168.1.11:80/ when submited
//for use with W5100 based ethernet shields
//
#include <SPI.h>
#include <Ethernet.h>
boolean etatLed=0;
boolean cliON=false;
unsigned long tpsDep;
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x1A, 0x71}; //physical mac address
byte ip[] = { 192, 168, 1, 17 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(80); //server port
String readString;
String oldReadString="";
boolean drapeauOnClick=false;
//******************************************************************************
void gestionClient()
{
// Create a client connection
EthernetClient client = server.available();
if (client)
{
Serial.println("Client en ligne !");
if (client.connected())
{
while (client.available())
{
char c = client.read();
delay(100);
//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
//now output HTML data header
if(readString.indexOf('?') >=0) //don't send new page
{
client.println("HTTP/1.1 204 JPC");
client.println();
client.println();
}
else
{
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("<TITLE>Arduino GET test page</TITLE>");
client.println("</HEAD>");
client.println("<BODY>");
client.println("<H1>JP's simple Arduino button<style>h1{color: green; text-align:center;text-decoration:underline;}</style></H1><hr>");
client.println("<p> Cliquer sur \"ON\" pour allumer la LED</p>");
client.println("<a href=\"/?on\" target=\"inlineframe\">ON</a><br />");
client.println("<p> Cliquer sur \"OFF\" pour eteindre la LED<style>color:blue; height:30px;</style></p>");
client.println("<a href=\"/?off\" target=\"inlineframe\">OFF</a>");
client.println("<p> Cliquer sur \"CLI\" pour faire clignotter la LED</p>");
client.println("<a href=\"/?cli\" target=\"inlineframe\">CLI</a>");
//client.println("<IFRAME name=inlineframe src=\"res://D:/WINDOWS/dnserror.htm\" width=1 height=1\">");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("</BODY>");
client.println("</HTML>");
}
delay(1);
//stopping client
client.stop();
Serial.println("Communication avec le Cient interrompue !");
Serial.println(oldReadString);
if(oldReadString != readString)
{
drapeauOnClick=true;
oldReadString = readString;
}
else
{
drapeauOnClick=false;
}
}
}
}
}
}
void gestionLed()
{
///////////////////// control arduino pin//////////////////////////////
if(drapeauOnClick) // si on aclique sur un autre lien
{
unsigned long tpsAct=millis();
// Serial.print("Duree = ");
// Serial.print(tpsAct-tpsDep);
// Serial.println("ms");
if(readString.indexOf("on") >0)//checks for on
{
etatLed=1;
cliON=false;
Serial.println("Led On");
}
else if(readString.indexOf("off") >0) //checks for off
{
etatLed=0;
cliON=false;
Serial.println("Led Off");
}
else if(readString.indexOf("cli") >0) //checks for CLI
{
cliON=true;
Serial.println("Led CLI");
}
if (cliON && (tpsAct - tpsDep > 250))
{
etatLed = !etatLed;
tpsDep=tpsAct; // on initialise le delai de temps de depart
}
digitalWrite(3, etatLed); // set pin 3 low
Serial.print("Le drapeau est a : ");
Serial.println(cliON);
//clearing string for next read
readString="";
}
}
//**********************************************************************************
void setup(){
pinMode(3, OUTPUT); //LED branchée sur pin 13
digitalWrite(3, LOW);
//start Ethernet
//////Ethernet.begin(mac, ip, gateway, subnet);
Ethernet.begin(mac, ip);
server.begin();
Serial.print ("*\n -> Le serveur est sur l'adresse : ");
Serial.println(Ethernet.localIP());
//enable serial data print
Serial.begin(9600);
Serial.println("servertest1"); // so I can keep track of what is loaded
}
void loop()
{
gestionClient();
gestionLed();
}