Search Code

ESP32 பயிற்சி 43/55 - IoT இணைய வானிலை நிலையம் | SunFounder இன் ESP32 IoT கற்றல் கிட்

ESP32 பயிற்சி 43/55 - IoT இணைய வானிலை நிலையம் | SunFounder இன் ESP32 IoT கற்றல் கிட்

இந்த பயிற்சியில், ESP32 மற்றும் SunFounder இன் கேமரா நீட்டிப்பு பலகையைப் பயன்படுத்தி இணையத்துடன் இணைக்கப்பட்ட வானிலை நிலையத்தை உருவாக்குவோம். இந்த திட்டம் ESP32 வெப்பநிலை மற்றும் ஈரப்பதம் உள்ளிட்ட நிகழ்நேர வானிலை தரவைப் பெறவும், அதை LCD திரையில் காண்பிக்கவும் உதவுகிறது. இந்த பயன்பாடு ESP32 இன் திறன்களை வெளிப்படுத்துவது மட்டுமல்லாமல், வெளிப்புற API இலிருந்து தரவைப் பெறுவது மற்றும் பாகுபடுத்துவது எப்படி என்பதையும் நிரூபிக்கிறது.

பயிற்சியில் முன்னேறும்போது, கூறுகளை இணைப்போம், குறியீட்டை உள்ளமைப்போம், மேலும் எல்லாம் சீராக ஒன்றாகச் செயல்படுவதை உறுதி செய்வோம். இறுதி முடிவு ஒரு முழுமையான செயல்பாட்டு வானிலை நிலையமாக இருக்கும், இது ஒவ்வொரு 10 வினாடிகளுக்கும் அதன் அளவீடுகளைப் புதுப்பித்து, தற்போதைய வானிலை நிலைமைகளின் தெளிவான மற்றும் சுருக்கமான காட்சியை வழங்கும் (வீடியோவில் 00:30 இல்).

வன்பொருள் விளக்கம்

இந்த திட்டத்தில் பயன்படுத்தப்படும் முக்கிய கூறுகளில் ESP32 மைக்ரோகண்ட்ரோலர், LCD காட்சி மற்றும் தேவையான வயரிங் ஆகியவை அடங்கும். ESP32 உள்ளமைக்கப்பட்ட Wi-Fi மற்றும் புளூடூத் கொண்டுள்ளது, இது இணையத்துடன் இணைக்கவும் தரவைப் பெறவும் அனுமதிக்கிறது. LCD காட்சி வெப்பநிலை மற்றும் ஈரப்பதம் உள்ளிட்ட தற்போதைய வானிலை தகவலைக் காண்பிக்கும்.

நாங்கள் பயன்படுத்தும் LCD என்பது 16x2 எழுத்து காட்சியாகும், அதாவது இது ஒவ்வொன்றும் 16 எழுத்துகள் கொண்ட இரண்டு வரிகளைக் காட்ட முடியும். இது எங்கள் வானிலை தகவல் வெளியீட்டிற்கு போதுமானதாக இருக்கும். ESP32 I2C வழியாக LCD உடன் தொடர்பு கொள்ளும், இது இரண்டு தரவு கோடுகளை மட்டுமே பயன்படுத்துவதன் மூலம் வயரிங்கை எளிதாக்குகிறது.

தரவுத்தாள் விவரங்கள்

உற்பத்தியாளர்Espressif
பகுதி எண்ESP32-WROOM-32
லாஜிக்/IO மின்னழுத்தம்3.3 V
வழங்கல் மின்னழுத்தம்3.0–3.6 V
வெளியீட்டு மின்னோட்டம் (ஒரு சேனலுக்கு)12 mA அதிகபட்சம்
உச்ச மின்னோட்டம் (ஒரு சேனலுக்கு)40 mA
PWM அதிர்வெண் வழிகாட்டுதல்1 kHz
உள்ளீட்டு லாஜிக் வரம்புகள்0.3 V (குறைந்த), 0.7 V (உயர்)
மின்னழுத்த வீழ்ச்சி / RDS(on) / செறிவு0.5 V
வெப்ப வரம்புகள்-40 முதல் 85 °C
பேக்கேஜ்QFN48
குறிப்புகள் / மாறுபாடுகள்வெவ்வேறு பயன்பாடுகளுக்கு பல மாறுபாடுகளை உள்ளடக்கியது

  • ESP32 நிலையான 3.3 V வழங்கலுடன் இயக்கப்படுவதை உறுதி செய்யவும்.
  • தொடர்பு சிக்கல்களைத் தவிர்க்க அனைத்து கூறுகளுக்கும் பொதுவான தரையைப் பயன்படுத்தவும்.
  • I2C ஸ்கேனரைப் பயன்படுத்தி LCD இன் I2C முகவரியைச் சரிபார்க்கவும்.
  • தரவு பெறுதலின் போது துண்டிப்புகளைத் தவிர்க்க Wi-Fi இணைப்பு நிலையைக் கண்காணிக்கவும்.
  • தரவு மீட்டெடுப்பில் வலிமைக்காக JSON பாகுபடுத்தல் பிழைகளைக் கையாளவும்.

வயரிங் வழிமுறைகள்

esp32-43-weather_station-wiring

கூறுகளை இணைக்க, LCD ஐ ESP32 உடன் இணைப்பதன் மூலம் தொடங்கவும். LCD I2C இடைமுகத்தைப் பயன்படுத்தும், எனவே LCD இன் SDA பின்னை ESP32 இல் GPIO21 உடன் இணைக்கவும், SCL பின்னை GPIO22 உடன் இணைக்கவும். LCD இன் மின்சாரம் மற்றும் தரை பின்களை முறையே ESP32 இல் உள்ள 5V மற்றும் GND பின்களுடன் இணைக்க மறக்காதீர்கள்.

அடுத்து, சேர்க்கப்பட்ட லித்தியம் பேட்டரி அல்லது USB இணைப்பைப் பயன்படுத்தி ESP32 சரியாக இயக்கப்படுவதை உறுதி செய்யவும். பேட்டரி பெயர்வுத்திறனை வழங்குகிறது, அதே நேரத்தில் USB இணைப்பு நிரலாக்க மற்றும் பிழைத்திருத்தத்திற்கு பயனுள்ளதாக இருக்கும். இறுதியாக, செயல்பாட்டை சீர்குலைக்கக்கூடிய தளர்வான வயரிங் சிக்கல்களைத் தவிர்க்க அனைத்து இணைப்புகளும் பாதுகாப்பானவை என்பதைச் சரிபார்க்கவும்.

குறியீடு எடுத்துக்காட்டுகள் & விளக்கம்

எங்கள் நிரலின் அமைவு கட்டத்தில், தொடர் தொடர்பைத் துவக்கி, வழங்கப்பட்ட SSID மற்றும் கடவுச்சொல்லைப் பயன்படுத்தி Wi-Fi நெட்வொர்க்குடன் இணைக்கிறோம். பின்வரும் குறியீடு பகுதி 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());

இந்த குறியீடு ESP32 எந்த தரவு பெறுதலுடன் தொடர்வதற்கு முன் குறிப்பிட்ட Wi-Fi நெட்வொர்க்குடன் இணைப்பதை உறுதி செய்கிறது. இணைப்பு தோல்வியுற்றால், அது தொடர்ந்து மீண்டும் இணைக்க முயற்சிக்கும்.

அடுத்து, வானிலை தரவைப் பெற HTTP GET கோரிக்கையை அனுப்ப வேண்டும். இது பின்வரும் குறியீடு பகுதியைப் பயன்படுத்தி செய்யப்படுகிறது:

String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," + countryCode + "&units=metric" + "&APPID=" + openWeatherMapApiKey;
jsonBuffer = httpGETRequest(serverPath.c_str());

இங்கே, API கோரிக்கைக்கான URL ஐ உருவாக்குகிறோம், இதில் நகரம், நாட்டு குறியீடு மற்றும் எங்கள் API விசை ஆகியவை அடங்கும். பின்னர் வானிலை தரவைப் பெற httpGETRequest செயல்பாடு அழைக்கப்படுகிறது.

இறுதியாக, JSON பதிலைப் பாகுபடுத்தி தொடர்புடைய தரவை 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("%");

இந்த குறியீடு பகுதி தற்போதைய நேரம், வானிலை நிலை, வெப்பநிலை மற்றும் ஈரப்பதத்துடன் LCD காட்சியைப் புதுப்பிக்கிறது. இது முந்தைய காட்சியை அழித்து, ஒவ்வொரு வரிக்கும் பொருத்தமான நிலைகளில் கர்சரை அமைக்கிறது.

நிரூபணம் / என்ன எதிர்பார்க்கலாம்

வெற்றிகரமான வயரிங் மற்றும் நிரலாக்கத்திற்குப் பிறகு, உங்கள் வானிலை நிலையம் Wi-Fi உடன் இணைந்து ஒவ்வொரு 10 வினாடிகளுக்கும் வானிலை தரவைப் பெறத் தொடங்கும். தற்போதைய வெப்பநிலை, ஈரப்பதம் மற்றும் வானிலை நிலைமைகள் LCD திரையில் காட்டப்படுவதைக் காண்பீர்கள். ESP32 Wi-Fi உடன் இணைக்கத் தவறினால், அது தொடர் மானிட்டரில் பிழை செய்தியை அச்சிடும்.

<ப>API அழைப்பு வரம்புகளை கவனத்தில் கொள்ளுங்கள், OpenWeatherMap சேவையிலிருந்து தடுக்கப்படுவதை தவிர்க்க. தரவு மீட்டெடுப்பில் ஏதேனும் சிக்கல்களை சந்தித்தால், உங்கள் API விசையை சரிபார்த்து, உங்கள் நகரம் மற்றும் நாட்டு குறியீடு சரியாக குறிப்பிடப்பட்டுள்ளதா என்பதை உறுதிசெய்யவும் (வீடியோவில் 15:45 இல்).

வீடியோ நேரக்குறியீடுகள்

  • 00:00 தொடக்கம்
  • 2:00 திட்ட அறிமுகம்
  • 5:04 OpenWeather கணக்கு
  • 6:11 வயரிங்
  • 8:05 Arduino குறியீடு விளக்கப்பட்டது
  • 14:13 குறியீட்டில் JASON உறுப்புகள்
  • 20:23 Arduino IDE இல் ESP32 போர்டு மற்றும் COM போர்ட்டை தேர்ந்தெடுத்தல்
  • 22:05 LCD1602 இல் வானிலை நிலையத்தின் செயல்விளக்கம்
  • 23:45 LCD2004 இல் வானிலை நிலையத்தின் செயல்விளக்கம்

படங்கள்

esp32-43-weather_station-main
esp32-43-weather_station-main
esp32-43-weather_station-wiring
esp32-43-weather_station-wiring
844-ESP32 Tutorial 43/55- Arduino code for IoT Internet Weather Station
மொழி: C++
/*
  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;
}

வளங்கள் மற்றும் மேற்கோள்கள்

கோப்புகள்📁

கோப்புகள் கிடைக்கவில்லை.