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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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: Fri, 07 Aug 2020 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);
}
}