1// REQUIRES the following Arduino libraries:
2// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
3// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor
4
5#include <Adafruit_Sensor.h>
6#include <DHT.h>
7#include <DHT_U.h>
8#include <ItemValue.h>
9#include <LcdMenu.h>
10#include <MenuScreen.h>
11#include <display/LiquidCrystal_I2CAdapter.h>
12#include <input/KeyboardAdapter.h>
13#include <renderer/CharacterDisplayRenderer.h>
14
15#define LCD_ROWS 2
16#define LCD_COLS 16
17
18#define DHTPIN 12 // Digital pin connected to the DHT sensor
19#define DHTTYPE DHT22 // DHT 22 (AM2302)
20
21float temp = 0.0;
22
23// Initialize the main menu items
24// clang-format off
25MENU_SCREEN(mainScreen, mainItems,
26 ITEM_VALUE("Temp", temp, "%.1fC"),
27 ITEM_BASIC("Placeholder 1"),
28 ITEM_BASIC("Placeholder 2"),
29 ITEM_BASIC("Placeholder 3"));
30// clang-format on
31
32LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
33CharacterDisplayRenderer renderer(new LiquidCrystal_I2CAdapter(&lcd), LCD_COLS, LCD_ROWS);
34LcdMenu menu(renderer);
35KeyboardAdapter keyboard(&menu, &Serial);
36DHT_Unified dht(DHTPIN, DHTTYPE);
37
38uint16_t dhtReadInterval;
39
40void setup() {
41 Serial.begin(9600);
42 dht.begin();
43 renderer.begin();
44 menu.setScreen(mainScreen);
45
46 // Get the sensor details
47 sensor_t sensor;
48 dht.temperature().getSensor(&sensor);
49 dhtReadInterval = sensor.min_delay / 1000;
50
51 Serial.print(F("DHT22 read interval: "));
52 Serial.println(dhtReadInterval);
53}
54
55void readTemperature() {
56 // Get temperature after the read interval
57 static unsigned long lastReadTime = 0;
58 if (millis() - lastReadTime > dhtReadInterval) {
59 sensors_event_t event;
60 dht.temperature().getEvent(&event);
61 if (event.temperature) {
62 temp = event.temperature;
63
64 Serial.print(F("Temperature: "));
65 Serial.println(temp);
66 }
67 lastReadTime = millis();
68 }
69}
70
71void loop() {
72 keyboard.observe();
73 menu.poll(dhtReadInterval - 100); // Polling is required for ITEM_VALUE
74 readTemperature();
75}