#include <WiFi.h>
#include <HTTPClient.h>
#include <WiFiClient.h>
int RMSextIdx = 0;
int RMS_NbCx[1] = {0};
int RMS_Note[1] = {0};
#include <WiFi.h>
//const char *ServerTest = "google.fr";
//uint16_t portTest = 80;
const char *ServerTest = "fr.pool.ntp.org";
uint16_t portTest = 80;
void setup() {
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println(" Connected!");
WiFiClient WC; // WiFiClient is a wrong name for class, that work also for EthernetClient ?
bool internetAccess = false;
for (int i=0; i<3; i++ ) { // on test un certain nomnre de fois si on a echec
if (WC.connect(ServerTest,portTest)) {
Serial.printf("access to %s on port %d\n",ServerTest,portTest);
WC.stop(); // nothing more
internetAccess = true;
break; // casse la boucle for
} else {
Serial.printf("Not access to %s on port %d\n",ServerTest,portTest);
}
}
Serial.println(String("We") + (internetAccess ? "" : " don't") + " have access to web");
Serial.println("That's all !");
}
void loop() {
Serial.println(getPayloadhttp("http://www.google.fr", "/index.html", 80, false));
delay(60000); // TODO: Build something amazing!
}
//retourne le resultat d'une requette http get ou chaine vide si probleme
//String getPayloadhttp(String host, String url, uint16_t port = 80, bool note = false) {
String getPayloadhttp(String host, String url, uint16_t port, bool note) { // default values for optional parameter are set in declaration of function
uint32_t tmo1 = 3000; // timeOut premier essai de connexion
uint32_t tmo2 = 3000; // timeOut second essai de connexion
uint32_t tmo3 = 5000; // timeOut attente reception premier caractere
uint32_t tmo4 = 5000; // timeOut reception entiereté du paquet
uint32_t tempo1 = 500; // delay entre 2 essais de connexion
uint32_t tempo2 = 100; // delay après client Stop sur echec de connexion
uint32_t tempo3 = 100; // delay après client Stop sur echec reception premier caractere
String S = "";
WiFiClient client; // Use WiFiClient class to create TCP connections
if (note && RMS_NbCx[RMSextIdx] < 100) RMS_NbCx[RMSextIdx]++;
if (!client.connect(host.c_str(), port, tmo1)) {
delay(tempo1);
if (!client.connect(host.c_str(), port, tmo2)) {
client.stop();
delay(tempo2);
Serial.println("Connection to " + host + " failed to get : " + url);
if (note && RMS_Note[RMSextIdx] > 0) RMS_Note[RMSextIdx]--;
return String();
}
}
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
uint32_t timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > tmo3) {
client.stop();
delay(tempo3);
Serial.println("client ESP_RMS Timeout !" + host + " " + url);
if (note && RMS_Note[RMSextIdx] > 0) RMS_Note[RMSextIdx]--;
return String();
}
}
timeout = millis();
while (client.available() && (millis() - timeout < tmo4)) {// Lecture des données brutes distantes
//S += client.readStringUntil('\r');
S += client.read();
}
if (S.indexOf("\r") > 0) {
log_e("Message reçu !%s! a raccourcir", S.c_str());
S = S.substring(0,S.indexOf("\r")); // on a recu le paquet qui se termine par \r\n, on supprime la fin
} else {
log_e("Message reçu !%s! Ok", S.c_str());
}
client.clear();
client.stop();
return S;
}