Hardware components | ||||||
| × | 1 | ||||
| × | 1 | ||||
| × | 1 | ||||
Software apps and online services | ||||||
| ||||||
|
Outdoor Weather Display:
By using esp32 and OLED screen we can display the weather forecast, Time, date and location of certain area. To create this weather station follow the given guidelines.
i.) Making connections:
OLED display has four pins; GND, VCC, SCL and SDA. Connect GND to GND pin of esp32 board, VCC will be connected to 5v pin SCL is to be connected to pin 22 and SDA is attached to pin 21 of an esp32 board respectively.
ii.) Getting API key and city id:
API key can be obtained from the website of https://openweathermap.org/ you need to log in and follow the step as shown in figure;
a.) API key:
b.) city id:
copy the city id and API key those are to be pasted in line number 14 and 15 of source code.
Code for this project.
C/C++ATTENTION!!!
-
In line Number 21 the value 19620 is according to my location which is to be changed as per your GMT. Just google your GMT and multiply it by 3600 which is value for GMT +1 and replace it with your value.
-
In line number 6 library of NTP is to be included that you need to download in zip file and include it. You can download it from : https://codeload.github.com/taranais/NTPClient/zip/refs/heads/master
-
In line number 11, 12 , 13 and 14 fill the credentials properly as directed. That's all.
-
In line Number 21 the value 19620 is according to my location which is to be changed as per your GMT. Just google your GMT and multiply it by 3600 which is value for GMT +1 and replace it with your value.
-
In line number 6 library of NTP is to be included that you need to download in zip file and include it. You can download it from : https://codeload.github.com/taranais/NTPClient/zip/refs/heads/master
-
In line number 11, 12 , 13 and 14 fill the credentials properly as directed. That's all.
#include <WiFi.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
const char* ssid = "your-ssid";
const char* password = "your wifi password";
String APIKEY = "copy it from open weather";
String CityID = "copy it from open weather";
bool id = false;
WiFiClient client;
char servername[] = "api.openweathermap.org";
String result;
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "my.pool.ntp.org", 19620, 60000); // kathmandu is GMT +5.45 so 19620 is the value. Just multiply your GMT with 3600 and you will get your value.
String formattedDate;
String dayStamp;
String timeStamp;
void setup() {
Serial.begin(115200);
Serial.print("Connecting to ");
WiFi.mode(WIFI_STA);
Serial.println(ssid);
WiFi.begin(ssid, password);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(200);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Connecting.");
display.display();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.print(".");
display.display();
}
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connected ");
display.println("IP Address: ");
display.println(WiFi.localIP());
display.display();
delay(1000);
display.clearDisplay();
timeClient.begin();
}
void loop()
{
timeClient.update();
if (client.connect(servername, 80))
{
client.println("GET /data/2.5/weather?id=" + CityID + "&units=metric&APPID=" + APIKEY);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
}
else {
Serial.println("connection failed");
Serial.println();
}
while (client.connected() && !client.available())
delay(1);
while (client.connected() || client.available())
{
char c = client.read();
result = result + c;
}
client.stop();
result.replace('[', ' ');
result.replace(']', ' ');
char jsonArray [result.length() + 1];
result.toCharArray(jsonArray, sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';
StaticJsonDocument<1024> doc;
DeserializationError error = deserializeJson(doc, jsonArray);
if (error) {
Serial.print(F("deserializeJson() failed with code "));
Serial.println(error.c_str());
return;
}
String location = doc["name"];
String country = doc["sys"]["country"];
int temperature = doc["main"]["temp"];
int humidity = doc["main"]["humidity"];
float pressure = doc["main"]["pressure"];
int id = doc["id"];
float Speed = doc["wind"]["speed"];
int degree = doc["wind"]["deg"];
float longitude = doc["coord"]["lon"];
float latitude = doc["coord"]["lat"];
Serial.println();
Serial.print("Country: ");
Serial.println(country);
Serial.print("Location:");
Serial.println(location);
Serial.print("Location ID: ");
Serial.println(id);
Serial.printf("Temperature: %d°C\r\n", temperature);
Serial.printf("Humidity: %d %%\r\n", humidity);
Serial.printf("Pressure: %.2f hpa\r\n", pressure);
Serial.printf("Wind speed: %.1f m/s\r\n", Speed);
Serial.printf("Wind degree: %d°\r\n", degree);
Serial.printf("Longitude: %.2f\r\n", longitude);
Serial.printf("Latitude: %.2f\r\n", latitude);
formattedDate = timeClient.getFormattedDate();
Serial.println(formattedDate);
int splitT = formattedDate.indexOf("T");
dayStamp = formattedDate.substring(0, splitT);
Serial.print("DATE: ");
Serial.println(dayStamp);
timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
Serial.print("HOUR: ");
Serial.println(timeStamp);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print("Location:");
display.print(country);
display.print(" ");
display.println(location);
display.println();
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
display.print("T: ");
display.print(temperature);
display.print((char)247);
display.print("C ");
display.print("H: ");
display.print(humidity);
display.println("% ");
display.print("Pressure:");
display.print(pressure);
display.println("hpa");
display.print("WS: ");
display.print(Speed);
display.print("m/s ");
display.print("WA: ");
display.print(degree);
display.println((char)247);
display.print("Lat: ");
display.print(latitude);
display.print(" ");
display.print("Lon: ");
display.println(longitude);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print("Date: ");
display.println(dayStamp);
display.print("Time: ");
display.println(timeStamp);
display.display();
delay(6000);
}
Comments