Envoi de données du ESP8266 vers un site web

Bonjour à vous tous.
J'ai récupéré un bout de code pour se connecter et envoyer une information sur un site web en https.
Le voici :

 if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

    client->setFingerprint(fingerprint);

    HTTPClient https;

    Serial.print("[HTTPS] début...\n");

    if (https.begin(*client, "https://www.test.ojjo.ca/HTTP/light.json")) { 

      Serial.print("[HTTPS] GET...\n");
      // Démarre la connexion et envoie l'entête (header) HTTP
      int httpCode = https.GET();

      // httpCode aura une valeur négative en cas d'erreur
      if (httpCode > 0) {
        // L'entête HTTP a été envoyée et l'entête de la réponse du Serveur a été reçue
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // Fichier non trouvé sur le Serveur
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
          int start = payload.indexOf("light\":");
          int end = payload.indexOf('\"', start + 11 );
          String positionString = "";
          for (int i= start + 9; i < end; i++)
          positionString += payload.charAt(i);
          
          // gestion de la LED
          if (positionString == "on") {
            Serial.print("LED Allumée \n");
            digitalWrite(13, HIGH);
          }
          if (positionString == "off") {
            Serial.print("LED Eteinte \n");
            digitalWrite(13, LOW);
            
          }

        }
      } else {
        Serial.printf("[HTTPS] GET... échoué, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Connexion impossible \n");
    }
  }

  Serial.println("Attendre 1s avant de lire le fichier...");
  delay(1000);
}

Je n'arrive pas à le transformer pour lire accéder à mon site web en http. Je n'ai par exemple pas besoin du finger print. mon site est www.test.ojjo.ca

merci de votre aide

pouvez vous me guider ?

Tu n'indique pas ce que fait ton programme, c'est dommage, tu te limite à ceux qui ont déjà essayés d'exécuter des programmes très similaires :slight_smile:

si votre site web utilise HTTP et pas HTTPS, il ne faut pas prendre ce code avec un WiFiClientSecure

regardez par exemple ESP32 HTTP GET and HTTP POST with Arduino IDE | Random Nerd Tutorials

son site semble être en HTTPS non ?

Il a mentionné

Donc je ne sais pas trop ce qu’il voulait faire vraiment mais son site http://www.test.ojjo.ca n’est pas sécurisé

Bon ok je suis désolé pour l'explication pas terrible de mon post .
je recommence :wink: On oublie le site en http
Sur le site web en https, je voudrais que quand je clique sur On ou OFF, cela allume ma LED qui est connecté sur mon Esp8266
Voici le Code sur mon ESP8266

/**
   Programme ESP_LED.ino 
   créé le 11/03/2020 à partir de BasicHTTPSClient.ino
   https://github.com/esp8266/Arduino/blob/master/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino
*/

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <WiFiClientSecureBearSSL.h>
// Empreinte du certificat pour la démo, expire le 30/05/2020 à 03:00:49, à mettre à jour en fonction de la date
// SHA1 du certificat
// 64:43:E4:28:0B:7B:B8:F2:E6:F2:F1:74:53:DE:BF:C9:93:BC:32:D8
//const uint8_t fingerprint[20] = {0x64, 0xa43, 0xe4, 0x28, 0x0b, 0x7b, 0xb8, 0xf2, 0xe6, 0xf2, 0xf1, 0x74, 0x53, 0xde, 0xbf, 0xc9, 0x93, 0xbc, 0x32, 0xd8};
// j'ai chang. pour mon finger print 
const uint8_t fingerprint[32] = {0xc8, 0x1e, 0x2a, 0x3f, 0x90, 0x62 , 0x79, 0x60, 0xe5, 0xc4, 0x37, 0x02, 0x2d, 0xd7, 0x9c, 0xc2, 0x25, 0xe5, 0xfe, 0x34, 0x5b, 0x28, 0x9f, 0x4e, 0x6b, 0x23, 0x5f, 0x23, 0x37, 0xa5, 0x14, 0xdd};


ESP8266WiFiMulti WiFiMulti;
const char* ssid = "xxxx";
const char* password = "xxxxxxxxx";

void setup() {

  // Initialiser la patte 13 en sortie
  pinMode(13, OUTPUT); 


  Serial.begin(115200);
  // Activer la ligne ci-dessous -debug- pour des infos supplémentaires
  // Par exemple l'adresse IP de connexion à la box au démarrage
  //Serial.setDebugOutput(true);
  delay(2000);
  Serial.println("\n");
  Serial.println("== Test connexion ==");
  Serial.println("--------------------");
  Serial.println(" Pilotage de LED par Internet ");

  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("ssid", "password");
   Serial.println("");
  Serial.println("WIFI connected ");
  Serial.println("IP adresse locale : ");
  Serial.println(WiFi.localIP());
}

void loop() {
  //Serial.print("Début de boucle ... \n");
  
  // Attendre la connexion WiFi
  if ((WiFiMulti.run() == WL_CONNECTED)) {

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

    client->setFingerprint(fingerprint);

    HTTPClient https;

    Serial.print("[HTTPS] début...\n");

   // if (https.begin(*client, "https://www.test.ojjo.ca/HTTP/light.json")) { 
    if (https.begin(*client, "https://www.test.ojjo.ca/light.json")) { 

      Serial.print("[HTTPS] GET...\n");
      // Démarre la connexion et envoie l'entête (header) HTTP
      int httpCode = https.GET();

      // httpCode aura une valeur négative en cas d'erreur
      if (httpCode > 0) {
        // L'entête HTTP a été envoyée et l'entête de la réponse du Serveur a été reçue
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // Fichier non trouvé sur le Serveur
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
          int start = payload.indexOf("light\":");
          int end = payload.indexOf('\"', start + 11 );
          String positionString = "";
          for (int i= start + 9; i < end; i++)
          positionString += payload.charAt(i);
          
          // gestion de la LED
          if (positionString == "on") {
            Serial.print("LED Allumée \n");
            digitalWrite(13, HIGH);
          }
          if (positionString == "off") {
            Serial.print("LED Eteinte \n");
            digitalWrite(13, LOW);
            
          }

        }
      } else {
        Serial.printf("[HTTPS] GET... échoué, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Connexion impossible \n");
    }
  }

  Serial.println("Attendre 1s avant de lire le fichier...");
  delay(1000);
}

le code coté web est correct .
mais quand je fais tourner ce code je n'ai que le message
Attendre 1s avant de lire le fichier...
qui apparait
et évidement cela ne fonctionne pas si je clique sur ON ou OFf sur mon site test.ojjo.ca

merci
J'espere avoir été plus clair et encore désolé pour mon manque d'explications .

essayez un truc comme cela

const char* root_ca = \
                      "-----BEGIN CERTIFICATE-----\n" \
                      "MIIF2DCCA8CgAwIBAgIQTKr5yttjb+Af907YWwOGnTANBgkqhkiG9w0BAQwFADCB\n" \
                      "hTELMAkGA1UEBhMCR0IxGzAZBgNVBAgTEkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4G\n" \
                      "A1UEBxMHU2FsZm9yZDEaMBgGA1UEChMRQ09NT0RPIENBIExpbWl0ZWQxKzApBgNV\n" \
                      "BAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNhdGlvbiBBdXRob3JpdHkwHhcNMTAwMTE5\n" \
                      "MDAwMDAwWhcNMzgwMTE4MjM1OTU5WjCBhTELMAkGA1UEBhMCR0IxGzAZBgNVBAgT\n" \
                      "EkdyZWF0ZXIgTWFuY2hlc3RlcjEQMA4GA1UEBxMHU2FsZm9yZDEaMBgGA1UEChMR\n" \
                      "Q09NT0RPIENBIExpbWl0ZWQxKzApBgNVBAMTIkNPTU9ETyBSU0EgQ2VydGlmaWNh\n" \
                      "dGlvbiBBdXRob3JpdHkwggIiMA0GCSqGSIb3DQEBAQUAA4ICDwAwggIKAoICAQCR\n" \
                      "6FSS0gpWsawNJN3Fz0RndJkrN6N9I3AAcbxT38T6KhKPS38QVr2fcHK3YX/JSw8X\n" \
                      "pz3jsARh7v8Rl8f0hj4K+j5c+ZPmNHrZFGvnnLOFoIJ6dq9xkNfs/Q36nGz637CC\n" \
                      "9BR++b7Epi9Pf5l/tfxnQ3K9DADWietrLNPtj5gcFKt+5eNu/Nio5JIk2kNrYrhV\n" \
                      "/erBvGy2i/MOjZrkm2xpmfh4SDBF1a3hDTxFYPwyllEnvGfDyi62a+pGx8cgoLEf\n" \
                      "Zd5ICLqkTqnyg0Y3hOvozIFIQ2dOciqbXL1MGyiKXCJ7tKuY2e7gUYPDCUZObT6Z\n" \
                      "+pUX2nwzV0E8jVHtC7ZcryxjGt9XyD+86V3Em69FmeKjWiS0uqlWPc9vqv9JWL7w\n" \
                      "qP/0uK3pN/u6uPQLOvnoQ0IeidiEyxPx2bvhiWC4jChWrBQdnArncevPDt09qZah\n" \
                      "SL0896+1DSJMwBGB7FY79tOi4lu3sgQiUpWAk2nojkxl8ZEDLXB0AuqLZxUpaVIC\n" \
                      "u9ffUGpVRr+goyhhf3DQw6KqLCGqR84onAZFdr+CGCe01a60y1Dma/RMhnEw6abf\n" \
                      "Fobg2P9A3fvQQoh/ozM6LlweQRGBY84YcWsr7KaKtzFcOmpH4MN5WdYgGq/yapiq\n" \
                      "crxXStJLnbsQ/LBMQeXtHT1eKJ2czL+zUdqnR+WEUwIDAQABo0IwQDAdBgNVHQ4E\n" \
                      "FgQUu69+Aj36pvE8hI6t7jiY7NkyMtQwDgYDVR0PAQH/BAQDAgEGMA8GA1UdEwEB\n" \
                      "/wQFMAMBAf8wDQYJKoZIhvcNAQEMBQADggIBAArx1UaEt65Ru2yyTUEUAJNMnMvl\n" \
                      "wFTPoCWOAvn9sKIN9SCYPBMtrFaisNZ+EZLpLrqeLppysb0ZRGxhNaKatBYSaVqM\n" \
                      "4dc+pBroLwP0rmEdEBsqpIt6xf4FpuHA1sj+nq6PK7o9mfjYcwlYRm6mnPTXJ9OV\n" \
                      "2jeDchzTc+CiR5kDOF3VSXkAKRzH7JsgHAckaVd4sjn8OoSgtZx8jb8uk2Intzna\n" \
                      "FxiuvTwJaP+EmzzV1gsD41eeFPfR60/IvYcjt7ZJQ3mFXLrrkguhxuhoqEwWsRqZ\n" \
                      "CuhTLJK7oQkYdQxlqHvLI7cawiiFwxv/0Cti76R7CZGYZ4wUAc1oBmpjIXUDgIiK\n" \
                      "boHGhfKppC3n9KUkEEeDys30jXlYsQab5xoq2Z0B15R97QNKyvDb6KkBPvVWmcke\n" \
                      "jkk9u+UJueBPSZI9FoJAzMxZxuY67RIuaTxslbH9qh17f4a+Hg4yRvv7E491f0yL\n" \
                      "S0Zj/gA0QHDBw7mh3aZw4gSzQbzpgJHqZJx64SIDqZxubw5lT2yHh17zbqD5daWb\n" \
                      "QOhTsiedSrnAdyGN/4fy3ryM7xfft0kL0fJuMAsaDk527RH89elWsn2/x20Kk4yl\n" \
                      "0MC2Hb46TpSi125sC8KKfPog88Tk5c0NqMuRkrF8hey1FGlmDoLnzc7ILaZRfyHB\n" \
                      "NVOFBkpdn627G190\n" \
                      "-----END CERTIFICATE-----\n";



#include <WiFi.h>
#include <WiFiClientSecure.h>

const char* ssid = "xxx";
const char* password = "xxx";
const char* host = "www.test.ojjo.ca";
const int httpsPort = 443;

void setup() {
  Serial.begin(115200);
  delay(1000);

  Serial.println("Connecting to WiFi...");
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  WiFiClientSecure client;
  client.setCACert(root_ca);

  Serial.print("Connecting to "); Serial.println(host);

  if (!client.connect(host, httpsPort)) {
    Serial.println("Connection failed");
    return;
  }

  Serial.print("Requesting light.json");
  client.println("GET /light.json HTTP/1.1");
  client.print("Host: "); client.println(host);
  client.println("User-Agent: ESP32");
  client.println("Connection: close\r\n");
  Serial.println("Request sent");

  Serial.println("Response:");
  while (client.connected()) {
    while (client.available()) Serial.write(client.read());
  }
  Serial.println("Closing connection");
  client.stop();
}

void loop() {}

ça devrait vous montrer le résultat de la requête (avec les headers)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.