I presume I am somehow running afoul of the ethernet capabilities of the W5500 Chip, but I've had mixed success depending and how I do it, and what I'm trying to accomplish as to this project in regards to how much I/O and how fast I'm trying to send / recieve the data.
So first off my Source :
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Encoder.h>
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
bool debugLog = true;
// This device is using DHCP if you need to set a static IP, you need to do that here.
byte mac[] = {0x04, 0xE9, 0xE5, 0x05, 0x83, 0xA5};
unsigned short localPort = 8887;
EthernetUDP udp;
// Encoder Setup
// Pin numbers to the pins connected to your encoder.
Encoder X_Encoder(1, 2);
Encoder Y_Encoder(4, 5);
Encoder Z_Encoder(7, 8);
// Initial Encoder Positons.
int cacheX = -999;
int cacheY = -999;
int cacheZ = -999;
// Setup Variables for exposing read json values
bool dataStream;
bool dataReset;
void setup() {
// Initialize serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize Ethernet libary
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to initialize Ethernet library"));
return;
}
Serial.print("Sending from : ");
Serial.println(Ethernet.localIP());
// Enable UDP
udp.begin(localPort);
}
// Get JSON Data
void getUDPJsonData(){
StaticJsonDocument<100> readDoc; // Init Json Memory Heap
int packetSize = udp.parsePacket();
if(packetSize)
{
//Get UDP Data
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(udp.remotePort());
// read the packet into packetBufffer
udp.read(packetBuffer,48);
Serial.print("Data Read : ");
Serial.println(packetBuffer);
//char json[] = "{\"SendData\": false,\"ResetData\": false}"; // For sizing memory for the decoder.
DeserializationError error = deserializeJson(readDoc, packetBuffer);
// Test if parsing succeeds.
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
else {
Serial.print(F("deserializeJson() succeeded: "));
Serial.println(packetBuffer);
}
dataStream = bool (readDoc["SendData"]);
dataReset = readDoc["ResetData"];
if (dataStream){
Serial.println("Told to Stream Data");
}
if (dataReset){
Serial.println("Told to Reset Data");
}
}
}
//Reply With Sensor Data
void sendUDPJsonData(){
StaticJsonDocument<72> writeDoc; // Init Json Memory Heap
// Grab Data From Encoder
int X_Pos, Y_Pos, Z_Pos;
X_Pos = X_Encoder.read();
Y_Pos = Y_Encoder.read();
Z_Pos = Z_Encoder.read();
// If Streaming is desired
if (dataStream) {
// If Data has Changed
if (X_Pos != cacheX || Y_Pos != cacheY || Z_Pos != cacheZ) {
float mult = .15; // So that each rotation = 360 degrees
// Create the Json Document
writeDoc["SubjectName"].set("KinoWheels");
JsonArray values = writeDoc.createNestedArray("Rotation");
values.add(X_Pos*mult);
values.add(Y_Pos*mult);
values.add(Z_Pos*mult);
if (debugLog){
// Log
Serial.print(F("Sending to "));
Serial.print(udp.remoteIP());
Serial.print(F(" on port "));
Serial.println(udp.remotePort());
serializeJson(writeDoc, Serial); // Debug String to see what was sent.
}
// Send UDP packet
udp.beginPacket(udp.remoteIP(), udp.remotePort());
serializeJson(writeDoc, udp);
//udp.println("{\"SendData\": false,\"ResetData\": false}"); // Static test to check for memory leak
udp.println();
udp.endPacket();
}
}
// Update position for next comparrison
cacheX = X_Pos;
cacheY = Y_Pos;
cacheZ = Z_Pos;
}
void loop() {
getUDPJsonData();
sendUDPJsonData();
}
Then the output is what's puzzling... it does most things as I'd expect :
Sending from : 192.168.0.88
Received packet of size 38
From 192.168.0.55, port 8888
Data Read : {"SendData": true,"ResetData": true}
deserializeJson() succeeded: SendData
Told to Stream Data
Told to Reset Data
Sending to 192.168.0.55 on port 8888
{"SubjectName":"KinoWheels","Rotation":[0,0]}Sending to 192.168.0.55 on port 8888
{"SubjectName":"KinoWheels","Rotation":[-0.15,0]}Sending to 192.168.0.55 on port 8888
{"SubjectName":"KinoWheels","Rotation":[0,0]}Sending to 192.168.0.55 on port 8888
{"SubjectName":"KinoWheels","Rotation":[-0.15,0]}Sending to 192.168.0.55 on port 8888
{"SubjectName":"KinoWheels","Rotation":[0,0]}Sending to 192.168.0.55 on port 8888
And I keep messing with the encoders when it all of a sudden fails, or I can reset it and run it again, and it fails within 2-3 Lines, or on other times if I go slow and steady I can get 1400-4000 samples, and not run into an issue untill sample 4001 and it's all over... Each time it just locks up.
Now I've been reading some of the other UDP threads, and I understand there's only 24kb on the W5500 chip, so am I running afoul of that, and if so how do I "Fix" it?