Buongiorno a tutti
Ho realizzato un apricancello collegando un leonardo eth ad un telecomando a 433Mhz (ho fattto questo perche’ il telecomando in questione e’ un rolling code , e non ho trovato soluzione piu rapida).
Il tuttto funziona tramite una pagine web.
Il tutto funziona perfettamente senza intoppi per 3-4 giorni. Dopo circa quattro giorni quando mi connetto alla pagina web , questa non viene caricata (premetto che viene caricada da una sd), e restituisce una pagina vuota.
Semplicemente spegno e riaccendo l’arduino e tutto riprende a funzionare regolarmente.
Qualche idea sul perche faccia cosi?
questo il codice arduino
#include <SPI.h>
#include <Ethernet2.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xB8, 0x27, 0xEB, 0x0B, 0xE6, 0x06 };
IPAddress ip(192, 168, 0, 168); // IP address, may need to change depending on network
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
EthernetServer server(80); // create a server at port 80
File webFile;
String HTTP_req; // stores the HTTP request
void setup()
{
Serial.begin(9600); // for diagnostics
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm file.");
pinMode(7, OUTPUT);// switch is attached to Arduino pin 3
digitalWrite(7, LOW);
pinMode(3, INPUT);
Ethernet.begin(mac, ip, gateway, subnet); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.print("Server is at");
Serial.print(Ethernet.localIP());
pinMode(7, OUTPUT); // switch is attached to Arduino pin 3
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client) { // got client?
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) { // client data available to read
char c = client.read(); // read 1 byte (character) from client
HTTP_req += c; // save the HTTP request 1 char at a time
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// AJAX request for switch state
if (HTTP_req.indexOf("opengate") > -1) {
digitalWrite(7, HIGH);
delay(475);
int readopened = digitalRead(3);
delay(375);
digitalWrite(7, LOW);
if(readopened = HIGH){
client.println("opened");
}
if(readopened = LOW){
client.println("notopened");
}
}
else {
// web page request
// send web page
webFile = SD.open("index.htm"); // open web page file
if (webFile) {
while(webFile.available()) {
client.write(webFile.read()); // send web page to client
}
webFile.close();
}
}
// display received HTTP request on serial port
Serial.print(HTTP_req);
HTTP_req = ""; // finished with request, empty string
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
questa la pina web
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<link rel="icon" href="/favicon.ico" type="image/ico">
<style>
body {
text-align:center;
vertical-align:middle;
height: 100%;
}
div {
width:100%;
height:100%;
position:absolute;
top:40%;
margin: 0 0 0 0;
text-align:center;
vertical-align:middle;
}
button {
width: 4em;
height: 1.5em;
font-size:4em;
}
#opened{
font-size:2em;
color: #32CD32;
}
#notopened{
font-size:3em;
color: #FF0000;
}
#error{
font-size:5em;
color: #FF0000;
font-weight: bold;
}
</style>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="generator" content="PSPad editor, www.pspad.com">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script>
function Openthegate() {
nocache = "&nocache="+ Math.random() * 1000000;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (this.readyState == 4) {
if (this.status == 200) {
if (this.responseText = "opened") {
document.getElementById("opened").innerHTML = "Opened";
}
else if (this.responseText = "notopened") {
document.getElementById("notopened").innerHTML = "Something wrong, check the gate!!";
}
else{
document.getElementById("error").innerHTML = "ERROR, CALL CRISTIAN"; 11
}}}}
request.open("POST", "opengate" + nocache, true);
request.send(null);
}
</script>
<title></title>
</head>
<body>
<div >
<button type="button" onclick="Openthegate()">Apri</button>
<p id="opened"></p>
<p id="notopened"></p>
<p id="error"></p>
</div>
</body>
</html>
Grazie a tutti per l’aiuto