|
| 1 | +#include <Bridge.h> |
| 2 | +#include <BridgeClient.h> |
| 3 | + |
| 4 | +#include "Arduino-Websocket/WebSocketClient.h" |
| 5 | + |
| 6 | +#include "vor_utils.h" |
| 7 | +#include "vor_env.h" |
| 8 | +#include "vor_led.h" |
| 9 | +#include "vor_motion.h" |
| 10 | +#include "vor_methane.h" |
| 11 | + |
| 12 | +#define MAX_ATTEMPTS 10 |
| 13 | +#define INTERVAL 30000 |
| 14 | +#define HEARTBEAT_INTERVAL 25000 |
| 15 | + |
| 16 | +#define MOTION_PIN 2 |
| 17 | +#define METHANE_PIN A0 |
| 18 | + |
| 19 | +#define MESSAGE_FORMAT "42[\"message\",{\"id\":\"toilet8am\",\"type\":\"toilet\",\"reserved\":%s,\"methane\":%s}]" |
| 20 | + |
| 21 | +BridgeClient client; |
| 22 | +WebSocketClient ws(SERVER_URL); |
| 23 | + |
| 24 | +VorLed led; |
| 25 | +VorMotion motion(MOTION_PIN); |
| 26 | +VorMethane methane(METHANE_PIN); |
| 27 | + |
| 28 | +int prevMotionValue = HIGH; |
| 29 | + |
| 30 | +uint64_t heartbeatTimestamp = 0; |
| 31 | +uint64_t intervalTimestamp = 0; |
| 32 | + |
| 33 | +uint8_t attempts = 0; |
| 34 | +bool wifiRestarted = false; |
| 35 | +bool lininoRebooted = false; |
| 36 | + |
| 37 | +void setup() { |
| 38 | + Serial.begin(115200); |
| 39 | + bridge(); |
| 40 | +} |
| 41 | + |
| 42 | +void loop() { |
| 43 | + if (client.connected()) { |
| 44 | + uint64_t now = millis(); |
| 45 | + int motionValue = motion.read(); |
| 46 | + float methaneValue = methane.readProcessed(); |
| 47 | + bool change = prevMotionValue != motionValue; |
| 48 | + prevMotionValue = motionValue; |
| 49 | + bool reservedValue = motionValue == LOW; |
| 50 | + if (change || now - intervalTimestamp > INTERVAL) { |
| 51 | + intervalTimestamp = now; |
| 52 | + |
| 53 | + const char* reserved = reservedValue ? "true" : "false"; |
| 54 | + char methane[8]; |
| 55 | + dtostrf(methaneValue, 8, 2, methane); |
| 56 | + char message[128]; |
| 57 | + sprintf(message, MESSAGE_FORMAT, reserved, methane); |
| 58 | + ws.sendData(message); |
| 59 | + } |
| 60 | + if ((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) { |
| 61 | + heartbeatTimestamp = now; |
| 62 | + ws.sendData("2"); |
| 63 | + // TODO: check server response |
| 64 | + } |
| 65 | + } else { |
| 66 | + led.turnOn(); |
| 67 | + Serial.println(0); |
| 68 | + if (!client.connect(SERVER_URL, SERVER_PORT)) { |
| 69 | + if (!wifiRestarted) { |
| 70 | + writeLog("Restarting Wi-Fi"); |
| 71 | + connectToWifi(WIFI_SSID, WIFI_ENCRYPTION, WIFI_PASSWORD); |
| 72 | + delay(60000); |
| 73 | + wifiRestarted = true; |
| 74 | + } else if (!lininoRebooted) { |
| 75 | + writeLog("Rebooting Linino"); |
| 76 | + resetAndRebootLinino(); |
| 77 | + bridge(); |
| 78 | + lininoRebooted = true; |
| 79 | + } else { |
| 80 | + writeLog("Resetting Arduino"); |
| 81 | + resetArduino(); |
| 82 | + } |
| 83 | + } else { |
| 84 | + wifiRestarted = false; |
| 85 | + lininoRebooted = false; |
| 86 | + if (ws.handshake(client)) { |
| 87 | + ws.sendData("5"); |
| 88 | + // TODO: check server response |
| 89 | + led.turnOff(); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +void bridge() { |
| 96 | + led.turnOn(); |
| 97 | + delay(60000); // wait until linino has booted |
| 98 | + Bridge.begin(); |
| 99 | + led.turnOff(); |
| 100 | +} |
0 commit comments