我正在尝试从dweet.io转换到我自己的InfluxDB数据库。
我对dweet.io所做的请求看起来像这样:
client.print(String("GET /dweet/for/nJLDK4mm3Xl6TcT8Yr06?key=7hHa9AhSGp6u684LGfya4Y&temperature=") + String(t) + "&humidity=" + String(h) + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
所以我虽然我可以编辑它来处理数据库,但我尝试了这个:
client.print(String("GET /write?db=mydb' --data-binary 'temperature,host=ESP826601 value=") + String(t) + "' HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
它没有工作,它能够连接到服务器,但无法提出请求。我的代码基于:https://www.openhomeautomation.net/cloud-temperature-logger-esp8266/
答案 0 :(得分:0)
从ESP8266或ESP32向InfluxDB发送数据的更方便的方法是使用HTTPclient库而不是WiFiClient。以下代码设置标头和有效负载,然后通过其REST API将其发送到InfluxDB。
#include <ESP8266HTTPClient.h>
const char* influxUrl = "http://localhost:8086/write?db=mydb";
const char* influxUser = "username";
const char* influxPassword = "password";
// Add your own sensors here, e.g., read from OneWire or ADC
id[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
value[10] = {22, 25, 24, 21, 22, 26, 22, 25, 24, 21};
// Send to InfluxDB
Serial.println("Sending data to InfluxDB");
String influxData = "";
for(int i = 0; i < 10; i++){
// Set data type and add tags
influxData += "air_temperature,house=hydroponic,id=" + id[i];
// Add temperature value and delimiter for next measurement
influxData += " value=" + String(value[i]) + "\n";
}
// Create POST message
http.begin(influxUrl);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.setAuthorization(influxUser, influxPassword);
// The first attempt to send returns -1
int httpCode = -1;
while(httpCode == -1){
httpCode = http.POST(influxData);
http.writeToStream(&Serial);
}
http.end();
作为奖励,将它与ESP的deep sleep功能相结合是有意义的,可将功耗从80mA降至10mA以下(根据我的测量,限制为0.01A)。 / p>
注意:上面的代码是从较大的项目中提取的,因此转录中可能有一两个错误。