Note
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
Why Join?
Expert Support: Solve post-sale issues and technical challenges with help from our community and team.
Learn & Share: Exchange tips and tutorials to enhance your skills.
Exclusive Previews: Get early access to new product announcements and sneak peeks.
Special Discounts: Enjoy exclusive discounts on our newest products.
Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [here] and join today!
WeatherTime Screen
This sketch connects to a WiFi network, fetches weather data from OpenWeatherMap every minute, retrieves the current time from an NTP server, and displays the day, time, and weather information on an OLED screen.
Required Components
In this project, we need the following components.
It’s definitely convenient to buy a whole kit, here’s the link:
Name |
ITEMS IN THIS KIT |
LINK |
---|---|---|
Elite Explorer Kit |
300+ |
You can also buy them separately from the links below.
COMPONENT INTRODUCTION |
PURCHASE LINK |
---|---|
- |
|
Wiring
Schematic
OpenWeather
Get OpenWeather API keys
OpenWeather is an online service, owned by OpenWeather Ltd, that provides global weather data via API, including current weather data, forecasts, nowcasts and historical weather data for any geographical location.
Visit OpenWeather to log in/create an account.
Click into the API page from the navigation bar.
Find Current Weather Data and click Subscribe.
Under Current weather and forecasts collection, subscribe to the appropriate service. In our project, Free is good enough.
Copy the Key from the API keys page.
Copy it to the
arduino_secrets.h
.#define SECRET_SSID "<SSID>" // your network SSID (name) #define SECRET_PASS "<PASSWORD>" // your network password #define API_KEY "<OpenWeather_API_KEY>" #define LOCATION "<YOUR CITY>"
Set the time zone of your location.
Take the capital of Sweden, Stockholm, as an example. Search “stockholm timezone” on Google.
In the search results, you will see “GMT+1”, so you set the parameter of the function below to
3600 * 1
seconds.timeClient.setTimeOffset(3600 * 1); // Adjust for your time zone (this is +1 hour)
Install the Library
To install the library, use the Arduino Library Manager and search for “ArduinoMqttClient”, “FastLED”, “Adafruit GFX” and “Adafruit SSD1306” and install them.
ArduinoJson.h
: Used for handling JSON data (data obtained from openweathermap).
NTPClient.h
: Used for obtaining real-time time.
Adafruit_GFX.h
, Adafruit_SSD1306.h
: Used for OLED module.
Run the Code
Note
You can open the file
06_weather_oled.ino
under the path ofelite-explorer-kit-main\iot_project\06_weather_oled
directly.Or copy this code into Arduino IDE.
Note
In the code, SSID and password are stored in arduino_secrets.h
. Before uploading this example, you need to modify them with your own WiFi credentials. Additionally, for security purposes, ensure that this information is kept confidential when sharing or storing the code.
How it works?
Libraries and Definitions:
WiFiS3.h
: This is likely a library specific to a certain WiFi module or board to manage WiFi connections.ArduinoJson.h
: This library is used for decoding (and encoding) JSON data.arduino_secrets.h
: A separate file where sensitive data (like WiFi credentials) are stored. This is a good practice to keep credentials out of the main code.NTPClient & WiFiUdp: These are used for fetching the current time from an NTP (Network Time Protocol) server.
Adafruit libraries: Used for managing the OLED display.
Various global variables: These include WiFi credentials, server details, and more, which will be used throughout the script.
setup()
:It initializes the serial communication.
Checks and prints the WiFi module’s firmware version.
Tries to connect to the WiFi network using the provided SSID and password.
Prints the connected WiFi’s status (SSID, IP, Signal strength).
Initializes the OLED display.
Starts the NTP client to fetch the current time and sets a time offset (in this case, 8 hours which might correspond to a specific timezone).
read_response()
:Reads the response from the server, specifically looking for JSON data (denoted by
{
and}
).If JSON data is found, it decodes the data to extract weather details like temperature, humidity, pressure, wind speed, and wind direction.
Calls the
displayWeatherData
function to display the weather information on the OLED screen.
httpRequest()
:Closes any existing connections to ensure the WiFi module’s socket is free.
Tries to connect to the OpenWeatherMap server.
If connected, sends an HTTP GET request to fetch the weather data for a specific location defined by
LOCATION
(likely defined inarduino_secrets.h
or elsewhere).Records the time the request was made.
loop()
:Calls the
read_response
function to process any incoming data from the server.Updates the time from the NTP server.
Checks if it’s time to make another request to the weather server (based on the
postingInterval
). If so, it calls thehttpRequest
function.
printWifiStatus()
:The SSID of the connected network.
The local IP address of the board.
The signal strength (RSSI).
displayWeatherData()
:Clears the OLED screen.
Displays the current day of the week.
Displays the current time in HH:MM format.
Displays the provided weather data (temperature, humidity, pressure, and wind speed).