diff --git a/ArduinoUtils.cpp b/ArduinoUtils.cpp index 953b6c1..0229c8c 100644 --- a/ArduinoUtils.cpp +++ b/ArduinoUtils.cpp @@ -1,6 +1,8 @@ #include "ArduinoUtils.h" +#if defined(ARDUINO) #include #include +#include bool StartWifi(const char *wifiSsid, const char *wifiPassword, bool hotspotFallback) { @@ -106,4 +108,50 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword, } return (!hotSpotEnabled); -} \ No newline at end of file +} + + +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 \ No newline at end of file diff --git a/ArduinoUtils.h b/ArduinoUtils.h index e00514f..f2676ed 100644 --- a/ArduinoUtils.h +++ b/ArduinoUtils.h @@ -1,4 +1,10 @@ #pragma once +#if defined(ARDUINO) +#include bool StartWifi(const char *wifiSsid, const char *wifiPassword, - bool hotspotFallback); \ No newline at end of file + bool hotspotFallback); + +void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION); + +#endif \ No newline at end of file diff --git a/Participant.cpp b/Participant.cpp index e2fc0ea..21edb60 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -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(this); thisWindows->Setup(localPort, remoteIpAddress, remotePort); @@ -56,6 +60,7 @@ void Passer::Control::Participant::SetupUDP(int localPort, UdpArduino *thisArduino = static_cast(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); diff --git a/Participant.h b/Participant.h index f32a772..f89a25e 100644 --- a/Participant.h +++ b/Participant.h @@ -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