79 lines
2.3 KiB
C++
79 lines
2.3 KiB
C++
|
|
#include <Arduino.h>
|
||
|
|
#if defined(ESP8266)
|
||
|
|
#include <ESP8266WiFi.h>
|
||
|
|
#include <ESPAsyncTCP.h>
|
||
|
|
#elif defined(ESP32)
|
||
|
|
#include <AsyncTCP.h>
|
||
|
|
#include <WiFi.h>
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include <AsyncElegantOTA.h>
|
||
|
|
#include <ESPAsyncWebServer.h>
|
||
|
|
|
||
|
|
const char* ssid = "JoNelsDarlings";
|
||
|
|
const char* password = "Paris2016";
|
||
|
|
const int BUBBLE_GPIO = 4;
|
||
|
|
boolean isBubbleFirstContact = true;
|
||
|
|
unsigned long bubbleFreqSec = 0;
|
||
|
|
unsigned long lastMillis = millis();
|
||
|
|
|
||
|
|
AsyncWebServer server(80);
|
||
|
|
|
||
|
|
void setup(void) {
|
||
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
||
|
|
digitalWrite(LED_BUILTIN, LOW);
|
||
|
|
Serial.begin(115200);
|
||
|
|
WiFi.mode(WIFI_STA);
|
||
|
|
WiFi.begin(ssid, password);
|
||
|
|
Serial.println("");
|
||
|
|
|
||
|
|
// Wait for connection
|
||
|
|
while (WiFi.status() != WL_CONNECTED) {
|
||
|
|
delay(500);
|
||
|
|
Serial.print(".");
|
||
|
|
}
|
||
|
|
Serial.println("");
|
||
|
|
Serial.print("Connected to ");
|
||
|
|
Serial.println(ssid);
|
||
|
|
Serial.print("IP address: ");
|
||
|
|
Serial.println(WiFi.localIP());
|
||
|
|
|
||
|
|
server.on("/", HTTP_GET, [](AsyncWebServerRequest* request) {
|
||
|
|
request->send(
|
||
|
|
200, "text/html",
|
||
|
|
"Hi! This is the Wine-Sensor. Last bubble frequency: every <b>" +
|
||
|
|
String(bubbleFreqSec) + "</b> Seconds. [" + String(millis()) + "/" +
|
||
|
|
String(lastMillis) + "/" + String(isBubbleFirstContact) + "/" +
|
||
|
|
String(digitalRead(BUBBLE_GPIO)) +
|
||
|
|
"]"
|
||
|
|
"<script>window.setTimeout(_=>{window.location.reload();}"
|
||
|
|
",1000)</script>");
|
||
|
|
});
|
||
|
|
|
||
|
|
AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA
|
||
|
|
server.begin();
|
||
|
|
Serial.println("HTTP server started");
|
||
|
|
|
||
|
|
pinMode(BUBBLE_GPIO, INPUT_PULLDOWN);
|
||
|
|
digitalWrite(LED_BUILTIN, HIGH);
|
||
|
|
}
|
||
|
|
|
||
|
|
void loop(void) {
|
||
|
|
// digitalWrite(LED_BUILTIN, digitalRead(BUBBLE_GPIO));
|
||
|
|
|
||
|
|
// This runs real fast, so make sure that only the "first contact" is measured
|
||
|
|
if ((digitalRead(BUBBLE_GPIO) == HIGH) && isBubbleFirstContact) {
|
||
|
|
digitalWrite(LED_BUILTIN, LOW);
|
||
|
|
if (millis() > lastMillis) { // don't measure if we had an overflow...
|
||
|
|
bubbleFreqSec = (millis() - lastMillis) / 1000;
|
||
|
|
}
|
||
|
|
lastMillis = millis();
|
||
|
|
isBubbleFirstContact = false;
|
||
|
|
} else if ((digitalRead(BUBBLE_GPIO) == LOW)) {
|
||
|
|
isBubbleFirstContact = true;
|
||
|
|
}
|
||
|
|
|
||
|
|
// need to slow it down a bit to prevent fluctuating state cahange
|
||
|
|
delay(100);
|
||
|
|
}
|