Skip to content

Commit

Permalink
Check data (#5)
Browse files Browse the repository at this point in the history
* store wifi info

* add sensor lib

* stream only diff is greater than th

* add ci
  • Loading branch information
yuichiroaoki authored Oct 25, 2023
1 parent d5a438c commit 329bec4
Show file tree
Hide file tree
Showing 6 changed files with 239 additions and 66 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: PlatformIO CI

on: [push]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/cache@v3
with:
path: |
~/.cache/pip
~/.platformio/.cache
key: ${{ runner.os }}-pio
- uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install PlatformIO Core
run: pip install --upgrade platformio

- name: Build PlatformIO Project
run: pio run
125 changes: 125 additions & 0 deletions lib/ConfigManager/ConfigManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "ConfigManager.h"

Preferences preferences;
WebServer server(80);

const char *apSSID = "opencmm";
const char *apPassword = "opencmm1sensor";

const char *ssidKey = "wifi_ssid";
const char *passwordKey = "wifi_password";

IPAddress local_ip(192, 168, 1, 1);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);

void checkWifiInfo()
{
preferences.begin("credentials", false);

// Check if Wi-Fi credentials are already stored
if (preferences.getBool("configured", false))
{
// Wi-Fi credentials exist, so connect to Wi-Fi
connectToWiFi();
}
else
{
// Wi-Fi credentials don't exist, start in AP mode
startAPServer();
}
}

void runServer()
{
server.handleClient();
}

void startAPServer()
{
// Start in AP mode
WiFi.softAP(apSSID, apPassword);
WiFi.softAPConfig(local_ip, gateway, subnet);
delay(100);

// Define the web server routes
server.on("/", HTTP_GET, handleRoot);
server.on("/config", HTTP_POST, handleConfig);

server.begin();
}

void connectToWiFi()
{
String ssid = preferences.getString(ssidKey, "");
String password = preferences.getString(passwordKey, "");

delay(3000);

if (ssid != "" && password != "")
{
Serial.println("ssid: " + ssid);

// Attempt to connect to Wi-Fi
WiFi.begin(ssid.c_str(), password.c_str());
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print('.');
}
Serial.println("Successfully connected to WiFi.");
Serial.println(WiFi.localIP());
}
else
{
Serial.println("No Wi-Fi credentials found.");
}
}

void handleRoot()
{
String html = "<html><body>";
html += "<h2>Enter Wi-Fi Credentials</h2>";
html += "<form method='post' action='/config'>";
html += "SSID: <input type='text' name='ssid'><br>";
html += "Password: <input type='password' name='password'><br>";
html += "<input type='submit' value='Submit'>";
html += "</form></body></html>";

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

void handleConfig()
{
String ssid = server.arg("ssid");
String password = server.arg("password");

saveWiFiCredentials(ssid, password);

// Switch to Station mode and connect to the user's Wi-Fi network
WiFi.softAPdisconnect();
WiFi.begin(ssid.c_str(), password.c_str());
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}

Serial.println("Connected to the WiFi network");
server.close();

// Redirect to a success page or any other action you want
server.sendHeader("Location", "/");
server.send(302, "text/plain", "credentials saved");
}

void saveWiFiCredentials(String ssid, String password)
{
// Store Wi-Fi credentials in NVS
preferences.putString(ssidKey, ssid);
preferences.putString(passwordKey, password);
preferences.putBool("configured", true);
Serial.println("Network Credentials Saved using Preferences");
preferences.end();
}
11 changes: 11 additions & 0 deletions lib/ConfigManager/ConfigManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <WiFi.h>
#include <WebServer.h>
#include <Preferences.h>

void checkWifiInfo();
void runServer();
void startAPServer();
void saveWiFiCredentials(String ssid, String password);
void handleRoot();
void handleConfig();
void connectToWiFi();
47 changes: 47 additions & 0 deletions lib/Sensor/Sensor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <Sensor.h>

Adafruit_ADS1115 ads; /* Use this for the 16-bit version */

const float slowResponseTime = 10.0;
const int sensorControlPins[3] = {5, 18, 19};

void setupSensor() {
Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");

if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}

for (int i = 0; i < 3; i++) {
pinMode(sensorControlPins[i], OUTPUT);
}
switchSensor(true);
}


// Switch sensor
void switchSensor(bool on) {
for (int i = 0; i < 3; i++) {
digitalWrite(sensorControlPins[i], on ? HIGH : LOW);
}
}

int getSensorData() {
int16_t adc0;
float volts0;

adc0 = ads.readADC_SingleEnded(0);

// volts0 = ads.computeVolts(adc0);

// Serial.println("-----------------------------------------------------------");
// Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");

// response time
// fast: 1.5ms
// standard: 5ms
// slow: 10ms
delay(slowResponseTime);
return adc0;
}
6 changes: 6 additions & 0 deletions lib/Sensor/Sensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <Adafruit_ADS1X15.h>
#include <Arduino.h>

int getSensorData();
void switchSensor(bool on);
void setupSensor();
92 changes: 26 additions & 66 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
#include <Arduino.h>
#include <WiFi.h>
#include <WebSocketsServer.h>
#include <Adafruit_ADS1X15.h>
#include <ConfigManager.h>
#include <Sensor.h>

Adafruit_ADS1115 ads; /* Use this for the 16-bit version */

int getSensorData();
void ledBlink();
void switchSensor(bool on);

const char* ssid = "<ssid>";
const char* password = "<password>";
const float slowResponseTime = 10.0;
const int touchPin = 4; // GPIO4 as the touch-sensitive pin
const int sensorControlPins[3] = {5, 18, 19};
int sensorData = 0;
int interval = 1000; // 1 second
int threshold = 100;

WebSocketsServer webSocket(81);

Expand Down Expand Up @@ -56,83 +52,47 @@ void touchCallback() {

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

Serial.println("ADC Range: +/- 6.144V (1 bit = 3mV/ADS1015, 0.1875mV/ADS1115)");

if (!ads.begin()) {
Serial.println("Failed to initialize ADS.");
while (1);
}
setupSensor();

for (int i = 0; i < 3; i++) {
pinMode(sensorControlPins[i], OUTPUT);
}
switchSensor(true);
touchAttachInterrupt(touchPin, touchCallback, 40); // Attach touch interrupt

//Configure Touchpad as wakeup source
esp_sleep_enable_touchpad_wakeup();

// connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi..");
}

ledBlink();
Serial.println("Connected to the WiFi network");

// print the IP address
Serial.println(WiFi.localIP());

webSocket.begin();
webSocket.onEvent(webSocketEvent);
}

void loop() {
webSocket.loop();

// If streaming is enabled, get sensor data and send it to the client
if (streaming) {
int data = getSensorData();
String dataStr = String(data);
Serial.println(dataStr);
webSocket.broadcastTXT(dataStr.c_str(), dataStr.length());
delay(200);
if (WiFi.status() == WL_CONNECTED) {
webSocket.loop();

// If streaming is enabled, get sensor data and send it to the client
if (streaming) {
int currentData = getSensorData();
// if the difference is greater than threshold, send data
if (abs(currentData - sensorData) < threshold) {
return;
}
sensorData = currentData;
String dataStr = String(currentData);
Serial.println(dataStr);
webSocket.broadcastTXT(dataStr.c_str(), dataStr.length());
delay(interval);
}
} else {
runServer();
delay(1000);
}
delay(1000);
}

int getSensorData() {
int16_t adc0;
float volts0;

adc0 = ads.readADC_SingleEnded(0);

// volts0 = ads.computeVolts(adc0);

// Serial.println("-----------------------------------------------------------");
// Serial.print("AIN0: "); Serial.print(adc0); Serial.print(" "); Serial.print(volts0); Serial.println("V");

// response time
// fast: 1.5ms
// standard: 5ms
// slow: 10ms
delay(slowResponseTime);
return adc0;
}

// LED blink
void ledBlink() {
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
}

// Switch sensor
void switchSensor(bool on) {
for (int i = 0; i < 3; i++) {
digitalWrite(sensorControlPins[i], on ? HIGH : LOW);
}
}

0 comments on commit 329bec4

Please sign in to comment.