0
I have a esp 01(Wi-fi module) connected in my Arduino on port 2 and 3(TX RX), I can send a large String, but when I receive I can only receive at most 32 characters, here 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 am sending and receiving through this Python code the 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 that 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 did the following test by placing the module directly on TX RX ports and I sent the commands manually to connect to the network, and the string was (The one that came through Python), so the problem may be in door 2 and 3 of the Arduino that can not transmit all information as the RX TX port, or the Softwareserial library cannot capture every string, or what I hope it is, to catch the whole string requires a different method.
The bigger problem is I have no idea how I’m gonna do any type of test to verify what the real problem is.
UPDATE
I did the following test, this way I send directly to the serial monitor
what I get from esp8266, and all 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 the possibility of being the limit of the String object, but I did a test assigning the String directly and it also worked.
For some reason while making the conversion to String it ends up not getting 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 managed to do the assignment of the full string in a variable, but the problem is the delay, but I don’t understand why, because it’s outside the while, I thought about the possibility of this passing twice in while and completing the string the second time it runs, but that’s not what happens, to do this test I put something to be printed outside the while and the information was printed apart from the complete string, that is to say he completed the while and then do another action, but when I put a delay it does not take the full string, which to me does not make any sense, since the delay is on the outside of the while, theoretically it was not to make a difference.
while (esp8266.available() > 0) {
Serial.write(esp8266.read());
}
delay(1000);
Esp-8266 is the chip, including it is present in esp 32, there are several modules with the Esp-8266 and the model I am using is esp 01, which is the simplest model https://www.filipeflop.com/wp-content/uploads/2017/07/WiFi-Serial-Transceiver-Module.jpg
– Wictor Chaves
I updated the question, somehow when passing to String it is picking up the information, I do not know if it is because of the delay in the conversion.
– Wictor Chaves
Wictor, the platform is Espressif, the provider of all Esp chips.Yes the Esp01 is a member of the Esp8266 family, as is the Esp-12F as well. But the esp32 is not esp8266, but an upgrade of this, has 2 colors, Bluetooh, etc.
– JoaoLopesF
Wictor, the buffer variable is in the loop and is not Static, this may cause data loss
– JoaoLopesF
As I declare a Static variable?
– Wictor Chaves
So you can understand? Non-static variable, each time Arduino runs the loop, is created: void loop() { // Data receiving buffer String bufferRecSerial = "" .... } Static variable (see Static in declaration) -> tells the compiler to create only once and keep this in memory. e.g. : void loop() { // Data receiving buffer Static String bufferRecSerial = "" }
– JoaoLopesF
I made, put the global variable and also tested by placing 'Static' inside the loop method, but the result was the same.
– Wictor Chaves
Wictor, how are you doing ? Please send the changed code
– JoaoLopesF
I edited and posted the modification.
– Wictor Chaves