mirror of
https://github.com/SlimeVR/SlimeVR-Tracker-ESP.git
synced 2026-04-06 02:01:57 +02:00
Added support for serial port commands
Added support to set WiFi credentials with serial command SlimeVR-Tracker-ESP#6
This commit is contained in:
@@ -8,6 +8,10 @@
|
||||
; Please visit documentation for the other options and examples
|
||||
; https://docs.platformio.org/page/projectconf.html
|
||||
|
||||
[env]
|
||||
lib_deps=
|
||||
https://github.com/pvizeli/CmdParser.git
|
||||
|
||||
[env:esp12e]
|
||||
platform = espressif8266
|
||||
board = esp12e
|
||||
|
||||
@@ -113,5 +113,5 @@
|
||||
|
||||
#define batteryADCMultiplier 1.0 / 1024.0 * 5.0
|
||||
|
||||
#define FIRMWARE_BUILD_NUMBER 1
|
||||
#define FIRMWARE_VERSION "0.0.1-REV1"
|
||||
#define FIRMWARE_BUILD_NUMBER 2
|
||||
#define FIRMWARE_VERSION "0.0.2"
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -30,6 +30,7 @@
|
||||
#include "defines.h"
|
||||
#include "credentials.h"
|
||||
#include <i2cscan.h>
|
||||
#include "serialcommands.h"
|
||||
|
||||
#if IMU == IMU_BNO080 || IMU == IMU_BNO085
|
||||
BNO080Sensor sensor{};
|
||||
@@ -91,18 +92,20 @@ void setup()
|
||||
digitalWrite(CALIBRATING_LED, HIGH);
|
||||
digitalWrite(LOADING_LED, LOW);
|
||||
|
||||
Serial.begin(serialBaudRate);
|
||||
while(!Serial); // Wait for serial to startup
|
||||
setUpSerialCommands();
|
||||
|
||||
// join I2C bus
|
||||
Wire.begin(PIN_IMU_SDA, PIN_IMU_SCL);
|
||||
Wire.setClockStretchLimit(150000L); // Default streatch limit 150mS
|
||||
Wire.setClock(100000);
|
||||
Serial.begin(serialBaudRate);
|
||||
while (!Serial)
|
||||
; // wait for connection
|
||||
|
||||
if (hasConfigStored())
|
||||
{
|
||||
loadConfig(&config);
|
||||
}
|
||||
|
||||
setConfigRecievedCallback(setConfig);
|
||||
setCommandRecievedCallback(commandRecieved);
|
||||
// Wait for IMU to boot
|
||||
@@ -139,6 +142,7 @@ void setup()
|
||||
|
||||
void loop()
|
||||
{
|
||||
serialCommandsUpdate();
|
||||
wifiUpkeep();
|
||||
otaUpdate();
|
||||
clientUpdate(&sensor, &sensor2);
|
||||
|
||||
71
src/serialcommands.cpp
Normal file
71
src/serialcommands.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/*
|
||||
SlimeVR Code is placed under the MIT license
|
||||
Copyright (c) 2021 Eiren Rain
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "serialcommands.h"
|
||||
#include "wifi.h"
|
||||
#include <CmdCallback.hpp>
|
||||
|
||||
CmdCallback<5> cmdCallbacks;
|
||||
CmdParser cmdParser;
|
||||
CmdBuffer<64> cmdBuffer;
|
||||
|
||||
void cmdSet(CmdParser * parser) {
|
||||
if(parser->equalCmdParam(1, "WIFI")) {
|
||||
if(parser->getParamCount() < 3) {
|
||||
Serial.println("[ERR] CMD SET WIFI ERROR: Too few arguments");
|
||||
Serial.println("[NOTICE] Syntax: SET WIFI \"<SSID>\" \"<PASSWORD>\"");
|
||||
} else {
|
||||
setWiFiCredentials(parser->getCmdParam(2), parser->getCmdParam(3));
|
||||
Serial.println("[OK] CMD SET WIFI OK: New wifi credentials set, reconnecting");
|
||||
}
|
||||
} else {
|
||||
Serial.println("[ERR] CMD SET ERROR: Unrecognized variable to set");
|
||||
}
|
||||
}
|
||||
|
||||
void cmdGet(CmdParser * parser) {
|
||||
}
|
||||
|
||||
void cmdReport(CmdParser * parser) {
|
||||
// TODO Health and status report
|
||||
}
|
||||
|
||||
void cmdReboot(CmdParser * parser) {
|
||||
// TODO Reboot
|
||||
}
|
||||
|
||||
void cmdFactoryReset(CmdParser * parser) {
|
||||
// TODO Factory reset
|
||||
}
|
||||
|
||||
void setUpSerialCommands() {
|
||||
cmdCallbacks.addCmd("SET", &cmdSet);
|
||||
cmdCallbacks.addCmd("GET", &cmdGet);
|
||||
cmdCallbacks.addCmd("FRST", &cmdFactoryReset);
|
||||
cmdCallbacks.addCmd("REP", &cmdReport);
|
||||
cmdCallbacks.addCmd("REBOOT", &cmdReport);
|
||||
}
|
||||
|
||||
void serialCommandsUpdate() {
|
||||
cmdCallbacks.updateCmdProcessing(&cmdParser, &cmdBuffer, &Serial);
|
||||
}
|
||||
30
src/serialcommands.h
Normal file
30
src/serialcommands.h
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
SlimeVR Code is placed under the MIT license
|
||||
Copyright (c) 2021 Eiren Rain
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef _SERIALCOMMANDS_H_
|
||||
#define _SERIALCOMMANDS_H_ 1
|
||||
|
||||
void setUpSerialCommands();
|
||||
void serialCommandsUpdate();
|
||||
|
||||
#endif // _SERIALCOMMANDS_H_
|
||||
14
src/wifi.cpp
14
src/wifi.cpp
@@ -42,6 +42,12 @@ bool isWiFiConnected() {
|
||||
return isWifiConnected;
|
||||
}
|
||||
|
||||
void setWiFiCredentials(const char * SSID, const char * pass) {
|
||||
WiFi.stopSmartConfig();
|
||||
WiFi.begin(SSID, pass);
|
||||
wifiConnectionTimeout = millis();
|
||||
}
|
||||
|
||||
void setUpWiFi(DeviceConfig * const config) {
|
||||
Serial.println("Setting up WiFi");
|
||||
WiFi.mode(WIFI_STA);
|
||||
@@ -52,7 +58,7 @@ void setUpWiFi(DeviceConfig * const config) {
|
||||
|
||||
void onConnected() {
|
||||
isWifiConnected = true;
|
||||
Serial.printf("\nConnected successfully to SSID '%s', ip address %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
|
||||
Serial.printf("Connected successfully to SSID '%s', ip address %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
|
||||
onWiFiConnected();
|
||||
}
|
||||
|
||||
@@ -60,7 +66,9 @@ void wifiUpkeep() {
|
||||
if(WiFi.status() != WL_CONNECTED) {
|
||||
reportWifiError();
|
||||
if(!WiFi.smartConfigDone() && wifiConnectionTimeout + 11000 < millis()) {
|
||||
WiFi.beginSmartConfig();
|
||||
if(WiFi.beginSmartConfig()) {
|
||||
Serial.println("SmartConfig started");
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -68,5 +76,5 @@ void wifiUpkeep() {
|
||||
onConnected();
|
||||
return;
|
||||
}
|
||||
return; // Everything else is not used with smart config
|
||||
return;
|
||||
}
|
||||
@@ -29,5 +29,6 @@
|
||||
bool isWiFiConnected();
|
||||
void setUpWiFi(DeviceConfig * const config);
|
||||
void wifiUpkeep();
|
||||
void setWiFiCredentials(const char * SSID, const char * pass);
|
||||
|
||||
#endif // _WIFI_H_
|
||||
Reference in New Issue
Block a user