1

Below HTTP Get request is working but after few GET request(5-6) the code stops and once I press Arduino reset button then only it start working. Probably looks like something is causing OOM:

// This is a demo example using a public http server for testing both GET and POST requests
#include <Arduino.h>
#include <LiteESP8266Client.h>
#define PACKET_MTU 1500 // Standard network MTU is 1500 bytes
LiteESP8266 radio;
const char ssid[] PROGMEM = "abcd"; //change it to your wifi SSID
const char password[] PROGMEM = "abcd"; //change it to your wifi password
const char host[] PROGMEM = "192.168.0.156";
const int port = 8080;
const char http_get_request[] PROGMEM = "GET /getLedStatus HTTP/1.1\r\n";
const char http_useragent[] PROGMEM = "User-Agent: Arduino-stm32/0.1\r\n";
const char http_content_type_json[] PROGMEM = "Content-Type: application/json\r\n";
const char http_host[] PROGMEM = "Host: 192.168.0.156\r\n";
const char http_close_connection[] PROGMEM = "Connection: close\r\n\r\n";
const char http_content_length_header[] PROGMEM = "Content-Length: ";
const char success[] PROGMEM = "success";
const char failed[] PROGMEM = "failed";
const char CRLF[] PROGMEM = "\r\n";
const char error_data_null[] PROGMEM = "Error: data came back null.";
const int ledPin=11;
void setupStationMode() {
 Serial.print("Setup station mode... ");
 if (radio.set_station_mode()) {
 Serial.println("success");
 } else {
 Serial.println("failed");
 }
}
void joinAP() {
 Serial.print("Join AP ");
 Serial.print("... ");
 if (radio.connect_to_ap(ssid, password)) {
 Serial.println("Success");
 } else {
 Serial.println("Failed");
 }
}
void establishTcpConnect() {
 Serial.print("Establish TCP Connection... ");
 if (radio.connect_progmem(host, port)) {
 Serial.println( "Success");
 } else {
 Serial.println( "Failed");
 }
}
void getHttpPacket() {
 char *data;
 while ((data = radio.get_response_packet(PACKET_MTU, 5000))) {
 if (data) {
 Serial.println("Response Received...");
 Serial.println(data);
 if(strstr(data, "ON") != NULL) {
 digitalWrite(ledPin, HIGH);
 }else{
 digitalWrite(ledPin, LOW);
 }
 } else {
 Serial.println(error_data_null);
 }
 }
 free(data);
}
void httpGet() {
 Serial.println("Sending GET request... ");
 radio.send_progmem(http_get_request);
 radio.send_progmem(http_useragent);
 radio.send_progmem(http_host);
 radio.send_progmem(http_close_connection);
}
void setup() {
 delay(2000);
 pinMode(ledPin, OUTPUT);
 radio.begin(9600,2,3);
 Serial.begin(9600);
 while (!Serial) {};
 setupStationMode();
 joinAP();
}
void loop() {
 establishTcpConnect();
 httpGet();
 getHttpPacket();
 }

Complete OUTPUT(See Last TCP call when it stopped):

Setup station mode... success
Join AP ... Success
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 3
Date: 2020年8月07日 13:47:47 GMT
Connection: close
OFF
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 3
Date: 2020年8月07日 13:47:53 GMT
Connection: close
OFF
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 3
Date: 2020年8月07日 13:47:58 GMT
Connection: close
OFF
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 3
Date: 2020年8月07日 13:48:04 GMT
Connection: close
OFF
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 3
Date: 2020年8月07日 13:48:10 GMT
Connection: close
OFF
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: 2020年8月07日 13:48:16 GMT
Connection: close
ON
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: 2020年8月07日 13:48:21 GMT
Connection: close
ON
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: 2020年8月07日 13:48:27 GMT
Connection: close
ON
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: 2020年8月07日 13:48:33 GMT
Connection: close
ON
Establish TCP Connection... Success
Sending GET request... 
Establish TCP Connection...

Probably looks like memory is not getting freed. I am using Arduino UNO + ESP8266 sheild

Update 2 Tried with other REST Server and same issue is observed:

// This is a demo example using a public http server for testing both GET and POST requests
#include <Arduino.h>
#include <LiteESP8266Client.h>
#define PACKET_MTU 1500 // Standard network MTU is 1500 bytes
LiteESP8266 radio;
const char ssid[] PROGMEM = "abc"; //change it to your wifi SSID
const char password[] PROGMEM = "abc"; //change it to your wifi password
const char host[] PROGMEM = "httpbin.org";
const int port = 80;
const char http_get_request[] PROGMEM = "GET /get HTTP/1.1\r\n";
const char http_useragent[] PROGMEM = "User-Agent: Arduino-stm32/0.1\r\n";
const char http_content_type_json[] PROGMEM = "Content-Type: application/json\r\n";
const char http_host[] PROGMEM = "Host: httpbin.org\r\n";
const char http_close_connection[] PROGMEM = "Connection: close\r\n\r\n";
const char http_content_length_header[] PROGMEM = "Content-Length: ";
const char success[] PROGMEM = "success";
const char failed[] PROGMEM = "failed";
const char CRLF[] PROGMEM = "\r\n";
const char error_data_null[] PROGMEM = "Error: data came back null.";
const int ledPin=11;
void setupStationMode() {
 Serial.print("Setup station mode... ");
 if (radio.set_station_mode()) {
 Serial.println("success");
 } else {
 Serial.println("failed");
 }
}
void joinAP() {
 Serial.print("Join AP ");
 Serial.print("... ");
 if (radio.connect_to_ap(ssid, password)) {
 Serial.println("Success");
 } else {
 Serial.println("Failed");
 }
}
void establishTcpConnect() {
 Serial.print("Establish TCP Connection... ");
 if (radio.connect_progmem(host, port)) {
 Serial.println( "Success");
 } else {
 Serial.println( "Failed");
 }
}
void getHttpPacket() {
 char *data;
 while ((data = radio.get_response_packet(PACKET_MTU, 5000))) {
 if (data) {
 Serial.println("Response Received...");
 Serial.println(data);
 if(strstr(data, "ON") != NULL) {
 digitalWrite(ledPin, HIGH);
 }else{
 digitalWrite(ledPin, LOW);
 }
 } else {
 Serial.println(error_data_null);
 }
 }
 free(data);
}
void httpGet() {
 Serial.println("Sending GET request... ");
 radio.send_progmem(http_get_request);
 radio.send_progmem(http_useragent);
 radio.send_progmem(http_host);
 radio.send_progmem(http_close_connection);
}
void setup() {
 delay(2000);
 pinMode(ledPin, OUTPUT);
 radio.begin(9600,2,3);
 Serial.begin(9600);
 while (!Serial) {};
 setupStationMode();
 joinAP();
}
void loop() {
 establishTcpConnect();
 httpGet();
 getHttpPacket();
 }

Output:

Setup station mode... success
Join AP ... Success
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 OK
Date: 2020年8月07日 13:57:45 GMT
Content-Type: application/json
Content-Length: 238
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Response Received...
{
 "args": {}, 
 "headers": {
 "Host": "httpbin"origin": "106.51.29.214", 
 "url": "http://httpbin.org/get"
}
CLOSED
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Establish TCP Connection... Success
Sending GET request... 
Response Received...
HTTP/1.1 200 OK
Date: 2020年8月07日 13:57:57 GMT
Content-Type: application/json
Content-Length: 238
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Response Received...
{
 "args": {}, 
 "headers": {
 "Host": "httpbin"origin": "106.51.29.214", 
 "url": "http://httpbin.org/get"
}
CLOSED
⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
Establish TCP Connection... Success
Sending GET request... 
Establish TCP Connection...

Server side code

package com.test.iot;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
 private static final Logger logger = LoggerFactory.getLogger(HelloController.class);
 private String ledStatus="OFF";
 @RequestMapping("/test")
 public String index() {
 logger.info("invoked");
 return "Greetings from Spring Boot!";
 }
 @RequestMapping("/getLedStatus")
 public String getLedStatus() {
 logger.info("ledStatus");
 return ledStatus;
 }
 @RequestMapping("/toggleLedStatus")
 public String toggleLedStatus(){
 if(ledStatus.equals("ON")){
 ledStatus="OFF";
 } else {
 ledStatus="ON";
 }
 return String.format("New LED Status: %s", ledStatus);
 }
}
asked Aug 7, 2020 at 13:50
12
  • What is your server code? The cause could be on the server side instead of client side. Also do you run the client using a public-facing test server such as httpbin.org as in the example to see if you are facing the same problem? Commented Aug 7, 2020 at 14:01
  • Yup same issue observed in example . Response attached here Commented Aug 7, 2020 at 14:03
  • Server side code attached Commented Aug 7, 2020 at 14:04
  • If you faced the same problem on both servers, then there is something wrong with the client, I will take a look tomorrow as I need to re-flash my esp-01 to do the test. Commented Aug 7, 2020 at 14:10
  • Could you add a delay in the loop to see if there is any difference? Commented Aug 7, 2020 at 14:26

1 Answer 1

2

There is a bug on the library example webclient.ino on getHttpPacket() and getHttpResponse() that it didn't free the memory correctly, causing memory leak. I've updated the code on github repo.

Specifically on your getHttpPacket(), here is the corrected code:

void getHttpPacket() {
 char *data;
 while ((data = radio.get_response_packet(PACKET_MTU, 5000))) {
 if (data) {
 Serial.println(F("Response Received..."));
 Serial.println(data);
 if(strstr(data, "ON") != NULL) {
 digitalWrite(ledPin, HIGH);
 }else{
 digitalWrite(ledPin, LOW);
 }
 free(data); // free the memory in every packet as Arduino does not have enough memory
 } else {
 Serial.println(error_data_null);
 }
 }
 // free(data);
}

If you have further issue regarding the library, you can raise an "issue" on the github, and I will take a look.

answered Aug 8, 2020 at 5:13
3
  • Awsome I will take latest code from github Commented Aug 8, 2020 at 5:39
  • BTW, as I mostly use stm32 for my works, I can afford to allocate memory that is equal to one packet MTU (i.e. 1500 bytes), if you are using Arduino, this will leave very little memory for your sketch, although LiteESP8266Client library is designed to utilise very little SRAM, if you found that you need more memory, you can redefine PACKET_MTU to smaller number. It is also better to wrap the string literal with F() macro to keep it in flash. Commented Aug 8, 2020 at 5:40
  • Many thanks @hcheung. Working like a charm..so far..touch wood Commented Aug 8, 2020 at 11:23

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.