diff options
| -rw-r--r-- | SensorStation/SensorStation.ino | 68 |
1 files changed, 65 insertions, 3 deletions
diff --git a/SensorStation/SensorStation.ino b/SensorStation/SensorStation.ino index 4d9de86..46f792d 100644 --- a/SensorStation/SensorStation.ino +++ b/SensorStation/SensorStation.ino @@ -4,7 +4,12 @@ #define nelem(arr) (sizeof(arr) / sizeof(arr[0])) -enum { DHT_PIN = 21 }; +enum pins { + DHT_PIN = 21, + + UP_BTN = 16, + DOWN_BTN = 17, +}; enum { SECOND = 1000, PERIOD = 30*SECOND, @@ -16,11 +21,18 @@ const char domain[] = "hvac.samanthony.xyz"; const char humidityPath[] = "/humidity"; const char targetHumidityPath[] = "/target_humidity"; const char roomID[] = "SNbeEcs7XVWMEvjeEYgwZnp9XYjToVhh"; +const float defaultTarget = 35.0; // Default target humidity. +const float minTarget = 0.0; // Minimum target humidity. +const float maxTarget = 100.0; // Maximum target humidity. +const float incTarget = 0.5; // Target humidity increment from button press. DHT dht(DHT_PIN, DHT11); // Humidity sensor. void setup(void) { + pinMode(UP_BTN, INPUT); + pinMode(DOWN_BTN, INPUT); + Serial.begin(9600); while (!Serial) {} @@ -35,19 +47,41 @@ setup(void) { Serial.println(WiFi.localIP()); Serial.print("Initializing DHT11 humidity sensor..."); + delay(1*SECOND); /* Let sensor stabilize after power-on. */ dht.begin(); Serial.println(" done."); } void loop(void) { + static float targetHumidity = defaultTarget; + static unsigned long lastSample = 0; // Last time humidity was measured. + + unsigned long now = millis(); + if (now - lastSample > PERIOD) { + measureHumidity(); + lastSample = now; + } + + if (upButton() && targetHumidity+incTarget <= maxTarget) { + targetHumidity += incTarget; + Serial.printf("Up. Target humidity: %.2f%%\n", targetHumidity); + /* TODO: send target to server. */ + } else if (downButton() && targetHumidity-incTarget >= minTarget) { + targetHumidity -= incTarget; + Serial.printf("Down. Target humidity: %.2f%%\n", targetHumidity); + /* TODO: send target to server. */ + } +} + +// Measure the humidity and send it to the server. +void +measureHumidity(void) { float humidity = dht.readHumidity(); Serial.printf("Humidity: %.2f %% RH\n", humidity); if (send(humidity) != 0) Serial.println("Failed to send humidity to server."); - - delay(PERIOD); } // Send the measured humidity to the server. @@ -95,3 +129,31 @@ url(const char *domain, const char *path, const char *query) { Serial.println("URL string buffer overflow; truncating."); return buf; } + +// Return true if the UP button was pressed. +bool +upButton(void) { + static bool state = LOW; + return buttonPressed(UP_BTN, &state); +} + +// Return true if the DOWN button was pressed. +bool +downButton(void) { + static bool state = LOW; + return buttonPressed(DOWN_BTN, &state); +} + +// Return true if the button connected to the specified pin was pressed. +bool +buttonPressed(int pin, bool *lastState) { + if (digitalRead(pin)) { + if (*lastState == LOW) { // Rising event. + *lastState = HIGH; + return true; + } + } else { // Falling event. + *lastState = LOW; + } + return false; +} |