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+

Elite Explorer Kit

You can also buy them separately from the links below.

COMPONENT INTRODUCTION

PURCHASE LINK

Arduino Uno R4 WiFi

-

Jumper Wires

BUY

OLED Display Module

BUY

Wiring

../_images/06_weather_oled_bb.png

Schematic

../_images/06_weather_oled_schematic.png

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.

  1. Visit OpenWeather to log in/create an account.

    ../_images/06_owm_1.png
  2. Click into the API page from the navigation bar.

    ../_images/06_owm_2.png
  3. Find Current Weather Data and click Subscribe.

    ../_images/06_owm_3.png
  4. Under Current weather and forecasts collection, subscribe to the appropriate service. In our project, Free is good enough.

    ../_images/06_owm_4.png
  5. Copy the Key from the API keys page.

    ../_images/06_owm_5.png
  6. 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>"
    
  7. Set the time zone of your location.

    Take the capital of Sweden, Stockholm, as an example. Search “stockholm timezone” on Google.

    ../_images/06_weather_oled_01.png

    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 of elite-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?

  1. Libraries and Definitions:

    1. WiFiS3.h: This is likely a library specific to a certain WiFi module or board to manage WiFi connections.

    2. ArduinoJson.h: This library is used for decoding (and encoding) JSON data.

    3. 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.

    4. NTPClient & WiFiUdp: These are used for fetching the current time from an NTP (Network Time Protocol) server.

    5. Adafruit libraries: Used for managing the OLED display.

    6. Various global variables: These include WiFi credentials, server details, and more, which will be used throughout the script.

  2. setup():

    1. It initializes the serial communication.

    2. Checks and prints the WiFi module’s firmware version.

    3. Tries to connect to the WiFi network using the provided SSID and password.

    4. Prints the connected WiFi’s status (SSID, IP, Signal strength).

    5. Initializes the OLED display.

    6. 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).

  3. read_response():

    1. Reads the response from the server, specifically looking for JSON data (denoted by { and }).

    2. If JSON data is found, it decodes the data to extract weather details like temperature, humidity, pressure, wind speed, and wind direction.

    3. Calls the displayWeatherData function to display the weather information on the OLED screen.

  4. httpRequest():

    1. Closes any existing connections to ensure the WiFi module’s socket is free.

    2. Tries to connect to the OpenWeatherMap server.

    3. If connected, sends an HTTP GET request to fetch the weather data for a specific location defined by LOCATION (likely defined in arduino_secrets.h or elsewhere).

    4. Records the time the request was made.

  5. loop():

    1. Calls the read_response function to process any incoming data from the server.

    2. Updates the time from the NTP server.

    3. Checks if it’s time to make another request to the weather server (based on the postingInterval). If so, it calls the httpRequest function.

  6. printWifiStatus():

    1. The SSID of the connected network.

    2. The local IP address of the board.

    3. The signal strength (RSSI).

  7. displayWeatherData():

    1. Clears the OLED screen.

    2. Displays the current day of the week.

    3. Displays the current time in HH:MM format.

    4. Displays the provided weather data (temperature, humidity, pressure, and wind speed).