OTA support for PlatformIO cli
This commit is contained in:
parent
221bbe0f91
commit
c386b24fde
|
@ -24,6 +24,7 @@ extern "C" {
|
||||||
#include <BLEAdvertisedDevice.h>
|
#include <BLEAdvertisedDevice.h>
|
||||||
#include <AsyncMqttClient.h>
|
#include <AsyncMqttClient.h>
|
||||||
#include <ArduinoJSON.h>
|
#include <ArduinoJSON.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
#include "BLEBeacon.h"
|
#include "BLEBeacon.h"
|
||||||
#include "BLEEddystoneTLM.h"
|
#include "BLEEddystoneTLM.h"
|
||||||
#include "BLEEddystoneURL.h"
|
#include "BLEEddystoneURL.h"
|
||||||
|
@ -32,9 +33,11 @@ extern "C" {
|
||||||
BLEScan* pBLEScan;
|
BLEScan* pBLEScan;
|
||||||
int scanTime = 5; //In seconds
|
int scanTime = 5; //In seconds
|
||||||
int waitTime = scanInterval; //In seconds
|
int waitTime = scanInterval; //In seconds
|
||||||
|
bool updateInProgress = false;
|
||||||
|
|
||||||
uint16_t beconUUID = 0xFEAA;
|
uint16_t beconUUID = 0xFEAA;
|
||||||
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
|
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
|
||||||
|
#define LED_BUILTIN 2
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
AsyncMqttClient mqttClient;
|
AsyncMqttClient mqttClient;
|
||||||
|
@ -99,12 +102,14 @@ void WiFiEvent(WiFiEvent_t event) {
|
||||||
Serial.println(event);
|
Serial.println(event);
|
||||||
switch(event) {
|
switch(event) {
|
||||||
case SYSTEM_EVENT_STA_GOT_IP:
|
case SYSTEM_EVENT_STA_GOT_IP:
|
||||||
|
digitalWrite(LED_BUILTIN, 0);
|
||||||
Serial.println("WiFi connected");
|
Serial.println("WiFi connected");
|
||||||
Serial.println("IP address: ");
|
Serial.println("IP address: ");
|
||||||
Serial.println(WiFi.localIP());
|
Serial.println(WiFi.localIP());
|
||||||
connectToMqtt();
|
connectToMqtt();
|
||||||
break;
|
break;
|
||||||
case SYSTEM_EVENT_STA_DISCONNECTED:
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
||||||
|
digitalWrite(LED_BUILTIN, 1);
|
||||||
Serial.println("WiFi lost connection");
|
Serial.println("WiFi lost connection");
|
||||||
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
xTimerStop(mqttReconnectTimer, 0); // ensure we don't reconnect to MQTT while reconnecting to Wi-Fi
|
||||||
xTimerStart(wifiReconnectTimer, 0);
|
xTimerStart(wifiReconnectTimer, 0);
|
||||||
|
@ -278,7 +283,10 @@ class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
|
||||||
|
|
||||||
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
void onResult(BLEAdvertisedDevice advertisedDevice) {
|
||||||
|
|
||||||
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
|
digitalWrite(LED_BUILTIN, 1);
|
||||||
|
// Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
|
||||||
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
|
digitalWrite(LED_BUILTIN, 0);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -290,15 +298,12 @@ TaskHandle_t BLEScan;
|
||||||
|
|
||||||
void scanForDevices(void * parameter) {
|
void scanForDevices(void * parameter) {
|
||||||
while(1) {
|
while(1) {
|
||||||
if (WiFi.isConnected() && mqttClient.connected() && (millis() - last > (waitTime * 1000) || last == 0)) {
|
if (!updateInProgress && WiFi.isConnected() && mqttClient.connected() && (millis() - last > (waitTime * 1000) || last == 0)) {
|
||||||
Serial.print("Scanning...\t");
|
Serial.print("Scanning...\t");
|
||||||
BLEScanResults foundDevices = pBLEScan->start(scanTime);
|
BLEScanResults foundDevices = pBLEScan->start(scanTime);
|
||||||
Serial.printf("Scan done! Devices found: %d\t",foundDevices.getCount());
|
Serial.printf("Scan done! Devices found: %d\t",foundDevices.getCount());
|
||||||
for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
|
for (uint32_t i = 0; i < foundDevices.getCount(); i++) {
|
||||||
// Serial.printf("Getting device %d",i);
|
|
||||||
|
|
||||||
reportDevice(foundDevices.getDevice(i));
|
reportDevice(foundDevices.getDevice(i));
|
||||||
|
|
||||||
}
|
}
|
||||||
last = millis();
|
last = millis();
|
||||||
Serial.println("Reports sent");
|
Serial.println("Reports sent");
|
||||||
|
@ -306,10 +311,48 @@ void scanForDevices(void * parameter) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void configureOTA() {
|
||||||
|
ArduinoOTA
|
||||||
|
.onStart([]() {
|
||||||
|
updateInProgress = true;
|
||||||
|
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([]() {
|
||||||
|
updateInProgress = false;
|
||||||
|
digitalWrite(LED_BUILTIN, 0);
|
||||||
|
Serial.println("\nEnd");
|
||||||
|
})
|
||||||
|
.onProgress([](unsigned int progress, unsigned int total) {
|
||||||
|
byte percent = (progress / (total / 100));
|
||||||
|
Serial.printf("Progress: %u%%\n", percent);
|
||||||
|
digitalWrite(LED_BUILTIN, percent % 2);
|
||||||
|
})
|
||||||
|
.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");
|
||||||
|
ESP.restart();
|
||||||
|
});
|
||||||
|
|
||||||
|
ArduinoOTA.begin();
|
||||||
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
|
pinMode(LED_BUILTIN, OUTPUT);
|
||||||
|
|
||||||
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToMqtt));
|
||||||
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, (void*)0, reinterpret_cast<TimerCallbackFunction_t>(connectToWifi));
|
||||||
|
|
||||||
|
@ -323,9 +366,11 @@ void setup() {
|
||||||
|
|
||||||
connectToWifi();
|
connectToWifi();
|
||||||
|
|
||||||
|
configureOTA();
|
||||||
|
|
||||||
BLEDevice::init("");
|
BLEDevice::init("");
|
||||||
pBLEScan = BLEDevice::getScan(); //create new scan
|
pBLEScan = BLEDevice::getScan(); //create new scan
|
||||||
// pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
|
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
|
||||||
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
|
||||||
// pBLEScan->setInterval(100);
|
// pBLEScan->setInterval(100);
|
||||||
// pBLEScan->setWindow(200);
|
// pBLEScan->setWindow(200);
|
||||||
|
@ -345,4 +390,5 @@ void loop() {
|
||||||
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
|
TIMERG0.wdt_wprotect=TIMG_WDT_WKEY_VALUE;
|
||||||
TIMERG0.wdt_feed=1;
|
TIMERG0.wdt_feed=1;
|
||||||
TIMERG0.wdt_wprotect=0;
|
TIMERG0.wdt_wprotect=0;
|
||||||
|
ArduinoOTA.handle();
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,9 @@ Create a copy of the `Settings.h` file, and rename it to `Settings_local.h`. Thi
|
||||||
|
|
||||||
Open the folder in Atom, using the `open project` option in the PlatformIO Home screen. Modify the settings in the `Settings_local.h` file to match your environment. Set the correct port in the `platformio.ini` file (or remove the line to use auto-detection), and upload to the board. Open the serial monitor after successful upload to check for success.
|
Open the folder in Atom, using the `open project` option in the PlatformIO Home screen. Modify the settings in the `Settings_local.h` file to match your environment. Set the correct port in the `platformio.ini` file (or remove the line to use auto-detection), and upload to the board. Open the serial monitor after successful upload to check for success.
|
||||||
|
|
||||||
|
### OTA Support
|
||||||
|
It is possible to update the device using "Over the Air" (OTA) updates from the command line interface of PlatformIO. You will need to know the IP address of the device itself (check your router). From the command line, enter the command `platformio run -t upload --upload-port {{Device IP Address}}`
|
||||||
|
|
||||||
## Home Assistant Configuration
|
## Home Assistant Configuration
|
||||||
Once the device is running, it is important to configure Home Assistant to use the information from the MQTT topic to determine what devices to track. You can read the full documentation [on the Home Assistant website](https://www.home-assistant.io/components/sensor.mqtt_room/). It is critical that you configure your device IDs to include the Major version, but ignore Minor version (i.e. set it to 0). This is to match the configuration used on the ESP32.
|
Once the device is running, it is important to configure Home Assistant to use the information from the MQTT topic to determine what devices to track. You can read the full documentation [on the Home Assistant website](https://www.home-assistant.io/components/sensor.mqtt_room/). It is critical that you configure your device IDs to include the Major version, but ignore Minor version (i.e. set it to 0). This is to match the configuration used on the ESP32.
|
||||||
|
|
||||||
|
@ -48,5 +51,5 @@ Unfortunately, Apple does not allow devices to advertise iBeacon data in the bac
|
||||||
- [x] Scan interval Settings
|
- [x] Scan interval Settings
|
||||||
- [ ] Configuration via Web UI
|
- [ ] Configuration via Web UI
|
||||||
- [ ] Wifi Manager for managing access point credentials
|
- [ ] Wifi Manager for managing access point credentials
|
||||||
- [ ] Implement Over-The-Air (OTA) updates
|
- [x] Implement Over-The-Air (OTA) updates
|
||||||
- [x] Build and upload via [PlatformIO](platformio.org)
|
- [x] Build and upload via [PlatformIO](platformio.org)
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
# Name, Type, SubType, Offset, Size, Flags
|
||||||
|
nvs, data, nvs, 0x9000, 0x5000,
|
||||||
|
otadata, data, ota, 0xe000, 0x2000,
|
||||||
|
app0, app, ota_0, 0x10000, 0x1E0000,
|
||||||
|
app1, app, ota_1, 0x1F0000,0x1E0000,
|
||||||
|
eeprom, data, 0x99, 0x3F0000,0x1000,
|
||||||
|
spiffs, data, spiffs, 0x3F1000,0xF000,
|
|
Loading…
Reference in New Issue