Advertisement
Makertronic

tempoe sp32

Jan 1st, 2024 (edited)
2,815
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.07 KB | Source Code | 0 0
  1. /*
  2. *****************************************************************************
  3. *
  4. *                        Tempo EDF ESP32 LED
  5. *
  6. *****************************************************************************
  7.  
  8. Il vous faut : 6 led pour le projet (2 bleu, 2 blanches, 2 rouges),
  9.                2 résistances de 220 ohms,
  10.                1 ESP32 S1,
  11.                1 breadboard,
  12.                des fils de connexion.
  13.  
  14. Change ton mot de passe wifi et ton ssid dans le code( ligne 32 et 33).
  15.  
  16. Vous pouvez aussi passer par un écran LCD pour afficher la couleur du jour et de demain.
  17. Deux leds RGB ca ferais aussi bien le job.
  18. Merci de regarder ma petite chaine =)
  19.  
  20. * Web : https://www.makertronic-yt.com
  21. * version : 1.2
  22. * Copyright 2024
  23. */
  24.  
  25. // include des librairies
  26. #include <Arduino.h>
  27. #include <WiFi.h>
  28. #include "time.h"
  29. #include <HTTPClient.h>
  30. #include <ArduinoJson.h>
  31.  
  32. // connexion wifi
  33. const char* ssid = "xxxxxxxxxxxxxx";
  34. const char* password = "xxxxxxxxxxxxx";
  35.  
  36. // serveur NTP
  37. const char* ntpServer = "pool.ntp.org";
  38. const long  gmtOffset_sec = 3600 * 1;
  39. const int   daylightOffset_sec = 3600 * 0;
  40.  
  41. // leds
  42. const int ledJourBleu = 4;
  43. const int ledJourBlanc = 16;
  44. const int ledJourRouge = 17;
  45. const int ledDemainBleu = 25;
  46. const int ledDemainBlanc = 26;
  47. const int ledDemainRouge = 27;
  48.  
  49. void setup(){
  50.     Serial.begin(9600);
  51.     delay(1000);
  52.  
  53.     WiFi.mode(WIFI_STA); //Optional
  54.     WiFi.begin(ssid, password);
  55.     Serial.println("\nConnection Wifi ...");
  56.  
  57.     while(WiFi.status() != WL_CONNECTED){
  58.         Serial.print(".");
  59.         delay(100);
  60.     }
  61.  
  62.     Serial.println("\nConnecté au réseau Wifi !");
  63.     Serial.print("IP locale: ");
  64.     Serial.println(WiFi.localIP());
  65.  
  66.     // On configure le seveur NTP
  67.     configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  68.  
  69.     // leds en sortie
  70.     pinMode(ledJourBleu, OUTPUT);
  71.     pinMode(ledJourBlanc, OUTPUT);
  72.     pinMode(ledJourRouge, OUTPUT);
  73.     pinMode(ledDemainBleu, OUTPUT);
  74.     pinMode(ledDemainBlanc, OUTPUT);
  75.     pinMode(ledDemainRouge, OUTPUT);
  76. }
  77.  
  78.  
  79. void loop() {
  80.  
  81.   // recup du jour de l'année format AAAA/MM/JJ
  82.   struct tm timeinfo;
  83.   if (!getLocalTime(&timeinfo)) {
  84.     Serial.println("Erreur de recup de l'heure");
  85.   }
  86.  
  87.   char strftime_buf[20];
  88.   strftime(strftime_buf, sizeof(strftime_buf), "%Y-%m-%d", &timeinfo);
  89.   //Serial.println(strftime_buf); // debug date du jour
  90.  
  91.   // Construire l'URL avec la date formatée
  92.   String url = "https://particulier.edf.fr/services/rest/referentiel/searchTempoStore?dateRelevant=" + String(strftime_buf);
  93.   Serial.println(url); // debug url
  94.  
  95.   // Effectuer la requête HTTP
  96.   HTTPClient http;
  97.   http.begin(url);
  98.  
  99.   int httpCode = http.GET();
  100.   if (httpCode > 0) {
  101.     if (httpCode == HTTP_CODE_OK) {
  102.       String payload = http.getString();
  103.  
  104.       // Parse JSON avec ArduinoJSON
  105.       const size_t capacity = JSON_OBJECT_SIZE(2) + 100;
  106.       DynamicJsonDocument doc(capacity);
  107.       deserializeJson(doc, payload);
  108.  
  109.       // Extraire les valeurs
  110.       const char* couleurJourJ = doc["couleurJourJ"];
  111.       const char* couleurJourJ1 = doc["couleurJourJ1"];
  112.  
  113.       // DEBUG
  114.       //Serial.print("CouleurJourJ: ");
  115.       //Serial.println(couleurJourJ);
  116.       //Serial.print("CouleurJourJ1: ");
  117.       //Serial.println(couleurJourJ1);
  118.  
  119.       // Affichage des LEDs du jour
  120.       Serial.print("CouleurJourJ: ");
  121.       if (strcmp(couleurJourJ, "TEMPO_BLEU") == 0) {
  122.         Serial.println("bleu");
  123.         digitalWrite(ledJourBleu, HIGH);
  124.         digitalWrite(ledJourBlanc, LOW);
  125.         digitalWrite(ledJourRouge, LOW);
  126.       }
  127.       if (strcmp(couleurJourJ, "TEMPO_BLANC") == 0) {
  128.         Serial.println("blanc");
  129.         digitalWrite(ledJourBleu, LOW);
  130.         digitalWrite(ledJourBlanc, HIGH);
  131.         digitalWrite(ledJourRouge, LOW);
  132.       }
  133.       if (strcmp(couleurJourJ, "TEMPO_ROUGE") == 0) {
  134.         Serial.println("rouge");
  135.         digitalWrite(ledJourBleu, LOW);
  136.         digitalWrite(ledJourBlanc, LOW);
  137.         digitalWrite(ledJourRouge, HIGH);
  138.       }
  139.  
  140.       // Affichage des LEDs du lendemain
  141.       Serial.print("CouleurJourJ1: ");
  142.       if (strcmp(couleurJourJ1, "TEMPO_BLEU") == 0) {
  143.         Serial.println("bleu");
  144.         digitalWrite(ledDemainBleu, HIGH);
  145.         digitalWrite(ledDemainBlanc, LOW);
  146.         digitalWrite(ledDemainRouge, LOW);
  147.       }
  148.       if (strcmp(couleurJourJ1, "TEMPO_BLANC") == 0) {
  149.         Serial.println("blanc");
  150.         digitalWrite(ledDemainBleu, LOW);
  151.         digitalWrite(ledDemainBlanc, HIGH);
  152.         digitalWrite(ledDemainRouge, LOW);
  153.       }
  154.       if (strcmp(couleurJourJ1, "TEMPO_ROUGE") == 0) {
  155.         Serial.println("rouge");
  156.         digitalWrite(ledDemainBleu, LOW);
  157.         digitalWrite(ledDemainBlanc, LOW);
  158.         digitalWrite(ledDemainRouge, HIGH);
  159.       }
  160.      
  161.     } // fin if (httpCode == HTTP_CODE_OK) {
  162.  
  163.   } else {
  164.     Serial.println("Erreur de connexion au serveur EDF");
  165.   } //if (httpCode > 0) {
  166.  
  167.   http.end();
  168.  
  169.   delay(300000); // Attendre 5 minutes avant de recommencer
  170.   //delay(10000); // Attendre 10 secondes avant de recommencer (pour debug)
  171.  
  172. }
  173.  
  174.  
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement