fastoch13

Untitled

Nov 29th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFiClientSecure.h>
  2.  
  3.  
  4. const char* ssid = "********";
  5. const char* password = "*******";
  6.  
  7. const char* host = "maker.ifttt.com"; //host IFTTT
  8. const int httpsPort = 443; //port de connexion
  9.  
  10.  
  11. void setup() {
  12.   Serial.begin(115200); //Le port série pour l'affichage
  13.   Serial.println();
  14.  
  15.  
  16.   /*
  17.    *
  18.    * Connexion au WIFI selon les paramètres précédents
  19.    *
  20.    */
  21.  
  22.  
  23.   Serial.print("Connexion à ");
  24.   Serial.println(ssid);
  25.   WiFi.begin(ssid, password);
  26.   while (WiFi.status() != WL_CONNECTED) {
  27.     delay(500);
  28.     Serial.print(".");
  29.   }
  30.   Serial.println("");
  31.   Serial.println("WiFi connecté");
  32.   Serial.println("L'addresse IP est : ");
  33.   Serial.println(WiFi.localIP());//pas nécessaire dans se cas de figure
  34.  
  35.  
  36. /*
  37.  *
  38.  * Connexion à l'host IFTTT
  39.  *
  40.  */
  41.  
  42.  
  43.   WiFiClientSecure client;
  44.   Serial.print("Connexion à ");
  45.   Serial.println(host);
  46.   if (!client.connect(host, httpsPort)) { //similaire au WIFI!
  47.     Serial.println("La connexion à échoué");
  48.     return;
  49.   }
  50.  
  51.  
  52. /*
  53.  *
  54.  * Création de la requête HTTP à envoyer au serveur IFTTT
  55.  *
  56.  */
  57.  
  58.  
  59.   String url = "https://maker.ifttt.com/trigger/espconnected/with/key/*****************"; //A remplacer par ta requête!
  60.   Serial.print("La requête est : ");
  61.   Serial.println(url);
  62.  
  63.  
  64. /*
  65.  *
  66.  * On envois ensuite la requête en y ajoutant les informations nécessaire pour que le serveur puisse identifier le paquet HTTPs
  67.  *
  68.  *
  69.  */
  70.  
  71.  
  72.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  73.                "Host: " + host + "\r\n" +
  74.                "User-Agent: BuildFailureDetectorESP32\r\n" +
  75.                "Connection: close\r\n\r\n");
  76.  
  77. /*
  78.  *
  79.  * On retire l'entête HTTPs pour pouvoir récupérer les données utiles
  80.  *
  81.  */
  82.  
  83.  
  84.  
  85.   Serial.println("La requête a été envoyé");
  86.   while (client.connected()) {
  87.     String line = client.readStringUntil('\n');
  88.     if (line == "\r") {
  89.       Serial.println("L'entête du paquet a été reçus");
  90.       break;
  91.     }
  92.   }
  93.  
  94. /*
  95.  * On affiches les données utiles
  96.  * /!\ PAS OBLIGATOIRE POUR CET API /!\
  97.  *
  98.  */
  99.  
  100.  
  101.  
  102.   String line = client.readStringUntil('\n');
  103.  
  104.   Serial.println("La réponse du serveur était :");
  105.   Serial.println("==========");
  106.   Serial.println(line);
  107.   Serial.println("==========");
  108.   Serial.println("Fermeture de la connexion");
  109. }
  110.  
  111. void loop() {
  112. }
Advertisement
Add Comment
Please, Sign In to add comment