ESP32传感器数据上传InfluxDB后数值异常求助
Arduino传感器数据上传InfluxDB异常排查问题
第一段代码(含InfluxDB上传逻辑)
#include <Arduino.h> #include <Wire.h> #include <WiFiMulti.h> WiFiMulti wifiMulti; #define DEVICE "ESP32" #include <InfluxDbClient.h> #include <InfluxDbCloud.h> // WiFi AP SSID #define WIFI_SSID "Steven8" // WiFi password #define WIFI_PASSWORD "Skylim@86" #define INFLUXDB_URL "https://us-east-1-1.aws.cloud2.influxdata.com" #define INFLUXDB_TOKEN "d9gGniB2IP1LF0S3JRjoPkE7mbuEEENZbKUH-daOxJdu1YauRYa27o9fAmeOBbnnU9plVXnCEpGyj36VWIkHAA==" #define INFLUXDB_ORG "d037a8a6fb755fff" #define INFLUXDB_BUCKET "presentation" #define TZ_INFO "UTC+8" const int moisture_sensor = 2; int vibration_sensor = 12; int vib_data = 0; int moisture_data; int moisture_read; InfluxDBClient client(INFLUXDB_URL, INFLUXDB_ORG, INFLUXDB_BUCKET, INFLUXDB_TOKEN, InfluxDbCloud2CACert); Point sensor("Data collection"); void setup() { Serial.begin(9600); // Setup wifi WiFi.mode(WIFI_STA); wifiMulti.addAP(WIFI_SSID, WIFI_PASSWORD); Serial.print("Connecting to wifi"); while (wifiMulti.run() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(); // Add tags sensor.addTag("device", DEVICE); sensor.addTag("location", "home"); // Accurate time is necessary for certificate validation and writing in batches // For the fastest time sync find NTP servers in your area: https://www.pool.ntp.org/zone/ // Syncing progress and the time will be printed to Serial. timeSync(TZ_INFO, "pool.ntp.org", "time.nis.gov"); // Check server connection if (client.validateConnection()) { Serial.print("Connected to InfluxDB: "); Serial.println(client.getServerUrl()); } else { Serial.print("InfluxDB connection failed: "); Serial.println(client.getLastErrorMessage()); } } void loop() { vib_data = analogRead(vibration_sensor); moisture_read = analogRead(moisture_sensor); moisture_data = ( 100 - ( (moisture_read/4095.00) * 100 ) ); // Get latest sensor readings sensor.clearFields(); // Add readings as fields to point sensor.addField("Moisture", moisture_data); sensor.addField("Vibration", vib_data); if (wifiMulti.run() != WL_CONNECTED) Serial.println("Wifi connection lost"); if (!client.writePoint(sensor)) { Serial.print("InfluxDB write failed: "); Serial.println(client.getLastErrorMessage()); } Serial.print("Vibration: "); Serial.println(vib_data); Serial.print("Mositure Percentage: "); Serial.println(moisture_data); delay(1000); }
第二段代码(仅串口输出传感器数据)
const int moisture_sensor = 2; int vibration_sensor = 12; int vib_data = 0; int moisture_data; int moisture_read; void setup(void){ Serial.begin(115200); /* Set the baudrate to 115200*/ } void loop() { vib_data = analogRead(vibration_sensor); moisture_read = analogRead(moisture_sensor); moisture_data = ( 100 - ( (moisture_read/4095.00) * 100 ) ); // Get latest sensor readings Serial.print("Vibration: "); Serial.println(vib_data); Serial.print("Mositure Percentage: "); Serial.println(moisture_data); delay(1000); }
问题说明
第二段代码的串口监视器显示传感器数据正常,数值能随环境变化而改变;但第一段代码在WiFi和InfluxDB连接正常(已确认InfluxDB Bucket处于激活状态)的情况下,上传至InfluxDB的数值异常,仅固定显示0和100,完全不随传感器状态变化。已尝试修改波特率、将数值改为浮点型,问题仍未解决,请求协助排查是否为数据格式或连接相关问题。
内容的提问来源于stack exchange,提问作者lim kiat tsen
相关产品推荐
相关产品推荐

