#include #include #include #include #include #include const char* ssid = "JoNelsDarlings"; const char* password = "Paris2016"; const int BUBBLE_GPIO = 4; boolean isBubbleFirstContact = true; double bubbleFreqSec = 0; unsigned long lastMillis = millis(); unsigned int bVal; AsyncWebServer server(80); // 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(); } void setup(void) { pinMode(LED_BUILTIN, OUTPUT); digitalWrite(LED_BUILTIN, LOW); Serial.begin(9600); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 Serial.println(F("SSD1306 allocation failed")); for (;;) ; } delay(2000); writeToOled("Startup"); 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 " + String(bubbleFreqSec) + " Seconds. [" + String(millis()) + "/" + String(lastMillis) + "/" + String(isBubbleFirstContact) + "/" + String(bVal) + "]" ""); }); AsyncElegantOTA.begin(&server); // Start AsyncElegantOTA server.begin(); Serial.println("HTTP server started"); } void loop(void) { bVal = touchRead(BUBBLE_GPIO); if (bVal == 0) { digitalWrite(BUILTIN_LED, HIGH); } else { digitalWrite(BUILTIN_LED, LOW); } // writeToOled("* " + String(bVal)); // This runs real fast, so make sure that only the "first contact" is measured if ((bVal == 0) && (isBubbleFirstContact)) { if (millis() > lastMillis) { // don't measure if we had an overflow... bubbleFreqSec = (double)(millis() - lastMillis) / 1000; } lastMillis = millis(); isBubbleFirstContact = false; } else if (0 != bVal) { isBubbleFirstContact = true; } writeToOled("F:" + String(bubbleFreqSec, 2) + "\n[" + String(bVal) + "]"); // need to slow it down a bit to prevent fluctuating state cahange delay(10); }