从Arduino Uno发送UDP到InfluxDB

问题描述 投票:0回答:1

所以我有一个Arduino Uno和Ethernet Shield V2并将它们连接到温度传感器。一切都工作正常,温度显示为所需,问题是我似乎无法将结果保存在我的涌入数据库中。

这是我的素描:

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

const int sensorPin = A0;
int sensorVal;

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

byte host = {192, 168, 0, 153};
unsigned int port = 8089; // local port to listen on

EthernetUDP Udp;

void setup(){
//////////////////////
// PUT YOUR SETUP CODE HERE TO RUN ONCE
//////////////////////

  Serial.begin(9600); // open serial port

  Ethernet.begin(mac, host);
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // Check for Ethernet hardware present
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found. Sorry, can’t run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF) {
    Serial.println(“Ethernet cable is not connected.”);
  }
  // start UDP
  Udp.begin(port);
}

float getTemperature() {
  sensorVal = analogRead(sensorPin);
  float voltage = (sensorVal/1024.0) * 5.0;
  float temperatureC = (voltage - 0.5)*100;

  return temperatureC;
}

void loop(){
//////////////////////
// PUT YOUR MAIN CODE HERE; TO RUN REPEATEDLY
//////////////////////

  String line, temperature;
  delay(1000);
  temperature = String(getTemperature(), 2);
  Serial.println(temperature);
  line = String(“temperature value=” + temperature);
  Serial.println(line);

  Serial.println(“Sending UDP packet…”);
  Udp.beginPacket(host, port);
  Udp.print(“temperature value=”);
  Udp.print(temperature);
  Udp.endPacket();
}

这些是来自潮流数据库的配置文件的设置:

[[udp]]
enabled = true
bind-address = “:8089”
database = “arduino”

retention-policy = “”
InfluxDB precision for timestamps on received points ("" or “n”, “u”, “ms”, “s”, “m”, “h”)
precision = “s”

如果有人能给我一些关于我做错的线索,我将不胜感激。

干杯

arduino udp arduino-uno influxdb
1个回答
0
投票

According to the documentation,你想在调用Udp.beginPacket时传递一个远程IP(不是本地IP)。

host是否代表远程IP地址?看起来您正在使用host作为本地IP来启动以太网。您可能没有将数据包发送到远程主机。确保将本地IP传递给Ethernet.begin()并将远程IP传递给Udp.beginPacket()

© www.soinside.com 2019 - 2024. All rights reserved.