From d5e31ca7897b240c542b1ec4a10c3b2e5de44de0 Mon Sep 17 00:00:00 2001 From: Philipp Matthias Schaefer Date: Wed, 18 Nov 2020 22:45:14 +0100 Subject: [PATCH] Add initial (untested) version of Arduino sketch --- source/arduino/config.h | 11 +++++ source/arduino/door_status.ino | 87 ++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 source/arduino/config.h create mode 100644 source/arduino/door_status.ino diff --git a/source/arduino/config.h b/source/arduino/config.h new file mode 100644 index 0000000..d2f9073 --- /dev/null +++ b/source/arduino/config.h @@ -0,0 +1,11 @@ +// SSID and password of the WiFi network to which we broadcast the door lock's +// status. +char SSID[] = ""; +char PASSWORD[] = ""; + + +// Port on which to listen for status requests +unsigned int PORT = 12345; + +// Pin to which the reed switch is connected +uint8_t REED_PIN = 0; diff --git a/source/arduino/door_status.ino b/source/arduino/door_status.ino new file mode 100644 index 0000000..4a0f91b --- /dev/null +++ b/source/arduino/door_status.ino @@ -0,0 +1,87 @@ +#include +#include +#include + +#include "config.h" + +uint8_t OPEN = 1; +uint8_t CLOSED = 0; + +int wifi_status = WL_IDLE_STATUS; + +WiFiUDP Udp; + +void setup() { + pinMode(REED_PIN, INPUT_PULLUP); + + Serial.begin(9600); + while (!Serial); + + if (WiFi.status() == WL_NO_SHIELD) { + Serial.println("No WiFI shield present"); + // TODO: Create noShieldLoop with visual indication. + while (true); + } + + while (true) { + // TODO: Visually indicate that we are trying to connect + Serial.print("Connecting to SSID: "); + Serial.println(SSID); + status = WiFi.begin(SSID, PASSWORD); + + if (status == WL_CONNECTED) { + break; + } + + // TODO: Visually indicate that we waiting for trying to connect again + delay(10000); + } + + printNetworkInfo(); + +} + +void loop() { + int reed_state = digitalRead(REED_PIN); + + int packetSize = Udp.readPacket(); + if (packetSize) { + Udp.beginPacket(udp.remoteIP(), Udp.remotePort()); + + if (reed_state == HIGH) { + Udp.write(CLOSED); + } else { + Udp.write(OPEN); + } + + Udp.endPacket(); + } + // TODO: Visually indicate open/closed state. +} + + +void printNetworkInfo() { + + Serial.print("Connect to WiFi"); + + IPAddress ip = WiFi.localIP(); + Serial.print("IP Address: "); + Serial.println(ip); + + byte mac[6]; + WiFi.macAddress(mac); + Serial.print("MAC address: "); + for (int i = 5; i >= 0; --i) { + if (mac[i] < 16) { + Serial.print("0"); + } + + Serial.print(mac[i], HEX); + + if (i > 0) { + Serial.print(":"); + } + } + Serial.println(); + +}