User avatar
By Akshay Deshmukh
#74165 I am having a project where I use ESP8266 as a serial-to-wifi bridge. I send data serially from a python code running on windows7 to esp8266(esp-01) which it sends to a wifi client running on windows7 again. While sending a pdf file (79 KBs) in a similar fashion, I (due to limiting RAM), opted for a method where i receive serially chunks of file and send it to wifi immediately after receiving. This keeps on happening inside the loop until all the bytes of file are successfully transferred from serial to wifi. Doing this, initially esp8266(esp-01) was loosing some bytes sometimes. I figured there is somehow a mismatch between wifi transfer speed and serial baudrate which results in overflow of serial buffer and thus, loss of few bytes rendering final output file corrupt. So, I reduced baudrate from 500000 to 250000 and things worked fine confirming my theory. The issue is when I am running same code in esp8266(esp-12e), the baudrate 250000 is not enough and it still looses bytes. Further, reducing baudrate to 115200 makes things work almost fine (9 times success and one time lost bytes) while with 9600 baudrate file transfer is 100% success. But baudrate below 250000 is already slower as per my requirement.
My questions are:
1- Is my theory correct? Theory that serially data is coming too fast while receiving it and sending to wifi immediately takes some time, combined with a little slower wifi speed is resulting in overflow of serial buffer sometimes and thus, loss of bytes. (decreasing of baudrate did worked in both hardwares which sort of proves this point but I am not sure)

2- Why the same code do not work fine in esp-12e? Is there any difference in there wifi controller or handling or something else? I created a hotspot in both hardwares and checked their speed which is same (54 Mbps), so it may not be wifi speed but then what?

3- How to overcome this?

A simple version of code i used for testing is as follows:

python code on windows7 (python 3.6):
Code: Select allimport serial
import time

ser = serial.Serial('COM4', 250000, timeout=300)
time.sleep(1)
with open("C:/Users/aatibui/Desktop/current/output.pdf", "rb") as f:
   read_done = f.read()
   ser.write(read_done)

time.sleep(5)

ESP8266 code:
Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "atomic";
const char* password = "int1234";

WiFiServer server(23);
WiFiClient client;

void setup() {
  Serial.begin(115200);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.print("\nConnecting to ");
  Serial.println(ssid);

  while (WiFi.status() != WL_CONNECTED) delay(500);
  server.begin();

  Serial.print("Ready! Use 'telnet ");
  Serial.print(WiFi.localIP());
  Serial.println(" 23' to connect");

}

void loop() {
  if (server.hasClient()) {
    if (!client || !client.connected()) {
      if (client) client.stop();
      client = server.available();
      while (client.connected()) {
        while (Serial.available()) {
          size_t len = Serial.available();
          uint8_t sbuf[len];
          Serial.readBytes(sbuf, len);
          client.write(sbuf,len);
          delay(1);
        }
      }
    }
  }
}

Wifi Client code (jdk7):
Code: Select allimport java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Arrays;
import java.io.DataInputStream;


public class ClientArduinoFile_1 {
     
     public static final int BUFFER_SIZE = 20*1024;
     private byte[] buffer;
     
     public ClientArduinoFile_1() {
          buffer = new byte[BUFFER_SIZE];
     }

     public void startClient() throws Exception {
          Socket socket = new Socket("192.168.0.8", 23);
        BufferedInputStream in =
               new BufferedInputStream(socket.getInputStream());

        BufferedOutputStream out =
               new BufferedOutputStream(new FileOutputStream("C:/Users/aatibui/Desktop/test123.pdf"));
         
      
          int len = 0;
        int counter = 0;
        int bytes = 0;
        System.out.println("here");
          while ((len = in.read(buffer)) > 0) {
            
            System.out.print("bytes: "+len);
            bytes += len;
            System.out.print(" ; total: "+bytes);
            
            out.write(buffer, 0, len);
               System.out.print(" # ");
            System.out.println(counter++);
            if(bytes>=79965){
               System.out.println("reached end of file");
               break;
            }
      }
          in.close();
          out.flush();
          out.close();
          socket.close();
          System.out.println("\nDone!");
     }


     public static void main(String[] args) throws Exception {
          ClientArduinoFile_1 test = new ClientArduinoFile_1();
          test.startClient();
     }

}

Also attached the circuit diagrams of connection for esp-01 and esp-12e
You do not have the required permissions to view the files attached to this post.