ESP-IDF sending now works

This commit is contained in:
Pascal Serrarens 2025-06-27 15:00:54 +02:00
parent b5f07f77c2
commit 8901e6933a
3 changed files with 56 additions and 40 deletions

View File

@ -10,7 +10,6 @@ namespace RoboidControl {
void ParticipantUDP::SetupUDP(int localPort, void ParticipantUDP::SetupUDP(int localPort,
const char* remoteIpAddress, const char* remoteIpAddress,
int remotePort) { int remotePort) {
#if defined(IDF_VER)
std::cout << "Set up UDP\n"; std::cout << "Set up UDP\n";
GetBroadcastAddress(); GetBroadcastAddress();
@ -56,23 +55,23 @@ void ParticipantUDP::SetupUDP(int localPort,
// inet_pton(AF_INET, this->remoteSite->ipAddress, // inet_pton(AF_INET, this->remoteSite->ipAddress,
// &this->dest_addr.sin_addr.s_addr); // &this->dest_addr.sin_addr.s_addr);
this->connected = true;
std::cout << "Wifi sync started local " << localPort << ", remote " std::cout << "Wifi sync started local " << localPort << ", remote "
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port << this->remoteSite->ipAddress << ":" << this->remoteSite->port
<< "\n"; << "\n";
std::cout << "socket: " << (int)this->sock << std::endl; // std::cout << "socket: " << (int)this->sock << std::endl;
ParticipantMsg* msg = new ParticipantMsg(this->networkId); // ParticipantMsg* msg = new ParticipantMsg(this->networkId);
int bufferSize = msg->Serialize(this->buffer); // int bufferSize = msg->Serialize(this->buffer);
int err = sendto(this->sock, buffer, bufferSize, 0, // int err = sendto(this->sock, buffer, bufferSize, 0,
(struct sockaddr*)&dest_addr, sizeof(dest_addr)); // (struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (errno != 0) // if (errno != 0)
std::cout << "AASend error " << err << " or " << errno << "\n"; // std::cout << "AASend error " << err << " or " << errno << "\n";
else // else
std::cout << "AASend SUCCESS\n"; // std::cout << "AASend SUCCESS\n";
SendTest(); //SendTest();
#endif // IDF_VER
} }
void ParticipantUDP::GetBroadcastAddress() { void ParticipantUDP::GetBroadcastAddress() {
@ -151,20 +150,36 @@ ParticipantUDP::ParticipantUDP(int port) : ParticipantUDPGeneric(port) {}
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort) ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
: ParticipantUDPGeneric(ipAddress, port, localPort) {} : ParticipantUDPGeneric(ipAddress, port, localPort) {}
bool ParticipantUDP::SendTest() { // bool ParticipantUDP::SendTest() {
#if defined(IDF_VER) // #if defined(IDF_VER)
std::cout << "socket: " << (int)this->sock << std::endl; // // std::cout << "socket: " << (int)this->sock << std::endl;
ParticipantMsg* msg = new ParticipantMsg(this->networkId); // // UBaseType_t stack_size = uxTaskGetStackHighWaterMark(NULL); // NULL to check the main task
int bSize = msg->Serialize(this->buffer); // // size_t free_heap = xPortGetFreeHeapSize();
int err = sendto(this->sock, buffer, bSize, 0, (struct sockaddr*)&dest_addr, // // std::cout << "Stack High Water Mark: " << stack_size << " heap " << free_heap << std::endl;
sizeof(dest_addr));
if (errno != 0)
std::cout << "BBSend error " << err << " or " << errno << "\n";
else
std::cout << "BBSend SUCCESS\n";
#endif // ParticipantMsg* msg = new ParticipantMsg(this->networkId);
return true; // int bSize = msg->Serialize(this->buffer);
// // std::cout << "buffer size " << bSize << std::endl;
// int err = sendto(this->sock, buffer, bSize, 0, (struct sockaddr*)&dest_addr,
// sizeof(dest_addr));
// if (errno != 0)
// std::cout << "BBSend error " << err << " or " << errno << "\n";
// else
// std::cout << "BBSend SUCCESS\n";
// #endif
// return true;
// }
bool ParticipantUDP::Send(IMessage* msg) {
int bufferSize = msg->Serialize(this->buffer);
if (bufferSize <= 0)
return true;
std::cout << "send msg " << (static_cast<int>(this->buffer[0]) & 0xff)
<< " to " << this->remoteSite->ipAddress << std::endl;
return this->SendTo(this->remoteSite, bufferSize);
} }
bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant, bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
@ -174,7 +189,7 @@ bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
char ip_str[INET_ADDRSTRLEN]; char ip_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &dest_addr.sin_addr, ip_str, sizeof(ip_str)); inet_ntop(AF_INET, &dest_addr.sin_addr, ip_str, sizeof(ip_str));
std::cout << "Sending to " << ip_str << ":" << port << "\n"; std::cout << "Sending " << bufferSize << " bytes to " << ip_str << ":" << port << "\n";
// Print the IP address and port // Print the IP address and port
// printf("IP Address: %s\n", ip_str); // printf("IP Address: %s\n", ip_str);
@ -183,10 +198,9 @@ bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
this->dest_addr.sin_port = htons(remoteParticipant->port); this->dest_addr.sin_port = htons(remoteParticipant->port);
this->dest_addr.sin_addr.s_addr = inet_addr(remoteParticipant->ipAddress); this->dest_addr.sin_addr.s_addr = inet_addr(remoteParticipant->ipAddress);
std::cout << "socket: " << (int)this->sock << std::endl;
int err = sendto(this->sock, buffer, bufferSize, 0, int err = sendto(this->sock, buffer, bufferSize, 0,
(struct sockaddr*)&this->dest_addr, sizeof(this->dest_addr)); (struct sockaddr*)&this->dest_addr, sizeof(this->dest_addr));
if (errno != 0) if (errno < 0)
std::cout << "Send error " << err << " or " << errno << "\n"; std::cout << "Send error " << err << " or " << errno << "\n";
#endif #endif

View File

@ -9,7 +9,7 @@ namespace RoboidControl {
class ParticipantUDP : public ParticipantUDPGeneric { class ParticipantUDP : public ParticipantUDPGeneric {
public: public:
/// @brief Create a participant without connecting to a site /// @brief Create a participant without connecting to a site
/// @param port The port on which the participant communicates /// @param port The port on which the participant communicates
/// These participant typically broadcast Participant messages to let site /// These participant typically broadcast Participant messages to let site
/// servers on the local network know their presence. Alternatively they can /// servers on the local network know their presence. Alternatively they can
@ -19,19 +19,22 @@ class ParticipantUDP : public ParticipantUDPGeneric {
/// @param ipAddress The IP address of the site /// @param ipAddress The IP address of the site
/// @param port The port used by the site /// @param port The port used by the site
/// @param localPort The port used by the local participant /// @param localPort The port used by the local participant
ParticipantUDP(const char* ipAddress, ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
int port = 7681,
int localPort = 7681);
void Setup(int localPort, const char* remoteIpAddress, int remotePort); void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive(); void Receive();
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize); bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg); bool Publish(IMessage* msg);
bool SendTest(); // bool SendTest();
//bool Send(IMessage* msg) override; /// @brief Sens a message to the remote site (if set)
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) override; /// @param msg The message to send
/// @return True if a message could be sent.
bool Send(IMessage* msg) override;
void SetupUDP(int localPort,
const char* remoteIpAddress,
int remotePort) override;
void ReceiveUDP() override; void ReceiveUDP() override;
protected: protected:
@ -46,7 +49,6 @@ class ParticipantUDP : public ParticipantUDPGeneric {
void GetBroadcastAddress(); void GetBroadcastAddress();
}; };
} // namespace RoboidControl } // namespace RoboidControl
#endif #endif

View File

@ -231,18 +231,18 @@ void ParticipantUDPGeneric::Update(bool recurse) {
if (this->connected == false) if (this->connected == false)
begin(); begin();
// EspIdf::ParticipantUDP* thisEspIdf = ParticipantUDP* thisEspIdf =
// static_cast<EspIdf::ParticipantUDP*>(this); static_cast<ParticipantUDP*>(this);
// thisEspIdf->SendTest(); // thisEspIdf->SendTest();
if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) { if (this->publishInterval > 0 && currentTimeMs > this->nextPublishMe) {
ParticipantMsg* msg = new ParticipantMsg(this->networkId); ParticipantMsg* msg = new ParticipantMsg(this->networkId);
// thisEspIdf->SendTest();
if (this->remoteSite == nullptr) if (this->remoteSite == nullptr)
this->Publish(msg); this->Publish(msg);
else else
this->Send(msg); this->Send(msg);
// thisEspIdf->SendTest();
delete msg; delete msg;
this->nextPublishMe = currentTimeMs + this->publishInterval; this->nextPublishMe = currentTimeMs + this->publishInterval;