First steps

This commit is contained in:
Pascal Serrarens 2025-02-21 17:59:50 +01:00
parent aecd5783a6
commit 814df25aba
9 changed files with 114 additions and 25 deletions

@ -1,11 +1,42 @@
#include "ArduinoUtils.h" #include "ArduinoUtils.h"
#if defined(ARDUINO) #if defined(ARDUINO)
#include <Arduino.h> #include <Arduino.h>
#include <HTTPClient.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ESP8266httpUpdate.h> #include <ESP8266httpUpdate.h>
#elif defined(ESP32)
#include <HTTPUpdate.h>
#include <WiFi.h>
#endif
bool StartWifi(const char *wifiSsid, const char *wifiPassword, const char* hotspotSSID = "Roboid";
bool hotspotFallback) { 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 UNO_R4 || ARDUINO_ARCH_RP2040
if (WiFi.status() == WL_NO_MODULE) { if (WiFi.status() == WL_NO_MODULE) {
Serial.println("WiFi not present, WiFiSync is disabled"); Serial.println("WiFi not present, WiFiSync is disabled");
@ -83,10 +114,8 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword,
#if ESP32 #if ESP32
printf("Checking credentials in flash\n"); printf("Checking credentials in flash\n");
wifiPreferences.begin(PREFERENCES_NAMESPACE); wifiPreferences.begin(PREFERENCES_NAMESPACE);
wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials, wifiPreferences.getBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials));
sizeof(credentials)); if (strcmp(wifiSsid, credentials.ssid) != 0 || strcmp(wifiPassword, credentials.password) != 0) {
if (strcmp(wifiSsid, credentials.ssid) != 0 ||
strcmp(wifiPassword, credentials.password) != 0) {
printf("Updating credentials in flash..."); printf("Updating credentials in flash...");
const int ssidLen = strlen(wifiSsid); const int ssidLen = strlen(wifiSsid);
if (ssidLen < 32) { if (ssidLen < 32) {
@ -99,8 +128,7 @@ bool StartWifi(const char *wifiSsid, const char *wifiPassword,
memcpy(credentials.password, wifiPassword, pwdLen); memcpy(credentials.password, wifiPassword, pwdLen);
credentials.password[pwdLen] = '\0'; credentials.password[pwdLen] = '\0';
} }
wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials, wifiPreferences.putBytes(STORAGE_KEY_WIFI, &credentials, sizeof(credentials));
sizeof(credentials));
printf(" completed.\n"); printf(" completed.\n");
} }
wifiPreferences.end(); wifiPreferences.end();
@ -132,18 +160,26 @@ void CheckFirmware(String url, String FIRMWARE_NAME, int FIRMWARE_VERSION) {
Serial.println("Preparing to update firmware."); Serial.println("Preparing to update firmware.");
String firmwareURL = url + FIRMWARE_NAME + ".bin"; 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); t_httpUpdate_return ret = ESPhttpUpdate.update(client, firmwareURL);
#endif
switch (ret) { switch (ret) {
case HTTP_UPDATE_FAILED: case HTTP_UPDATE_FAILED:
Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", #if defined(ESP32)
ESPhttpUpdate.getLastError(), Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", httpUpdate.getLastError(),
ESPhttpUpdate.getLastErrorString().c_str()); httpUpdate.getLastErrorString().c_str());
break; #else
case HTTP_UPDATE_NO_UPDATES: Serial.printf("HTTP_UPDATE_FAILED Error (%d): %s", ESPhttpUpdate.getLastError(),
Serial.println("HTTP_UPDATE_NO_UPDATES"); ESPhttpUpdate.getLastErrorString().c_str());
break; #endif
case HTTP_UPDATE_OK: break;
break; case HTTP_UPDATE_NO_UPDATES:
Serial.println("HTTP_UPDATE_NO_UPDATES");
break;
case HTTP_UPDATE_OK:
break;
} }
} else { } else {
Serial.println("No Firmware update necessary."); Serial.println("No Firmware update necessary.");

@ -1,8 +1,13 @@
#include "UdpArduino.h" #include "Participant.h"
#if defined(ARDUINO) #if defined(ARDUINO)
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif #endif
#endif
namespace Passer { namespace Passer {
namespace RoboidControl { namespace RoboidControl {
namespace Arduino { namespace Arduino {
@ -45,7 +50,7 @@ void Participant::Receive() {
senderAddress.toCharArray(sender_ipAddress, 16); senderAddress.toCharArray(sender_ipAddress, 16);
int sender_port = udp.remotePort(); 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) { if (remoteParticipant == nullptr) {
remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port); remoteParticipant = this->AddParticipant(sender_ipAddress, sender_port);
// std::cout << "New sender " << 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) { bool Participant::Publish(IMessage* msg) {
#ifdef ARDUINO #ifdef ARDUINO
int bufferSize = msg->Serialize(this->buffer); int bufferSize = msg->Serialize((char*)this->buffer);
if (bufferSize <= 0) if (bufferSize <= 0)
return true; return true;

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Participant.h" #include "../Participant.h"
namespace Passer { namespace Passer {
namespace RoboidControl { namespace RoboidControl {

@ -2,7 +2,7 @@
#include "Thing.h" #include "Thing.h"
#include "Arduino/UdpArduino.h" #include "Arduino/Participant.h"
#include "Posix/Participant.h" #include "Posix/Participant.h"
#include "Windows/Participant.h" #include "Windows/Participant.h"

@ -30,7 +30,7 @@ namespace RoboidControl {
/// @brief A participant is device which can communicate with other participants /// @brief A participant is device which can communicate with other participants
class Participant : public RemoteParticipant { class Participant : public RemoteParticipant {
public: public:
char buffer[1024]; unsigned char buffer[1024];
long publishInterval = 3000; // 3 seconds long publishInterval = 3000; // 3 seconds
// unsigned char networkId = 0; // unsigned char networkId = 0;

11
Sensors/DigitalSensor.cpp Normal file

@ -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

23
Sensors/DigitalSensor.h Normal file

@ -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

@ -6,6 +6,9 @@
#include <list> #include <list>
#include <string.h> #include <string.h>
namespace Passer {
namespace RoboidControl {
Thing::Thing(Type thingType) : Thing((unsigned char)thingType) {} Thing::Thing(Type thingType) : Thing((unsigned char)thingType) {}
Thing::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; } 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 Thing::GenerateBinary(char *buffer, unsigned char *ix) {
(void)buffer; (void)buffer;
(void)ix; (void)ix;
@ -210,4 +219,5 @@ Spherical16 Thing::GetAngularVelocity() { return this->angularVelocity; }
// thing->Update(currentTimeMs); // thing->Update(currentTimeMs);
// } // }
// } // }
//} //}
}}

@ -154,6 +154,10 @@ public:
/// the only official supported model format is .obj /// the only official supported model format is .obj
void SetModel(const char *url); void SetModel(const char *url);
#if defined(ARDUINO)
void Update();
#endif
/// @brief Updates the state of the thing /// @brief Updates the state of the thing
/// @param currentTimeMs The current clock time in milliseconds /// @param currentTimeMs The current clock time in milliseconds
virtual void Update(unsigned long currentTimeMs) { (void)currentTimeMs; }; virtual void Update(unsigned long currentTimeMs) { (void)currentTimeMs; };