Reputation: 225
I use the WiFiClient to connect to an external host and read the payload. That part of the code works fine until I've tried to collect the chars into one string to parse it after.
There is the code
#include <ESP8266WiFi.h>
const char* ssid = "***";
const char* password = "***";
const char* host = "host.com";
void setup()
{
Serial.begin(115200);
Serial.println();
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
pinMode(D5, OUTPUT);
}
void runBullshit()
{
digitalWrite(D5, true);
delay(500);
digitalWrite(D5, false);
}
void loop()
{
WiFiClient client;
if (client.connect(host, 80))
{
client.print(String("GET /path") + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n" +
"\r\n"
);
char payload[] = "";
int size = 0;
while (client.connected())
{
if (client.available())
{
char data = client.read();
payload[size] = data;
}
size++;
}
payload[sizeof(payload)] = '\0';
Serial.println(payload);
client.stop();
}
else
{
Serial.println("connection failed!]");
client.stop();
}
delay(3000);
}
And there are the errors
Connecting to MikroTik-35C8D8 . connected
Exception (3):
epc1=0x40202611 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40016d7f depc=0x00000000
ctx: cont
sp: 3ffffd80 end: 3fffffd0 offset: 01a0
>>>stack>>>
3fffff20: 00000000 40203ce8 3ffe8510 00000000
3fffff30: 3fffdad0 3ffeeab4 40203e0c 3fffefb0
3fffff40: 3fffdad0 00000000 00000064 40203ec6
3fffff50: 3fffdad0 00000000 3fffff98 40203f09
3fffff60: 3fffdad0 00000000 00000048 40203482
3fffff70: 3ffe88b9 00000000 40016d7f 40202611
3fffff80: 40204348 00000000 00001388 40203908
3fffff90: 00000000 3ffef77c 00000000 00000000
3fffffa0: 00000000 00000000 00000000 00000000
3fffffb0: 3fffdad0 00000000 3ffeeaac 40203e98
3fffffc0: feefeffe feefeffe 3ffe8510 40100739
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(3,7)
load 0x4010f000, len 1384, room 16
tail 8
chksum 0x2d
csum 0x2d
vbb28d4a3
~ld
So, if I'll just put the data
into the Serial I'll see the whole payload with no errors, but in the case, up above I catch the Exception 3 and cause 2.
PS And yeah, in my code I have digitalWrite and so on because I'm going to use the response to decide set the pin to HIGH or not
Upvotes: 1
Views: 7948
Reputation: 2823
The problem is that you define payload
as an array of size 1
. You can not store your data into it.
On Arduino you can use the String
type:
String payload;
while (client.connected())
{
if (client.available())
{
char data = client.read();
payload += data;
}
}
Serial.println(payload);
client.stop();
Upvotes: 2