如何解决获取 [e][wifigeneric.cpp:739] hostbyname(): 执行 POST 请求时 dns 失败
我刚开始接触电子和微控制器编程。
我制作了一个简单的电路,我使用 DHT22 传感器来测量温度和湿度。我还在 Node 和 Express(MongoDB 作为数据库)中制作了自己的 API。它是非常简单的 API,只有两个端点:一个用于获取。一个用于发布数据。我可以使用 Postman(和浏览器)创建成功的请求。
为了清楚起见,我想将温度和湿度发送到我的 API,然后在 Vue 网站上对这些数据进行一些处理(我认为这根本不相关,但同样,只是为了清楚我我正在努力实现)。
现在我会说我在用什么:
Windows 10 操作系统 NodeMCU ESP32 微控制器 DHT22传感器 HTTPClient 库(我认为这个库引起了问题) PlatformIO 与 Arduino.h 头文件 一切正常,但是当我尝试将数据发送到我的数据库时,事情失败了。我总是收到以下错误
[e][wifigeneric.cpp:739] hostbyname(): dns 失败
我尝试同时使用 http://localhost:3000/endpoint 和 http://127.0.0.1/endpoint 发出 POST 请求(这部分真的很奇怪,为什么在使用没有域名的 IP 地址时出现 DNS 错误?) .
我已经在网上查找了解决方案。我在 github 上遇到过很多类似的问题,但其中任何一个都对我有用)。此外,他们都没有解决由第 739 行引起的错误。
这里我会留下我的代码。这是简单而简短的,所以我将发布所有内容。请不要因为我的 C++ 技能而责怪我,我正在变得更好:D
提前谢谢您,祝您有个愉快的一天或晚上。
亲切的问候,巴特克。
#include <Arduino.h>
#include <DHT.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#define DHTPIN 22
#define DHTTYPE DHT22
DHT dht(DHTPIN,DHTTYPE);
const char ssid[] = "MY_SSIID";
const char password[] = "MY_WIFI_PASSWORD";
const char apiAddress[] = "http://localhost:3000/reading";
unsigned long lastTime = 0;
unsigned long timerDelay = 60000;
struct sensorReading {
float temperature;
float humidity;
float heatIndex;
};
sensorReading readDataFromSensor();
void sendData(sensorReading * reading);
void setup() {
Serial.begin(9600);
dht.begin();
WiFi.begin(ssid,password);
WiFi.config(IPAddress(192,168,1,223),IPAddress(192,1),IPAddress(255,255,0));
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print('.');
}
}
void loop() {
if (millis() > lastTime + timerDelay) {
sensorReading data;
sensorReading * p_data = NULL;
data = readDataFromSensor();
p_data = &data;
sendData(p_data);
}
}
sensorReading readDataFromSensor() {
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
float heatIndex = dht.computeHeatIndex(temperature,humidity,false);
if (isnan(temperature) || isnan(humidity)) {
return sensorReading {};
}
const sensorReading dataToReturn {temperature,heatIndex};
return dataToReturn;
}
void sendData(sensorReading * reading) {
using namespace std;
if(WiFi.status() == WL_CONNECTED) {
HTTPClient http;
http.begin(apiAddress,3000);
http.addHeader("Content-Type","application/json");
DynamicJsonDocument doc(1024);
doc["temperature"] = reading->temperature;
doc["humidity"] = reading->humidity;
doc["heatIndex"] = reading->heatIndex;
String dataToSend;
serializeJson(doc,dataToSend);
int responseCode = http.POST(dataToSend);
Serial.println(responseCode);
http.end();
} else {
Serial.print("Ups...");
}
lastTime = millis();
}
解决方法
您正在使用两个参数调用 begin()
上的 http
方法,这两个参数是主机名和端口号(以及可选的 URI/路径)。您传递的不是主机名,而是一个完整的 URL,HTTP 客户端试图将其解析为主机名。
http.begin()
的单参数形式确实采用 URL。您调用的表单没有。
您可以通过 reading the source code 确认这一点,它允许 begin()
方法的这些声明:
bool begin(WiFiClient &client,String url);
bool begin(WiFiClient &client,String host,uint16_t port,String uri = "/",bool https = false);
#ifdef HTTPCLIENT_1_1_COMPATIBLE
bool begin(String url);
bool begin(String url,const char* CAcert);
bool begin(String host,String uri = "/");
bool begin(String host,String uri,const char* CAcert,const char* cli_cert,const char* cli_key);
#endif
这个问题的一个重要提示是您在 URL 和 http.begin()
的第二个参数中重复了端口号。
相反,您的代码应如下所示:
http.begin(apiAddress);
或
const char apiName[] = "localhost";
const unsigned apiPort = 3000;
const char apiPath[] = "reading";
...
http.begin(apiName,apiPort,apiPath);
但是 - 这也不起作用,因为 localhost
和 127.0.0.1
表示“自己”或“同一台计算机”。您需要提供您尝试访问的服务器的真实 IP 地址; localhost
和 127.0.0.1
仅在您在其上运行软件时引用它。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。