From 5e77c79071210c770d32af38306f3d9e6944ff03 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 7 Aug 2024 23:01:54 +0200 Subject: [PATCH] add ota --- otaflash.sh | 18 +++++++++ platformio.ini | 6 ++- src/main.cpp | 17 +++++++- src/simpleota.cpp | 99 +++++++++++++++++++++++++++++++++++++++++++++++ src/simpleota.h | 33 ++++++++++++++++ 5 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 otaflash.sh create mode 100644 src/simpleota.cpp create mode 100644 src/simpleota.h diff --git a/otaflash.sh b/otaflash.sh new file mode 100644 index 0000000..54f51fd --- /dev/null +++ b/otaflash.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +do_this_on_ctrl_c(){ + echo "Stopped searching!" + exit 0 +} + +trap 'do_this_on_ctrl_c' SIGINT + +printf "ip: %s" "$1" +printf "\n%s" "waiting for Device ..." +while ! ping -c 1 -n -w 1 $1 &> /dev/null +do + printf "%c" "." +done +printf "\n%s\n" "Device is online" +pio run -t upload --upload-port $1 + diff --git a/platformio.ini b/platformio.ini index f88909e..962e722 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,6 +14,10 @@ board = d1_mini framework = arduino monitor_speed = 115200 +upload_speed = 921600 lib_deps = - adafruit/Adafruit NeoPixel @ ^1.7.0 \ No newline at end of file + adafruit/Adafruit NeoPixel @ ^1.7.0 + +upload_flags = + --auth=ROLLERCOASTER diff --git a/src/main.cpp b/src/main.cpp index 5b9092b..5153269 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,7 @@ //flash as Wemos D1 R2 & mini #include +#include "simpleota.h" #include #ifdef __AVR__ @@ -103,6 +104,7 @@ void setup() { pinMode(PIN_LDR, INPUT); #endif + EEPROM.begin(4096); //set eeprom size, between 4 and 4096 bytes. @@ -110,6 +112,13 @@ void setup() { strip.setBrightness(BRIGHTNESS_RUN); //150 strip.show(); // Initialize all pixels to 'off' + + + checkOTA(); + if(initOTA()) { //initialize ota if ota enabled + return; //if ota do nothing else for setup + } + Serial.println("Started"); resetHeightmap(); @@ -278,7 +287,8 @@ void spawnWagon(){ side_multi=-1; //start in backward direction //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, side_startpos, _randomlength, side_multi*random(map(_randomlength,3,30,0,40)/10.0, map(_randomlength,3,30, 5,60))/10.0 , 0 , random(3.0,7.0) , Wheel((uint8_t)random(0,255))); //spawn new wagon - Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, side_startpos, _randomlength, side_multi*random(map(_randomlength,3,30,30,100)/10.0, map(_randomlength,3,30, 5,60))/10.0 , 0 , 1.8 , Wheel((uint8_t)random(0,255))); //spawn new wagon + //Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, side_startpos, _randomlength, side_multi*random(map(_randomlength,3,30,30,100)/10.0, map(_randomlength,3,30, 5,60))/10.0 , 0 , 1.8 , Wheel((uint8_t)random(0,255))); //spawn new wagon + Wagon tmpr = Wagon(maxid++,NUMPIXELS,&strip, height, side_startpos, _randomlength, side_multi*random(map(_randomlength,3,30,3,10)/10.0, map(_randomlength,3,30, 1,10))/10.0 , 0 , 1.8 , Wheel((uint8_t)random(0,255))); //spawn new wagon . start from near standstill //Mass: the lighter the faster is changes speed @@ -304,6 +314,11 @@ void spawnWagon(float pos, float wagonlength,float startvel, float startacc, flo void loop() { + + if (loopOTA()) { + return; //just wait for ota + } + loopmillis=millis(); #ifdef PIN_LDR diff --git a/src/simpleota.cpp b/src/simpleota.cpp new file mode 100644 index 0000000..5393942 --- /dev/null +++ b/src/simpleota.cpp @@ -0,0 +1,99 @@ +#include "simpleota.h" + + +uint16_t otaWaitCounter; + + +OTA_MODE otaMode; + +void checkOTA() { + otaMode = SEARCHING; + Serial.println("looking for OTA WiFi..."); + + // WARNING: to allow ESP-NOW work, this WiFi must be on Channel 1 + if (OTA_WIFI_PASSWORD!="") { + WiFi.begin(OTA_WIFI_SSID, OTA_WIFI_PASSWORD); //wifi with password + }else{ + WiFi.begin(OTA_WIFI_SSID); //open wifi + } + + while (WiFi.waitForConnectResult() != WL_CONNECTED) { + Serial.println("no OTA WiFi found, proceed normal boot"); + otaMode = NONE; + WiFi.disconnect(); + return; + } + + otaMode = WAITING; +} + + +bool initOTA() { + if (otaMode == WAITING) { + Serial.println("connected to OTA WiFi. Waiting for firmware..."); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + + ArduinoOTA.setPassword((const char *)OTA_PASSWORD); + + ArduinoOTA.onStart([]() { + otaMode = UPDATING; + 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); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + int prog = (progress / (total / 100)); + Serial.printf("Progress: %u%%\r", prog); + + }); + ArduinoOTA.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(); + return 1; //ota ok + }else{ + return 0; //not using ota + } +} + + +bool loopOTA() { + if (otaMode != NONE) { + ArduinoOTA.handle(); + + if(otaMode == WAITING) { + static long mil = millis(); + static boolean huehott = false; + + if(millis() - mil > 100) { + huehott = !huehott; + mil = millis(); + + otaWaitCounter++; + if(otaWaitCounter >= OTA_WAIT_TIMEOUT) { + Serial.println("OTA wait timeout, proceeding normal boot"); + otaMode = NONE; + } + } + } + return true; + + } else { + return false; + } +} \ No newline at end of file diff --git a/src/simpleota.h b/src/simpleota.h new file mode 100644 index 0000000..af4c52e --- /dev/null +++ b/src/simpleota.h @@ -0,0 +1,33 @@ +#ifndef simpleota_h +#define simpleota_h + +#include +#include +#include +#include +#include + + +#define OTA_WIFI_SSID "" +#define OTA_WIFI_PASSWORD "" +#define OTA_WAIT_TIMEOUT 50 // in 0.1s increments -> 10s +#define OTA_PASSWORD "ROLLERCOASTER" //password needed for ota flashing + + + +void checkOTA(); +bool initOTA(); +bool loopOTA(); + +enum OTA_MODE { + NONE, + SEARCHING, + WAITING, + UPDATING +}; + +extern OTA_MODE otaMode; + +extern uint16_t otaWaitCounter; + +#endif \ No newline at end of file