我一天的大部分时间都在工作,让电容式土壤传感器通过我的 ESP32 DEVKITV1 与我的 Pi 服务器上的 InfluxDB 设置进行通信。我已经成功地设置了读取传感器并连接到我家的 WIFI,但我还无法让它与我的 InfluxDB 通信。
我正在使用由 davidgs 在 https://github.com/davidgs/ESP8266_Influx_DB_V2 分叉的 InfluxDB 处理库。这是我的第一个 ESP32 项目,自从 15 年前上大学以来我就没有接触过 C ....
我的设置:
我的 ESP32 代码:
#include "WiFi.h"
#include "InfluxDbV2.h"
//Setup variables and definitions
//definitions for WIFI
#define WIFI_SSID "redacted"
#define WIFI_PASS "redacted"
int status = WL_IDLE_STATUS;//initial status for setup start
#define INFLUXDB_HOST "192.168.1.220"//pi_server static ip
#define INFLUXDB_PORT 8086//port for influxdb on pi_server
InfluxdbV2 influx(INFLUXDB_HOST, INFLUXDB_PORT);
float asoilmoist=analogRead(34);//variable holding moisture reading for sensor 1
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 600 /* Time ESP32 will go to sleep (in seconds) */
void setup()
{
Serial.begin(115200); //Serial Print Deactivated if not needed
delay(10);
//Connect to WIFI
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
WiFi.setSleep(false);//to protect from it going to sleep
//While it's connecting, print a .
while (status != WL_CONNECTED) {
Serial.print(".");
delay(100);
status = WiFi.begin(WIFI_SSID, WIFI_PASS);
}
//Success message once connected to wifi
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
//influxdb setups
influx.setOrg("default");
influx.setBucket("db_soil_moisture");
influx.setToken();
//Success message
Serial.println("Setup complete");
}
void loop()
{
Serial.println((String)"Sensor 1 Soil Mosture:="+asoilmoist); //Debug Only
//Setup data to be written to db
InfluxDataV2 measurement ("Soil_Moisture");
measurement.addTag("Sensor", "1");
measurement.addValue("Value", asoilmoist);
//Write the data to table
influx.write(measurement);
delay(5000); //wait for it to write if slow
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); //Go to Sleep for Time X
Serial.println((String)"Going to sleep "+TIME_TO_SLEEP); //for debugging
esp_deep_sleep_start();
}
我目前遇到的错误是:
Arduino: 1.8.13 (Linux), Board: "DOIT ESP32 DEVKIT V1, 80MHz, 921600, None"
/home/nathan/Arduino/test_soil_moisture_V2/test_soil_moisture_V2.ino: In function 'void setup()':
test_soil_moisture_V2:50:19: error: no matching function for call to 'InfluxdbV2::setToken()'
influx.setToken();
^
In file included from /home/nathan/Arduino/test_soil_moisture_V2/test_soil_moisture_V2.ino:3:0:
/home/nathan/Arduino/libraries/ESP8266_Influx_DB_V2/InfluxDbV2.h:28:8: note: candidate: void InfluxdbV2::setToken(String)
void setToken(String token);
^
/home/nathan/Arduino/libraries/ESP8266_Influx_DB_V2/InfluxDbV2.h:28:8: note: candidate expects 1 argument, 0 provided
exit status 1
no matching function for call to 'InfluxdbV2::setToken()'
我尝试用 NULL、()、"" 和假字符串设置令牌。似乎没有任何效果。我深入研究了 InfluxDbV2.cpp、InfluxDbV2.h 和 InfluxDataV2.h 文件以尝试解决此问题,但我的成功率为零。
感谢@hcheung 上面的评论,我将行设置为
influx.setToken("SomeFakeToken")
,它删除了错误candidate expects 1 argument, 0 provided
,但它仍然无法正确编译。
然后我注意到这条线
'null' was not declared in this scope
if(_token == null || _token.length() < 10){
这是我整个周末都遇到的同样的错误。然后我记得读过 C 需要 NULL 和 js 使用 null 的方式。我更新了 InfluxDbV2.cpp 并将
null
更改为 NULL
并最终编译!