Components and supplies
40 colored male-male jumper wires
Push Button
ESP32
Grove - OLED Display 0.96"
Apps and platforms
Arduino IDE
OpenWeatherMap
Project description
Code
weather
cpp
1#include <WiFi.h> 2#include <HTTPClient.h> 3#include <Adafruit_GFX.h> 4#include <Adafruit_SSD1306.h> 5#include <ArduinoJson.h> 6 7// WiFi credentials 8const char* ssid = //yourssid; 9const char* password = //yourpsd; 10 11// OpenWeatherMap API 12const char* apiKey = //yourapikey; 13const char* location = //your location; 14const char* apiUrl = //your url; 15 16// OLED display setup 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// Button setup 23const int buttonPin = 0; // GPIO for the button 24int buttonState = 0; 25int lastButtonState = 0; 26int displayMode = 0; // 0: Temperature, 1: Humidity, 2: Wind Speed 27 28// Weather data variables 29float temperature = 0.0; 30int humidity = 0; 31float windSpeed = 0.0; 32 33void setup() { 34 // Initialize serial 35 Serial.begin(115200); 36 37 // Initialize button pin 38 pinMode(buttonPin, INPUT_PULLUP); 39 40 // Initialize OLED display 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 // Connect to WiFi 49 connectToWiFi(); 50 51 // Fetch initial weather data 52 fetchWeatherData(); 53} 54 55void loop() { 56 // Read button state 57 buttonState = digitalRead(buttonPin); 58 59 // Check if the button was pressed 60 if (buttonState == LOW && lastButtonState == HIGH) { 61 displayMode = (displayMode + 1) % 3; // Cycle through modes 62 displayWeatherData(); 63 } 64 lastButtonState = buttonState; 65 66 // Fetch weather data every 10 minutes (600000 ms) 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); // Debugging: Print raw JSON response 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}
Documentation
WIRING DIAGRAM
image_2024-12-24_123030423.png
Comments
Only logged in users can leave comments