# HG changeset patch # User Dennis Fink # Date 2020-01-14 21:22:25 # Node ID 6716eabd0aae5cb12910ad954feeaf78dc818f55 Added SpaceApiButton.ino diff --git a/SpaceApiButton.ino b/SpaceApiButton.ino new file mode 100644 --- /dev/null +++ b/SpaceApiButton.ino @@ -0,0 +1,136 @@ +#include +#include +#include +#include +#include +#include + +#include "Config.h" + +WiFiClient WiFiclient; +MQTTClient client; +ESP8266WebServer httpServer(80); +ESP8266HTTPUpdateServer httpUpdater; + +// Variables will change: +int buttonState; // the current reading from the input pin +int lastButtonState = LOW; // the previous reading from the input pin +int send_trigger = 0; + +// the following variables are unsigned longs because the time, measured in +// milliseconds, will quickly become a bigger number than can be stored in an int. +unsigned long lastDebounceTime = 0; // the last time the output pin was toggled +unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers + +const size_t bufferSize = JSON_OBJECT_SIZE(4) + 70; + +void connect() { + Serial.print("\nChecking WiFi..."); + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("Connection Failed! Reboot..."); + delay(5000); + ESP.restart(); + } + Serial.println("Connected to WiFi!"); + Serial.print("Connected to: "); + Serial.println(WiFi.SSID()); + Serial.print("IP: "); + Serial.println(WiFi.localIP()); + Serial.print("Connecting to MQTT Server..."); + while (!client.connect(MQTT_CLIENT_ID, MQTT_USERNAME, MQTT_PASSWORD)) { + Serial.print("."); + delay(1000); + } + Serial.println("\nConnected to MQTT!"); + client.publish(MQTT_AVAILABILITY_TOPIC, "online", true, 2); + client.subscribe(MQTT_STATE_TOPIC); +} + +void on_state_received(String &topic, String &payload) { + StaticJsonDocument jb; + auto error = deserializeJson(jb, payload); + + if (not error) { + if (jb["open"]) { + digitalWrite(D2, HIGH); + } else { + digitalWrite(D2, LOW); + } + } else { + digitalWrite(D2, LOW); + } +} + +void handle_root() { + char temp[400]; + snprintf(temp, 400, "This is the spaceapibutton!"); + httpServer.send(200, "text/html", temp); +} + +void setup() { + Serial.begin(115200); + Serial.println("Booting..."); + + pinMode(LED_BUILTIN, OUTPUT); + + WiFi.hostname(HOSTNAME); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + + client.begin(MQTT_SERVER, WiFiclient); + client.setWill(MQTT_AVAILABILITY_TOPIC, "offline", true, 2); + client.onMessage(on_state_received); + + connect(); + + Serial.println("Starting MDNS..."); + MDNS.begin(HOSTNAME); + Serial.println("Starting HTTP server..."); + httpUpdater.setup(&httpServer); + httpServer.on("/", handle_root); + httpServer.begin(); + + Serial.println("Adding HTTP server to MDNS..."); + MDNS.addService("http", "tcp", 80); + Serial.printf("HTTPUpdateServer ready! Open http://%s.local/update in your browser\n", HOSTNAME); + + pinMode(D2, OUTPUT); + pinMode(D1, INPUT); + +} + +void loop() { + client.loop(); + httpServer.handleClient(); + MDNS.update(); + delay(10); + + + if (!client.connected()) { + connect(); + } + + int reading = digitalRead(D1); + if (reading != lastButtonState) { + lastDebounceTime = millis(); + } + + if ((millis() - lastDebounceTime) > debounceDelay) { + digitalWrite(LED_BUILTIN, LOW); + if (reading != buttonState) { + buttonState = reading; + if (buttonState == HIGH) { + send_trigger = 1; + + } + } + } + lastButtonState = reading; + //Serial.println(send_trigger); + if (send_trigger == 1) { + client.publish(MQTT_TRIGGER_TOPIC, "1", false, 2); + send_trigger = 0; + + } + digitalWrite(LED_BUILTIN, HIGH); +}