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):
import 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:
#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):
import 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