Skip to content

Commit

Permalink
Add OTA, disable all WiFi powersave, pass name
Browse files Browse the repository at this point in the history
  • Loading branch information
easytarget committed Jun 8, 2021
1 parent 7908a7d commit a682b7f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 44 deletions.
143 changes: 102 additions & 41 deletions esp32-cjmcu-531-demo.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <WebServer.h>
#include <ArduinoJson.h>
#include <Wire.h>
#include <ArduinoOTA.h>

// Currently using the SparkFun library for the sensor:
// https://github.com/sparkfun/SparkFun_VL53L1X_Arduino_Library/
Expand All @@ -23,7 +24,7 @@
// Comment out to disable servo (lidar) functionality
#define LIDAR

// Embedded web page kept in a header file (& stored in progmem)
// Embedded web page is kept in a seperate header file (& stored in progmem)
#include "index.h"

// Sensor setup; see:
Expand Down Expand Up @@ -51,6 +52,18 @@ String mode = "mid"; // range mode
const char* password = ""; // no password == very insecure, but very easy to demo
#endif

#if defined (DEVICENAME)
String myName = DEVICENAME;
#else
String myName = "ESP32/VL53L1X Demo";
#endif

#if defined(NO_OTA)
bool otaEnabled = false;
#else
bool otaEnabled = true;
#endif

// Webserver
WebServer server(80);

Expand Down Expand Up @@ -153,52 +166,98 @@ void setup(void){
// Serial
Serial.begin(115200);
Serial.println();
Serial.println(myName);
Serial.println("Booting Sketch...");

// Turn the LED on once serial begun
digitalWrite(LED, HIGH);

#ifdef ACCESSPOINT
// Access point
IPAddress ourIP(ACCESSPOINTIP);
IPAddress ourMask(ACCESSPOINTMASK);
WiFi.mode(WIFI_AP); //Access Point mode
WiFi.softAP(ssid, password);
WiFi.softAPConfig(ourIP, ourIP, ourMask);
Serial.print("Access Point started: ");
Serial.print(ssid);
Serial.print(":");
Serial.println(password);
Serial.print("AP Address: ");
Serial.println(WiFi.softAPIP());
#else
// Connect to existing wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to: ");
Serial.println(ssid);
//Wait for WiFi to connect
int aline = 0;
while(WiFi.waitForConnectResult() != WL_CONNECTED)
{
delay(1000);
aline++;
if (aline > 80) {
aline = 0;
Serial.println(".");
}
else
// Disable power saving on WiFi to improve responsiveness
// (https://github.com/espressif/arduino-esp32/issues/1484)
WiFi.setSleep(false);

#ifdef ACCESSPOINT
// Access point
IPAddress ourIP(ACCESSPOINTIP);
IPAddress ourMask(ACCESSPOINTMASK);
WiFi.mode(WIFI_AP); //Access Point mode
WiFi.softAP(ssid, password);
WiFi.softAPConfig(ourIP, ourIP, ourMask);
Serial.print("Access Point started: ");
Serial.print(ssid);
Serial.print(":");
Serial.println(password);
Serial.print("AP Address: ");
Serial.println(WiFi.softAPIP());
#else
// Connect to existing wifi
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to: ");
Serial.println(ssid);
//Wait for WiFi to connect
int aline = 0;
while(WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.print(".");
delay(1000);
aline++;
if (aline > 80) {
aline = 0;
Serial.println(".");
}
else
{
Serial.print(".");
}
}
}
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
#endif
//If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); //IP address assigned to your ESP
#endif

if (otaEnabled) {
// Start OTA once connected
Serial.println("Setting up OTA");
// Port defaults to 3232
// ArduinoOTA.setPort(3232);
// Hostname defaults to esp3232-[MAC]
ArduinoOTA.setHostname(myName.c_str());
// No authentication by default
#if defined (OTA_PASSWORD)
ArduinoOTA.setPassword(OTA_PASSWORD);
Serial.printf("OTA Password: %s\n\r", OTA_PASSWORD);
#endif
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
} else {
Serial.println("OTA is disabled");
}

// HTTP request responders
server.on("/", handleRoot); // Main page
Expand Down Expand Up @@ -530,7 +589,8 @@ void handleData() {
response["HasServo"] = false;
#endif
char id [8]; sprintf(id, "0x%x", distanceSensor.getSensorID());
response["Sensor ID"] = id;
response["SensorID"] = id;
response["Name"] = myName;

serializeJsonPretty(response, out);
server.send(200, "text/plain", out);
Expand Down Expand Up @@ -637,6 +697,7 @@ void usernotify(char message[]) {

void loop(void){
server.handleClient();
if (otaEnabled) ArduinoOTA.handle();
#ifdef BUTTON
handleButton();
#endif
Expand Down
10 changes: 8 additions & 2 deletions index.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const char MAIN_page[] PROGMEM = R"=====(
<!-- The page itself -->
<body>
<div class="card">
<h3 style="text-align: center;">ESP32/VL53L1X Demo</h3>
<h3 id="MyName" style="text-align: center;">ESP32/VL53L1X Demo</h3>
<hr>
<div id="signal" class="signal">Comms Timeout</div>
<div>
Expand Down Expand Up @@ -335,7 +335,13 @@ const char MAIN_page[] PROGMEM = R"=====(
var scanY = Math.floor(Math.cos(rad)*scanValue);
scan.fillStyle = "#DDDDDD";
scan.fillRect(ScanXYRadius+scanX, ScanXYRadius-scanY, 3, 3);
}
}

// If an Name field is present, use it.
if (response.hasOwnProperty('Name')) {
document.getElementById("MyName").innerHTML = response.Name;
}

}
}
xhttp.open("GET", "/data", true);
Expand Down
15 changes: 14 additions & 1 deletion mywifi.sample.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Wifi Network Definitions:
// Copy ior rename this file to 'mywifi.h' and edit to provide your own WiFi details
// Copy or rename this file to 'mywifi.h' and edit to provide your own WiFi details

// Conect to an existing network
const char* ssid = "my-wifi";
Expand All @@ -12,3 +12,16 @@ const char* password = "my-password";
//#define ACCESSPOINTMASK 255,255,255,0
//const char* ssid = "my-demo";
//const char* password = "my-password";

/* Uncomment to set a custom name for the Device */
//#define DEVICENAME "ESP32/VL53L1X Demo"

/*
* Over The Air firmware updates can be disabled by uncommenting the folowing line
*/
//#define NO_OTA

/*
* OTA can be password protected to prevent the device being hijacked
*/
//#define OTA_PASSWORD "SuperVisor"

0 comments on commit a682b7f

Please sign in to comment.