implement connecting to wifi on OTA request
This commit is contained in:
parent
2ee22f12e6
commit
8096c8335b
6 changed files with 109 additions and 44 deletions
|
|
@ -8,7 +8,7 @@ build_image:
|
|||
echo "#define BUILD_NUMBER $CI_PIPELINE_IID
|
||||
#define BUILD_DATE \"$(date '+%Y-%m-%d %H:%M:%S')\"
|
||||
#define BUILD_BRANCH \"$CI_COMMIT_REF_NAME\"
|
||||
#define BUILD_COMMIT \"$(echo $CI_COMMIT_SHA| cut -c1-8)\"" | tee src/buildinfo.h
|
||||
#define BUILD_COMMIT \"$(echo $CI_COMMIT_SHA| cut -c1-8)\"" | tee lib/buildinfo/buildinfo.h
|
||||
- pio run
|
||||
- mv .pio/build/heltec_wifi_lora_32_V3/firmware.bin firmware.bin
|
||||
artifacts:
|
||||
|
|
|
|||
10
.vscode/settings.json
vendored
10
.vscode/settings.json
vendored
|
|
@ -4,6 +4,14 @@
|
|||
"fstream": "cpp",
|
||||
"istream": "cpp",
|
||||
"ostream": "cpp",
|
||||
"sstream": "cpp"
|
||||
"sstream": "cpp",
|
||||
"array": "cpp",
|
||||
"deque": "cpp",
|
||||
"string": "cpp",
|
||||
"vector": "cpp",
|
||||
"string_view": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"regex": "cpp",
|
||||
"new": "cpp"
|
||||
}
|
||||
}
|
||||
|
|
@ -14,4 +14,5 @@ board = heltec_wifi_lora_32_V3
|
|||
framework = arduino
|
||||
lib_deps =
|
||||
jgromes/RadioLib@7.1.2
|
||||
jgromes/RadioBoards@^1.0.0
|
||||
jgromes/RadioBoards@^1.0.0
|
||||
tzapu/WiFiManager@^2.0.17
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef _RADIOLIB_EX_LORAWAN_CONFIG_H
|
||||
#define _RADIOLIB_EX_LORAWAN_CONFIG_H
|
||||
|
||||
|
||||
#include <WiFiManager.h>
|
||||
#include <RadioLib.h>
|
||||
|
||||
// first you have to set your radio model and pin configuration
|
||||
|
|
@ -149,4 +151,8 @@ uint64_t getDevEUI();
|
|||
|
||||
#define SAMPLE_COUNT 50
|
||||
#define SAMPLE_SLEEP 20 * 1000 // Needs to be in microseconds
|
||||
|
||||
|
||||
#define OTAURL "https://dus.s3.jonasled.de/lorapowermeter/" + BUILD_BRANCH + ".bin"
|
||||
|
||||
#endif
|
||||
132
src/main.cpp
132
src/main.cpp
|
|
@ -3,7 +3,7 @@
|
|||
Preferences store;
|
||||
|
||||
// LoRaWAN config, credentials & pinmap
|
||||
#include "config.h"
|
||||
#include "config.h"
|
||||
#include "buildinfo.h"
|
||||
|
||||
#include <RadioLib.h>
|
||||
|
|
@ -17,25 +17,32 @@ RTC_DATA_ATTR uint8_t LWsession[RADIOLIB_LORAWAN_SESSION_BUF_SIZE];
|
|||
float getBatVoltage();
|
||||
float batVoltage;
|
||||
uint16_t getCurrentSensorMv(int gpio);
|
||||
WiFiManager wm;
|
||||
void runOtaUpdate();
|
||||
|
||||
// abbreviated version from the Arduino-ESP32 package, see
|
||||
// https://espressif-docs.readthedocs-hosted.com/projects/arduino-esp32/en/latest/api/deepsleep.html
|
||||
// for the complete set of options
|
||||
void print_wakeup_reason() {
|
||||
void print_wakeup_reason()
|
||||
{
|
||||
esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause();
|
||||
if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER) {
|
||||
if (wakeup_reason == ESP_SLEEP_WAKEUP_TIMER)
|
||||
{
|
||||
Serial.println(F("Wake from sleep"));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.print(F("Wake not caused by deep sleep: "));
|
||||
Serial.println(wakeup_reason);
|
||||
}
|
||||
|
||||
Serial.print(F("Boot count: "));
|
||||
Serial.println(++bootCount); // increment before printing
|
||||
Serial.println(++bootCount); // increment before printing
|
||||
}
|
||||
|
||||
// put device in to lowest power deep-sleep mode
|
||||
void gotoSleep(uint32_t seconds) {
|
||||
void gotoSleep(uint32_t seconds)
|
||||
{
|
||||
esp_sleep_enable_timer_wakeup(seconds * 1000UL * 1000UL); // function uses uS
|
||||
Serial.println(F("Sleeping\n"));
|
||||
Serial.flush();
|
||||
|
|
@ -49,10 +56,11 @@ void gotoSleep(uint32_t seconds) {
|
|||
ESP.restart();
|
||||
}
|
||||
|
||||
int16_t lwActivate() {
|
||||
int16_t lwActivate()
|
||||
{
|
||||
int16_t state = RADIOLIB_ERR_UNKNOWN;
|
||||
|
||||
char devEUIstr[16]; // 16 hex chars + null terminator
|
||||
char devEUIstr[16]; // 16 hex chars + null terminator
|
||||
(String(ESP.getEfuseMac(), HEX) + "0000").toCharArray(devEUIstr, 17);
|
||||
Serial.print("DEVEUI: ");
|
||||
Serial.println(devEUIstr);
|
||||
|
|
@ -64,10 +72,11 @@ int16_t lwActivate() {
|
|||
// ##### setup the flash storage
|
||||
store.begin("radiolib");
|
||||
// ##### if we have previously saved nonces, restore them and try to restore session as well
|
||||
if (store.isKey("nonces")) {
|
||||
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
|
||||
store.getBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // get them from the store
|
||||
state = node.setBufferNonces(buffer); // send them to LoRaWAN
|
||||
if (store.isKey("nonces"))
|
||||
{
|
||||
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
|
||||
store.getBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // get them from the store
|
||||
state = node.setBufferNonces(buffer); // send them to LoRaWAN
|
||||
debug(state != RADIOLIB_ERR_NONE, F("Restoring nonces buffer failed"), state, false);
|
||||
|
||||
// recall session from RTC deep-sleep preserved variable
|
||||
|
|
@ -79,40 +88,44 @@ int16_t lwActivate() {
|
|||
|
||||
// if Nonces and Session restored successfully, activation is just a formality
|
||||
// moreover, Nonces didn't change so no need to re-save them
|
||||
if (state == RADIOLIB_ERR_NONE) {
|
||||
if (state == RADIOLIB_ERR_NONE)
|
||||
{
|
||||
Serial.println(F("Succesfully restored session - now activating"));
|
||||
state = node.activateOTAA();
|
||||
debug((state != RADIOLIB_LORAWAN_SESSION_RESTORED), F("Failed to activate restored session"), state, true);
|
||||
|
||||
// ##### close the store before returning
|
||||
store.end();
|
||||
return(state);
|
||||
return (state);
|
||||
}
|
||||
|
||||
} else { // store has no key "nonces"
|
||||
}
|
||||
else
|
||||
{ // store has no key "nonces"
|
||||
Serial.println(F("No Nonces saved - starting fresh."));
|
||||
}
|
||||
|
||||
// if we got here, there was no session to restore, so start trying to join
|
||||
state = RADIOLIB_ERR_NETWORK_NOT_JOINED;
|
||||
while (state != RADIOLIB_LORAWAN_NEW_SESSION) {
|
||||
while (state != RADIOLIB_LORAWAN_NEW_SESSION)
|
||||
{
|
||||
Serial.println(F("Join ('login') to the LoRaWAN Network"));
|
||||
state = node.activateOTAA();
|
||||
|
||||
// ##### save the join counters (nonces) to permanent store
|
||||
Serial.println(F("Saving nonces to flash"));
|
||||
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
|
||||
uint8_t *persist = node.getBufferNonces(); // get pointer to nonces
|
||||
memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // copy in to buffer
|
||||
uint8_t buffer[RADIOLIB_LORAWAN_NONCES_BUF_SIZE]; // create somewhere to store nonces
|
||||
uint8_t *persist = node.getBufferNonces(); // get pointer to nonces
|
||||
memcpy(buffer, persist, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // copy in to buffer
|
||||
store.putBytes("nonces", buffer, RADIOLIB_LORAWAN_NONCES_BUF_SIZE); // send them to the store
|
||||
|
||||
|
||||
// we'll save the session after an uplink
|
||||
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION) {
|
||||
if (state != RADIOLIB_LORAWAN_NEW_SESSION)
|
||||
{
|
||||
Serial.print(F("Join failed: "));
|
||||
Serial.println(state);
|
||||
|
||||
// how long to wait before join attempts. This is an interim solution pending
|
||||
// how long to wait before join attempts. This is an interim solution pending
|
||||
// implementation of TS001 LoRaWAN Specification section #7 - this doc applies to v1.0.4 & v1.1
|
||||
// it sleeps for longer & longer durations to give time for any gateway issues to resolve
|
||||
// or whatever is interfering with the device <-> gateway airwaves.
|
||||
|
|
@ -133,15 +146,16 @@ int16_t lwActivate() {
|
|||
// reset the failed join count
|
||||
bootCountSinceUnsuccessfulJoin = 0;
|
||||
|
||||
delay(1000); // hold off off hitting the airwaves again too soon - an issue in the US
|
||||
|
||||
delay(1000); // hold off off hitting the airwaves again too soon - an issue in the US
|
||||
|
||||
// ##### close the store
|
||||
store.end();
|
||||
return(state);
|
||||
store.end();
|
||||
return (state);
|
||||
}
|
||||
|
||||
// setup & execute all device functions ...
|
||||
void setup() {
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println(F("\nSetup"));
|
||||
print_wakeup_reason();
|
||||
|
|
@ -172,7 +186,7 @@ void setup() {
|
|||
|
||||
digitalWrite(Vext, HIGH); // disable external power
|
||||
|
||||
int16_t state = 0; // return value for calls to RadioLib
|
||||
int16_t state = 0; // return value for calls to RadioLib
|
||||
|
||||
// setup the radio based on the pinmap (connections) in config.h
|
||||
Serial.println(F("Initalise the radio"));
|
||||
|
|
@ -208,31 +222,55 @@ void setup() {
|
|||
uplinkPayload[12] = highByte(currentSensor6);
|
||||
uplinkPayload[13] = lowByte(BUILD_NUMBER);
|
||||
uplinkPayload[14] = highByte(BUILD_NUMBER);
|
||||
|
||||
|
||||
// perform an uplink
|
||||
state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload));
|
||||
uint8_t downlink[1];
|
||||
size_t downlinkSize;
|
||||
state = node.sendReceive(uplinkPayload, sizeof(uplinkPayload), 1, downlink, &downlinkSize);
|
||||
debug((state < RADIOLIB_ERR_NONE) && (state != RADIOLIB_ERR_NONE), F("Error in sendReceive"), state, false);
|
||||
|
||||
Serial.print(F("FCntUp: "));
|
||||
Serial.println(node.getFCntUp());
|
||||
|
||||
if (state > 0)
|
||||
{
|
||||
Serial.println(F("Received a downlink"));
|
||||
Serial.print(F("[LoRaWAN] Data:\t\t"));
|
||||
if (downlinkSize > 0)
|
||||
{
|
||||
arrayDump(downlink, downlinkSize);
|
||||
if (downlink[0] == 1)
|
||||
{
|
||||
runOtaUpdate();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("<MAC commands only>"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println(F("No downlink received"));
|
||||
}
|
||||
|
||||
// now save session to RTC memory
|
||||
uint8_t *persist = node.getBufferSession();
|
||||
memcpy(LWsession, persist, RADIOLIB_LORAWAN_SESSION_BUF_SIZE);
|
||||
|
||||
|
||||
// wait until next uplink - observing legal & TTN FUP constraints
|
||||
gotoSleep(uplinkIntervalSeconds);
|
||||
|
||||
}
|
||||
|
||||
|
||||
uint64_t getDevEUI() {
|
||||
uint64_t getDevEUI()
|
||||
{
|
||||
uint64_t mac = ESP.getEfuseMac();
|
||||
uint64_t devEUI = (mac << 16); // Append 0000 to the end
|
||||
uint64_t devEUI = (mac << 16); // Append 0000 to the end
|
||||
return devEUI;
|
||||
}
|
||||
|
||||
float getBatVoltage() {
|
||||
float getBatVoltage()
|
||||
{
|
||||
pinMode(VBAT_CTRL, OUTPUT);
|
||||
digitalWrite(VBAT_CTRL, HIGH);
|
||||
delay(10);
|
||||
|
|
@ -241,11 +279,13 @@ float getBatVoltage() {
|
|||
return vbat;
|
||||
}
|
||||
|
||||
uint16_t getCurrentSensorMv(int gpio) {
|
||||
uint16_t getCurrentSensorMv(int gpio)
|
||||
{
|
||||
pinMode(gpio, INPUT);
|
||||
uint32_t data = 0;
|
||||
|
||||
for (byte i = 0; i < SAMPLE_COUNT; i++) {
|
||||
for (byte i = 0; i < SAMPLE_COUNT; i++)
|
||||
{
|
||||
data += analogReadMilliVolts(gpio);
|
||||
delayMicroseconds(SAMPLE_SLEEP);
|
||||
}
|
||||
|
|
@ -258,9 +298,19 @@ uint16_t getCurrentSensorMv(int gpio) {
|
|||
return (uint16_t)data;
|
||||
}
|
||||
|
||||
|
||||
// The ESP32 wakes from deep-sleep and starts from the very beginning.
|
||||
// It then goes back to sleep, so loop() is never called and which is
|
||||
// why it is empty.
|
||||
|
||||
void loop() {}
|
||||
void loop() {}
|
||||
|
||||
void runOtaUpdate()
|
||||
{
|
||||
Serial.println("Received OTA request");
|
||||
if (!wm.autoConnect())
|
||||
{
|
||||
Serial.println("Failed to connect to wifi");
|
||||
return;
|
||||
}
|
||||
Serial.println("OTA done");
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue