iT邦幫忙

0

Arduino mega 2560 + ESP8266 連接XAMPP Server錯誤

  • 分享至 

  • xImage

為什麼資料出到一半就會[WiFiEsp] TIMEOUT: 215。
有方法可以延長TIMEOUT的時間嗎?

問題與這個差不多:https://forum.arduino.cc/t/solved-esp-01-wifiesp-unable-to-get-request-response-from-some-sites/457983

getdata.php

<?php
echo "Hello";
?>

Arduino Code

#include "WiFiEsp.h"
#define ESP_BAUDRATE 115200
char ssid[] = "xxxxx";
char pass[] = "xxxxx";
int status = WL_IDLE_STATUS;
WiFiEspClient client;

int    HTTP_PORT   = 80;
String HTTP_METHOD = "GET";
char   HOST_NAME[] = "192.168.50.24";
String PATH_NAME   = "/getdata.php";

void setup() {
  Serial.begin(115200);
  Serial1.begin(ESP_BAUDRATE);
  WiFi.init(&Serial1);
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
      Serial.print("...");
      delay(5000);
    }
  }
  Serial.println("\nConnected");
  
  WiFiEspClient client;

  if(client.connect(HOST_NAME, HTTP_PORT)) {
    client.println("GET /getdata.php HTTP/1.1");       
    client.println("Host: 192.168.50.24"); 
    client.println("Connection: close"); 
    client.println();
    delay(5000);
    while(client.connected()) {
      if(client.available()){
        char c = client.read();
        Serial.print(c);
      }
    }
    client.stop();
    Serial.println();
    Serial.println("disconnected");
  } else {
    Serial.println("connection failed");
  }
  Serial.println("\nFinish");
}
void loop() {
}

Serial Monitor

17:48:07.443 -> [WiFiEsp] Initializing ESP module
17:48:11.759 -> [WiFiEsp] Warning: Unsupported firmware 3.0.4
17:48:15.757 -> Attempting to connect to SSID: xxxx
17:48:20.514 -> [WiFiEsp] Connected to xxxx
17:48:20.514 -> ...
17:48:25.535 -> Connected
17:48:25.535 -> [WiFiEsp] Connecting to 192.168.50.24
17:48:30.715 -> HTTP/1.1 200 OK
17:48:30.715 -> Date: Sun, 29 Oc[WiFiEsp] TIMEOUT: 215
17:48:32.753 -> 
17:48:32.753 -> disconnected
17:48:32.753 -> 
17:48:32.753 -> Finish

我把thingspeak.h library的 connection function修改來使用就成功了。
雖然改的有點亂,不過能跑就行,有需要的可以參考一下。

#include "Arduino.h"
#include <Client.h>
#include "WiFiEsp.h"
#include <SPI.h> 
#include <Wire.h> 

#define SERVER_URL "192.168.50.24"
#define SERVER_PORT_NUMBER 80
#define TIMEOUT_MS_SERVERRESPONSE 5000  // Wait up to five seconds for server to respond
#define TS_OK_SUCCESS              200     // OK / Success
#define TS_ERR_BADAPIKEY           400     // Incorrect API key (or invalid server address)
#define TS_ERR_BADURL              404     // Incorrect API key (or invalid server address)
#define TS_ERR_OUT_OF_RANGE        -101    // Value is out of range or string is too long (> 255 bytes)
#define TS_ERR_INVALID_FIELD_NUM   -201    // Invalid field number specified
#define TS_ERR_SETFIELD_NOT_CALLED -210    // setField() was not called before writeFields()
#define TS_ERR_CONNECT_FAILED      -301    // Failed to connect to server
#define TS_ERR_UNEXPECTED_FAIL     -302    // Unexpected failure during write to server
#define TS_ERR_BAD_RESPONSE        -303    // Unable to parse response
#define TS_ERR_TIMEOUT             -304    // Timeout waiting for server to respond
#define TS_ERR_NOT_INSERTED        -401  

unsigned int port = SERVER_PORT_NUMBER;
String hostname = SERVER_URL;
const int status = WL_IDLE_STATUS;
int lastReadStatus;
WiFiEspClient wifiClient;
Client * client = &wifiClient;

bool connectServer()
{
  bool connectSuccess = false;
      
  connectSuccess = client->connect(SERVER_URL, port);

  Serial.println(connectSuccess);
    
  return connectSuccess;
}

int getHTTPResponse(String & response)
{

  client->flush();
  
  long timeoutTime = millis() + TIMEOUT_MS_SERVERRESPONSE;
  
  while(client-> available() < 17){
    delay(2);
    if(millis() > timeoutTime){
      return TS_ERR_TIMEOUT;
    }
  }
  
  if(!client->find(const_cast<char *>("HTTP/1.1")))
  {
    Serial.println("P");
    return TS_ERR_BAD_RESPONSE; 
  }
  
  int status = client->parseInt();

  if(status != TS_OK_SUCCESS)
  {
    return status;
  }

  // Find Content-Length
  if(!client->find(const_cast<char *>("Content-Length:"))){
    Serial.println("L");
    return TS_ERR_BAD_RESPONSE;
  }
  int contentLength = client->parseInt();
  
  if(!client->find(const_cast<char *>("\r\n\r\n")))
  {
    Serial.println("M");
    return TS_ERR_BAD_RESPONSE;
  }
  
  timeoutTime = millis() + TIMEOUT_MS_SERVERRESPONSE;
  
  while(client->available() < contentLength){
    delay(2);
    if(millis() > timeoutTime){
      return TS_ERR_TIMEOUT;
    }
  }
  
  String tempString = String("");
  char y = 0;
  for(int i = 0; i < contentLength; i++){
    y = client->read();
    tempString.concat(y);
  }
  response = tempString;
  
  return status;
}

void emptyStream(){
  while(client->available() > 0){
    client->read();
  }
}

String read_id(String id)
{

  if(!connectServer())
  {
      lastReadStatus = TS_ERR_CONNECT_FAILED;
      return String("");
  }

  String readURL = String("/");
  readURL.concat("getdata.php");
  readURL.concat("?id="+id);

  if(!client->print("GET ")) Serial.println("F");
  if(!client->print(readURL)) Serial.println("F");
  if(!client->print(" HTTP/1.1\r\n")) Serial.println("F");
  if(!client->print("Host: " + hostname + "\r\n")) Serial.println("F");
  if(!client->print("\r\n")) Serial.println("F");
  
  String content = String();
  Serial.println(content);
  int status = getHTTPResponse(content);
  Serial.println(status);
    
  lastReadStatus = status;
  
  emptyStream();

  client->stop();

  if(status != TS_OK_SUCCESS) return String("");

  return content;
}
主程式
void setup() {
...
Serial.print(read_id("001"));
...
}
froce iT邦大師 1 級 ‧ 2023-10-30 08:32:02 檢舉
http://yhhuang1966.blogspot.com/2017/11/xampp.html

但我覺得應該用其他機器檢查訊號是否ok,網路是否通暢,網頁是否正確設置,稍微看了你的code,覺得不應該會有timeout的狀況。
應該不是網頁問題,因為連接其他網站也會有相同問題。網路方面我的手機和電腦也是連接同一網絡,沒有出現問題,而且TIMEOUT: 215每次都是出現在這地方 
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
jesswang
iT邦見習生 ‧ 2023-11-13 16:26:27

tempest,你用的硬體組合跟我一樣。
你的問題解決了嗎?

因為我發現thingspeak.h library的function可以成功讀到API中的Data,所以我就把它的connection function抄下來改為自己的URL/Host就可以了。

我要發表回答

立即登入回答