1#include <WiFi.h>
2#include <HTTPClient.h>
3#include <Adafruit_GFX.h>
4#include <Adafruit_SSD1306.h>
5#include <ArduinoJson.h>
6
7
8const char* ssid =
9const char* password =
10
11
12const char* apiKey =
13const char* location =
14const char* apiUrl =
15
16
17#define SCREEN_WIDTH 128
18#define SCREEN_HEIGHT 64
19#define OLED_RESET -1
20Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
21
22
23const int buttonPin = 0;
24int buttonState = 0;
25int lastButtonState = 0;
26int displayMode = 0;
27
28
29float temperature = 0.0;
30int humidity = 0;
31float windSpeed = 0.0;
32
33void setup() {
34
35 Serial.begin(115200);
36
37
38 pinMode(buttonPin, INPUT_PULLUP);
39
40
41 if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
42 Serial.println("SSD1306 allocation failed");
43 while (1);
44 }
45 display.clearDisplay();
46 display.display();
47
48
49 connectToWiFi();
50
51
52 fetchWeatherData();
53}
54
55void loop() {
56
57 buttonState = digitalRead(buttonPin);
58
59
60 if (buttonState == LOW && lastButtonState == HIGH) {
61 displayMode = (displayMode + 1) % 3;
62 displayWeatherData();
63 }
64 lastButtonState = buttonState;
65
66
67 static unsigned long lastUpdate = 0;
68 if (millis() - lastUpdate > 600000) {
69 fetchWeatherData();
70 lastUpdate = millis();
71 displayWeatherData();
72 }
73}
74
75void connectToWiFi() {
76 Serial.print("Connecting to WiFi");
77 WiFi.begin(ssid, password);
78
79 while (WiFi.status() != WL_CONNECTED) {
80 delay(1000);
81 Serial.print(".");
82 }
83 Serial.println(" Connected!");
84}
85
86void fetchWeatherData() {
87 if (WiFi.status() == WL_CONNECTED) {
88 HTTPClient http;
89 http.begin(apiUrl);
90
91 int httpCode = http.GET();
92 if (httpCode > 0) {
93 String payload = http.getString();
94 Serial.println(payload);
95
96 StaticJsonDocument<1024> doc;
97 DeserializationError error = deserializeJson(doc, payload);
98
99 if (!error) {
100 temperature = doc["main"]["temp"].as<float>();
101 humidity = doc["main"]["humidity"].as<int>();
102 windSpeed = doc["wind"]["speed"].as<float>();
103 } else {
104 Serial.print("JSON parsing failed: ");
105 Serial.println(error.c_str());
106 }
107 } else {
108 Serial.print("HTTP GET failed: ");
109 Serial.println(http.errorToString(httpCode));
110 }
111 http.end();
112 } else {
113 Serial.println("WiFi not connected");
114 }
115}
116
117void displayWeatherData() {
118 display.clearDisplay();
119
120 switch (displayMode) {
121 case 0:
122 display.setTextSize(1);
123 display.setTextColor(WHITE);
124 display.setCursor(0, 0);
125 display.println("Weather in Karachi");
126 display.setTextSize(2);
127 display.setCursor(0, 20);
128 display.print("Temp: ");
129 display.print(temperature);
130 display.println(" C");
131 break;
132
133 case 1:
134 display.setTextSize(1);
135 display.setTextColor(WHITE);
136 display.setCursor(0, 0);
137 display.println("Weather in Karachi");
138 display.setTextSize(2);
139 display.setCursor(0, 20);
140 display.print("Hum: ");
141 display.print(humidity);
142 display.println(" %");
143 break;
144
145 case 2:
146 display.setTextSize(1);
147 display.setTextColor(WHITE);
148 display.setCursor(0, 0);
149 display.println("Weather in Karachi");
150 display.setTextSize(2);
151 display.setCursor(0, 20);
152 display.print("Wind: ");
153 display.print(windSpeed);
154 display.println(" m/s");
155 break;
156 }
157
158 display.display();
159}
henryfab
a month ago
Very nice project, now you are seeing your parameters in display, you can use Ardubridge to send the data to windguru, awekas, pwsweather... and this kind of sites with few steps more. Regards!