summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2024-11-26 14:41:02 -0500
committerSam Anthony <sam@samanthony.xyz>2024-11-26 14:41:02 -0500
commit5f24b031fa0b10e1cf8fbc7345daa223908e1872 (patch)
tree549cada275bb983cd5eba2f9513f63e4fa31779d
parenta9f5dc13c2aaa26e295f165cb0c141b496d169dc (diff)
downloadsoen422-5f24b031fa0b10e1cf8fbc7345daa223908e1872.zip
HvacStation: post duty cycle to server
-rw-r--r--HvacStation/HvacStation.ino63
1 files changed, 54 insertions, 9 deletions
diff --git a/HvacStation/HvacStation.ino b/HvacStation/HvacStation.ino
index 02b2af9..7d687ac 100644
--- a/HvacStation/HvacStation.ino
+++ b/HvacStation/HvacStation.ino
@@ -2,6 +2,8 @@
#include <HTTPClient.h>
#include <PID_v1.h>
+#define nelem(arr) (sizeof(arr) / sizeof(arr[0]))
+
enum {
SECOND = 1000,
PERIOD = 30*SECOND,
@@ -18,6 +20,7 @@ const char ssid[] = "Pixel_6504";
const char password[] = "zj3av9sjev7ed8j";
const char humidityUrl[] = "http://hvac.samanthony.xyz/humidity";
const char targetUrl[] = "http://hvac.samanthony.xyz/target_humidity";
+const char dutyCycleUrl[] = "http://hvac.samanthony.xyz/duty_cycle";
double pidInput, pidOutput, pidSetpoint;
PID pid(&pidInput, &pidOutput, &pidSetpoint, P, I, D, DIRECT);
@@ -45,24 +48,33 @@ setup(void) {
void
loop(void) {
- static unsigned long lastUpdate = 0; // Last time humidity and target were retrived from server.
static float humidity = 0.0; // Measured humidity of the building.
static float target = 0.0; // Target humidity.
+ static unsigned long lastUpdate = 0; // Last time the server was contacted.
+
+ pidInput = humidity;
+ pidSetpoint = target;
+ pid.Compute();
+ writeSolenoidPin(pidOutput);
unsigned long now = millis();
if (now - lastUpdate > PERIOD) {
- // Retrieve measured and target humidity from the server.
- if (get(humidityUrl, &humidity) != 0 || get(targetUrl, &target) != 0)
- Serial.println("Failed to get humidity from server.");
lastUpdate = now;
+
+ if (get(humidityUrl, &humidity) != 0)
+ Serial.println("Failed to get humidity from server.");
+
+ if (get(targetUrl, &target) != 0)
+ Serial.println("Failed to get target from server.");
+
+ float dc = pidOutput / SOLENOID_WINDOW * 100.0;
+ if (postDuty(dc) != 0)
+ Serial.println("Failed to post duty cycle to server.");
+
Serial.printf("Measured humidity: %.2f%%\n", humidity);
Serial.printf("Target humidity: %.2f%%\n", target);
+ Serial.printf("Duty cycle: %.0f%%\n", dc);
}
-
- pidInput = humidity;
- pidSetpoint = target;
- pid.Compute();
- writeSolenoidPin(pidOutput);
}
// Make a GET request to the server and set *x to the float value that it responds with.
@@ -91,6 +103,39 @@ get(const char *url, float *x) {
return status;
}
+// POST the duty cycle to the server. Return non-zero on error.
+int
+postDuty(float duty) {
+ static char url[512];
+ int n;
+
+ n = snprintf(url, nelem(url), "%s?%.0f", dutyCycleUrl, duty);
+ if (n >= nelem(url))
+ Serial.println("Duty cycle url string buffer overflow; truncating.");
+ return post(url);
+}
+
+// Make a POST request to the server. Return non-zero on error.
+int
+post(const char *url) {
+ if (WiFi.status() != WL_CONNECTED) {
+ Serial.println("WiFi not connected.");
+ return 1;
+ }
+
+ WiFiClient client;
+ HTTPClient http;
+
+ Serial.printf("POST %s\n", url);
+ http.begin(client, url);
+ int responseCode = http.POST("");
+ http.end();
+ Serial.printf("HTTP response code: %d\n", responseCode);
+ if (responseCode != HTTP_CODE_OK)
+ return 1;
+ return 0;
+}
+
// Parse the value of str into *x. Returns non-zero on error.
int
parseFloat(const char *str, float *x) {