From 814df25aba4acf5dbcae13639713e7532a587d47 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Fri, 21 Feb 2025 17:59:50 +0100 Subject: [PATCH] First steps --- Arduino/ArduinoUtils.cpp | 72 +++++++++++++++------ Arduino/{UdpArduino.cpp => Participant.cpp} | 11 +++- Arduino/{UdpArduino.h => Participant.h} | 2 +- Participant.cpp | 2 +- Participant.h | 2 +- Sensors/DigitalSensor.cpp | 11 ++++ Sensors/DigitalSensor.h | 23 +++++++ Thing.cpp | 12 +++- Thing.h | 4 ++ 9 files changed, 114 insertions(+), 25 deletions(-) rename Arduino/{UdpArduino.cpp => Participant.cpp} (90%) rename Arduino/{UdpArduino.h => Participant.h} (94%) create mode 100644 Sensors/DigitalSensor.cpp create mode 100644 Sensors/DigitalSensor.h diff --git a/Arduino/ArduinoUtils.cpp b/Arduino/ArduinoUtils.cpp index ccc85d6..06e8ff8 100644 --- a/Arduino/ArduinoUtils.cpp +++ b/Arduino/ArduinoUtils.cpp @@ -1,11 +1,42 @@ #include "ArduinoUtils.h" + #if defined(ARDUINO) + #include +#include + +#if defined(ARDUINO_ARCH_ESP8266) #include #include +#elif defined(ESP32) +#include +#include +#endif -bool StartWifi(const char *wifiSsid, const char *wifiPassword, - bool hotspotFallback) { +const char* hotspotSSID = "Roboid"; +const char* hotspotPassword = "alchemy7000"; + +#if ESP32 +// Flash storage +#include "Preferences.h" + +#define PREFERENCES_NAMESPACE "roboidControl" +Preferences wifiPreferences; + +#define STORAGE_KEY_WIFI "rc/wifi" +struct WifiCredentials { + char ssid[32] = "\0"; + char password[32] = "\0"; +} credentials; + +#define STORAGE_KEY_NSS "rc/nss" +struct NssServer { + char ipAddress[16] = "127.0.0.1\0"; + unsigned short port = 7681; +} nssServer; +#endif + +bool StartWifi(const char* wifiSsid, const char* wifiPassword, bool hotspotFallback) { #if UNO_R4 || ARDUINO_ARCH_RP2040 if (WiFi.status() == WL_NO_MODULE) { Serial.println("WiFi not present, WiFiSync is disabled"); @@ -83,10 +114,8 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword, #if ESP32 printf("Checking credentials in flash\n"); wifiPreferences.begin(PREFERENCES_NAMESPACE); - wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials, - sizeof(credentials)); - if (strcmp(wifiSsid, credentials.ssid) != 0 || - strcmp(wifiPassword, credentials.password) != 0) { + wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials)); + if (strcmp(wifiSsid, credentials.ssid) != 0 || strcmp(wifiPassword, credentials.password) != 0) { printf("Updating credentials in flash..."); const int ssidLen = strlen(wifiSsid); if (ssidLen < 32) { @@ -99,8 +128,7 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword, memcpy(credentials.password, wifiPassword, pwdLen); credentials.password[pwdLen] = '\0'; } - wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials, - sizeof(credentials)); + wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials)); printf(" completed.\n"); } wifiPreferences.end(); @@ -132,18 +160,26 @@ void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) { Serial.println("Preparing to update firmware."); String firmwareURL = url + FIRMWARE_NAME + ".bin"; +#if defined(ESP32) + t_httpUpdate_return ret = httpUpdate.update(client, firmwareURL); +#else t_httpUpdate_return ret = ESPhttpUpdate.update(client, firmwareURL); +#endif 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; + case HTTP_UPDATE_FAILED: +#if defined(ESP32) + Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(), + httpUpdate.getLastErrorString().c_str()); +#else + Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", ESPhttpUpdate.getLastError(), + ESPhttpUpdate.getLastErrorString().c_str()); +#endif + 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."); diff --git a/Arduino/UdpArduino.cpp b/Arduino/Participant.cpp similarity index 90% rename from Arduino/UdpArduino.cpp rename to Arduino/Participant.cpp index f67cce6..838aa1c 100644 --- a/Arduino/UdpArduino.cpp +++ b/Arduino/Participant.cpp @@ -1,8 +1,13 @@ -#include "UdpArduino.h" +#include "Participant.h" #if defined(ARDUINO) +#if defined(ARDUINO_ARCH_ESP8266) #include +#elif defined(ESP32) +#include #endif +#endif + namespace Passer { namespace RoboidControl { namespace Arduino { @@ -45,7 +50,7 @@ void Participant::Receive() { senderAddress.toCharArray(sender_ipAddress, 16); int sender_port = udp.remotePort(); - Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port); + RoboidControl::Participant* remoteParticipant = this->GetParticipant(sender_ipAddress, sender_port); if (remoteParticipant == nullptr) { remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); // std::cout << "New sender " << sender_ipAddress << ":" << sender_port @@ -75,7 +80,7 @@ bool Participant::Send(RemoteParticipant* remoteParticipant, int bufferSize) { bool Participant::Publish(IMessage* msg) { #ifdef ARDUINO - int bufferSize = msg->Serialize(this->buffer); + int bufferSize = msg->Serialize((char*)this->buffer); if (bufferSize <= 0) return true; diff --git a/Arduino/UdpArduino.h b/Arduino/Participant.h similarity index 94% rename from Arduino/UdpArduino.h rename to Arduino/Participant.h index ddc30c6..dc754c2 100644 --- a/Arduino/UdpArduino.h +++ b/Arduino/Participant.h @@ -1,6 +1,6 @@ #pragma once -#include "Participant.h" +#include "../Participant.h" namespace Passer { namespace RoboidControl { diff --git a/Participant.cpp b/Participant.cpp index a0e18dc..7f1f4c4 100644 --- a/Participant.cpp +++ b/Participant.cpp @@ -2,7 +2,7 @@ #include "Thing.h" -#include "Arduino/UdpArduino.h" +#include "Arduino/Participant.h" #include "Posix/Participant.h" #include "Windows/Participant.h" diff --git a/Participant.h b/Participant.h index da5b169..47a355d 100644 --- a/Participant.h +++ b/Participant.h @@ -30,7 +30,7 @@ namespace RoboidControl { /// @brief A participant is device which can communicate with other participants class Participant : public RemoteParticipant { public: - char buffer[1024]; + unsigned char buffer[1024]; long publishInterval = 3000; // 3 seconds // unsigned char networkId = 0; diff --git a/Sensors/DigitalSensor.cpp b/Sensors/DigitalSensor.cpp new file mode 100644 index 0000000..de21a15 --- /dev/null +++ b/Sensors/DigitalSensor.cpp @@ -0,0 +1,11 @@ +#include "DigitalSensor.h" + +namespace Passer { +namespace RoboidControl { + +DigitalSensor::DigitalSensor() {} + +DigitalSensor::DigitalSensor(unsigned char networkId, unsigned char thingId) {} + +} // namespace RoboidControl +} // namespace Passer diff --git a/Sensors/DigitalSensor.h b/Sensors/DigitalSensor.h new file mode 100644 index 0000000..8328b33 --- /dev/null +++ b/Sensors/DigitalSensor.h @@ -0,0 +1,23 @@ +#pragma once + +#include "Thing.h" + +namespace Passer { +namespace RoboidControl { + +/// @brief A digital (on/off, 1/0, true/false) sensor +class DigitalSensor : public Thing { + public: + /// @brief The sigital state + bool state = 0; + + /// @brief The default constructor + DigitalSensor(); + /// @brief Create a temperature sensor with the given ID + /// @param networkId The network ID of the sensor + /// @param thingId The ID of the thing + DigitalSensor(unsigned char networkId, unsigned char thingId); +}; + +} // namespace RoboidControl +} // namespace Passer \ No newline at end of file diff --git a/Thing.cpp b/Thing.cpp index 8b701d5..b1c2266 100644 --- a/Thing.cpp +++ b/Thing.cpp @@ -6,6 +6,9 @@ #include #include +namespace Passer { + namespace RoboidControl { + Thing::Thing(Type thingType) : Thing((unsigned char)thingType) {} Thing::Thing(unsigned char thingType) { @@ -142,6 +145,12 @@ Thing *Thing::GetChildByIndex(unsigned char ix) { return this->children[ix]; } void Thing::SetModel(const char *url) { this->modelUrl = url; } +#if defined(ARDUINO) +void Thing::Update() { + Update(millis()); +} +#endif + void Thing::GenerateBinary(char *buffer, unsigned char *ix) { (void)buffer; (void)ix; @@ -210,4 +219,5 @@ Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; } // thing->Update(currentTimeMs); // } // } -//} \ No newline at end of file +//} + }} \ No newline at end of file diff --git a/Thing.h b/Thing.h index 59cccf9..97845e4 100644 --- a/Thing.h +++ b/Thing.h @@ -154,6 +154,10 @@ public: /// the only official supported model format is .obj void SetModel(const char *url); + #if defined(ARDUINO) + void Update(); + #endif + /// @brief Updates the state of the thing /// @param currentTimeMs The current clock time in milliseconds virtual void Update(unsigned long currentTimeMs) { (void)currentTimeMs; };