Skocz do zawartości

Niestabilny WebSerwer na Arduino


mglowinski

Pomocna odpowiedź

Witam,

chciałem zrobić sterowanie przekaźnikami oraz odczyt z kilku czujników przez internet.

Użyłem do tego Arduino Leonardo oraz rutera Huawei B683.

Problem polega na tym, że serwer jest niestabilny, czasem się wyłączony, czasem przytnie itp.

Z czego może wynikać problem? Użyłem składni przestarzałego HTML-a?

Dodam, że WebSerwer z przykładu Arduino działa bez zarzutu.

Mam jeszcze problem ze sterowaniem z przekaźnikiem.

Mianowicie gdy przerzucam z Off na On wszystko jest ok. Problem pojawia się, gdy przełączam z On na Off. Pierwszy raz gdy klikam On na Off nic się nie dzieje, dopiero za drugim razie przeskakuje na Off. Gdzie może być przyczyna?

mój kod :

#include <SPI.h>
#include <Ethernet.h>

void Processcheckbox(EthernetClient cl);

String HTTP_req;          // stores the HTTP request
volatile boolean LED_status = 0;   // state of LED, off by default
volatile boolean LED_status2 = 0;


byte mac[] = {
 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 1, 110);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup() {
 // Open serial communications and wait for port to open:
 //pinMode(2,OUTPUT);
 //pinMode(3,OUTPUT);
 Serial.begin(9600);
 while (!Serial) {
   ; // wait for serial port to connect. Needed for Leonardo only
 }


 // start the Ethernet connection and the server:
 Ethernet.begin(mac, ip);
 server.begin();
 Serial.print("server is at ");
 Serial.println(Ethernet.localIP());
 pinMode(2, OUTPUT); 
 pinMode(3, OUTPUT); 
}


void loop() {
 // listen for incoming clients
 EthernetClient client = server.available();
 if (client) {
   Serial.println("new client");
   // an http request ends with a blank line
   boolean currentLineIsBlank = true;
   while (client.connected()) {
     if (client.available()) {
       char c = client.read();
       HTTP_req += c;
       Serial.write(c);
       // if you've gotten to the end of the line (received a newline
       // character) and the line is blank, the http request has ended,
       // so you can send a reply
       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: close");  // the connection will be closed after completion of the response
         client.println("Refresh: 5");  // refresh the page automatically every 5 sec
         client.println();
         client.println("<!DOCTYPE HTML>");
         client.println("<html>");
         client.println("<title>");
         client.println("Moj projekt");
         client.println("</title>");
         client.println("<head><h1>");
         client.println("Czujniki");
         client.println("</h1></head>");
         client.println("<br />");
         // output the value of each analog input pin
         for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
           int sensorReading = analogRead(analogChannel);
           client.print("analog input ");
           client.print(analogChannel);
           client.print(" is ");
           client.print(sensorReading);
           client.println("<br />");
         }


         client.println("<h1>");
         client.println("Przekazniki");
         client.println("</h1>");
         client.println("<p>Przekaznik nr.1</p>");
                   client.println("<form method=\"get\">");
                   ProcessCheckbox(client);
                   client.println("</form>");

         client.println("<p>Przekaznik nr.2</p>");
                   client.println("<form method=\"get\">");
                   ProcessCheckbox2(client);
                   client.println("</form>");

         client.println("</html>");
         Serial.print(HTTP_req);
                   HTTP_req = "";
         break;
       }

       if (c == '\n') {
         // you're starting a new line
         currentLineIsBlank = true;
       }
       else if (c != '\r') {
         // you've gotten a character on the current line
         currentLineIsBlank = false;
       }
     }
   }
   // give the web browser time to receive the data
   delay(1);
   // close the connection:
   client.stop();
   Serial.println("client disconnected");
 }
}


void ProcessCheckbox(EthernetClient cl)
{
   if (HTTP_req.indexOf("LED2=2") > -1) {  // see if checkbox was clicked
       // the checkbox was clicked, toggle the LED
       //if (LED_status) {
           LED_status = 1;
       }
       else {
           LED_status = 0;
       }
   //}

   if (LED_status) {    // switch LED on
       digitalWrite(2, HIGH);
       // checkbox is checked
       cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
       onclick=\"submit();\" checked>On");
   }
   else {              // switch LED off
       digitalWrite(2, LOW);
       // checkbox is unchecked
       cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"2\" \
       onclick=\"submit();\">Off");
   }
}

void ProcessCheckbox2(EthernetClient cl)
{
   if (HTTP_req.indexOf("LED2=3") > -1) {  // see if checkbox was clicked
       // the checkbox was clicked, toggle the LED
       //if (LED_status) {
           LED_status2 = 1;
       }
       else {
           LED_status2 = 0;
       }
   //}

   if (LED_status2) {    // switch LED on
       digitalWrite(3, HIGH);
       // checkbox is checked
       cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"3\" \
       onclick=\"submit();\" checked>On");
   }
   else {              // switch LED off
       digitalWrite(3, LOW);
       // checkbox is unchecked
       cl.println("<input type=\"checkbox\" name=\"LED2\" value=\"3\" \
       onclick=\"submit();\">Off");
   }
}

Z góry dziękuje za pomoc

Link do komentarza
Share on other sites

Dołącz do dyskusji, napisz odpowiedź!

Jeśli masz już konto to zaloguj się teraz, aby opublikować wiadomość jako Ty. Możesz też napisać teraz i zarejestrować się później.
Uwaga: wgrywanie zdjęć i załączników dostępne jest po zalogowaniu!

Anonim
Dołącz do dyskusji! Kliknij i zacznij pisać...

×   Wklejony jako tekst z formatowaniem.   Przywróć formatowanie

  Dozwolonych jest tylko 75 emoji.

×   Twój link będzie automatycznie osadzony.   Wyświetlać jako link

×   Twoja poprzednia zawartość została przywrócona.   Wyczyść edytor

×   Nie możesz wkleić zdjęć bezpośrednio. Prześlij lub wstaw obrazy z adresu URL.

×
×
  • Utwórz nowe...

Ważne informacje

Ta strona używa ciasteczek (cookies), dzięki którym może działać lepiej. Więcej na ten temat znajdziesz w Polityce Prywatności.