diff --git a/lib/ConfigManager/ConfigManager.cpp b/lib/ConfigManager/ConfigManager.cpp
index a68d815..6711e9b 100644
--- a/lib/ConfigManager/ConfigManager.cpp
+++ b/lib/ConfigManager/ConfigManager.cpp
@@ -44,6 +44,7 @@ void startAPServer()
// Define the web server routes
server.on("/", HTTP_GET, handleRoot);
+ server.on("/connect", HTTP_GET, handleConnect);
server.on("/config", HTTP_POST, handleConfig);
server.begin();
@@ -79,14 +80,49 @@ void connectToWiFi()
void handleRoot()
{
- String html = "
";
+ String html = "";
+ int n = WiFi.scanNetworks();
+ Serial.println("Scan done");
+
+ if (n == 0)
+ {
+ Serial.println("no networks found");
+ html += "No Wi-Fi Networks Found
";
+ html += "Refresh the page to scan again.
";
+ }
+ else
+ {
+ Serial.print(n);
+ Serial.println(" networks found");
+ delay(10);
+ html += "Select SSID
";
+
+ for (int i = 0; i < n; i++)
+ {
+ html += "" + WiFi.SSID(i) + "
";
+ }
+ }
+ html += "";
+
+ Serial.println("");
+ WiFi.scanDelete();
+
+ server.send(200, "text/html", html);
+}
+
+void handleConnect()
+{
+ String ssid = server.arg("ssid");
+ Serial.println("ssid: " + ssid);
+ String html = "";
html += "Enter Wi-Fi Credentials
";
+ html += "SSID: " + ssid + "
";
html += "";
-
+ html += "";
+ html += "";
server.send(200, "text/html", html);
}
@@ -122,4 +158,14 @@ void saveWiFiCredentials(String ssid, String password)
preferences.putBool("configured", true);
Serial.println("Network Credentials Saved using Preferences");
preferences.end();
+}
+
+bool isIntervalValid(int interval)
+{
+ return interval >= 2 && interval <= 1000;
+}
+
+bool isThresholdValid(int threshold)
+{
+ return threshold >= 1 && threshold <= 20000;
}
\ No newline at end of file
diff --git a/lib/ConfigManager/ConfigManager.h b/lib/ConfigManager/ConfigManager.h
index b292a45..5978e7d 100644
--- a/lib/ConfigManager/ConfigManager.h
+++ b/lib/ConfigManager/ConfigManager.h
@@ -8,4 +8,7 @@ void startAPServer();
void saveWiFiCredentials(String ssid, String password);
void handleRoot();
void handleConfig();
+void handleConnect();
void connectToWiFi();
+bool isIntervalValid(int interval);
+bool isThresholdValid(int threshold);
\ No newline at end of file
diff --git a/platformio.ini b/platformio.ini
index 8a98f51..3ba9d9a 100644
--- a/platformio.ini
+++ b/platformio.ini
@@ -16,3 +16,4 @@ monitor_speed = 115200
lib_deps =
links2004/WebSockets@^2.4.1
adafruit/Adafruit ADS1X15@^2.4.0
+ bblanchon/ArduinoJson@^6.21.3
\ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 43c3c14..15c4bb4 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -3,6 +3,7 @@
#include
#include
#include
+#include
void ledBlink();
@@ -15,34 +16,68 @@ WebSocketsServer webSocket(81);
bool streaming = false;
-void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) {
- switch (type) {
- case WStype_DISCONNECTED:
- // streaming = false; // Stop streaming data when a client disconnects
- break;
- case WStype_TEXT:
- if (strcmp((char*)payload, "startStreaming") == 0) {
- streaming = true; // Start streaming data when "startStreaming" message is received
- Serial.println("Streaming started");
- } else if (strcmp((char*)payload, "stopStreaming") == 0) {
- streaming = false; // Stop streaming data when "stopStreaming" message is received
- Serial.println("Streaming stopped");
- } else if (strcmp((char*)payload, "deepSleep") == 0) {
- streaming = false;
- Serial.println("Going to sleep now");
- delay(1000);
- // turn off sensor
- switchSensor(false);
- delay(1000);
- esp_deep_sleep_start();
+void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length)
+{
+ switch (type)
+ {
+ case WStype_DISCONNECTED:
+ // streaming = false; // Stop streaming data when a client disconnects
+ break;
+ case WStype_TEXT:
+ {
+ const uint8_t size = JSON_OBJECT_SIZE(3);
+ StaticJsonDocument json;
+ DeserializationError err = deserializeJson(json, payload);
+ if (err)
+ {
+ Serial.print(F("deserializeJson() failed with code "));
+ Serial.println(err.c_str());
+ }
+ const char *command = json["command"];
+
+ if (strcmp(command, "start") == 0)
+ {
+ streaming = true; // Start streaming data when "startStreaming" message is received
+ const int _interval = json["interval"];
+ if (isIntervalValid(_interval))
+ {
+ interval = _interval;
+ Serial.println("Interval: " + String(interval));
}
- break;
- default:
- break;
+
+ const int _threshold = json["threshold"];
+ if (isThresholdValid(_threshold))
+ {
+ threshold = _threshold;
+ Serial.println("Threshold: " + String(threshold));
+ }
+ Serial.println("Streaming started");
+ }
+ else if (strcmp(command, "stop") == 0)
+ {
+ streaming = false; // Stop streaming data when "stopStreaming" message is received
+ Serial.println("Streaming stopped");
+ }
+ else if (strcmp(command, "deepSleep") == 0)
+ {
+ streaming = false;
+ Serial.println("Going to sleep now");
+ delay(1000);
+ // turn off sensor
+ switchSensor(false);
+ delay(1000);
+ esp_deep_sleep_start();
+ }
+ }
+
+ break;
+ default:
+ break;
}
}
-void touchCallback() {
+void touchCallback()
+{
// wake up from deep sleep
Serial.println("Touch detected");
// turn on sensor
@@ -50,7 +85,8 @@ void touchCallback() {
ledBlink();
}
-void setup() {
+void setup()
+{
Serial.begin(115200);
checkWifiInfo();
@@ -58,7 +94,7 @@ void setup() {
touchAttachInterrupt(touchPin, touchCallback, 40); // Attach touch interrupt
- //Configure Touchpad as wakeup source
+ // Configure Touchpad as wakeup source
esp_sleep_enable_touchpad_wakeup();
ledBlink();
@@ -67,15 +103,19 @@ void setup() {
webSocket.onEvent(webSocketEvent);
}
-void loop() {
- if (WiFi.status() == WL_CONNECTED) {
+void loop()
+{
+ if (WiFi.status() == WL_CONNECTED)
+ {
webSocket.loop();
// If streaming is enabled, get sensor data and send it to the client
- if (streaming) {
+ if (streaming)
+ {
int currentData = getSensorData();
// if the difference is greater than threshold, send data
- if (abs(currentData - sensorData) < threshold) {
+ if (abs(currentData - sensorData) < threshold)
+ {
return;
}
sensorData = currentData;
@@ -84,14 +124,17 @@ void loop() {
webSocket.broadcastTXT(dataStr.c_str(), dataStr.length());
delay(interval);
}
- } else {
+ }
+ else
+ {
runServer();
delay(1000);
}
}
// LED blink
-void ledBlink() {
+void ledBlink()
+{
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);