I have planned to send the consumed amperes from my solar system to the influxdb,So i have decided to use arduino with Ethernet shield and i have sucessfully enable udp in myinfluxdb.
But when i run the code it didn’t send the values to the influx db.
Here is the code i have used:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
// 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, 1, 37};
int port = 8089; // local port to listen on
EthernetUDP udp;
const int analogIn = A0;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500;
double Voltage = 0;
double Amps = 0;
void setup() {
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 getAmps() {
RawValue = analogRead(analogIn);
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
Amps = ((Voltage - ACSoffset) / mVperAmp);
Serial.print("Raw Value = " ); // shows pre-scaled value
Serial.print(RawValue);
Serial.print("\t mV = "); // shows the voltage measured
Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
Serial.print("\t Amps = "); // shows the voltage measured
Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
delay(2500);
return Amps;
return Voltage;
}
void loop() {
String line1,line2,ampere,volts;
delay(1000);
// get the current temperature from the sensor, to 2 decimal places
ampere = String(getAmps(), 2);
// concatenate the temperature into the line protocol
line1 = String("ampere value=" + ampere);
Serial.println(line1);
// send the packet
Serial.println("Sending UDP packet...");
udp.beginPacket(host, port);
udp.print(line1);
udp.endPacket();
}