Receive a long String with a esp 01

0

I have a esp 01 (wifi module) connected in my Arduino on port 2 and 3 (TX RX), I can send a large String, but when I receive it I can only receive a maximum of 32 characters, here it is the code of my Arduino:

#include <SoftwareSerial.h>

//TX RX
SoftwareSerial esp8266(2, 3);

void setup() {
    Serial.begin(9600);
    esp8266.begin(19200);
    ...
}

...


void loop() {

...

    if (esp8266.available() > 0) {

        String string = "";

        if (esp8266.find("[msg]:")) {
            string = esp8266.readStringUntil('\r');
        }

        /* Eu tentei desta forma também, mas o resultado foi o mesmo
        while (esp8266.available()) {
            string += (char) esp8266.read();
        }
        */

        Serial.print("\r\n" + string);  
    }

...

I'm sending and receiving through this Python code information:

import socket

class socketClient(object):
    socket = None

    def __init__(self, ip, port):
        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.connect(((ip, port)))

    def send(self, msg):
        self.socket.send(msg)

    def close(self):
        self.socket.close()

    def receive(self):
        return self.socket.recv(10240)

status = raw_input('Entre com o estado: ')
json = '[msg]:' + status + '\r\n';
print json

#This part was to test with a local server python
#socket = socketClient('127.0.0.1', 7000)

socketClient('192.168.0.43', 80)
socket.send(json)
msg = socket.receive()
print(msg.decode('utf-8'))
socket.close()

To make sure the problem is in Arduino I did a local simulation using another Python code:

import socket

host = '127.0.0.1'
port = 7000
addr = (host, port)
serv_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serv_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serv_socket.bind(addr)
serv_socket.listen(10)
print 'aguardando conexao'
con, cliente = serv_socket.accept()
print 'conectado'
print "aguardando mensagem"
recebe = con.recv(1024)
print "mensagem recebida: "+ recebe
serv_socket.close()

And I got the whole string normally.

UPDATE

  

I performed the following test by placing the module directly on the TX RX ports and   I manually sent the commands to connect to the network, and the string was   complete (The one that came through Python), then the problem might be in   port 2 and 3 of Arduino that can not transmit all the information   such as the TX RX port, or the SoftwareSerial library can not   capture every string, or what I hope it is, to pick up the whole   string a different method is required.

     

The biggest problem is that I have no idea how I'm going to do any   type of test to see what the real problem is.

UPDATE

I made the following test, this way I send directly to serial monitor what I get from esp8266, and all the characters are shown:

while (esp8266.available() > 0) {
    Serial.write(esp8266.read());
}

Somehow when trying to pass to String it cuts the information, I thought about being the limit of the String object, but I did a test assigning the String directly and it worked too.

For some reason when doing the conversion to String it does not get all the information.

UPDATE

String buffer = "";

void loop() {
    if (esp8266.available() > 0) {
        buffer = esp8266.readStringUntil('\r');
    }
    if(buffer != ""){
        Serial.print("\r\n" + buffer);
    }
    buffer = "";
    delay(1000);
}

UPDATE

I did other tests, and I was able to assign the complete string to a variable, but the problem is the delay, but I do not understand why, because it is outside the while, I thought about the possibility of this passing two times in the while and completing the string the second time it runs, but this is not what happens, to do this test I put something to be printed outside the while and the information was printed separates from the complete string, ie it completed the while to do another action, but when I put a delay it does not take the full string, which for me does not make any sense, since the delay is outside the while, theoretically it was not to make a difference.

while (esp8266.available() > 0) {
    Serial.write(esp8266.read());
}
delay(1000);
    
asked by anonymous 29.04.2018 / 22:37

1 answer

0

Hello, I do not work with the ESP-01, only with the Esp-8266 and Esp-32, but if you have the limit of 32 bytes, you can do the following:

  • And in Esp, you do the following:

    void loop() {
    
        // Buffer de recepção de dados
    
        static String bufferRecSerial = ""; // Note que é static, senão o conteúdo é perdido entre um loop e outro
    
        ///.....
    
        // Recebimento de dados
    
        if (esp8266.available() > 0) {
    
            while (esp8266.available()) {
    
                char caract = (char) esp8266.read();
    
                if (caract == '\n') { // Nova linha
    
                    // Processa a mensagem (agora sem o \r\n)
    
                    if (bufferRecSerial.length() > 0) {
    
                       processarMensagem(bufferRecSerial);
    
                       // Zera o buffer
    
                       bufferRecSerial = "";
                    }
    
                } else if (caract != '\r') { // Ignora o \r
    
                  // Adiciona no buffer // TODO: fazer logica de timeout se necessario
    
                  bufferRec += caract;
    
                }
            }
        }
    }
    
01.05.2018 / 16:33