Skip to content

Commit a80e7bd

Browse files
committed
Implement ESP-Now based wifi provisioning
1 parent cfa5e94 commit a80e7bd

7 files changed

Lines changed: 53 additions & 27 deletions

File tree

platformio.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ build_flags =
5252

5353
build_unflags = -Os -std=gnu++11 -std=gnu++17
5454

55-
; If you want to enable OTA Updates, uncomment and set OTA password here and in credentials.h
55+
; If you want to enable OTA Updates, uncomment and set OTA password here and in credentials.cpp
5656
; You can set upload_port to device's ip after it's set up for the first time
5757
; Use the same password in SlimeVR Server to let it OTA Update the device
5858
;upload_protocol = espota

src/credentials.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
1-
#pragma once
1+
/* SlimeVR Code is placed under the MIT license
2+
Copyright (c) 2025 Gorbit99 & SlimeVR Contributors
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.
21+
*/
222

323
// The OTA password is public, server should know it for OTA updates,
424
// and devices don't have any authentication anyway.
@@ -7,6 +27,7 @@
727
// firmware. We don't have any hardware buttons for the user to confirm
828
// OTA update, so this is the best way we have.
929
// OTA is allowed only for the first 60 seconds after device startup.
30+
1031
const char* otaPassword
1132
= "SlimeVR-OTA"; // YOUR OTA PASSWORD HERE, LEAVE EMPTY TO DISABLE OTA UPDATES
1233

src/network/wifihandler.cpp

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ bool WiFiNetwork::isConnected() const {
5252

5353
void WiFiNetwork::setWiFiCredentials(const char* SSID, const char* pass) {
5454
wifiProvisioning.stopSearchForProvider();
55+
wifiProvisioning.stopProvisioning();
56+
WiFi.persistent(true);
5557
tryConnecting(false, SSID, pass);
5658
retriedOnG = false;
5759
// Reset state, will get back into provisioning if can't connect
@@ -65,7 +67,7 @@ void WiFiNetwork::setUp() {
6567
// Don't need to save the already saved credentials or the hardcoded ones
6668
WiFi.persistent(false);
6769
wifiHandlerLogger.info("Setting up WiFi");
68-
WiFi.mode(WIFI_STA);
70+
WiFi.mode(WIFI_AP_STA);
6971
WiFi.hostname("SlimeVR FBT Tracker");
7072
wifiHandlerLogger.info(
7173
"Loaded credentials for SSID '%s' and pass length %d",
@@ -201,26 +203,8 @@ void WiFiNetwork::upkeep() {
201203
return;
202204
case WiFiReconnectionStatus::Failed: // Couldn't connect with second set of
203205
// credentials or server credentials
204-
// Return to the default PHY Mode N.
205-
#if ESP8266
206-
if constexpr (USE_ATTENUATION) {
207-
WiFi.setOutputPower(20.0 - ATTENUATION_N);
208-
}
209-
WiFi.setPhyMode(WIFI_PHY_MODE_11N);
210-
#endif
211-
// Start smart config
212-
if (!hadWifi && !WiFi.smartConfigDone()
213-
&& millis() - wifiConnectionTimeout
214-
>= static_cast<uint32_t>(WiFiTimeoutSeconds * 1000)) {
215-
if (WiFi.status() != WL_IDLE_STATUS) {
216-
wifiHandlerLogger.error(
217-
"Can't connect from any credentials, error: %d, reason: %s.",
218-
static_cast<int>(statusToFailure(WiFi.status())),
219-
statusToReasonString(WiFi.status())
220-
);
221-
wifiConnectionTimeout = millis();
222-
}
223-
wifiProvisioning.startProvisioning();
206+
if (startedProvisioning) {
207+
return;
224208
}
225209
wifiHandlerLogger.error(
226210
"Can't connect from any credentials, error: %d, reason: %s.",
@@ -229,6 +213,7 @@ void WiFiNetwork::upkeep() {
229213
);
230214
wifiHandlerLogger.info("Starting wifi provisioning");
231215
wifiProvisioning.startSearchForProvider();
216+
startedProvisioning = true;
232217
return;
233218
}
234219
}

src/network/wifihandler.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class WiFiNetwork {
5555
void setWiFiCredentials(const char* SSID, const char* pass);
5656
static IPAddress getAddress();
5757
WiFiReconnectionStatus getWiFiState();
58+
bool startedProvisioning = false;
5859

5960
private:
6061
static constexpr float WiFiTimeoutSeconds = 11;

src/sensorinterface/DirectSPIInterface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
THE SOFTWARE.
2222
*/
2323

24-
#include "DirectSPIInterface.h"
25-
2624
#include <Arduino.h>
2725
#include <PinInterface.h>
2826

27+
#include "DirectSPIInterface.h"
28+
2929
namespace SlimeVR {
3030

3131
DirectSPIInterface::DirectSPIInterface(SPIClass& spiClass, SPISettings spiSettings)
@@ -51,4 +51,6 @@ void DirectSPIInterface::endTransaction(PinInterface* csPin) {
5151

5252
const SPISettings& DirectSPIInterface::getSpiSettings() { return m_spiSettings; }
5353

54+
void WiFiProvisioning::delPeer(uint8_t* macAddress) { esp_now_del_peer(macAddress); }
55+
5456
} // namespace SlimeVR

src/sensorinterface/SensorInterface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323

2424
#include "SensorInterface.h"
2525

26+
#if ESP32
27+
#include <esp_now.h>
28+
#endif
29+
2630
namespace SlimeVR {
2731

2832
EmptySensorInterface EmptySensorInterface::instance;

src/serial/serialcommands.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@
3535
#endif
3636

3737
#ifdef EXT_SERIAL_COMMANDS
38-
#define CALLBACK_SIZE 7 // Increase callback size to allow for debug commands
38+
#define CALLBACK_SIZE 8 // Increase callback size to allow for debug commands
3939
#include "i2cscan.h"
4040
#endif
4141

4242
#ifndef CALLBACK_SIZE
43-
#define CALLBACK_SIZE 6 // Default callback size
43+
#define CALLBACK_SIZE 7 // Default callback size
4444
#endif
4545

4646
namespace SerialCommands {
@@ -470,13 +470,26 @@ void cmdScanI2C(CmdParser* parser) {
470470
}
471471
#endif
472472

473+
void cmdStart(CmdParser* parser) {
474+
if (parser->getParamCount() == 1) {
475+
logger.info("Usage:");
476+
logger.info(" START PROVISION: start wifi provisioning");
477+
return;
478+
}
479+
480+
if (parser->equalCmdParam(1, "PROVISION")) {
481+
wifiProvisioning.startProvisioning();
482+
}
483+
}
484+
473485
void setUp() {
474486
cmdCallbacks.addCmd("SET", &cmdSet);
475487
cmdCallbacks.addCmd("GET", &cmdGet);
476488
cmdCallbacks.addCmd("FRST", &cmdFactoryReset);
477489
cmdCallbacks.addCmd("REBOOT", &cmdReboot);
478490
cmdCallbacks.addCmd("DELCAL", &cmdDeleteCalibration);
479491
cmdCallbacks.addCmd("TCAL", &cmdTemperatureCalibration);
492+
cmdCallbacks.addCmd("START", &cmdStart);
480493
#if EXT_SERIAL_COMMANDS
481494
cmdCallbacks.addCmd("SCANI2C", &cmdScanI2C);
482495
#endif

0 commit comments

Comments
 (0)