问题描述
我需要使用HTTP进行简单的GET调用。尽管已成功连接到WiFi网络,并正确获取了IP,网关和DNS,但HTTP客户端仍保持连接失败错误。我也尝试使用WiFi客户端,但是没有运气。它不认为问题出在我的代码上,我也尝试使用examples文件夹中的代码,但它也失败了。我尝试连接到本地内部服务器,但失败了。我花了整整一整天的时间,无法弄清楚到底是怎么回事。
代码如下。这就是目前的所有内容。
我正在Windows上使用Visual Code Editor在Platform IO上使用Arduino for ESPRESSIF。
从setup()调用WifiConnect函数
最初,我只调用WiFi.begin()函数进行连接,该功能也与WiFi连接,但是由于连接失败,我根据示例代码尝试了ESP8266WiFiMulti。
我什至检查了主机名解析是否也有效
IPAddress remote_addr;
WiFi.hostByName("192.168.2.107",remote_addr);
Serial.println(remote_addr);
但是连接到任何服务器总是失败。我尝试连接到网络内部的服务器,Internet上的服务器,在特殊端口上运行http的服务器,普通端口80等。但是它顽固地拒绝连接。
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include "Ticker.h"
#include <WiFiClient.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti WiFiMulti;
void WifiConnect()
{
WiFi.mode(WIFI_STA);
WiFiMulti.addAP("MySSID","MySECret Password");
Serial.print("Connecting");
while (WiFiMulti.run() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Connected,IP address: ");
Serial.println(WiFi.localIP());
Serial.println(WiFi.dnsIP());
Serial.println(WiFi.gatewayIP());
Serial1.println(WiFi.localIP());
}
void logToThinkSpeakt(){
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
WiFiClient client;
HTTPClient http;
Serial.print("[HTTP] begin...\n");
if (http.begin(client,"http://jigsaw.w3.org/HTTP/connection.html")) { // HTTP
Serial.print("[HTTP] GET...\n");
// start connection and send HTTP header
int httpCode = http.GET();
// httpCode will be negative on error
if (httpCode > 0) {
// HTTP header has been send and Server response header has been handled
Serial.printf("[HTTP] GET... code: %d\n",httpCode);
// file found at server
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
String payload = http.getString();
Serial.println(payload);
}
} else {
Serial.printf("[HTTP] GET... failed,error: %s\n",http.errorToString(httpCode).c_str());
}
http.end();
} else {
Serial.printf("[HTTP} Unable to connect\n");
}
}
}
平台:Espressif 8266 2.6.2> NodeMCU 1.0(ESP-12E模块) 硬件:ESP8266 80MHz,80KB RAM,4MB Flash 包装:
- framework-arduinoespressif8266 3.20704.0(2.7.4)
- tool-esptool 1.413.0(4.13)
- tool-esptoolpy 1.20800.0(2.8.0)
- tool-mklittlefs 1.203.200522(2.3)
- tool-mkspiffs 1.200.0(2.0)
- 工具链-xtensa 2.40802.200502(4.8.2) LDF:库依赖查找器 LDF模式:查找器〜链,兼容性〜软 找到29个兼容的库 扫描依赖项... 依赖图 |-1.0 |-1.2 | |-1.0 |-1.0 在发布模式下构建
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)