Numerous fixes, first working commit.

This commit is contained in:
Anna Rose Wiggins 2020-05-14 18:26:02 +00:00
parent 3569508c88
commit e74fe369e5
3 changed files with 125 additions and 76 deletions

View file

@ -19,31 +19,6 @@ const int OLD_HIGH = 255;
// Gets read from by set_pins().
int pin_states[NUM_PINS] = {0};
void init_serial() {
Serial.begin(9600);
}
void init_pins() {
for (int i = 0; i < NUM_PINS; i++) {
digitalWrite(PIN_MAP[i][0], LOW);
}
}
void setup() {
init_pins();
init_serial();
init_wifi();
}
void loop() {
poll_server();
set_pins();
int elapsed = handle_momentary();
Serial.flush();
delay(REQUEST_RATE - elapsed);
}
// if any momentary pins went high this loop, we
// wait the agreed upon delay and then turn them off.
// if any momentary data input went low, we "reset"
@ -81,20 +56,10 @@ int handle_momentary() {
// poll_server makes the actual HTTP request and handles
// the result. It returns false if an error occurred.
bool poll_server() {
client.begin(transport, WEBHOOK_URL);
int status = client.GET();
if (status < 0) {
Serial.print("Client error communicating with server: ");
Serial.println(status);
return false;
}
if (status <= 400) {
Serial.print("Received HTTP status code ");
Serial.println(status);
return false;
}
parse_webhook_response(client.getString());
String data = FetchURL(WEBHOOK_URL);
if (data == "") return false;
parse_webhook_response(data);
return true;
}
void parse_webhook_response(String raw_data) {
@ -122,8 +87,18 @@ void parse_webhook_response(String raw_data) {
}
}
// Set the pins to the right state, and set them all to high during
// initialization.
void initPins() {
for (int i = 0; i < NUM_PINS; i++) {
pinMode(PIN_MAP[i][0], OUTPUT);
digitalWrite(PIN_MAP[i][0], HIGH);
}
}
// Uses the current pin_states to actually write to the pins
void set_pins() {
void writePins() {
for (int i = 0; i < NUM_PINS; i++) {
if (tripped(i)) {
continue;
@ -139,3 +114,18 @@ void set_pins() {
bool tripped(int i) {
return PIN_MAP[i][1] == 0 && pin_states[i] == OLD_HIGH;
}
void setup() {
Serial.begin(9600);
initPins();
InitWifi();
}
void loop() {
poll_server();
writePins();
int elapsed = handle_momentary();
Serial.flush();
delay(REQUEST_RATE - elapsed);
}