diff options
| author | Sam Anthony <sam@samanthony.xyz> | 2024-11-07 17:02:47 -0500 |
|---|---|---|
| committer | Sam Anthony <sam@samanthony.xyz> | 2024-11-07 17:02:47 -0500 |
| commit | c10cf2f165c313ac4c317a7596bbab0bbc84b602 (patch) | |
| tree | 94e389132e73d49dbf89f290ff8d40aa31750ae8 | |
| parent | 6369bac6e9c62bed3be9395c35f2824b4a8d4904 (diff) | |
| download | soen422-c10cf2f165c313ac4c317a7596bbab0bbc84b602.zip | |
implement SensorStation
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 33 | ||||
| -rw-r--r-- | SensorStation/SensorStation.ino | 94 |
3 files changed, 128 insertions, 0 deletions
@@ -9,3 +9,4 @@ *.out *.run.xml server/server +*/build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..089a0f4 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +SENSOR_SRC = SensorStation/SensorStation.ino +HVAC_SRC = HvacStation/HvacStation.ino + +BOARD = esp32:esp32:lilygo_t_display +PORT = /dev/ttyACM0 + +CFLAGS = -b ${BOARD} +UPLOADFLAGS = -p ${PORT} ${CFLAGS} + +all: SensorStation/build HvacStation/build + +SensorStation/build: ${SENSOR_SRC} + arduino-cli compile ${CFLAGS} SensorStation + echo "" > $@ + @echo done + +upload_sensor: SensorStation/build + arduino-cli upload ${UPLOADFLAGS} SensorStation + @echo done + +HvacStation/build: ${HVAC_SRC} + arduino-cli compile ${CFLAGS} HvacStation + echo "" > $@ + @echo done + +upload_hvac: HvacStation/build + arduino-cli upload ${UPLOADFLAGS} HvacStation + @echo done + +monitor: + arduino-cli monitor -b ${BOARD} -p ${PORT} + +.PHONY: monitor upload_sensor upload_hvac diff --git a/SensorStation/SensorStation.ino b/SensorStation/SensorStation.ino new file mode 100644 index 0000000..6fc5f5b --- /dev/null +++ b/SensorStation/SensorStation.ino @@ -0,0 +1,94 @@ +#include <WiFi.h> +#include <HTTPClient.h> +#include <DHT.h> + +#define nelem(arr) (sizeof(arr) / sizeof(arr[0])) + +enum { DHT_PIN = 21 }; +enum { + SECOND = 1000, + PERIOD = 30*SECOND, +}; + +const char ssid[] = "Pixel_6504"; +const char password[] = "zj3av9sjev7ed8j"; +const char domain[] = "hvac.samanthony.xyz"; +const char humidityPath[] = "/humidity"; +const char targetHumidityPath[] = "/target_humidity"; +const char roomID[] = "SNbeEcs7XVWMEvjeEYgwZnp9XYjToVhh"; + +DHT dht(DHT_PIN, DHT11); // Humidity sensor. + +void +setup(void) { + Serial.begin(9600); + while (!Serial) {} + + WiFi.begin(ssid, password); + Serial.print("Connecting to WiFi..."); + while (WiFi.status() != WL_CONNECTED) { + Serial.print("."); + delay(500); + } + Serial.println(" connected."); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + Serial.print("Initializing DHT11 humidity sensor..."); + dht.begin(); + Serial.println(" done."); +} + +void +loop(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); +} + +int +send(float humidity) { + if (WiFi.status() != WL_CONNECTED) { + Serial.println("WiFi not connected."); + return 1; + } + + WiFiClient client; + HTTPClient http; + + const char *url = humidityURL(humidity); + Serial.printf("POST %s...\n", url); + http.begin(client, url); + int responseCode = http.POST(""); + http.end(); + Serial.printf("HTTP response: %d\n", responseCode); + if (responseCode != HTTP_CODE_OK) + return 1; + return 0; +} + +char * +humidityURL(float humidity) { + static char query[256]; + int n; + + n = snprintf(query, nelem(query), "room=%s&humidity=%.2f", roomID, humidity); + if (n >= nelem(query)) + Serial.println("Humidity query string buffer overflow; truncating."); + return url(domain, humidityPath, query); +} + +char * +url(const char *domain, const char *path, const char *query) { + static char buf[512]; + int n; + + n = snprintf(buf, nelem(buf), "http://%s%s?%s", domain, path, query); + if (n >= nelem(buf)) + Serial.println("URL string buffer overflow; truncating."); + return buf; +} |