Can't transfer all my pixel data through UDP (WiFi)

ZePedro80
Posts: 5
Joined: Wed Mar 17, 2021 12:17 pm

Can't transfer all my pixel data through UDP (WiFi)

Postby ZePedro80 » Thu Jun 24, 2021 1:49 pm

Hello everyone
I'm trying to send continuously and randomly generated pixel colors (80x60) in Adafruit ESP32 Feather to my C# program, over Wifi, using UDP and TCP protocols. Any other type of data I can send without problems through any of the protocols, but this String that I create that sends the 4800 (80x60) values causes some difficulty, particularly in UDP.

In the TCP protocol, all data is sent by ESP32 and received by the C# program, despite the protocol being very slow and, consequently, the video obtained by them has very few frames per second.
In the case of UDP, which was expected to be faster (and it was my great hope), the generated data is not entirely received by the C# program, and the video only has the first pixels (approximately 410 - "Imagem1").

Both protocols are defined the same way in the C# program, with the exception that TCP is in Stream format and UDP is in Datagrams (it is not possible to define Stream).
On the other hand, my code in ESP32 is as follows:

Code: Select all

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
#include <WiFiUdp.h>
#include <Udp.h>

const char* ssid = "SensorIR_test";
const char* password = "123456789"; 

WiFiServer server(80);

WiFiUDP Udp;
const int udpPort = 80;      // local port to listen on
int reply;      // a int to send back
String str_reply, inputs;
String str_reply_UDP[60];
int speedx;

int i, j;
int subS_l;
int index_of_S;

void setup() {

    Serial.begin(250000);
   
  delay(10);

  Serial.print("Setting AP (Access Point)…");
  WiFi.softAP(ssid, password);
  IPAddress IP = WiFi.softAPIP();
  Serial.print("AP IP address: ");
  Serial.println(IP);
  
  server.begin();
  Udp.begin(udpPort);

}

void loop() {
  WiFiClient client = server.available();   // Listen for incoming clients
   int packetSize = Udp.parsePacket();   
   inputs = "";
   i=1;
   j=1;

   if (client) {    
     
     // If a new client connects,
    Serial.println("New Client.");          // print a message out in the serial port
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,

       client.setNoDelay(1);
  
     //break;

     int c=client.read();
       
       //   else {
     // teste=teste+1;

      for (int y=0; y<60; y++) {
         for (int k=0; k<80; k++) {
         //speedx=random(0,255);
         str_reply+=random(0,255);
         str_reply+=" ";
          }
       client.print(str_reply);
       Serial.print(str_reply);
       client.print(" ");       
       Serial.print(" ");
       }
       
      }
    }
        // Close the connection
    client.stop();
    Serial.println("TCP communication disconnected.");
    Serial.println("");
  }
  else if (packetSize) {
  for (int y=1; y<61; y++) {
    str_reply_UDP[y]="";
  }

    int d = Udp.read();
    // Serial.println(d);
    if (d==59) {    //ASCII=";"
      Serial.println("New UDP Client.");          // print a message out in the serial port
    }
  
       else if (d==33)  {   //ASCII="!"

      for (int y=0; y<60; y++) {
         for (int k=0; k<80; k++) {
         str_reply_UDP[y]+=random(0,255);
         str_reply_UDP[y]+=" ";
      }
      }

      }

    Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());

    for (int u=0; u<61; u++) {
      Serial.println(str_reply_UDP[u]);
      Udp.println(str_reply_UDP[u]);
    }

    Udp.endPacket();  
  } 

}
Does anyone have an idea of what should I do?
Attachments
Imagem1.png
Imagem1.png (25.98 KiB) Viewed 2601 times

Who is online

Users browsing this forum: No registered users and 84 guests