initial commit. basic structure. untested.
This commit is contained in:
173
src/main.cpp
Normal file
173
src/main.cpp
Normal file
@@ -0,0 +1,173 @@
|
||||
#include <Arduino.h>
|
||||
#include <AsyncElegantOTA.h>
|
||||
|
||||
//Peripherals includes
|
||||
#include <Wire.h>
|
||||
#include <SPI.h>
|
||||
|
||||
//Web server includes
|
||||
#include <AsyncElegantOTA.h>
|
||||
#include <FS.h>
|
||||
#include "SPIFFS.h"
|
||||
#include <ESPmDNS.h>
|
||||
#include <WiFi.h>
|
||||
#include <AsyncTCP.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include <SPIFFSEditor.h>
|
||||
#include <Arduino_JSON.h>
|
||||
|
||||
#include <Preferences.h>
|
||||
Preferences settings;
|
||||
|
||||
#define LED_PIN 2
|
||||
|
||||
//*********************************************************
|
||||
// Web server variable declarations
|
||||
//*********************************************************
|
||||
|
||||
// Create AsyncWebServer object on port 80
|
||||
AsyncWebServer server(80);
|
||||
AsyncWebSocket ws("/ws");
|
||||
AsyncEventSource events("/events");
|
||||
|
||||
//Variables to save values from HTML form
|
||||
JSONVar scannedSSIDs;
|
||||
String ssid;
|
||||
String pass;
|
||||
String ip;
|
||||
String gateway;
|
||||
int timesConnected = 0;
|
||||
|
||||
const String http_username = "admin";
|
||||
const String http_password = "admin";
|
||||
|
||||
IPAddress localIP;
|
||||
//IPAddress localIP(192, 168, 1, 200); // hardcoded
|
||||
|
||||
// Set your Gateway IP address
|
||||
IPAddress localGateway;
|
||||
//IPAddress localGateway(192, 168, 1, 1); //hardcoded
|
||||
IPAddress subnet(255, 255, 0, 0);
|
||||
|
||||
// Timer variables
|
||||
unsigned long previousMillis = 0;
|
||||
const long interval = 10000; // interval to wait for Wi-Fi connection (milliseconds)
|
||||
#define WIFI_TIMEOUT 10000
|
||||
|
||||
// Stores LED state
|
||||
String ledState;
|
||||
|
||||
|
||||
// function declarations
|
||||
|
||||
|
||||
|
||||
void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
}
|
||||
|
||||
// functions
|
||||
// Initialize SPIFFS
|
||||
void initSPIFFS() {
|
||||
if (!SPIFFS.begin(true)) {
|
||||
Serial.println("An error has occurred while mounting SPIFFS");
|
||||
}
|
||||
Serial.println("SPIFFS mounted successfully");
|
||||
}
|
||||
|
||||
// Initialize WiFi
|
||||
bool initWiFi() {
|
||||
|
||||
//scan nearby networks
|
||||
WiFi.mode(WIFI_STA);
|
||||
int n = WiFi.scanNetworks();
|
||||
if(n){
|
||||
//networks found, try to connect
|
||||
String availableNetworkParams = "";
|
||||
settings.begin("WiFi");
|
||||
for(int i=0;i<n;i++){
|
||||
//["WiFI":
|
||||
// ["SSID":"pass,ip,gateway,timesConnected",
|
||||
// ...
|
||||
// ]
|
||||
//]
|
||||
availableNetworkParams = settings.getString(WiFi.SSID(i).c_str());
|
||||
if(availableNetworkParams.length() > 1){
|
||||
//known network
|
||||
ssid = WiFi.SSID(i).c_str();
|
||||
String netparams[4];
|
||||
int s=0;
|
||||
for(int j=0;j<4;j++){
|
||||
int e = availableNetworkParams.indexOf(",",s);
|
||||
netparams[j]=availableNetworkParams.substring(s,e);
|
||||
s=e+1;
|
||||
}
|
||||
pass = netparams[0];
|
||||
ip = netparams[1];
|
||||
localIP.fromString(ip);
|
||||
gateway = netparams[2];
|
||||
localGateway.fromString(gateway);
|
||||
timesConnected = netparams[3].toInt();
|
||||
|
||||
if (WiFi.config(localIP, localGateway, subnet)){
|
||||
WiFi.begin(ssid.c_str(), pass.c_str());
|
||||
Serial.println("Connecting to WiFi...");
|
||||
|
||||
unsigned long currentMillis = millis();
|
||||
previousMillis = currentMillis;
|
||||
|
||||
bool res = true;
|
||||
|
||||
while(WiFi.status() != WL_CONNECTED) {
|
||||
currentMillis = millis();
|
||||
if (currentMillis - previousMillis >= WIFI_TIMEOUT) {
|
||||
Serial.println("Failed to connect.");
|
||||
// return false;
|
||||
res = false;
|
||||
}
|
||||
if(!res) break;
|
||||
}
|
||||
// Serial.println(WiFi.localIP());
|
||||
// return true;
|
||||
if(res){
|
||||
//save connection
|
||||
settings.putString(ssid.c_str(),(pass + "," + localIP.toString() + "," + localGateway.toString() + "," + String(timesConnected + 1)).c_str());
|
||||
//initWiFi() true
|
||||
return true;
|
||||
}
|
||||
}else{
|
||||
Serial.println("STA Failed to configure");
|
||||
//return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
settings.end();
|
||||
}
|
||||
//no networks found
|
||||
Serial.println("Setting AP (Access Point)");
|
||||
// NULL sets an open Access Point
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("ESP-WIFI", NULL);
|
||||
|
||||
localIP = WiFi.softAPIP();
|
||||
Serial.print("AP IP address: ");
|
||||
Serial.println(localIP);
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
// Replaces placeholder with LED state value
|
||||
String getLEDState(const String& var) {
|
||||
if(var == "STATE") {
|
||||
if(digitalRead(LED_PIN)) {
|
||||
ledState = "ON";
|
||||
}
|
||||
else {
|
||||
ledState = "OFF";
|
||||
}
|
||||
return ledState;
|
||||
}
|
||||
return String();
|
||||
}
|
||||
Reference in New Issue
Block a user