#include <Adafruit_SSD1306.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <WiFi.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HTTPClient http;
const String weatherURL = "https://api.openweathermap.org/data/2.5/weather?q=Izhevsk&mode=json&units=metric&appid=008085524361f23db75cbf869044bbba";
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Don't proceed, loop forever
}
display.display();
delay(1000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.println("Connecting to WiFi...");
display.display();
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
display.print("Connected to ");
display.println("Wokwi-GUEST");
display.display();
delay(5000);
}
void loop() {
if (WiFi.status() == WL_CONNECTED)
{
http.begin(weatherURL);
int httpCode = http.GET();
Serial.print("HTTP Code: ");
Serial.println(httpCode);
if (httpCode > 0)
{
StaticJsonDocument<768> doc;
DeserializationError error = deserializeJson(doc, http.getString());
if (error) {
Serial.print(F("deserializeJson failed: "));
Serial.println(error.f_str());
delay(2500);
return;
}
String weather = doc["weather"][0]["main"];
String temperature = doc["main"]["temp"];
String feeltemp = doc["main"]["feels_like"];
display.clearDisplay();
display.setTextSize(1);
printCenter("Izhevsk", 0, 0);
display.setTextSize(1);
printCenter("Curr weather: " + weather, 0, 15);
display.setTextSize(1);
printCenter("Curr temp: " + temperature, 0, 30);
display.setTextSize(1);
printCenter("Feels like: " + feeltemp, 0, 45);
display.display();
}
delay(600000);
}
}
void printCenter(const String buf, int x, int y)
{
int16_t x1, y1;
uint16_t w, h;
display.getTextBounds(buf, x, y, &x1, &y1, &w, &h); //calc width of new string
display.setCursor((x - w / 2) + (128 / 2), y);
display.print(buf);
}
Loading
esp32-c3-devkitm-1
esp32-c3-devkitm-1