Fonction :
/*********************************************************************************************************************
* Sous Programme
* Fonction : Envoie une page WEB
* utilise la sous prog envoiPageWEB (pour envoyer avec tempon le site en utilisant les variables PROGMEM)
/*********************************************************************************************************************
* Entree : (client de EtherntClient)
*
* Sortie : Envoie des DATA vers le navigateur WEB
/*********************************************************************************************************************/
void PageWEB(EthernetClient &client)
{
Serial.println(); Serial.println();
// Page HTML : ConfPage.html (Page 2)*************************************************************************************************
if (StrContains(HTTP_req ,"GET /ConfPage.html") ) {
Serial.println(F("Envoie Page WEB : ConfigIP.html "));
ramFree(nullptr);
// envoiPageWEB ( nom du client, Nom de la chaine PROGMEM, taille de la chaine PROGMEM );
EnvoiPageWEB(client, page_WEB_ConfigIP , strlen_P(page_WEB_ConfigIP));
// Exemple d'envoie du navigateur sur la liaison serie
/* Envoie de la page WEB
http://192.168.0.190/ConfPage.html?ST=0&IP0=192&IP1=168&IP2=0&IP3=15&MSR0=255&MSR1=255&MSR2=255&MSR3=0&GAT0=192&GAT1=168&GAT2=0&GAT3=254
// Recu pr la liason serie
GET /ConfPage.html?ST=0&IP0=192&IP1=168&IP2=0&IP3=15&MSR0=255&MSR1=255&MSR2=255&MSR3=0&GAT0=192&GAT1=168&GAT2=0&GAT3=254 HTTP/1.1
Host: 192.168.0.14
Connection: keep-alive
Accept: text/html,ap
*/
// Fonction sscanf permet de tranformer le tableau de char avec les infos de la conf ip et les mettre dans la structure conf.IP conf.gateway ....
// https://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm // http://koor.fr/C/cstdio/fscanf.wp //https://en.cppreference.com/w/c/io/fscanf
// Fonction scanf qui decoupe la chaine de caractere en fonction de delimiteur // https://stackoverflow.com/questions/14146434/using-scanf-for-reading-an-unsigned-char
//sscanf(HTTP_req, "GET /ConfPage.html?ST=%hhu&IP0=%hhu&IP1=%hhu&IP2=%hhu&IP3=%hhu&MSR0=%hhu&MSR1=%hhu&MSR2=%hhu&MSR3=%hhu&GAT0=%hhu&GAT1=%hhu&GAT2=%hhu&GAT3=%hhu", &conf.dhcp, &conf.ip[0], &conf.ip[1], &conf.ip[2], &conf.ip[3], &conf.subnet[0], &conf.subnet[1], &conf.subnet[2], &conf.subnet[3], &conf.gateway[0], &conf.gateway[1], &conf.gateway[2], &conf.gateway[3] );
// A l'acces a la page WEB de configuration IP par le navigateur celle ci est execute une fois (et donc bug et REST !!!) on met donc un compteur pour ne pas executer le reset la permiere fois
//Serial.print("Compteur : "); Serial.println(compteur);
if (compteur == 1)
{
sscanf(HTTP_req, "GET /ConfPage.html?ST=%hhu&IP0=%hhu&IP1=%hhu&IP2=%hhu&IP3=%hhu&MSR0=%hhu&MSR1=%hhu&MSR2=%hhu&MSR3=%hhu&GAT0=%hhu&GAT1=%hhu&GAT2=%hhu&GAT3=%hhu", &conf.dhcp, &conf.ip[0], &conf.ip[1], &conf.ip[2], &conf.ip[3], &conf.subnet[0], &conf.subnet[1], &conf.subnet[2], &conf.subnet[3], &conf.gateway[0], &conf.gateway[1], &conf.gateway[2], &conf.gateway[3] );
delay(1);
printConf("1"); // Affiche toute la fonfiguration reseau
saveConf(nullptr);
Serial.println(F("Enregistrement de la configuration IP en EEPROM : Ok!")); Serial.println();
client.print(F("<p> Configuration Enregistre <p> "));
client.print(F("L'Arduino est accessible dans quelques instant a l'adresse IP : "));
if( conf.dhcp == 0)
{
client.print(conf.ip[0],DEC); client.print("."); client.print(conf.ip[1],DEC); client.print("."); client.print(conf.ip[2],DEC); client.print("."); client.print(conf.ip[3],DEC);
}
else client.print(F("Arduino DHCP OK !"));
client.print(F("<p> RESET Arduino : Now ............... <p> "));
Serial.println(F("------- RESET ARDUINO !!!! -------")); Serial.println();
compteur = 0;
resetNow(nullptr);
}
compteur++;
return;
} // Fin Page WEB configuration
// Page HTML : index.html*************************************************************************************************
if (StrContains(HTTP_req, "GET / ") || StrContains(HTTP_req, "GET /index.htm") || StrContains(HTTP_req, "?BoutonON") || StrContains(HTTP_req, "?BoutonOFF") ) {
Serial.println(F("Envoie Page WEB : index.html"));
ramFree(nullptr);
// envoiPageWEB ( nom du cleint, Nom de la chaine PROGMEM, taille de la chaine PROGMEM
EnvoiPageWEB(client, page_WEB_index_HTML , strlen_P(page_WEB_index_HTML));
delay(1);
//client.stop(); //stopping client
// Partie Controle des Boutons
if (StrContains(HTTP_req ,"?BoutonON") ){
conf.led = 1;
digitalWrite(pinLED, HIGH);
Serial.println(F("LED est ON"));
//client.print(F("<style>p { color: green;}</style>
<p><a>LED est ON</a></p>"));
client.print(F("
<p><font color=\"green\"><font size=6><strong><a>LED est ON</a></strong></font></p>"));
}
if (StrContains(HTTP_req ,"?BoutonOFF") ){
conf.led = 0;
digitalWrite(pinLED, LOW);
Serial.println(F("LED est OFF"));
client.print(F("
<p><font color=\"red\"><font size=6><strong><a>LED est OFF</a></strong></font></p>"));
}
EEPROM.put(adressBase+1, conf.led);
//return;
} // Fin page HTML
// ***********************************************************************************************************************
// ICI on peut placer une autre pages WEB
}
/*********************************************************************************************************************
* Sous Programme
* Fonction : Envoie une page WEB en utilisant les variables PROGMEM
/*********************************************************************************************************************
* Entree : Nom du PROGMEM
*
* Sortie : Envoie des DATA vers le navigateur WEB
/*********************************************************************************************************************/
void EnvoiPageWEB(EthernetClient &client, const char *str_P, int taille)
{
const int tailleTampon = 32; // taille du tampon d'envoi
char buffer[tailleTampon];
int restant = taille;
const char *aEnvoyer = str_P; // Charge le nom du PROGMEM reponse_debut ou reponse_fin
int tailleEnvoi = tailleTampon;
Serial.println(F("Routine EnvoiPageWEB : "));
ramFree(nullptr);
while (restant > 0) {
if (tailleEnvoi > restant)
tailleEnvoi = restant;
memcpy_P(buffer, aEnvoyer, tailleEnvoi); // void * memcpy_P (void *, const void *, size_t)
if (client.write((const uint8_t *)buffer, tailleEnvoi) != tailleEnvoi)
return; // erreur
restant -= tailleEnvoi;
aEnvoyer += tailleEnvoi;
}
}