Advertisement
pleasedontcode

Weather Station

Jul 22nd, 2024
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.50 KB | Source Code | 0 0
  1. #include <Wire.h>
  2. #include <Adafruit_Sensor.h>
  3. #include <DHT.h>
  4. #include <Adafruit_BMP085.h>
  5. #include <WiFi.h>
  6. #include <Adafruit_GFX.h>
  7. #include <Adafruit_SSD1306.h>
  8.  
  9. #define DHTPIN 4
  10. #define DHTTYPE DHT22
  11. #define SCREEN_WIDTH 128
  12. #define SCREEN_HEIGHT 64
  13.  
  14. const char* ssid = "your_SSID";
  15. const char* password = "your_PASSWORD";
  16.  
  17. DHT dht(DHTPIN, DHTTYPE);
  18. Adafruit_BMP085 bmp;
  19. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
  20.  
  21. float previousPressure = 0.0;
  22.  
  23. void setup() {
  24.   Serial.begin(115200);
  25.   WiFi.begin(ssid, password);
  26.  
  27.   while (WiFi.status() != WL_CONNECTED) {
  28.     delay(1000);
  29.     Serial.println("Connecting to WiFi...");
  30.   }
  31.   Serial.println("Connected to WiFi");
  32.  
  33.   dht.begin();
  34.   if (!bmp.begin()) {
  35.     Serial.println("Could not find a valid BMP085 sensor, check wiring!");
  36.     while (1) {}
  37.   }
  38.  
  39.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  40.     Serial.println(F("SSD1306 allocation failed"));
  41.     while (1) {}
  42.   }
  43.   display.clearDisplay();
  44. }
  45.  
  46. void loop() {
  47.   float temperature = dht.readTemperature();
  48.   float humidity = dht.readHumidity();
  49.   float pressure = bmp.readPressure() / 100.0F;  // Convert to hPa
  50.  
  51.   if (isnan(temperature) || isnan(humidity) || isnan(pressure)) {
  52.     Serial.println("Failed to read from sensors!");
  53.     return;
  54.   }
  55.  
  56.   Serial.print("Temperature: ");
  57.   Serial.print(temperature);
  58.   Serial.println(" *C");
  59.   Serial.print("Humidity: ");
  60.   Serial.print(humidity);
  61.   Serial.println(" %");
  62.   Serial.print("Pressure: ");
  63.   Serial.print(pressure);
  64.   Serial.println(" hPa");
  65.  
  66.   String weatherPrediction = predictWeather(pressure);
  67.  
  68.   display.clearDisplay();
  69.   display.setTextSize(1);
  70.   display.setTextColor(SSD1306_WHITE);
  71.   display.setCursor(0,0);
  72.   display.print("Temp: "); display.print(temperature); display.println(" *C");
  73.   display.print("Hum: "); display.print(humidity); display.println(" %");
  74.   display.print("Pres: "); display.print(pressure); display.println(" hPa");
  75.   display.print("Prediction: ");
  76.   display.println(weatherPrediction);
  77.   display.display();
  78.  
  79.   previousPressure = pressure;
  80.  
  81.   delay(60000);  // Delay for 1 minute
  82. }
  83.  
  84. String predictWeather(float currentPressure) {
  85.   if (previousPressure == 0.0) {
  86.     previousPressure = currentPressure;
  87.     return "Gathering data...";
  88.   }
  89.  
  90.   if (currentPressure > previousPressure) {
  91.     return "Improving weather";
  92.   } else if (currentPressure < previousPressure) {
  93.     return "Worsening weather";
  94.   } else {
  95.     return "Stable weather";
  96.   }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement