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,
const char* remoteIpAddress,
int remotePort) {
#if defined(IDF_VER)
std::cout << "Set up UDP\n";
GetBroadcastAddress();
@ -56,23 +55,23 @@ void ParticipantUDP::SetupUDP(int localPort,
// inet_pton(AF_INET, this->remoteSite->ipAddress,
// &this->dest_addr.sin_addr.s_addr);
this->connected = true;
std::cout << "Wifi sync started local " << localPort << ", remote "
<< this->remoteSite->ipAddress << ":" << this->remoteSite->port
<< "\n";
std::cout << "socket: " << (int)this->sock << std::endl;
ParticipantMsg* msg = new ParticipantMsg(this->networkId);
int bufferSize = msg->Serialize(this->buffer);
int err = sendto(this->sock, buffer, bufferSize, 0,
(struct sockaddr*)&dest_addr, sizeof(dest_addr));
if (errno != 0)
std::cout << "AASend error " << err << " or " << errno << "\n";
else
std::cout << "AASend SUCCESS\n";
// std::cout << "socket: " << (int)this->sock << std::endl;
// ParticipantMsg* msg = new ParticipantMsg(this->networkId);
// int bufferSize = msg->Serialize(this->buffer);
// int err = sendto(this->sock, buffer, bufferSize, 0,
// (struct sockaddr*)&dest_addr, sizeof(dest_addr));
// if (errno != 0)
// std::cout << "AASend error " << err << " or " << errno << "\n";
// else
// std::cout << "AASend SUCCESS\n";
SendTest();
#endif // IDF_VER
//SendTest();
}
void ParticipantUDP::GetBroadcastAddress() {
@ -151,20 +150,36 @@ ParticipantUDP::ParticipantUDP(int port) : ParticipantUDPGeneric(port) {}
ParticipantUDP::ParticipantUDP(const char* ipAddress, int port, int localPort)
: ParticipantUDPGeneric(ipAddress, port, localPort) {}
bool ParticipantUDP::SendTest() {
#if defined(IDF_VER)
std::cout << "socket: " << (int)this->sock << std::endl;
ParticipantMsg* msg = new ParticipantMsg(this->networkId);
int bSize = msg->Serialize(this->buffer);
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";
// bool ParticipantUDP::SendTest() {
// #if defined(IDF_VER)
// // std::cout << "socket: " << (int)this->sock << std::endl;
// // UBaseType_t stack_size = uxTaskGetStackHighWaterMark(NULL); // NULL to check the main task
// // size_t free_heap = xPortGetFreeHeapSize();
// // std::cout << "Stack High Water Mark: " << stack_size << " heap " << free_heap << std::endl;
#endif
return true;
// ParticipantMsg* msg = new ParticipantMsg(this->networkId);
// 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,
@ -174,7 +189,7 @@ bool ParticipantUDP::SendTo(RemoteParticipantUDP* remoteParticipant,
char ip_str[INET_ADDRSTRLEN];
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
// 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_addr.s_addr = inet_addr(remoteParticipant->ipAddress);
std::cout << "socket: " << (int)this->sock << std::endl;
int err = sendto(this->sock, buffer, bufferSize, 0,
(struct sockaddr*)&this->dest_addr, sizeof(this->dest_addr));
if (errno != 0)
if (errno < 0)
std::cout << "Send error " << err << " or " << errno << "\n";
#endif

View File

@ -9,7 +9,7 @@ namespace RoboidControl {
class ParticipantUDP : public ParticipantUDPGeneric {
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
/// These participant typically broadcast Participant messages to let site
/// 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 port The port used by the site
/// @param localPort The port used by the local participant
ParticipantUDP(const char* ipAddress,
int port = 7681,
int localPort = 7681);
ParticipantUDP(const char* ipAddress, int port = 7681, int localPort = 7681);
void Setup(int localPort, const char* remoteIpAddress, int remotePort);
void Receive();
bool SendTo(RemoteParticipantUDP* remoteParticipant, int bufferSize);
bool Publish(IMessage* msg);
bool SendTest();
// bool SendTest();
//bool Send(IMessage* msg) override;
void SetupUDP(int localPort, const char* remoteIpAddress, int remotePort) override;
/// @brief Sens a message to the remote site (if set)
/// @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;
protected:
@ -46,7 +49,6 @@ class ParticipantUDP : public ParticipantUDPGeneric {
void GetBroadcastAddress();
};
} // namespace RoboidControl
#endif

View File

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