ciao a tutti,
ho sviluppato un software che carica una pagina web salvata su SD la quale comanda in GET delle uscite.
ma succede qualcosa di strano, se apro la pagina e poi magari faccio un refresh dal browser si blocca tutto, quindi devo spegnere tutto e al riavvio riesco ad aprire la pagina.
in più spesso capita che i comandi passati tramite URL, arduino non riesca a leggerli. e da quel momento non funzia più niente.
uso un arduino uno con ethernet shield w5500.
questo è il codice :
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
unsigned long previousMillis = 0;
int out1 = 6;
int out2 = 7;
int out3 = 8;
int out4 = 9;
int mode=0;
int time = 0;
int cicli;
int ledStateOut1 = LOW;
int ledStateOut2 = LOW;
int ledStateOut3 = LOW;
int ledStateOut4 = LOW;
int state;
#define maxLength 25
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 130 };
byte gateway[] = { 192, 168, 0, 1 };
byte subnet[] = { 255, 255, 255, 0 };
File htmlFile;
EthernetServer server(80);
void (*resetBoard)() = 0;
void setup() {
Serial.println("SETUP");
Ethernet.begin(mac, ip);
server.begin();
if (!SD.begin(4)) { return; }
Serial.begin(9600);
pinMode(out1, OUTPUT);
pinMode(out2, OUTPUT);
pinMode(out3, OUTPUT);
pinMode(out4, OUTPUT);
}
void loop() {
//Serial.println("LOOP");
//PAGINA WEB
char* file_to_load = "home.htm";
String inString = String(maxLength);
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
String line = String();
while (client.connected()) {
if (client.available()) {
char c = client.read();
line.concat(c);
if (inString.length() < maxLength) {
inString += c;
}
if (c == '\n' && currentLineIsBlank) {
//Serial.print("sono qui");
/* if (inString.indexOf(".htm") > -1) {
String new_file_load;
int rootIndex = inString.indexOf("/");
new_file_load = inString.substring((rootIndex+1), (rootIndex+13));
int endIndex = new_file_load.indexOf(" ");
if (endIndex > -1) {
new_file_load = new_file_load.substring(0, endIndex);
}
if (new_file_load != "") {
new_file_load.toCharArray(file_to_load,12);
}
}*/
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
//read_file( "header.htm",client );
//read_file( file_to_load,client );
//read_file( "footer.htm",client );
htmlFile = SD.open( "home.htm" );
if (htmlFile) {
while (htmlFile.available()) {
client.write(htmlFile.read());
}
// close the file:
htmlFile.close();
}
break;
}
if (c == '\n') {
currentLineIsBlank = true;
Serial.print(line);
if (line.indexOf("hz_2")>0) {
state = 1;
//cicli=0;
}
if (line.indexOf("hz_3")>0) {
state = 2;
//cicli=0;
}
if (line.indexOf("hz_4") >0 ) {
state = 3;
//cicli=0;
}
if (line.indexOf("modo_1") >0 ) {
mode = 1;
//cicli=0;
}
if (line.indexOf("modo_2")>0 ) {
mode = 2;
//cicli=0;
}
if (line.indexOf("hz_0")>0 ) {
state = 0;
mode = 0;
}
if (line.indexOf("modo_0")>0 ) {
mode = 0;
state = 0;
}
if (line.indexOf("finish")>0 ) {
state = 0;
mode = 0;
}
line = String();
}
else if (c != '\r') {
currentLineIsBlank = false;
}
}
}
delay(1);
client.stop();
}
//char state = Serial.read();
unsigned long currentMillis = millis();
//SETTAGGIO FREQUENZA
//if (Serial.available() > 0) {
if (state==1){
cicli=0;
time=250;
//mode=1;
}else{
if (state==2){
cicli=0;
time=187;
//mode=2;
}else{
if (state==3){
cicli=0;
time=125;
//mode=1;
}else{
if (state==0){
cicli=0;
time=0;
//mode=0;
}
}
}
}
//}
const long interval=time;
//TEMPORIZZATORE
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
if(mode==1){
if (((ledStateOut1 == LOW)&&(ledStateOut3 == LOW))&&((ledStateOut2 == HIGH)&&(ledStateOut4 == HIGH))) {
ledStateOut1 = HIGH;
ledStateOut2 = LOW;
ledStateOut3 = HIGH;
ledStateOut4 = LOW;
}else{
ledStateOut1 = LOW;
ledStateOut2 = HIGH;
ledStateOut3 = LOW;
ledStateOut4 = HIGH;
}
cicli++;
}
if(mode==2){
if (((ledStateOut1 == LOW)&&(ledStateOut3 == HIGH))&&((ledStateOut2 == HIGH)&&(ledStateOut4 == LOW))) {
ledStateOut1 = HIGH;
ledStateOut2 = LOW;
ledStateOut3 = LOW;
ledStateOut4 = HIGH;
}else{
ledStateOut1 = LOW;
ledStateOut2 = HIGH;
ledStateOut3 = HIGH;
ledStateOut4 = LOW;
}
cicli++;
}
if(mode==0){
ledStateOut1 = LOW;
ledStateOut2 = LOW;
ledStateOut3 = LOW;
ledStateOut4 = LOW;
}
}
digitalWrite(out1, ledStateOut1);
digitalWrite(out2, ledStateOut2);
digitalWrite(out3, ledStateOut3);
digitalWrite(out4, ledStateOut4);
// Serial.print(ledStateOut1);
// Serial.print(ledStateOut2);
//Serial.println(cicli);
//Serial.println(state);
//Serial.println(mode);
}
//CARICA FILE DA SD CARD
// void read_file( char* page_html, EthernetClient client ){
//}