WiFi between ESP32-CAM and C# program on laptop

I've got an ESP32-CAM that I'm programming using the Arduio IDE. I'm using its WiFiClient software to communicate over WiFi with a C# program that I wrote and is running on my laptop. I am successfully sending text commands from the ESP32-CAM to the laptop. I'm also successfully send in byte data in that direction. The byte data is image data obtained from the ESP32-CAM's camera and I'm successfully receiving it on the laptop and even displaying the received data in a PictureBox on the C# program.

So far, so good. That's all working just fine.

But I also want to send some text from the C# program running on the laptop back over to the Arduino code running on the ESP32-CAM. That part is not working, and I haven't figured out why. I'm hoping someone has some insight and can help me get it doing that part.

In the C# program I'm setting up the WiFi parts like this:

TcpClient client = myserver.AcceptTcpClient();
Stream ByteStreamReaderComingInFromClient = client.GetStream();
StreamReader TextStreamReaderComingInFromClient = new StreamReader(client.GetStream());
StreamWriter TextStreamWriterGoingToClient = new StreamWriter(client.GetStream());

I'm successfully connecting the laptop (as a server) to the client (the ESP32-CAM).
I'm successfully using the TextStreamReaderComingInFromClient object in the C# code on the laptop to read text coming in over the WiFi connection from the client (the ESP32-CAM).
I'm successfully using the ByteStreamReaderComingInFromClient object in the C# code on the laptop to read byte data coming in over the WiFi connection from the client (the ESP32-CAM). That data constitutes the image data captured by the ESP32-CAM that I want to be displayed on the laptop.

So all of that works just fine. I can SEND text and bytes from the ESP32-CAM client. I can RECEIVE that text and bytes at the laptop server. I can can eve display the received image within a PictureBox on the C# Windows program running on the laptop.

BUT, when I use the TextTStreamWriterGoingToClient object on the C# program running on the laptop to sent text over to the ESP32-CAM, it doesn't get there. I do something simple like:

TextStreamWriterGoingToClient.WriteLine("HELLO WORLD);

and it returns from that function call just fine. (WriteLine is a void function so it doesn't return any status info). It does not throw any exceptions. So it SEEMS like it's succeeding in sending it.

But over in the Arduino code on the ESP32-CAM it never receives the text. At that end I'm doing:

String lineOfRetrievedText = theWiFiClientConnectedToTheComputerServerApp.readStringUntil('\n');

And it never returns from that call to readStringUntil().

The theWiFiClientConnectedToTheComputerServerApp is set by doing:

WiFiClient theWiFiClientConnectedToTheComputerServerApp;
WiFiMulti WiFiMulti;
bool ConnectToComputerServerAppViaWifi()
{
  // We start by connecting to a WiFi Station
  char WiFiStation_ssid[] = "TP-LINK_ABCD";
  char WiFiStation_password[] = "847265";
  WiFiMulti.addAP(WiFiStation_ssid, WiFiStation_password);

 while (WiFiMulti.run() != WL_CONNECTED)
  {
    delay(500);
  }

  delay(500);

  const uint16_t ComputerPort = 1000;
  const char * ComputerHost_IP = "192.168.0.106";

  while (!theWiFiClientConnectedToTheComputerServerApp.connect(ComputerHost_IP, ComputerPort))
  {
    Serial.println("Connection failed.");
    Serial.println("Waiting 5 seconds before retrying...");
    delay(5000);
  }

  return true;
}

And, as stated previously, after doing that setup I am able to send text and data from the ESP32-CAM over to the C# program on the laptop.

Its just when I try to send text from the laptop over to the ESP32-CAM that it fails. It seems to do the writeLine() OK on the laptop, but it never succeeds in receiving the text via the readStringUntil() on the ESP32-CAM. It just never returns from that function call.

I also tried calling the available() function...

theWiFiClientConnectedToTheComputerServerApp.available()

which returns the number of incoming bytes waiting at the ESP32-CAM to be read and that simply keeps returning 0, indicating that no bytes have actually been received.

So, what am I doing wrong? I can send both text and data bytes from the ESP32-CAM to the C# program on the
laptop, but I can't seem to get it to send text in the other direction. Is there something that I'm not setting up correctly? Do I have to enable something to get it to allow for data going in that direction?

1 Like

Well, it's rather disappointing that no one suggested a solution.

But after lots and lots of testing and trying out one thing after another, I came across the flush() method for the server. Doing a flush after the WriteLine() fixed the issue.

TextStreamWriterGoingToClient.WriteLine("HELLO WORLD);
TextStreamWriterGoingToClient.flush();

I had expected that when a WriteLine()is called the server would soon after actually send whatever it has to send. I never encountered anything like that from the client side, where I send lots of text and data bytes over to the server, with no need to explicitly tell it to send it, i.e. no comparable flush() call.

2 Likes