kahve
May 2, 2020, 9:05am
1
I am trying to receive some data from a remote server with the ESP-01 module and an Arduino UNO. Everything is fine while receiving a few characters but if i want to receive more than 68 characters, the received data becomes null. Is there any way I can receive more than 68 characters? And by the way, here is my code:
void sendRequest(){
ESP8266.println("AT+CIPSTART=4,\"TCP\",\"returnnull.xyz\",80");
delay(1000);
printResponse();
String cmd = "GET /pokenull/?name="+uniqname+"&pass="+password+"&page="+page+" HTTP/1.0";
ESP8266.println("AT+CIPSEND=4," + String(cmd.length() + 4));
delay(1000);
Serial.println(cmd);
ESP8266.println(cmd);
delay(1000);
ESP8266.println("");
fetch = false;
}
And here is the part that catches the result:
String s = "";
if (ESP8266.available()) {
s+=ESP8266.readStringUntil('\n')+"\n";
String as = s.substring(0,4);
if(as == "+IPD"){
Serial.println("DATA");
isData=true;
//lcd.clear();
}
if(isData==true){
dataCnt++;
if(dataCnt == 4){
Serial.println("COUNT");
datalength = s.substring(16);
datalength = datalength.substring(0,datalength.length()-2);
dlen = datalength.toInt();
Serial.println(dlen);
}
if(dataCnt == 8){
Serial.println("HERE");
data = s.substring(0,dlen);
splitByDel(data,';');
// strtok_r(data,";");
Serial.println(data);
//lcd.print(data);
Serial.println("RESET");
dataCnt=0;
isData=false;
}
}
//Serial.print(dataCnt);
//Serial.print( ": ");
//Serial.println(s);
}
Juraj
May 2, 2020, 9:19am
2
what happens between the two code snippets?
kahve
May 2, 2020, 9:25am
3
Nothing. The loop is like this:
void loop() {
if(digitalRead(4) == LOW){digitalWrite(9, HIGH); page=lpage; fetch=true; Serial.println(page);}else if(digitalRead(8) == LOW){digitalWrite(9, HIGH); page=rpage; fetch=true; Serial.println(page);} else {digitalWrite(9, LOW); }
if(digitalRead(5) == LOW){digitalWrite(11, HIGH); page=cpage; fetch=true; Serial.println(page);}else {digitalWrite(11, LOW);}
if(digitalRead(7) == LOW){digitalWrite(10, HIGH); page=spage; fetch=true; Serial.println(page);}else {digitalWrite(10, LOW);}
//if (millis() - lastTimeMillis >7000) {
// lastTimeMillis = millis();
if(fetch){
sendRequest();
}
// }
String s = "";
if (ESP8266.available()) {
s+=ESP8266.readStringUntil('\n')+"\n";
String as = s.substring(0,4);
if(as == "+IPD"){
Serial.println("DATA");
isData=true;
//lcd.clear();
}
if(isData==true){
dataCnt++;
if(dataCnt == 4){
Serial.println("COUNT");
datalength = s.substring(16);
datalength = datalength.substring(0,datalength.length()-2);
dlen = datalength.toInt();
Serial.println(dlen);
}
if(dataCnt == 8){
Serial.println("HERE");
data = s.substring(0,dlen);
splitByDel(data,';');
Serial.println(data);
Serial.println("RESET");
dataCnt=0;
isData=false;
}
}
//Serial.print(dataCnt);
//Serial.print( ": ");
//Serial.println(s);
}
}
void splitByDel(String in, char del){
//Serial.println("ENTERED" + in);
int inc = 0;
lcd.clear();
String dataComing;
while(in.indexOf(del) > -1){
if(in[0] == del){
if(inc == 0){
lcd.print(dataComing);
}
if(inc == 1){
spage = dataComing;
}
if(inc == 2){
cpage = dataComing;
}
if(inc == 3){
lpage = dataComing;
}
if(inc == 4){
rpage = dataComing;
}
inc++;
//Serial.println("************"+dataComing+"************");
dataComing = "";
in.remove(0,1);
}
dataComing += in[0];
in.remove(0,1);
//Serial.println(in + dataComing);
}
}
Juraj
May 2, 2020, 9:44am
4
so I would expect ESP8266.available() to be 0 right after you send the request. then you send it again and the response data from the first request overflows the RX buffer while you send the request again
kahve
May 2, 2020, 9:50am
5
I just send the request once because I have a bool called "fetch". And it only goes true when I press a button. I set it false when I send a request.
Juraj
May 2, 2020, 10:13am
6
you underestimate the speed of the MCU. it makes many loops while you push the button
kahve
May 2, 2020, 10:41am
7
I give a 3 second delay when making a request. It should not be possible.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
If the data coming to the Uno is longer than 64 bytes (the size of the serial input buffer) you must ensure that the function to take data from the buffer is called more often than the length of time it takes to fill the buffer. As you have not posted the complete program I can't see what baud rate you are using.
...R
Juraj
May 2, 2020, 1:10pm
9
kahve:
I give a 3 second delay when making a request. It should not be possible.
then the response overflows the RX while in delay.
you must start to read the response immediately as the AT firmware start to send it.
kahve
May 2, 2020, 1:56pm
10
The thing is, the problem should be at the arduino because ESP-01 also sends the data length as 0.
kahve
May 2, 2020, 2:09pm
11
But if I send the commands manually, there is no problem.
I recognise all the words but I don't understand Replies #9 and #10
Please keep in mind that you know all about your project but we only know what you tell us.
...R