Wrote a program in rust that sends UDP packets containing int arrays for an led strip connected to an esp32 via wifi and it works ok. Getting some dropped packets at range because wifi. I am now trying to implement the same process on a wt32-eth01 with the hopes of having less dropped packets because ethernet. I am using a modified version of:
khoih-prog / WebServer_WT32_ETH01
Here is the code:
/****************************************************************************************************************************
UDPSendReceive.ino - Simple Arduino web server sample for ESP8266/ESP32 AT-command shield
For Ethernet shields using WT32_ETH01 (ESP32 + LAN8720)
WebServer_WT32_ETH01 is a library for the Ethernet LAN8720 in WT32_ETH01 to run WebServer
Based on and modified from ESP8266 https://github.com/esp8266/Arduino/releases
Built by Khoi Hoang https://github.com/khoih-prog/WebServer_WT32_ETH01
Licensed under MIT license
*****************************************************************************************************************************/
#define DEBUG_ETHERNET_WEBSERVER_PORT Serial
// Debug Level from 0 to 4
#define _ETHERNET_WEBSERVER_LOGLEVEL_ 3
#include <FastLED.h>
#include <string>
#include <vector>
#include <sstream>
#define PIN 2
#define CL_PIN 4
#include <WebServer_WT32_ETH01.h>
// Select the IP address according to your local network
IPAddress myIP(192, 168, 4, 78);
IPAddress myGW(192, 168, 4, 1);
IPAddress mySN(255, 255, 255, 0);
// Google DNS Server IP
IPAddress myDNS(8, 8, 8, 8);
int const num_pixels = 60;
unsigned int localPort = 8080; //10002; // local port to listen on
char packetBuffer[450]; // buffer to hold incoming packet
int loop_count = 0;
// A UDP instance to let us send and receive packets over UDP
WiFiUDP Udp;
CRGB ledstrip[num_pixels];
void setup()
{
Serial.begin(115200);
while (!Serial);
// Using this if Serial debugging is not necessary or not using Serial port
//while (!Serial && (millis() < 3000));
Serial.print("\nStarting UDPSendReceive on " + String(ARDUINO_BOARD));
Serial.println(" with " + String(SHIELD_TYPE));
Serial.println(WEBSERVER_WT32_ETH01_VERSION);
// To be called before ETH.begin()
WT32_ETH01_onEvent();
//bool begin(uint8_t phy_addr=ETH_PHY_ADDR, int power=ETH_PHY_POWER, int mdc=ETH_PHY_MDC, int mdio=ETH_PHY_MDIO,
// eth_phy_type_t type=ETH_PHY_TYPE, eth_clock_mode_t clk_mode=ETH_CLK_MODE);
//ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER, ETH_PHY_MDC, ETH_PHY_MDIO, ETH_PHY_TYPE, ETH_CLK_MODE);
ETH.begin(ETH_PHY_ADDR, ETH_PHY_POWER);
// Static IP, leave without this line to get IP via DHCP
//bool config(IPAddress local_ip, IPAddress gateway, IPAddress subnet, IPAddress dns1 = 0, IPAddress dns2 = 0);
ETH.config(myIP, myGW, mySN, myDNS);
WT32_ETH01_waitForConnect();
Serial.println(F("\nStarting connection to server..."));
// if you get a connection, report back via serial:
Udp.begin(localPort);
Serial.print(F("Listening on port "));
Serial.println(localPort);
Serial.println("");
Serial.println("Ethernet connected");
Serial.println("IP address: ");
Serial.println(ETH.localIP());
}
void loop()
{
int packetSize = Udp.parsePacket();
if (packetSize)
{
loop_count = 0;
// read the packet into packetBufffer
int len = Udp.read(packetBuffer, 450);
//Serial.printf("\npacketBuffer:");
//Serial.println(packetBuffer);
//Serial.printf("\nlen=");
//Serial.println(len);
if (len > 0)
{
packetBuffer[len] = 0;
}
//Serial.printf("d%", packetBuffer);
Serial.printf("UDP packet contents: %s\n", packetBuffer);
//Serial.printf(packetBuffer);
//Serial.println(packetBuffer);
int k=0;
int strip[num_pixels][3];
for (int i = 0; i < num_pixels; i++) {
for (int j = 0; j < 3; j++) {
strip[i][j] = packetBuffer[k];
//Serial.printf("packetBuffer[k]:");
//Serial.println(packetBuffer[k]);
k++;
}
}
//Serial.printf("Strip:");
//Serial.printf("%d",strip);
for (int l = 0; l < (num_pixels); l++) {
ledstrip[l].setRGB(strip[l][0], strip[l][1], strip[l][2]);
//int R = strip[l][0];
//int G = strip[l][1];
//int B = strip[l][2];
//Serial.printf("%d, %d, %d\n",R, G, B);
}
FastLED.show();
}
loop_count++;
//if (loop_count >= 1000) {
// for (int i = 0; i < num_pixels; i++) {
// ledstrip[i] = CRGB(0, 0, 0);
// }
//FastLED.show();
//}
//Serial.println(F("Contents:"));
//Serial.println(packetBuffer);
}
The result is that when I serial print packetBuffer it apprears to be empty. This is from serial monitor:
12:48:52.580 -> Received packet of size 450
12:48:52.580 -> From 192.168.4.79, port 8080
12:48:52.580 -> Contents:
12:48:52.580 ->
And when I print each element of packetBuffer individually I get:
12:48:52.580 ->
2:48:52.580 -> -
12:48:52.580 -> >
12:48:52.580 -> o
12:48:52.580 -> <
12:48:52.580 -> '
12:48:52.580 -> [
12:48:52.580 -> Q
12:48:52.580 ->
12:48:52.580 -> ]
12:48:52.580 -> o
12:48:52.580 ->
12:48:52.613 -> i
12:48:52.613 -> ⸮
ect......
So it appears to connect and to receive packets of the correct size, but I'm not seeing my data. The functions that handle it, don't, as it appears they are receiving junk.
I have also tried just running the example without my modifications with the same result.
As my rust code works with the esp32 I don't think there's a problem there but for good measure here's my rust code. All of the socket code is in main.rs.
rust code
I am relatively new to this and I am not a pro C++ programmer. Have googled this for days and have tried everything I can think of and am out of ideas. Can anyone help?