-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* store wifi info * add sensor lib * stream only diff is greater than th * add ci
- Loading branch information
1 parent
d5a438c
commit 329bec4
Showing
6 changed files
with
239 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters