forked from Krautspace/doorstatus
106 lines
2.3 KiB
C++
106 lines
2.3 KiB
C++
/*
|
|
Copyright (c):
|
|
|
|
Philipp Matthias Schäfer (philipp.matthias.schaefer@posteo.de), 2020
|
|
|
|
This file is part of the KrautStatus' Arduino code.
|
|
|
|
The Clean CommonMark library is free software: you can redistribute it and/or
|
|
modify it under the terms of the GNU Affero General Public License as published
|
|
by the Free Software Foundation, either version 3 of the License, or (at your
|
|
option) any later version.
|
|
|
|
The Clean CommonMark library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License along
|
|
with the Clean CommonMark library. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <SPI.h>
|
|
#include <WiFi101.h>
|
|
#include <WiFiUdp.h>
|
|
|
|
#include "config.h"
|
|
|
|
uint8_t OPEN = 1;
|
|
uint8_t CLOSED = 0;
|
|
|
|
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();
|
|
|
|
}
|