#include <Wire.h>
#include <WiFi.h>
#include <ArduinoJson.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ThingSpeak.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const char* ssid = "Wokwi-GUEST";
const char* password = "";
String APIKEY = "8c9f6eac52a56ea89b8c36162a6d60c7";
String CityID = "1185241"; // Example City ID
WiFiClient client;
char servername[] = "api.openweathermap.org";
String result;
unsigned long channelID = 2235258;
const char* writeAPIKey = "IU90PCW31HECJ1V5";
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(200);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("Connecting...");
display.display();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
display.print(".");
display.display();
}
display.clearDisplay();
display.setCursor(0, 0);
display.println("Connected to WiFi");
display.display();
delay(1000);
display.clearDisplay();
}
void loop() {
if (client.connect(servername, 80)) {
client.println("GET /data/2.5/weather?id=" + CityID + "&units=metric&APPID=" + APIKEY);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
} else {
Serial.println("connection failed");
Serial.println();
}
while (client.connected() && !client.available())
delay(1);
while (client.connected() || client.available()) {
char c = client.read();
result = result + c;
}
client.stop();
// Parse JSON
DynamicJsonDocument doc(1024);
deserializeJson(doc, result);
String location = doc["name"];
String country = doc["sys"]["country"];
float temperature = doc["main"]["temp"].as<float>();
int humidity = doc["main"]["humidity"];
float windSpeed = doc["wind"]["speed"].as<float>();
// Send data to ThingSpeak
ThingSpeak.begin(client);
ThingSpeak.setField(1, temperature);
ThingSpeak.setField(2, humidity);
ThingSpeak.setField(3, windSpeed);
int httpCode = ThingSpeak.writeFields(channelID, writeAPIKey);
if (httpCode == 200) {
Serial.println("Data sent to ThingSpeak successfully");
} else {
Serial.print("Error sending data to ThingSpeak. HTTP code: ");
Serial.println(httpCode);
}
Serial.println();
Serial.print("Country: ");
Serial.println(country);
Serial.print("Location: ");
Serial.println(location);
Serial.print("Location ID: ");
Serial.println(CityID); // Print the City ID you used
Serial.printf("Temperature: %.2f°C\r\n", temperature);
Serial.printf("Humidity: %d %%\r\n", humidity);
Serial.printf("Wind speed: %.2f m/s\r\n", windSpeed);
display.clearDisplay();
display.setCursor(0, 0);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE);
display.print(" Location: ");
display.print(country);
display.print(" ");
display.println(location);
display.println();
display.setTextColor(SSD1306_WHITE, SSD1306_BLACK);
display.print("Temperature: ");
display.print(temperature, 2);
display.print((char)247);
display.print("C ");
display.print("Humidity: ");
display.print(humidity);
display.println("% ");
display.print("Wind Speed: ");
display.print(windSpeed, 2);
display.display();
delay(60000); // 1 minute delay
}