【问题标题】:Sending UDP from Arduino Uno to InfluxDB将 UDP 从 Arduino Uno 发送到 InfluxDB
【发布时间】:2019-04-15 00:10:12
【问题描述】:

所以我有一个 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”

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

干杯

【问题讨论】:

    标签: arduino udp arduino-uno influxdb


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多