UNO R4 WiFi Weather Dashboard
In this demo, we use the UNO R4 WiFi's built-in LED matrix to create a scrolling weather dashboard powered by the free WeatherAPI.com service. No extra sensors. No shields. Just Wi-Fi, JSON, and professional-grade debugging.
Components and supplies
1
Arduino R4 WIFI
Apps and platforms
1
Visual Studio 2019
1
Visual Micro
1
Visual Studio 2022
1
Visual Studio 2017
Project description
Code
WeatherAPI.ino
cpp
1/* -------------------- WeatherAPI.com fetch -------------------- */ 2void fetchWeather() { 3 if (!client.connect(host, 80)) { 4 Serial.println("HTTP connect failed"); 5 return; 6 } 7 8 String url = "/v1/current.json?key=" + apiKey + "&q=" + city + "&aqi=no"; 9 client.println("GET " + url + " HTTP/1.1"); 10 client.println("Host: " + String(host)); 11 client.println("Connection: close"); 12 client.println(); 13 14 String payload; 15 while (client.connected() || client.available()) { 16 if (client.available()) { 17 payload += client.readStringUntil('\n'); 18 } 19 } 20 client.stop(); 21 22 // BREAKPOINT: inspect payload 23 // Watch: payload 24 25 int jsonStart = payload.indexOf('{'); 26 if (jsonStart < 0) { 27 Serial.println("No JSON found."); 28 return; 29 } 30 31 DynamicJsonDocument doc(2048); 32 DeserializationError err = deserializeJson(doc, payload.substring(jsonStart)); 33 if (err) { 34 Serial.print("JSON parse error: "); 35 Serial.println(err.c_str()); 36 return; 37 } 38 39 tempC = doc["current"]["temp_c"].as<float>(); 40 conditionText = doc["current"]["condition"]["text"].as<String>(); 41 weatherText = "T:" + String(tempC, 1) + "C " + conditionText + " "; 42 43 // TRACEPOINT: Weather updated: {tempC}C 44 // Watch: tempC, conditionText, weatherText 45 46 Serial.println(weatherText.c_str()); 47 48 49}
LEDMatrix.ino
cpp
1/* -------------------- LED Matrix Text Draw -------------------- */ 2void showScrolling(const String& s) { 3 matrix.beginDraw(); 4 matrix.textFont(Font_5x7); 5 matrix.textScrollSpeed(75); // ms per pixel 6 matrix.beginText(0, 1, 0xFFFFFF); 7 matrix.print(" "); 8 matrix.print(s); 9 matrix.print(" "); 10 matrix.endText(SCROLL_LEFT); 11 matrix.endDraw(); 12}
Secrets.h
cpp
1#pragma once 2// Add the API Key from WeatherAPI.com 3#define API_KEY "xxxxxxxxxxxxxxxxxxxxxxx" 4// Add the Credentials for your WiFi Network 5#define WIFI_SSID "xxxxxxxx" 6#define WIFI_PASS "xxxxxxxx"
WeatherDashboard.ino
cpp
Main Sketch
1/* 2 UNO R4 WiFi - Weather Dashboard (WeatherAPI.com) 3 Scrolls temperature + condition on the 12x8 LED matrix. 4 5 Included in this Solution:- 6 Board Package: 7 Arduino Renesas UNO R4 Boards v 1.5.1 8 9 External Libraries Included in this Solution: 10 ArduinoGraphics v1.1.4 11 ArduinoJson v7.4.2 12 13Debug Options (No Additional Hardware Needed!):- 14 Serial Debugger (+Plot Charts) 15 Hardware Debugger 16*/ 17 18#include <ArduinoJson.hpp> 19#include "ArduinoGraphics.h" 20#include "Arduino_LED_Matrix.h" 21#include <WiFiS3.h> 22#include <ArduinoJson.h> 23#include "Secrets.h" 24 25ArduinoLEDMatrix matrix; 26 27const char* ssid = WIFI_SSID; 28const char* pass = WIFI_PASS; 29 30// WeatherAPI endpoint (free tier) 31const char* host = "api.weatherapi.com"; 32String apiKey = API_KEY; 33String city = "Birmingham"; 34 35// Data fields 36String weatherText = "Starting... "; 37float tempC = 0.0; 38String conditionText; 39 40unsigned long lastFetch = 0; 41const unsigned long FETCH_INTERVAL_MS = 5UL * 60UL * 1000UL; // 5 min 42 43WiFiClient client; 44 45void setup() { 46 Serial.begin(115200); 47 matrix.begin(); 48 49#if VM_DEBUG_GDB 50 delay(5000); // Allow GDB to attach if in Debug Mode 51#endif 52 53 Serial.print("Connecting to Wi-Fi"); 54 WiFi.begin(ssid, pass); 55 56 while (WiFi.status() != WL_CONNECTED) { 57 Serial.print("."); 58 delay(500); 59 } 60 Serial.println("\nConnected!"); 61 showScrolling("Wi-Fi OK "); 62 63 fetchWeather(); // initial 64} 65 66void loop() { 67 if (millis() - lastFetch >= FETCH_INTERVAL_MS) { 68 fetchWeather(); 69 } 70 else { 71 // Just keep showing the last information we received repeatedly 72 showScrolling(weatherText); 73 lastFetch = millis(); 74 } 75 delay(1000); 76}
Comments
Only logged in users can leave comments