Èkọ́ 43/55 ESP32 - Ibi Ìwò Oju-ọjọ́ Internet IoT | Àkójọpọ̀ Ẹ̀kọ́ IoT ESP32 ti SunFounder
Nínú ẹ̀kọ́ yìí, a ó kọ́ ibùdókọ̀ ojú-ọjọ́ tí ó ní ìsopọ̀ pẹ̀lú ayélujára nípa lílo ESP32 àti àwọ̀n kàmẹ́rà rẹ̀ láti ọ̀dọ̀ SunFounder. Iṣẹ́ yìí jẹ́ kí ESP32 gba àwọn ìsọfúnni ojú-ọjọ́ ní àkókò gidi, pẹ̀lú ìwọ̀n-otútù àti ọ̀rinrin, kí ó sì fi wọ́n hàn lórí ojú-ìkọ̀kọ̀ LCD. Ẹ̀kọ́ yìí kì í ṣe àfihàn agbára ESP32 nìkan, ṣùgbọ́n ó tún ṣàfihàn bí a ṣe ń gba àti ṣe àtúpalẹ̀ dàtà láti API òde.

Bí a ṣe ń tẹ̀síwájú nínú ẹ̀kọ́ yìí, a ó so àwọn ẹ̀yà pọ̀, ṣe ìtòlẹ́tà kóòdù, kí a sì rí i pé ohun gbogbo ń ṣiṣẹ́ papọ̀ dáradára. Àbájáde rẹ̀ yóò jẹ́ ibùdókọ̀ ojú-ọjọ́ tí ó ṣiṣẹ́ pátápátá tí yóò máa ṣe àtúnṣe àwọn ìwọ̀n rẹ̀ ní gbogbo ìṣẹ́jú 10, tí yóò sì fi àwọn ipò ojú-ọjọ́ lọ́wọ́lọ́wọ́ hàn kedere (nínú fídíò ní 00:30).
Àlàyé Àwọn Ẹ̀yà
Àwọn ẹ̀yà pàtàkì tí a lò nínú iṣẹ́ yìí ni ESP32 microcontroller, ojú-ìkọ̀kọ̀ LCD, àti àwọn okùn tí a nílò. ESP32 ní Wi-Fi àti Bluetooth tí a kọ́ sínú rẹ̀, tí ó jẹ́ kí ó ní ìsopọ̀ pẹ̀lú ayélujára àti gba dàtà. Ojú-ìkọ̀kọ̀ LCD yóò fi àwọn ìsọfúnni ojú-ọjọ́ lọ́wọ́lọ́wọ́ hàn, pẹ̀lú ìwọ̀n-otútù àti ọ̀rinrin.
LCD tí a ń lò jẹ́ ojú-ìkọ̀kọ̀ ohun-kikọ 16x2, èyí tí ó túmọ̀ sí pé ó lè fi ìlà méjì hàn, ọ̀kọ̀ọ̀kan ní ohun-kikọ 16. Èyí yóò tó fún ìjàpẹ̀ ojú-ọjọ́ wa. ESP32 yóò bá LCD sọ̀rọ̀ nípasẹ̀ I2C, èyí tí ó rọrùn ìsopọ̀ nítorí pé ó ń lo ìlà dàtà méjì péré.
Àwọn Àlàyé Datasheet
| Olùṣelọ́pọ̀ | Espressif |
|---|---|
| Nọ́mbà apá | ESP32-WROOM-32 |
| Fóltíìjì Logic/IO | 3.3 V |
| Fóltíìjì ìpèsè | 3.0–3.6 V |
| Ìwọ̀n-ìtújáde (fún ikanni kọ̀ọ̀kan) | 12 mA tó pọ̀jù |
| Ìwọ̀n-ìtújáde tó ga jù (fún ikanni kọ̀ọ̀kan) | 40 mA |
| Ìtọ́sọ́nà ìgbohùnsáfẹ́fẹ́ PWM | 1 kHz |
| Àwọn ìpele ìwọ̀n-ìkóhùn | 0.3 V (rẹ̀lẹ̀), 0.7 V (ga) |
| Ìdínkù fóltíìjì / RDS(on) / ìkúnra | 0.5 V |
| Àwọn ìwọ̀n-ìgbóná | -40 sí 85 °C |
| Àkójọpọ̀ | QFN48 |
| Àwọn àkíyèsí / àwọn oríṣiríṣi | Ó ní ọ̀pọ̀lọpọ̀ oríṣiríṣi fún àwọn ìlò tí ó yàtọ̀ |
- Rí i pé a fi fóltíìjì 3.3 V tí ó dúró ṣinṣin pèsè agbára fún ESP32.
- Lo ilẹ̀ kan náà fún gbogbo àwọn ẹ̀yà láti yẹra fún àwọn ìṣòro ìbánisọ̀rọ̀.
- Ṣàyẹ̀wò àdírẹ́sì I2C ti LCD nípa lílo ọ̀múntún-ìwò I2C.
- Mọ́jútó ipò ìsopọ̀ Wi-Fi láti yẹra fún àwọn ìpínkúrùn nígbà tí a ń gba dàtà.
- Mú àwọn àṣìṣe ìtúpalẹ̀ JSON lọ́wọ́ fún ìmúdúró nínú gbígba dàtà.
Àwọn Ìtọ́sọ́nà Ìsopọ̀

Láti so àwọn ẹ̀yà pọ̀, bẹ̀rẹ̀ nípa sísopọ̀ LCD sí ESP32. LCD yóò lo ojú-ìsopọ̀ I2C, nítorí náà so pín SDA ti LCD sí GPIO21 lórí ESP32 àti pín SCL sí GPIO22. Rí i pé o so àwọn pín agbára àti ilẹ̀ ti LCD sí àwọn pín 5V àti GND lórí ESP32, ní ọ̀nà tí ó yẹ.
Lẹ́yìn náà, rí i pé a fi agbára pèsè fún ESP32 dáradára nípa lílo bàtì lítiọ́mù tí ó wà nínú rẹ̀ tàbí ìsopọ̀ USB. Bàtì náà ń pèsè ìgbé-ẹ̀rù, nígbà tí ìsopọ̀ USB wúlò fún kíkọ kóòdù àti ṣíṣàtúnṣe àṣìṣe. Ní ìparí, ṣàyẹ̀wò gbogbo àwọn ìsopọ̀ láti rí i pé wọ́n dúró ṣinṣin láti yẹra fún àwọn ìṣòro ìsopọ̀ tí ó lè dá ìṣiṣẹ́ lẹ́kùn.
Àwọn Àpẹẹrẹ Kóòdù & Ìtọ́sọ́nà
Nínú ìpele ìmúràdà ètò wa, a ń bẹ̀rẹ̀ ìbánisọ̀rọ̀ serial àti ìsopọ̀ pẹ̀lú nẹ́tíwọ́kì Wi-Fi nípa lílo SSID àti ọ̀rọ̀ìbaniọ̀rọ̀ tí a pèsè. Àpẹẹrẹ kóòdù tí ó tẹ̀lé yìí ń ṣe ìsopọ̀ Wi-Fi:
WiFi.begin(ssid, password);
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
Kóòdù yìí rí i pé ESP32 ní ìsopọ̀ pẹ̀lú nẹ́tíwọ́kì Wi-Fi tí a yàn kí ó tó tẹ̀síwájú pẹ̀lú gbígba dàtà. Bí ìsopọ̀ bá kùnà, yóò máa gbìyànjú láti tún ṣe ìsopọ̀ nígbà gbogbo.
Lẹ́yìn náà, a nílò láti fi ìbéèrè HTTP GET ránṣẹ́ láti gba dàtà ojú-ọjọ́. A ṣe èyí nípa lílo àpẹẹrẹ kóòdù tí ó tẹ̀lé:
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&units=metric" + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());
Níhìn-ín, a ń kọ́ URL fún ìbéèrè API, èyí tí ó ní ìlú, kóòdù orílẹ̀-èdè, àti bọ́tìnnì API wa. A ó pe iṣẹ́ httpGETRequest láti gba dàtà ojú-ọjọ́.
Ní ìparí, a tú JSON dàtà náà, a sì fi àwọn ìsọfúnni tí ó yẹ hàn lórí 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("%");
Àpẹẹrẹ kóòdù yìí ń ṣe àtúnṣe ojú-ìkọ̀kọ̀ LCD pẹ̀lú àkókò lọ́wọ́lọ́wọ́, ipò ojú-ọjọ́, ìwọ̀n-otútù, àti ọ̀rinrin. Ó ń pa àwọn ohun tí ó hàn tẹ́lẹ̀ rẹ́, ó sì ń fi kọ̀sọ́ sí àwọn ipò tí ó tọ́ fún ìlà kọ̀ọ̀kan.
Ìṣàfihàn / Ohun Tí O Lè Retí
Lẹ́yìn ìsopọ̀ àti kíkọ kóòdù tí ó yàn, ibùdókọ̀ ojú-ọjọ́ rẹ yóò ní ìsopọ̀ pẹ̀lú Wi-Fi, yóò sì bẹ̀rẹ̀ sí gba dàtà ojú-ọjọ́ ní gbogbo ìṣẹ́jú 10. Ìwọ yóò rí ìwọ̀n-otútù lọ́wọ́lọ́wọ́, ọ̀rinrin, àti àwọn ipò ojú-ọjọ́ tí a fi hàn lórí ojú-ìkọ̀kọ̀ LCD. Bí ESP32 bá kùnà láti ní ìsopọ̀ pẹ̀lú Wi-Fi, yóò tẹ̀ ìròyìn àṣìṣe sí ojú-ìwò serial.
Ṣọ́ra fún àwọn ààlà ìpè API láti má báa dínà kúrò ní iṣẹ́ OpenWeatherMap. Bí o bá pàdé àwọn ìṣòro pẹ̀lú gbígba data, ṣàyẹ̀wò bọ́tìnnì API rẹ kí o sì rí i dájú pé àkọsílẹ̀ ìlú àti kóòdù orílẹ̀-èdè rẹ tọ́ (nínú fídíò ní 15:45).
Àwọn Àsìkò Fídíò
- 00:00 Ìbẹ̀rẹ̀
- 2:00 Ìfihàn sí iṣẹ́-àgbè
- 5:04 Àkọọ́lẹ̀ OpenWeather
- 6:11 Ìsopọ̀ oníná
- 8:05 Àlàyé kóòdù Arduino
- 14:13 Àwọn Ẹ̀yà JASON nínú kóòdù
- 20:23 Yíyàn bọ́tìnnì ESP32 àti èbúté COM nínú Arduino IDE
- 22:05 Ìfihàn Ilé-Ìwò Ojúọ̀jọ́ lórí LCD1602
- 23:45 Ìfihàn Ilé-Ìwò Ojúọ̀jọ́ lórí 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
Orisun & itọkasi
-
Iwe-ẹriESP32 Tutorial 43/55 - SunFounder doc page for IoT Internet Weather Stationdocs.sunfounder.com
Fáìlìs📁
Ko si awọn faili ti o wa.