bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
548 stars 210 forks source link

Read response timeout when send GET request #194

Closed xkvnn closed 4 years ago

xkvnn commented 4 years ago

I use MEGA+WiFi R3 ATmega2560+ESP8266

I only can read first-line response header then throw timeout. HTTP/1.0 200 OK

Code

#include <MCUFRIEND_kbv.h>
#include "WiFiEsp.h"

char AP[] = "ht";
char PASS[] = "12345678";
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char HOST[] = "192.168.0.2";
int PORT = 8080;

unsigned long lastConnectionTime = 0;         // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10000L; // delay between updates, in milliseconds

// Initialize the Ethernet client object
WiFiEspClient client;

void setup(void) {
  Serial.begin(9600);

  Serial3.begin(115200);
  WiFi.init(&Serial3);
  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(AP);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(AP, PASS);
  }

  Serial.println("You're connected to the network");

  printWifiStatus();
}

void loop(void) {
  // if there's incoming data from the net connection send it out the serial port
  // this is for debugging purposes only
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if 10 seconds have passed since your last connection,
  // then connect again and send data
  if (millis() - lastConnectionTime > postingInterval) {
    httpRequest();
  }

  delay(5000);
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

// this method makes a HTTP connection to the server
void httpRequest()
{
  Serial.println();

  // close any connection before send a new request
  // this will free the socket on the WiFi shield
  client.stop();

  // if there's a successful connection
  if (client.connect(HOST, PORT)) {
    Serial.println("Connecting...");

    // send the HTTP PUT request
    client.println(F("GET /mega HTTP/1.1"));
    client.println(F("Host: 192.168.0.2"));
    client.println("Connection: close");
    client.println();

    // note the time that the connection was made
    lastConnectionTime = millis();
  }
  else {
    // if you couldn't make a connection
    Serial.println("Connection failed");
  }
}

Serial log

10:58:39.241 -> [WiFiEsp] Initializing ESP module
10:58:42.678 -> [WiFiEsp] Initilization successful - 2.2.1
10:58:42.713 -> Attempting to connect to WPA SSID: ht
10:58:47.716 -> [WiFiEsp] Connected to ht
10:58:47.716 -> You're connected to the network
10:58:47.751 -> SSID: ht
10:58:47.784 -> IP Address: 192.168.0.213
10:58:47.819 -> Signal strength (RSSI):-602 dBm
10:58:52.759 -> 
10:58:52.759 -> [WiFiEsp] Connecting to 192.168.0.2
10:58:52.794 -> Connecting...
10:58:57.972 -> HTTP/1.0 200 OK
10:59:03.994 -> [WiFiEsp] TIMEOUT: 162
10:59:03.994 -> 
10:59:03.994 -> [WiFiEsp] Disconnecting  3
10:59:04.029 -> [WiFiEsp] Connecting to 192.168.0.2
10:59:04.063 -> Connecting...
10:59:09.212 -> HTTP/1.0 200 OK
10:59:15.225 -> [WiFiEsp] TIMEOUT: 162
JAndrassy commented 4 years ago

use 9600 baud or my WiFiEspAT library https://github.com/jandrassy/WiFiEspAT#Why-a-new-wifiesp-library

xkvnn commented 4 years ago

use 9600 baud or my WiFiEspAT library https://github.com/jandrassy/WiFiEspAT#Why-a-new-wifiesp-library

If I use 9600 for Serial3, I can't init ESP module

[WiFiEsp] Initializing ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] Cannot initialize ESP module
[WiFiEsp] >>> TIMEOUT >>>
[WiFiEsp] No tag found
WiFi shield not present
JAndrassy commented 4 years ago

you have to change the baud rate in AT firmware too.

but you should really replace the AT firmware and use my library (if you don't need SSL)