====== Basic Meteo Station ====== ===== Summary ===== A basic station giving temperature and humidity using a DHT22 sensor and a 0.91 OLED display {{projets:arduino_thermometer_light.jpg?500}} ===== Wiring ===== {{projets:am2302_temp_station_bb.png?500}} ===== Code ===== === Upgrades to implement === Sleep mode === Prototype 1 === Designed to run on Attiny85 chip === Prototype 0 === ++++ Basic Arduino UNO Code with Serial Coms | // Project based on a DHT22 Temperature & Humidity Sensor // and a 0.91 OLED 128 x 32 display // Uses various libs written by Adafruit // Released under an MIT license // Project created by mh8 // REQUIRES the following Arduino libraries: // - Adafruit DHT Sensor Library // - Adafruit Unified Sensor Lib // - Adafruit SSD 1306 Lib // - Wire & SPI // - Adafruit GFX Lib #include #include #include #include #include #include #include // DHT creation #define DHTPIN 2 // Digital pin connected to the DHT sensor #define DHTTYPE DHT22 // DHT 22 (AM2302) DHT_Unified dht(DHTPIN, DHTTYPE); uint32_t delayMS; // OLED creation #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); void setup() { Serial.begin(9600); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for (;;); // Don't proceed, loop forever } // Init DHT device. dht.begin(); Serial.println(F("DHTxx Unified Sensor Example")); // Print temperature sensor details. sensor_t sensor; dht.temperature().getSensor(&sensor); Serial.println(F("------------------------------------")); Serial.println(F("Temperature Sensor")); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C")); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C")); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C")); Serial.println(F("------------------------------------")); // Print humidity sensor details. dht.humidity().getSensor(&sensor); Serial.println(F("Humidity Sensor")); Serial.print (F("Sensor Type: ")); Serial.println(sensor.name); Serial.print (F("Driver Ver: ")); Serial.println(sensor.version); Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id); Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%")); Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%")); Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%")); Serial.println(F("------------------------------------")); // Set delay between sensor readings based on sensor details. delayMS = sensor.min_delay / 1000; } void loop() { // Delay between measurements. delay(delayMS); // Declare some variables float t; float h; // Get temperature event and print its value. sensors_event_t event; dht.temperature().getEvent(&event); if (isnan(event.temperature)) { Serial.println(F("Error reading temperature!")); } else { Serial.print(F("Temperature: ")); Serial.print(event.temperature); Serial.println(F("°C")); t = event.temperature; } // Get humidity event and print its value. dht.humidity().getEvent(&event); if (isnan(event.relative_humidity)) { Serial.println(F("Error reading humidity!")); } else { Serial.print(F("Humidity: ")); Serial.print(event.relative_humidity); Serial.println(F("%")); h = event.temperature; } if (!isnan(event.temperature) && !isnan(event.relative_humidity)) { writeText(t, h); } } void writeText(float t, float h) { String t_str; String h_str; display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(WHITE); // Draw white text display.setCursor(0, 2); // Start at top-left corner display.println(F("Temp. : ")); t_str = String(t); display.setCursor(70, 2); display.print(t); display.setCursor(100,2); display.print(F(" C")); display.setCursor(0, 20); display.println(F("Hum. : ")); display.setCursor(70, 20); h_str = String(h); display.print(h); display.setCursor(100,20); display.print(F(" %")); display.display(); } ++++ ===== Power Consumption ===== Prototype draws 3.2mA average and spikes at 4.5mA on an Arduino Uno with Serial Connection activated and lighting the screen 100% of the time and with no deep sleep implemented. ===== Potential Upgrades ===== *Build a lightweight version running an attiny with a pushbutton to display the data *Build a 3D casing for it *Check autonomy on a battery *Add an RTC for timestamping the data *Swap the UNO for an upgraded card with WiFi connectivity and upload the data to a server *Make graphs from uploaded data