arduino-libraries / WiFiNINA

140 stars 106 forks source link

Get Server status and restart #47

Closed moebius911 closed 5 years ago

moebius911 commented 5 years ago

Hello

currently i use the function "server.begin ()" to launch the web server on my arduino and it works very well. However I noticed that after several wifi disconnection the server ended up crashing. My problem is that I do not see any function to obtain the state of the server or to restart it. Do you have a solution to offer me?

sandeepmistry commented 5 years ago

Hi @moebius911,

Do you have some steps to reproduce the "I noticed that after several wifi disconnection the server ended up crashing" condition?

moebius911 commented 5 years ago

here is a piece of my program that I have simplified. the server crash after about 10min

#include <WiFiNINA.h>  // for use with Arduino Uno WiFi Rev2
#include <avr/wdt.h>
#include <avr/io.h>
#include <avr/interrupt.h>      // library for interrupts handling
#include <avr/sleep.h>          // library for sleep
#include <util/delay.h>

//********************************Variable globales************************************
int batterie = A0;
int hydrometrie1 = A1;
int hydrometrie2 = A2;
int hydrometrie3 = A3;
int hydrometrie4 = A4;
int hydrometrie5 = A5;
int FondCuve = 10;

// set water relays
int vanne4 = 2;
int vanne3 = 3;
int vanne2 = 4;
int vanne1 = 5;
int pompe = 6;
int chauffage = 7;
int ventilation = 8;
int relais8 = 9;

// Network
char ssid[] = "freebox_Alex";     // your network SSID (name)
char pass[] = "****************";  // your network password
WiFiClient client; // Initialize the client library
int status = WL_IDLE_STATUS;
WiFiServer server(80);
byte IP_Serveur[] = { 192, 168, 1, 252 };
int Port = 8080; 

/*****************************************************************/
/*                       Initialisation                          */
/*****************************************************************/
void setup() {
  //initialisation des dialogues
  Serial.begin(9600);  // initialize serial communication
  Serial.println(F("Initialisation en cours......."));
  pinMode(vanne1, OUTPUT);
  pinMode(vanne2, OUTPUT);
  pinMode(vanne3, OUTPUT);
  pinMode(vanne4, OUTPUT);
  pinMode(pompe, OUTPUT);
  pinMode(chauffage, OUTPUT);
  pinMode(ventilation, OUTPUT);
  pinMode(relais8, OUTPUT);
  pinMode(hydrometrie1, INPUT);
  pinMode(hydrometrie2, INPUT);
  pinMode(hydrometrie3, INPUT);
  pinMode(hydrometrie4, INPUT);
  pinMode(hydrometrie5, INPUT);
  pinMode(FondCuve, INPUT);
  digitalWrite(vanne1, HIGH);
  digitalWrite(vanne2, HIGH);
  digitalWrite(vanne3, HIGH);
  digitalWrite(vanne4, HIGH);
  digitalWrite(pompe, HIGH);
  digitalWrite(chauffage, HIGH);
  digitalWrite(ventilation, HIGH);
  digitalWrite(relais8, HIGH);  
  WiFi.lowPowerMode(); 
  timer(1); //timer interruption en sec
}

/*****************************************************************/
/*                    Programme Principal                        */
/*****************************************************************/
void loop() {
  ConnexionWifi();
  Serial.println(F("deconnexion dans 30sec"));
  delay(30000);
  WiFi.disconnect();
  Serial.println(F("Reconnexion dans 30sec"));
  delay(30000);
}

/*****************************************************************/
/*                      Connexion au wifi                        */
/*****************************************************************/
char ConnexionWifi(){
  WiFi.disconnect(); //*************
  delay(1000);
  status = WL_IDLE_STATUS; //reset le status wifi
   // Check for the presence of the WiFi device
  if (WiFi.status() == WL_NO_SHIELD || WiFi.status() == WL_NO_MODULE) {
    Serial.println("!!! Module wifi ne repond pas !!!");
    //while (true);  // don't continue
  } else {
    Serial.println("Module WiFi [OK]");
  }
  String fv = WiFi.firmwareVersion();
  if (fv < "1.2.0") {
    Serial.println("Please upgrade the firmware.");
  }
  // Attempt to connect to WiFi network
  int compteur_wifi=3;
  while ((status != WL_CONNECTED) && (compteur_wifi != 0)) {
    Serial.print(" En attente de connection au reseau: ");
    Serial.println(ssid);  // print the network name (SSID)
    compteur_wifi = compteur_wifi - 1;
    if (compteur_wifi == 0) {
      Serial.println("**************************************");
      Serial.println("ATTENTION : Connection wifi impossible");
      Serial.println("**************************************");
    }
    // Connect to WPA/WPA2 network.  Change this line if using open or WEP network.
    status = WiFi.begin(ssid, pass);
    delay(1000);  // wait 10 seconds for connection
  }
    server.begin();     // start the web server on port 80
    printWifiStatus();  // you're connected now, so print the status
    Serial.println();
}

/*****************************************************************/
/*    ecoute client en attente pour affichage page web           */
/*****************************************************************/
void ControlClientWebPage() {
    WiFiClient client = server.available();  // listen for incoming clients

  if (client) {                            // if you get a client,
    Serial.println("New Client");          // print a message to the serial port
    String currentLine = "";               // make a string to hold incoming data from the client
    while (client.connected()) {           // loop while the client is connected
      if (client.available()) {            // if there are bytes to read from the client,
        char c = client.read();            // read a byte, then
        Serial.write(c);                   // print it to the serial monitor
        if (c == '\n') {                   // if the byte is a newline character
          // If the current line is blank, you got two newline characters in a row
          // and that is the end of the client HTTP request, so send a response
          if (currentLine.length() == 0) {
            showWebPage(client);  // construct and display web page
            break;                // break out of the while loop
          } else {  // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        performRequest(currentLine);  // complete the client request
      }
    }
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
  }
}
`
/*****************************************************************/
/*                 construction de la page web                   */
/*****************************************************************/
void showWebPage(WiFiClient client) {
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a Content-Type so the client knows what's coming, then a blank line
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  //refresh
  client.println("<meta http-equiv=\"refresh\" content=\"10\">");

  // The content of the HTTP response follows the header
  client.println("<h1>Serre Automatisee</h1>");
  client.println("<table border=1 style='text-align:center'>");
  client.println("<tr><th>Component</th><th>Status</th><th>Control</th></tr>");

 //affichage Status reservoir Eau
  client.print("<tr><td>Eau Bac 1</td><td>");
  if (digitalRead(FondCuve) == HIGH) {  // Change la couleur en fonction de l'etat Sonde 1
    client.println("<font style='color:green;'>OK</font>");
  } else {
    client.println("<font style='color:red;'>NOK</font>");
  }  

  // Cpateur1
  client.print("<tr><td>Capteur Bac 1</td><td>");
  if (analogRead(hydrometrie1) > 100) {  // show colored status based on state of Capteur1
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie1) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur2
  client.print("<tr><td>Capteur Bac 2</td><td>");
  if (analogRead(hydrometrie2) > 100) {  // show colored status based on state of Capteur2
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie2) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur3
  client.print("<tr><td>Capteur Bac 3</td><td>");
  if (analogRead(hydrometrie3) > 100) {  // show colored status based on state of Capteur3
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie3) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur4
  client.print("<tr><td>Capteur Bac 4</td><td>");
  if (analogRead(hydrometrie4) > 100) {  // show colored status based on state of Capteur4
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie4) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // vanne1
  client.print("<tr><td>Vanne 1</td><td>");
  if (digitalRead(vanne1)) {  // show colored status based on state of vanne1
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne1/on'>ON</a> / <a href='/vanne1/off'>OFF</a></td></tr>");  // display redLED control links

  // vanne2
  client.print("<tr><td>Vanne 2</td><td>");
  if (digitalRead(vanne2)) {  // show colored status based on state of vanne2
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne2/on'>ON</a> / <a href='/vanne2/off'>OFF</a></td></tr>");  // display vanne2 control links

  // vanne3
  client.print("<tr><td>Vanne 3</td><td>");
  if (digitalRead(vanne3)) {  // show colored status based on state of vanne3
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne3/on'>ON</a> / <a href='/vanne3/off'>OFF</a></td></tr>");  // display vanne3 control links

  // vanne4
  client.print("<tr><td>Vanne 4</td><td>");
  if (digitalRead(vanne4)) {  // show colored status based on state of vanne4
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne4/on'>ON</a> / <a href='/vanne4/off'>OFF</a></td></tr>");  // display vanne4 control links

  // pompe
  client.print("<tr><td>Pompe</td><td>");
  if (digitalRead(pompe)) {  // show colored status based on state of pompe
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/pompe/on'>ON</a> / <a href='/pompe/off'>OFF</a></td></tr>");  // display pompe control links

  // chauffage
  client.print("<tr><td>Chauffage</td><td>");
  if (digitalRead(chauffage)) {  // show colored status based on state of chauffage
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/chauffage/on'>ON</a> / <a href='/chauffage/off'>OFF</a></td></tr>");  // display chauffage control links

  // ventilation
  client.print("<tr><td>Ventilation</td><td>");
  if (digitalRead(ventilation)) {  // show colored status based on state of ventilation
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/ventilation/on'>ON</a> / <a href='/ventilation/off'>OFF</a></td></tr>");  // display ventilation control links

  // relais8
  client.print("<tr><td>Relais 8</td><td>");
  if (digitalRead(relais8)) {  // show colored status based on state of relais8
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/relais8/on'>ON</a> / <a href='/relais8/off'>OFF</a></td></tr>");  // display relais8 control links

  // reboot
  client.print("<tr><td>Reboot</td><td>");
  client.print("<font style='color:purple;'>REBOOT</font>");
  client.println("</td><td><a href='/reboot'>REBOOT</a></td></tr>");  // display reboot control links
  client.println("</table>");

  // The HTTP response ends with another blank line
  client.println();
}
`
/*****************************************************************/
/*             Gestion des actions sur la page web               */
/*****************************************************************/
void performRequest(String line) {
  if (line.endsWith("GET /vanne1/on")) {             // turn on vanne1
    digitalWrite(vanne1, HIGH);
  } else if (line.endsWith("GET /vanne1/off")) {     // turn off vanne1
    digitalWrite(vanne1, LOW);
  } else if (line.endsWith("GET /vanne2/on")) {   // turn on vanne2
    digitalWrite(vanne2, HIGH);
  } else if (line.endsWith("GET /vanne2/off")) {  // turn off vanne2
    digitalWrite(vanne2, LOW);
  } else if (line.endsWith("GET /vanne3/on")) {    // turn on vanne3
    digitalWrite(vanne3, HIGH);
  } else if (line.endsWith("GET /vanne3/off")) {   // turn off vanne3
    digitalWrite(vanne3, LOW);
  } else if (line.endsWith("GET /vanne4/on")) {    // turn on vanne4
    digitalWrite(vanne4, HIGH);
  } else if (line.endsWith("GET /vanne4/off")) {   // turn off vanne4
    digitalWrite(vanne4, LOW);
   } else if (line.endsWith("GET /pompe/on")) {    // turn on pompe
    digitalWrite(pompe, HIGH);
  } else if (line.endsWith("GET /pompe/off")) {   // turn off pompe
    digitalWrite(pompe, LOW);
   } else if (line.endsWith("GET /chauffage/on")) {    // turn on chauffage
    digitalWrite(chauffage, HIGH);
  } else if (line.endsWith("GET /chauffage/off")) {   // turn off chauffage
    digitalWrite(chauffage, LOW);
   } else if (line.endsWith("GET /ventilation/on")) {    // turn on ventilation
    digitalWrite(ventilation, HIGH);
  } else if (line.endsWith("GET /ventilation/off")) {   // turn off ventilation
    digitalWrite(ventilation, LOW);
   } else if (line.endsWith("GET /relais8/on")) {    // turn on relais8
    digitalWrite(relais8, HIGH);
   } else if (line.endsWith("GET /relais8/off")) {    // turn off relais8
    digitalWrite(relais8, LOW);
  } else if (line.endsWith("GET /reboot")) {   // turn off relais8
    wdt_enable(WDTO_2S);
  }
}

/*****************************************************************/
/*                Affichage infos connexion wifi                 */
/*****************************************************************/
void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi device's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");

  // Print where to go in a browser
  Serial.print("Acces à la page principale : http://");
  Serial.println(ip);
}

/****************************************************************************/
/*                  Configuration de l'interruption timer                   */
/****************************************************************************/
void timer(int secs){
  while (RTC.STATUS > 0) {} // attend que tous les registre soit synchronisée

  RTC.PER = 1024*secs; //secs = interuption timer en secondes.
  RTC.INTCTRL = 0 << RTC_CMP_bp
  | 1 << RTC_OVF_bp; //Overflow interrupt.

    RTC.CTRLA = RTC_PRESCALER_DIV1_gc //NO Prescaler
  | 1 << RTC_RTCEN_bp         //active RTC
  | 1 << RTC_RUNSTDBY_bp;     //fonctionne en standby

  RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // 32KHz divisé par 32, fonctionne à 1.024kHz
  sei(); //active l'interruption
}
/****************************************************************************/
/*                           Interuption par Timer                          */
/****************************************************************************/
ISR(RTC_CNT_vect)
{
        RTC.INTFLAGS = RTC_OVF_bm;
        ControlClientWebPage(); //ecoute les connections clients
}
Rocketct commented 5 years ago

Hi @moebius911 i had try your code and reproduced the issue, on my case happen that after the the first re connection the server was not able to response. There is a server.status() API that return if there are sockets opened or not, to restart on this case the only way is use again the begin, but you have to be sure about the connection, sometimes i had see the status function return 0 that means not-connected. I have modified your sketch in order to:

#include <WiFiNINA.h>  // for use with Arduino Uno WiFi Rev2
#include <avr/wdt.h>
#include <avr/io.h>
#include <avr/interrupt.h>      // library for interrupts handling
#include <avr/sleep.h>          // library for sleep
#include <util/delay.h>

//********************************Variable globales************************************
int batterie = A0;
int hydrometrie1 = A1;
int hydrometrie2 = A2;
int hydrometrie3 = A3;
int hydrometrie4 = A4;
int hydrometrie5 = A5;
int FondCuve = 10;

// set water relays
int vanne4 = 2;
int vanne3 = 3;
int vanne2 = 4;
int vanne1 = 5;
int pompe = 6;
int chauffage = 7;
int ventilation = 8;
int relais8 = 9;

// Network
char ssid[] = "ssid";     // your network SSID (name)
char pass[] = "**********";  // your network password
WiFiClient client; // Initialize the client library
int status = WL_IDLE_STATUS;
WiFiServer server(80);
byte IP_Serveur[] = { 192, 168, 1, 252 };
int Port = 8080; 

/*****************************************************************/
/*                       Initialisation                          */
/*****************************************************************/
void setup() {
  //initialisation des dialogues
  Serial.begin(9600);  // initialize serial communication
  Serial.println(F("Initialisation en cours......."));
  pinMode(vanne1, OUTPUT);
  pinMode(vanne2, OUTPUT);
  pinMode(vanne3, OUTPUT);
  pinMode(vanne4, OUTPUT);
  pinMode(pompe, OUTPUT);
  pinMode(chauffage, OUTPUT);
  pinMode(ventilation, OUTPUT);
  pinMode(relais8, OUTPUT);
  pinMode(hydrometrie1, INPUT);
  pinMode(hydrometrie2, INPUT);
  pinMode(hydrometrie3, INPUT);
  pinMode(hydrometrie4, INPUT);
  pinMode(hydrometrie5, INPUT);
  pinMode(FondCuve, INPUT);
  digitalWrite(vanne1, HIGH);
  digitalWrite(vanne2, HIGH);
  digitalWrite(vanne3, HIGH);
  digitalWrite(vanne4, HIGH);
  digitalWrite(pompe, HIGH);
  digitalWrite(chauffage, HIGH);
  digitalWrite(ventilation, HIGH);
  digitalWrite(relais8, HIGH);  
  WiFi.lowPowerMode(); 
  timer(1); //timer interruption en sec
}

/*****************************************************************/
/*                    Programme Principal                        */
/*****************************************************************/
void loop() {
  ConnexionWifi();
  Serial.println(F("deconnexion dans 30sec"));
  delay(30000);
  WiFi.disconnect();
  Serial.println(F("Reconnexion dans 30sec"));
  delay(30000);
}

/*****************************************************************/
/*                      Connexion au wifi                        */
/*****************************************************************/
char ConnexionWifi(){

  delay(1000);
  status = WL_IDLE_STATUS; //reset le status wifi
   // Check for the presence of the WiFi device
  if (WiFi.status() == WL_NO_SHIELD || WiFi.status() == WL_NO_MODULE) {
    Serial.println("!!! Module wifi ne repond pas !!!");
    //while (true);  // don't continue
  } else {
    Serial.println("Module WiFi [OK]");
  }
  String fv = WiFi.firmwareVersion();
  if (fv < "1.2.0") {
    Serial.println("Please upgrade the firmware.");
  }
  WiFi.config(IP_Serveur);
  // Attempt to connect to WiFi network
  //int compteur_wifi=3;
  while (status != WL_CONNECTED){// && (compteur_wifi != 0)) {
    Serial.print(" En attente de connection au reseau: ");
    Serial.println(ssid);  // print the network name (SSID)
///    compteur_wifi = compteur_wifi - 1;
//    if (compteur_wifi == 0) {
//      Serial.println("**************************************");
//      Serial.println("ATTENTION : Connection wifi impossible");
//      Serial.println("**************************************");
//    }
    // Connect to WPA/WPA2 network.  Change this line if using open or WEP network.
    status = WiFi.begin(ssid, pass);
    delay(1000);  // wait 10 seconds for connection
  }
   while (server.status() != 1) {
    server.begin();     // start the web server on port 80
    printWifiStatus();  // you're connected now, so print the status
    Serial.println(server.status());
    Serial.println();
  }
}

/*****************************************************************/
/*    ecoute client en attente pour affichage page web           */
/*****************************************************************/
void ControlClientWebPage() {
    WiFiClient client = server.available();  // listen for incoming clients

  if (client) {                            // if you get a client,
    Serial.println("New Client");          // print a message to the serial port
    String currentLine = "";               // make a string to hold incoming data from the client
    while (client.connected()) {           // loop while the client is connected
      if (client.available()) {            // if there are bytes to read from the client,
        char c = client.read();            // read a byte, then
        Serial.write(c);                   // print it to the serial monitor
        if (c == '\n') {                   // if the byte is a newline character
          // If the current line is blank, you got two newline characters in a row
          // and that is the end of the client HTTP request, so send a response
          if (currentLine.length() == 0) {
            showWebPage(client);  // construct and display web page
            break;                // break out of the while loop
          } else {  // if you got a newline, then clear currentLine
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }
        performRequest(currentLine);  // complete the client request
      }
    }
    // Close the connection
    client.stop();
    Serial.println("Client disconnected.");
  }
}

/*****************************************************************/
/*                 construction de la page web                   */
/*****************************************************************/
void showWebPage(WiFiClient client) {
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a Content-Type so the client knows what's coming, then a blank line
  client.println("HTTP/1.1 200 OK");
  client.println("Content-Type: text/html");
  client.println();

  //refresh
  client.println("<meta http-equiv=\"refresh\" content=\"10\">");

  // The content of the HTTP response follows the header
  client.println("<h1>Serre Automatisee</h1>");
  client.println("<table border=1 style='text-align:center'>");
  client.println("<tr><th>Component</th><th>Status</th><th>Control</th></tr>");

 //affichage Status reservoir Eau
  client.print("<tr><td>Eau Bac 1</td><td>");
  if (digitalRead(FondCuve) == HIGH) {  // Change la couleur en fonction de l'etat Sonde 1
    client.println("<font style='color:green;'>OK</font>");
  } else {
    client.println("<font style='color:red;'>NOK</font>");
  }  

  // Cpateur1
  client.print("<tr><td>Capteur Bac 1</td><td>");
  if (analogRead(hydrometrie1) > 100) {  // show colored status based on state of Capteur1
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie1) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur2
  client.print("<tr><td>Capteur Bac 2</td><td>");
  if (analogRead(hydrometrie2) > 100) {  // show colored status based on state of Capteur2
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie2) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur3
  client.print("<tr><td>Capteur Bac 3</td><td>");
  if (analogRead(hydrometrie3) > 100) {  // show colored status based on state of Capteur3
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie3) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // Cpateur4
  client.print("<tr><td>Capteur Bac 4</td><td>");
  if (analogRead(hydrometrie4) > 100) {  // show colored status based on state of Capteur4
    client.println("<font style='color:green;'>OK</font>");}
  else if (analogRead(hydrometrie4) == 0) { 
    client.println("<font style='color:red;'>ERREUR</font>");}
  else {client.println("<font style='color:orange;'>NOK</font>");}

  // vanne1
  client.print("<tr><td>Vanne 1</td><td>");
  if (digitalRead(vanne1)) {  // show colored status based on state of vanne1
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne1/on'>ON</a> / <a href='/vanne1/off'>OFF</a></td></tr>");  // display redLED control links

  // vanne2
  client.print("<tr><td>Vanne 2</td><td>");
  if (digitalRead(vanne2)) {  // show colored status based on state of vanne2
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne2/on'>ON</a> / <a href='/vanne2/off'>OFF</a></td></tr>");  // display vanne2 control links

  // vanne3
  client.print("<tr><td>Vanne 3</td><td>");
  if (digitalRead(vanne3)) {  // show colored status based on state of vanne3
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne3/on'>ON</a> / <a href='/vanne3/off'>OFF</a></td></tr>");  // display vanne3 control links

  // vanne4
  client.print("<tr><td>Vanne 4</td><td>");
  if (digitalRead(vanne4)) {  // show colored status based on state of vanne4
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/vanne4/on'>ON</a> / <a href='/vanne4/off'>OFF</a></td></tr>");  // display vanne4 control links

  // pompe
  client.print("<tr><td>Pompe</td><td>");
  if (digitalRead(pompe)) {  // show colored status based on state of pompe
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/pompe/on'>ON</a> / <a href='/pompe/off'>OFF</a></td></tr>");  // display pompe control links

  // chauffage
  client.print("<tr><td>Chauffage</td><td>");
  if (digitalRead(chauffage)) {  // show colored status based on state of chauffage
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/chauffage/on'>ON</a> / <a href='/chauffage/off'>OFF</a></td></tr>");  // display chauffage control links

  // ventilation
  client.print("<tr><td>Ventilation</td><td>");
  if (digitalRead(ventilation)) {  // show colored status based on state of ventilation
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/ventilation/on'>ON</a> / <a href='/ventilation/off'>OFF</a></td></tr>");  // display ventilation control links

  // relais8
  client.print("<tr><td>Relais 8</td><td>");
  if (digitalRead(relais8)) {  // show colored status based on state of relais8
    client.print("<font style='color:green;'>ON</font>");
  } else {
    client.print("<font style='color:red;'>OFF</font>");
  }
  client.println("</td><td><a href='/relais8/on'>ON</a> / <a href='/relais8/off'>OFF</a></td></tr>");  // display relais8 control links

  // reboot
  client.print("<tr><td>Reboot</td><td>");
  client.print("<font style='color:purple;'>REBOOT</font>");
  client.println("</td><td><a href='/reboot'>REBOOT</a></td></tr>");  // display reboot control links
  client.println("</table>");

  // The HTTP response ends with another blank line
  client.println();
}

/*****************************************************************/
/*             Gestion des actions sur la page web               */
/*****************************************************************/
void performRequest(String line) {
  if (line.endsWith("GET /vanne1/on")) {             // turn on vanne1
    digitalWrite(vanne1, HIGH);
  } else if (line.endsWith("GET /vanne1/off")) {     // turn off vanne1
    digitalWrite(vanne1, LOW);
  } else if (line.endsWith("GET /vanne2/on")) {   // turn on vanne2
    digitalWrite(vanne2, HIGH);
  } else if (line.endsWith("GET /vanne2/off")) {  // turn off vanne2
    digitalWrite(vanne2, LOW);
  } else if (line.endsWith("GET /vanne3/on")) {    // turn on vanne3
    digitalWrite(vanne3, HIGH);
  } else if (line.endsWith("GET /vanne3/off")) {   // turn off vanne3
    digitalWrite(vanne3, LOW);
  } else if (line.endsWith("GET /vanne4/on")) {    // turn on vanne4
    digitalWrite(vanne4, HIGH);
  } else if (line.endsWith("GET /vanne4/off")) {   // turn off vanne4
    digitalWrite(vanne4, LOW);
   } else if (line.endsWith("GET /pompe/on")) {    // turn on pompe
    digitalWrite(pompe, HIGH);
  } else if (line.endsWith("GET /pompe/off")) {   // turn off pompe
    digitalWrite(pompe, LOW);
   } else if (line.endsWith("GET /chauffage/on")) {    // turn on chauffage
    digitalWrite(chauffage, HIGH);
  } else if (line.endsWith("GET /chauffage/off")) {   // turn off chauffage
    digitalWrite(chauffage, LOW);
   } else if (line.endsWith("GET /ventilation/on")) {    // turn on ventilation
    digitalWrite(ventilation, HIGH);
  } else if (line.endsWith("GET /ventilation/off")) {   // turn off ventilation
    digitalWrite(ventilation, LOW);
   } else if (line.endsWith("GET /relais8/on")) {    // turn on relais8
    digitalWrite(relais8, HIGH);
   } else if (line.endsWith("GET /relais8/off")) {    // turn off relais8
    digitalWrite(relais8, LOW);
  } else if (line.endsWith("GET /reboot")) {   // turn off relais8
    wdt_enable(WDTO_2S);
  }
}

/*****************************************************************/
/*                Affichage infos connexion wifi                 */
/*****************************************************************/
void printWifiStatus() {
  // Print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // Print your WiFi device's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // Print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");

  // Print where to go in a browser
  Serial.print("Acces à la page principale : http://");
  Serial.println(ip);
}

/****************************************************************************/
/*                  Configuration de l'interruption timer                   */
/****************************************************************************/
void timer(int secs){
  while (RTC.STATUS > 0) {} // attend que tous les registre soit synchronisée

  RTC.PER = 1024*secs; //secs = interuption timer en secondes.
  RTC.INTCTRL = 0 << RTC_CMP_bp
  | 1 << RTC_OVF_bp; //Overflow interrupt.

    RTC.CTRLA = RTC_PRESCALER_DIV1_gc //NO Prescaler
  | 1 << RTC_RTCEN_bp         //active RTC
  | 1 << RTC_RUNSTDBY_bp;     //fonctionne en standby

  RTC.CLKSEL = RTC_CLKSEL_INT1K_gc; // 32KHz divisé par 32, fonctionne à 1.024kHz
  sei(); //active l'interruption
}
/****************************************************************************/
/*                           Interuption par Timer                          */
/****************************************************************************/
ISR(RTC_CNT_vect)
{
        RTC.INTFLAGS = RTC_OVF_bm;
        ControlClientWebPage(); //ecoute les connections clients
}

Could you test and let me know if works on your side?

pozoo commented 5 years ago

I have a similar problem: I am also using WifiNina on Arduino MKR1010 to run a simple web server. For the first couple of minutes everything works fine. The the server simply stops responding, all requests just time out. I can still ping the device. The call: WiFiClient client = webServer.available(); simply does not seem to return any clients anymore.

Any idea how to make it respond again?

Rocketct commented 5 years ago

Hi @pozoo could you share a simplified version of your sketch to reproduce the error?

moebius911 commented 5 years ago

Thank you for your help, my program now seems stable bye