- Sun Aug 12, 2018 9:34 am
#200031
I'm having a lot of trouble getting the WiFi Shield to run as an HTTP server. I'm using code adapted from ESP8266_Shield_Demo, as follows:
But that's not the main problem here. I just can't get a reliable conversation going between my computer and the device. I'm using Postman (if you don't know about it you're missing out on the best network testing tool there is) to send a simple GET, and sometimes I get a response but usually after a handful of messages the reply never gets back. Postman doesn't report seeing any response at all. Other times it seems the Arduino doesn't receive a message, though this is rarer. Here's an example from the log, where it failed on the second message:
Code: Select all
I had occasional problems getting the device to connect; this was solved by forcing a reset after 10 failed attempts. The function "resetFunc" sits at address zero so when it's called the CPU resets. It sometimes takes a while but pretty well always succeeds in the end.#include <SoftwareSerial.h>
#include <SparkFunESP8266WiFi.h>
const char mySSID[] = "My router SSID";
const char myPSK[] = "My PSK key";
const String htmlHeader = "HTTP/1.1 200 OK\r\n"
"Access-Control-Allow-Origin: *\r\n"
"Content-Type: text/plain\r\n\r\n";
ESP8266Server server = ESP8266Server(80);
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup()
{
Serial.begin(9600);
// Initialize the ESP8266 shield, make sure it's present:
int count = 0;
while (esp8266.begin() != true)
{
Serial.print(".");
delay(1000);
if (++count == 10) {
Serial.println(F("resetting"));
delay(100);
resetFunc();
}
}
Serial.println();
Serial.println(F("Connected to ESP8266"));
// Set pin 5 (STAT LED) to OUTPUT:
esp8266.pinMode(5, OUTPUT);
esp8266.digitalWrite(5, HIGH);
count = 0;
while (esp8266.connect(mySSID, myPSK) < 0)
{
Serial.print("-");
delay(1000);
if (++count == 10) {
Serial.println(F("resetting"));
delay(100);
resetFunc();
}
}
IPAddress myIP = esp8266.localIP(); // Get the ESP8266's local IP
Serial.print(F("Connected to AP as "));
Serial.println(myIP);
server.begin();
esp8266.digitalWrite(5, LOW);
}
void loop()
{
// listen for incoming clients
ESP8266Client client = server.available(500);
if (client) {
Serial.println("Client request");
char c;
while (client.available())
{
c = client.read();
Serial.print(c);
}
Serial.println("Got the request");
client.print(htmlHeader);
// send the response
client.print("{\"r\":\"");
client.print("OK");
client.print("\"}");
delay(100);
client.stop();
Serial.println("Reply sent");
}
}
But that's not the main problem here. I just can't get a reliable conversation going between my computer and the device. I'm using Postman (if you don't know about it you're missing out on the best network testing tool there is) to send a simple GET, and sometimes I get a response but usually after a handful of messages the reply never gets back. Postman doesn't report seeing any response at all. Other times it seems the Arduino doesn't receive a message, though this is rarer. Here's an example from the log, where it failed on the second message:
Code: Select all
I've tried everything I can think of, such as increasing the memory available for the server, but nothing has any effect. Now I'm aware that no network is ever 100% reliable so I'm expecting to need a retry mechanism. However, at the moment it's so unreliable - not to mention slow - that I'm getting at best 1 message in 10, which makes it unusable for any practical applications. I'm hoping it's just my buggy code; something I haven't understood. Does anyone have any experience in this area?
Connected to ESP8266
Connected to AP as 192.168.1.7
Client request
+IPD,0,227:GET /?testdata HTTP/1.1
cache-control: no-cache
Postman-Token: b0ba6b30-553a-4884-9b53-54da0dd13e41
User-Agent: PostmanRuntime/7.2.0
Accept: */*
Host: 192.168.1.7
accept-encoding: gzip, deflate
Connection: keep-alive
Got the request
Reply sent
Client request
+IPD,0,227:GET /?testdata HTTP/1.1
cache-control: no-cache
Postman-Token: a9ca03e9-0373-4e80-b23d-a0d254fe5e67
User-Agent: PostmanRuntime/7.2.0
Accept: */*
Host: 192.168.1.7
accept-encoding: gzip, deflate
Connection: keep-alive
Got the request
Reply sent