Tutoriel ESP32 43/55 - Station météo Internet IoT | Kit d'apprentissage IoT ESP32 de SunFounder
Na boyekoli oyo, tokotonga station ya meteorologie oyo esengeli na Internet na kosalela ESP32 mpe carte ya extension ya camera na yango ya SunFounder. Projet oyo epesi ESP32 nguya ya kozua ba données ya météo ya sika, na ndenge ya température mpe humidité, mpe kolakisa yango na écran LCD. Application oyo ezali kolakisa te kaka makoki ya ESP32 kasi mpe ndenge ya kozua mpe kososola ba données na API ya libanda.

Ntango tozali kokende liboso na boyekoli oyo, tokosangisa ba composants, tokokonfigurer code, mpe tokomeka ete nyonso esalaka malamu elongo. Sukya ya mosala ekozala station ya météo oyo esalaka malamu mpe oyo ezali kobongisa ba lectures na yango na ntango nyonso ya 10 secondes, mpe ezali kolakisa ba conditions ya météo ya sika na ndenge ya polele mpe ya mokuse (na vidéo na 00:30).
Hardware Explicado
Ba composants ya minene oyo basalelaka na projet oyo ezali na microcontrôleur ESP32, écran LCD, mpe ba fils oyo esengeli. ESP32 ezali na Wi-Fi mpe Bluetooth oyo esalemi na kati, oyo epesaka yango nguya ya kokangama na Internet mpe kozua ba données. Écran LCD ekolakisa ba informations ya météo ya sika, na ndenge ya température mpe humidité.
LCD oyo tozali kosalela ezali écran ya ba caractères 16x2, elingi koloba ete ekoki kolakisa ba lignes mibale ya ba caractères 16 na moko na moko. Oyo ekoki mpo na ba informations na biso ya météo. ESP32 ekoloba na LCD na nzela ya I2C, oyo ezali kosala ete ba fils ezala mokuse mpo ete esalelaka kaka ba lignes mibale ya données.
Ba Détails ya Datasheet
| Fabricant | Espressif |
|---|---|
| Numéro ya pièce | ESP32-WROOM-32 |
| Tension ya logique/IO | 3.3 V |
| Tension ya alimentation | 3.0–3.6 V |
| Courant ya sortie (na canal) | 12 mA max |
| Courant ya pointe (na canal) | 40 mA |
| Guidance ya fréquence PWM | 1 kHz |
| Seuils ya logique ya entrée | 0.3 V (bas), 0.7 V (haut) |
| Chute ya tension / RDS(on) / saturation | 0.5 V |
| Limites thermiques | -40 to 85 °C |
| Paquet | QFN48 |
| Notes / variantes | Ezali na ba variantes ebele mpo na ba applications ndenge na ndenge |
- Kokanisa ete ESP32 ezali na tension ya 3.3 V oyo ezali ya katikati.
- Salela terre commune mpo na ba composants nyonso mpo na kokima ba problèmes ya communication.
- Vérifier adresse I2C ya LCD na kosalela scanner I2C.
- Kotala état ya connexion Wi-Fi mpo na kokima ba déconnexions ntango ozali kozua ba données.
- Kokamba ba erreurs ya parsing JSON mpo na robustesse na kozua ba données.
Instructions ya Wiring

Mpo na kosangisa ba composants, banda na kokangisa LCD na ESP32. LCD ekolakisa interface I2C, yango wana kanga pin SDA ya LCD na GPIO21 na ESP32 mpe pin SCL na GPIO22. Kanisa kokangisa ba pins ya puissance mpe terre ya LCD na ba pins 5V mpe GND na ESP32, na ndenge ya mibale.
Na sima, kanisa ete ESP32 ezali na puissance ya malamu na kosalela batterie ya lithium oyo ezali na kati to connexion USB. Batterie epesaka mobilité, ntango connexion USB ezali malamu mpo na programmation mpe débogage. Na suka, tala ete ba connexions nyonso ezali ya katikati mpo na kokima ba problèmes ya fils oyo ezali ya kopengwa oyo ekoki kobebisa fonctionnement.
Ba Exemples ya Code & Walkthrough
Na phase ya mise en place ya programme na biso, tozali ko-initialiser communication série mpe kokangama na réseau Wi-Fi na kosalela SSID mpe mot de passe oyo epesamaki. Snippet ya code oyo ezali kokamba connexion Wi-Fi:
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connecté na réseau Wi-Fi na adresse IP: ");
Serial.println(WiFi.localIP());
Code oyo ezali kokanisa ete ESP32 ekokangama na réseau Wi-Fi oyo epesamaki liboso ya kokende liboso na kozua ba données. Soki connexion ekomi te, ekozala na essai ya kokangama lisusu na ntango nyonso.
Na sima, tosengeli kotinda demande HTTP GET mpo na kozua ba données ya météo. Oyo esalemaka na kosalela snippet ya code oyo:
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&units=metric" + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Awa, tozali kotonga URL mpo na demande API, oyo ezali na engumba, code ya mboka, mpe clé API na biso. Fonction httpGETRequest ekomelamaka na sima mpo na kozua ba données ya météo.
Na suka, tozali kososola réponse JSON mpe kolakisa ba données oyo esengeli na LCD:
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(time);
lcd.print(" ");
lcd.print(myObject["weather"][0]["main"]);
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(myObject["main"]["temp"]);
lcd.print("\xDF"); // "°" char
lcd.print("C ");
lcd.print("H:");
lcd.print(myObject["main"]["humidity"]);
lcd.print("%");
Snippet ya code oyo ezali kobongisa écran LCD na ntango ya sika, condition ya météo, température, mpe humidité. Ezali kolimbola affichage ya liboso mpe kotia curseur na ba positions oyo esengeli mpo na ligne na ligne.
Démonstration / Nini Okokanisa
Soki wiring mpe programmation esalami malamu, station ya météo na yo ekokangama na Wi-Fi mpe ebanda kozua ba données ya météo na ntango nyonso ya 10 secondes. Okomona température ya sika, humidité, mpe ba conditions ya météo kolakisa na écran LCD. Soki ESP32 ekoki te kokangama na Wi-Fi, ekolakisa message ya erreur na moniteur série.
Keba ngala na ba ngambo ya API mpo na kokima epekisami na OpenWeatherMap. Soki okutani na mikakatano na kozua ba données, tala API key na yo mpe tia ete engumba na yo mpe code ya mboka ezali ya solo (na vidéo na 15:45).
Ba Timestamps ya Vidéo
- 00:00 Ebandeli
- 2:00 Bokambi ya projet
- 5:04 Compte OpenWeather
- 6:11 Bokangisi ya ba fils
- 8:05 Ndimbola ya code Arduino
- 14:13 Ba éléments JASON na code
- 20:23 Kopona carte ESP32 mpe port COM na Arduino IDE
- 22:05 Ndimbola ya Station Météo na LCD1602
- 23:45 Ndimbola ya Station Météo na LCD2004
/*
Rui Santos
Complete project details at Complete project details at https://RandomNerdTutorials.com/esp32-http-get-open-weather-map-thingspeak-arduino/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino_JSON.h>
// Replace the next variables with your SSID/Password combination
const char* ssid = "SSID";
const char* password = "PASSWORD";
// Your Domain name with URL path or IP address with path
String openWeatherMapApiKey = "openWeatherMapApiKey";
// Replace with your country code and city
// Fine the country code by https://openweathermap.org/find
String city = "CITY";
String countryCode = "COUNTRY CODE";
// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES
// For a final application, check the API call limits per hour/minute to avoid getting blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 10000;
String jsonBuffer;
// set the LCD number of columns and rows
int lcdColumns = 16;
int lcdRows = 2;
// set LCD address, number of columns and rows
// SDA -> GPIO21, SCL -> GPIO22
// lcd address is 0x27, run an I2C scanner sketch
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
// NTP Server time
const char* ntpServer = "pool.ntp.org";
long gmtOffset_sec = 0;
int daylightOffset_sec = 0; //3600;
void setup() {
Serial.begin(115200);
// WiFi
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before publishing the first reading.");
// initialize LCD
lcd.init();
// turn on LCD backlight
lcd.backlight();
}
void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&units=metric" + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);
// JSON.typeof(jsonVar) can be used to get the type of the var
if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}
// Serial.print("JSON object = ");
// Serial.println(myObject);
// Serial.print("Temperature: ");
// Serial.println(myObject["main"]["temp"]);
// Serial.print("Pressure: ");
// Serial.println(myObject["main"]["pressure"]);
// Serial.print("Humidity: ");
// Serial.println(myObject["main"]["humidity"]);
// Serial.print("Wind Speed: ");
// Serial.println(myObject["wind"]["speed"]);
// Get time
gmtOffset_sec = myObject["timezone"];
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
// time =
String time = printLocalTime();
// LCD Print
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(time);
lcd.print(" ");
lcd.print(myObject["weather"][0]["main"]);
lcd.setCursor(0, 1);
lcd.print("T:");
lcd.print(myObject["main"]["temp"]);
lcd.print("\xDF"); // "°" char
lcd.print("C ");
lcd.print("H:");
lcd.print(myObject["main"]["humidity"]);
lcd.print("%");
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}
String printLocalTime(){
struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return "null";
}
// Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
// get time format HH:MM
char timeHour[3];
strftime(timeHour,3, "%H", &timeinfo);
char timeMinute[3];
strftime(timeMinute,3, "%M", &timeinfo);
String time = String(timeHour) + ":" + String(timeMinute);
Serial.println(time)
; return time;
}
String httpGETRequest(const char* serverName) {
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Send HTTP POST request
int httpResponseCode = http.GET();
String payload = "{}";
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
return payload;
}
Common Course Links
Common Course Files
Mokili na bisika
-
DokumentasiESP32 Tutorial 43/55- SunFounder doc page for IoT Internet Weather Stationdocs.sunfounder.com
Fichiers📁
Aucun fichier disponible.