将UDP从Arduino Uno发送到InfluxDB

时间:2019-04-14 08:33:19

标签: arduino udp arduino-uno influxdb

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

这是我的草图:

#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();
}

这些是influxDB的配置文件中的设置:

[[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”

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

欢呼

1 个答案:

答案 0 :(得分:0)

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

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