Allow the ESP32TimeLapseCam the option of running in AP mode, for use in the field.

This commit is contained in:
Feilim Breatnach 2020-02-10 21:08:45 +00:00
parent fd2fe15b02
commit 3b778afc0d
1 changed files with 127 additions and 40 deletions

View File

@ -1,40 +1,127 @@
#include <WiFi.h> #include <WiFi.h>
#include "file.h" #include "file.h"
#include "camera.h" #include "camera.h"
#include "lapse.h" #include "lapse.h"
const char *ssid = "..."; const char* ssid = "...";
const char *password = "..."; const char* password = "...";
const char* ap_password = "esp32_time_lapse"; // password to connect to this esp32 when in AP mode
void startCameraServer();
void startCameraServer();
void setup()
{ void setup()
Serial.begin(115200); {
Serial.setDebugOutput(true); Serial.begin(115200);
Serial.println(); Serial.setDebugOutput(true);
initFileSystem(); Serial.println();
initCamera();
initFileSystem();
WiFi.begin(ssid, password); initCamera();
while (WiFi.status() != WL_CONNECTED)
{ int wifiMode = determineWifiMode();
delay(500); Serial.printf("Wifi Mode: %u\n", wifiMode);
Serial.print("."); initWifi(wifiMode);
}
Serial.println(""); startCameraServer();
Serial.println("WiFi connected"); Serial.print("Camera Ready! Use 'http://");
startCameraServer(); if (wifiMode == 0)
Serial.print("Camera Ready! Use 'http://"); {
Serial.print(WiFi.localIP()); Serial.print(WiFi.softAPIP());
Serial.println("' to connect"); }
} else
{
void loop() Serial.print(WiFi.localIP());
{ }
unsigned long t = millis(); Serial.println("' to connect");
static unsigned long ot = 0; }
unsigned long dt = t - ot;
ot = t; // Determine on boot: 0 = AP Mode, 1 = STA Mode
processLapse(dt); int determineWifiMode()
} {
Serial.println("Scan start..");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.println("Scan complete :)");
if (n == 0) {
Serial.println("No networks found :(");
return 0;
} else {
Serial.print(n);
Serial.println(" networks found:");
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
delay(10);
if (WiFi.SSID(i).equals(ssid)) {
return 1;
}
}
}
return 0;
}
void initWifi(int wifiMode)
{
if (wifiMode == 0)
{
// IP address of the ESP32 when in AP mode
IPAddress apIP = IPAddress(192, 168, 1, 101);
const char *hostname = "esp32TimeLapseCam";
Serial.println("Starting softAP");
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIP, apIP, IPAddress(255, 255, 255, 0));
bool result = WiFi.softAP(hostname, ap_password, 1, 0);
if (!result)
{
Serial.println("AP Config failed.");
return;
}
else
{
Serial.println("=======================================");
Serial.println("AP Config Success.");
Serial.print("AP MAC: ");
Serial.println(WiFi.softAPmacAddress());
IPAddress ip = WiFi.softAPIP();
Serial.print("IP Address: ");
Serial.println(ip);
Serial.print("AP Name: ");
Serial.println(hostname);
Serial.print("AP Password: ");
Serial.println(ap_password);
Serial.println("=======================================");
}
}
else {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}
}
void loop()
{
unsigned long t = millis();
static unsigned long ot = 0;
unsigned long dt = t - ot;
ot = t;
processLapse(dt);
}