Skip to content

Commit

Permalink
Pass settings (#6)
Browse files Browse the repository at this point in the history
* scan wifi

* pass conf

* add vaildation
  • Loading branch information
yuichiroaoki authored Oct 26, 2023
1 parent 329bec4 commit 7282894
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 36 deletions.
54 changes: 50 additions & 4 deletions lib/ConfigManager/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -79,14 +80,49 @@ void connectToWiFi()

void handleRoot()
{
String html = "<html><body>";
String html = "<!DOCTYPE html><html><body>";
int n = WiFi.scanNetworks();
Serial.println("Scan done");

if (n == 0)
{
Serial.println("no networks found");
html += "<h2>No Wi-Fi Networks Found</h2>";
html += "<p>Refresh the page to scan again.</p>";
}
else
{
Serial.print(n);
Serial.println(" networks found");
delay(10);
html += "<p>Select SSID</p>";

for (int i = 0; i < n; i++)
{
html += "<p><a href='/connect?ssid=" + WiFi.SSID(i) + "'>" + WiFi.SSID(i) + "</a></p>";
}
}
html += "</body></html>";

Serial.println("");
WiFi.scanDelete();

server.send(200, "text/html", html);
}

void handleConnect()
{
String ssid = server.arg("ssid");
Serial.println("ssid: " + ssid);
String html = "<!DOCTYPE html><html><body>";
html += "<h2>Enter Wi-Fi Credentials</h2>";
html += "<p>SSID: " + ssid + "</p>";
html += "<form method='post' action='/config'>";
html += "SSID: <input type='text' name='ssid'><br>";
html += "<input type='hidden' name='ssid' value='" + ssid + "'>";
html += "Password: <input type='password' name='password'><br>";
html += "<input type='submit' value='Submit'>";
html += "</form></body></html>";

html += "</form>";
html += "</body></html>";
server.send(200, "text/html", html);
}

Expand Down Expand Up @@ -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;
}
3 changes: 3 additions & 0 deletions lib/ConfigManager/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
1 change: 1 addition & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
107 changes: 75 additions & 32 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <WebSocketsServer.h>
#include <ConfigManager.h>
#include <Sensor.h>
#include <ArduinoJson.h>

void ledBlink();

Expand All @@ -15,50 +16,85 @@ 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<size> 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
switchSensor(true);
ledBlink();
}

void setup() {
void setup()
{
Serial.begin(115200);
checkWifiInfo();

setupSensor();

touchAttachInterrupt(touchPin, touchCallback, 40); // Attach touch interrupt

//Configure Touchpad as wakeup source
// Configure Touchpad as wakeup source
esp_sleep_enable_touchpad_wakeup();

ledBlink();
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit 7282894

Please sign in to comment.