2022-09-17 18:02:23 -05:00
|
|
|
#include <Adafruit_SSD1306.h>
|
2022-09-17 09:31:30 -05:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
#include <AsyncElegantOTA.h>
|
2022-09-17 18:02:23 -05:00
|
|
|
#include <AsyncTCP.h>
|
2022-09-17 09:31:30 -05:00
|
|
|
#include <ESPAsyncWebServer.h>
|
2022-09-17 18:02:23 -05:00
|
|
|
#include <WiFi.h>
|
2022-09-17 09:31:30 -05:00
|
|
|
|
|
|
|
|
const char* ssid = "JoNelsDarlings";
|
|
|
|
|
const char* password = "Paris2016";
|
|
|
|
|
const int BUBBLE_GPIO = 4;
|
|
|
|
|
boolean isBubbleFirstContact = true;
|
2022-09-20 13:09:20 -05:00
|
|
|
double bubbleFreqSec = 0;
|
2022-09-17 09:31:30 -05:00
|
|
|
unsigned long lastMillis = millis();
|
2022-09-20 13:09:20 -05:00
|
|
|
unsigned int bVal;
|
2022-09-17 09:31:30 -05:00
|
|
|
|
|
|
|
|
AsyncWebServer server(80);
|
|
|
|
|
|
2022-09-17 18:02:23 -05:00
|
|
|
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
|
|
|
|
|
// I connected SCL to D22 and SDA to D21
|
|
|
|
|
#define SCREEN_WIDTH 128 // OLED display width, in pixels
|
|
|
|
|
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
|
|
|
|
|
|
|
|
|
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
|
|
|
|
|
|
|
|
|
|
void writeToOled(String text) {
|
|
|
|
|
display.clearDisplay();
|
|
|
|
|
display.setTextColor(WHITE);
|
|
|
|
|
display.setCursor(0, 0);
|
|
|
|
|
display.setTextSize(2);
|
|
|
|
|
display.print(text);
|
|
|
|
|
display.display();
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-17 09:31:30 -05:00
|
|
|
void setup(void) {
|
|
|
|
|
pinMode(LED_BUILTIN, OUTPUT);
|
|
|
|
|
digitalWrite(LED_BUILTIN, LOW);
|
2022-09-17 18:02:23 -05:00
|
|
|
Serial.begin(9600);
|
|
|
|
|
|
|
|
|
|
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
|
|
|
|
|
Serial.println(F("SSD1306 allocation failed"));
|
|
|
|
|
for (;;)
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
delay(2000);
|
|
|
|
|
|
|
|
|
|
writeToOled("Startup");
|
|
|
|
|
|
2022-09-17 09:31:30 -05:00
|
|
|
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) + "/" +
|
2022-09-20 13:09:20 -05:00
|
|
|
String(bVal) +
|
2022-09-17 09:31:30 -05:00
|
|
|
"]"
|
|
|
|
|
"<script>window.setTimeout(_=>{window.location.reload();}"
|
|
|
|
|
",1000)</script>");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA
|
|
|
|
|
server.begin();
|
|
|
|
|
Serial.println("HTTP server started");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void loop(void) {
|
2022-09-20 13:09:20 -05:00
|
|
|
bVal = touchRead(BUBBLE_GPIO);
|
|
|
|
|
if (bVal == 0) {
|
|
|
|
|
digitalWrite(BUILTIN_LED, HIGH);
|
|
|
|
|
} else {
|
|
|
|
|
digitalWrite(BUILTIN_LED, LOW);
|
|
|
|
|
}
|
|
|
|
|
// writeToOled("* " + String(bVal));
|
2022-09-17 09:31:30 -05:00
|
|
|
|
|
|
|
|
// This runs real fast, so make sure that only the "first contact" is measured
|
2022-09-20 13:09:20 -05:00
|
|
|
if ((bVal == 0) && (isBubbleFirstContact)) {
|
2022-09-17 09:31:30 -05:00
|
|
|
if (millis() > lastMillis) { // don't measure if we had an overflow...
|
2022-09-20 13:09:20 -05:00
|
|
|
bubbleFreqSec = (double)(millis() - lastMillis) / 1000;
|
2022-09-17 09:31:30 -05:00
|
|
|
}
|
|
|
|
|
lastMillis = millis();
|
|
|
|
|
isBubbleFirstContact = false;
|
2022-09-20 13:09:20 -05:00
|
|
|
} else if (0 != bVal) {
|
2022-09-17 09:31:30 -05:00
|
|
|
isBubbleFirstContact = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 13:09:20 -05:00
|
|
|
writeToOled("F:" + String(bubbleFreqSec, 2) + "\n[" + String(bVal) + "]");
|
|
|
|
|
|
2022-09-17 09:31:30 -05:00
|
|
|
// need to slow it down a bit to prevent fluctuating state cahange
|
2022-09-20 13:09:20 -05:00
|
|
|
delay(10);
|
2022-09-17 09:31:30 -05:00
|
|
|
}
|