moren utils

This commit is contained in:
Pascal Serrarens 2025-01-31 10:31:10 +01:00
parent f6900b8ed9
commit 9021da88ff
4 changed files with 73 additions and 7 deletions

View File

@ -1,6 +1,8 @@
#include "ArduinoUtils.h"
#if defined(ARDUINO)
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h>
bool StartWifi(const char *wifiSsid, const char *wifiPassword,
bool hotspotFallback) {
@ -106,4 +108,50 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword,
}
return (!hotSpotEnabled);
}
}
void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
Serial.println("Checking for firmware updates.");
WiFiClient client;
HTTPClient httpClient;
String versionURL = url + FIRMWARE_NAME + ".version";
httpClient.begin(client, versionURL);
int httpCode = httpClient.GET();
if (httpCode == 200) {
String newFWVersion = httpClient.getString();
Serial.print("Current firmware version: ");
Serial.println(FIRMWARE_VERSION);
Serial.print("Available firmware version: ");
Serial.println(newFWVersion);
int newVersion = newFWVersion.toInt();
if (newVersion > FIRMWARE_VERSION) {
Serial.println("Preparing to update firmware.");
String firmwareURL = url + FIRMWARE_NAME + ".bin";
t_httpUpdate_return ret = ESPhttpUpdate.update(client, firmwareURL);
switch (ret) {
case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s",
ESPhttpUpdate.getLastError(),
ESPhttpUpdate.getLastErrorString().c_str());
break;
case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
break;
}
} else {
Serial.println("No Firmware update necessary.");
}
} else {
Serial.print("Http Error: ");
Serial.println(httpCode);
}
}
#endif

View File

@ -1,4 +1,10 @@
#pragma once
#if defined(ARDUINO)
#include <Arduino.h>
bool StartWifi(const char *wifiSsid, const char *wifiPassword,
bool hotspotFallback);
bool hotspotFallback);
void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION);
#endif

View File

@ -30,7 +30,8 @@ Participant::Participant(int port) {
this->participants.push_back(this);
int randomPort = (rand() % (65535 - 49152 + 1)) + 49152;
SetupUDP(randomPort, ipAddress, port);
this->localPort = randomPort;
// SetupUDP(randomPort, ipAddress, port);
}
Participant::Participant(const char *ipAddress, int port) {
@ -40,12 +41,15 @@ Participant::Participant(const char *ipAddress, int port) {
this->participants.push_back(this);
int randomPort = (rand() % (65535 - 49152 + 1)) + 49152;
SetupUDP(randomPort, ipAddress, port);
this->localPort = randomPort;
// SetupUDP(randomPort, ipAddress, port);
}
void Passer::Control::Participant::SetupUDP(int localPort,
const char *remoteIpAddress,
int remotePort) {
void Participant::begin() {
SetupUDP(this->localPort, this->ipAddress, this->port);
}
void Participant::SetupUDP(int localPort, const char *remoteIpAddress, int remotePort) {
#if defined(_WIN32) || defined(_WIN64)
UdpWindows *thisWindows = static_cast<UdpWindows *>(this);
thisWindows->Setup(localPort, remoteIpAddress, remotePort);
@ -56,6 +60,7 @@ void Passer::Control::Participant::SetupUDP(int localPort,
UdpArduino *thisArduino = static_cast<UdpArduino *>(this);
thisArduino->Setup(localPort, remoteIpAddress, remotePort);
#endif
this->connected = true;
}
#if defined(ARDUINO)
@ -63,6 +68,9 @@ void Participant::Update() { this->Update(millis()); }
#endif
void Participant::Update(unsigned long currentTimeMs) {
if (this->connected == false)
begin();
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
ClientMsg *msg = new ClientMsg(this->networkId);
this->Publish(msg);

View File

@ -35,6 +35,7 @@ public:
const char *ipAddress = "0.0.0.0";
int port = 0;
int localPort = 0;
#if defined(ARDUINO)
const char *remoteIpAddress = nullptr;
@ -62,6 +63,9 @@ public:
// i.e.
// Participant p = Participant("127.0.0.1", 8000);
void begin();
bool connected = false;
#if defined(ARDUINO)
virtual void Update();
#endif