-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketch.ino
117 lines (91 loc) · 2.74 KB
/
sketch.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Make sure to install those libraries.
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// Replace these with your actual WiFi credentials
const char* ssid = "Your-WiFi-Network";
const char* password = "Your-WiFi-Password";
// Replace this with the latitude and longitude of your location
const float latitude = 0.00;
const float longitude = 0.000000;
// Make sure to replace "0" with latiude and longitude.
// Replace this with your LCD I2C address
int lcdColumns = 20;
int lcdRows = 4;
int lcdAddr = 0x27; // Change this to your I2C address
LiquidCrystal_I2C lcd(lcdAddr, lcdColumns, lcdRows);
// Flag to indicate which weather information to display
bool displayTemp = true;
void setup() {
Serial.begin(115200);
// Initialize LCD
lcd.init();
lcd.backlight();
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
lcd.clear();
delay(1000);
Serial.println("Connecting to WiFi...");
lcd.print("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
lcd.clear();
// Print IP address
Serial.println(WiFi.localIP());
}
void loop() {
// Fetch weather data
String weatherData = getWeatherData();
// Parse weather data
String weather;
if (displayTemp) {
weather = parseTemperature(weatherData);
} else {
weather = parseWindSpeed(weatherData);
}
// Display weather on LCD
displayOnLCD(weather);
// Toggle display flag every 3 seconds
delay(3000);
displayTemp = !displayTemp;
}
String getWeatherData() {
HTTPClient http;
String weatherData;
// Open Meteo API. Does not requires API key.
String url = "https://api.open-meteo.com/v1/forecast?latitude=" + String(latitude) + "&longitude=" + String(longitude) + "¤t=temperature_2m,wind_speed_10m&hourly=temperature_2m,relative_humidity_2m,wind_speed_10m";
http.begin(url);
int httpCode = http.GET();
if (httpCode > 0) {
if (httpCode == HTTP_CODE_OK) {
weatherData = http.getString();
}
} else {
Serial.println("Error getting weather data");
}
http.end();
return weatherData;
}
String parseTemperature(String weatherData) {
DynamicJsonDocument doc(2048);
deserializeJson(doc, weatherData);
float currentTemp = doc["current"]["temperature_2m"];
String result = "Temp: " + String(currentTemp) + "C";
return result;
}
String parseWindSpeed(String weatherData) {
DynamicJsonDocument doc(2048);
deserializeJson(doc, weatherData);
float currentWindSpeed = doc["current"]["wind_speed_10m"];
String result = "Wind: " + String(currentWindSpeed) + "km/h";
return result;
}
void displayOnLCD(String message) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(message);
}