Files @ 6716eabd0aae
Branch filter:

Location: ChaosStuff/spaceapibutton/SpaceApiButton.ino

Dennis Fink
Added SpaceApiButton.ino
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
#include <MQTT.h>
#include <ArduinoJson.h>

#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<bufferSize> 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, "<html><body>This is the spaceapibutton!</body></html>");
  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);
}