Korishan
Administrator
- Joined
- Jan 7, 2017
- Messages
- 7,525
I now have InfluxDB and Grafana running on my laptop with my powermonitor connected to it. I use python to do the polling and import into InfluxDB.
I have a questions here.
1) I'm having an issue with receiving full lines from the serial monitor. I've coded up the python script to recognize the errors and keep going. I get no errors sent to the DB now.
I will randomly get these results:
It's like the arduino nano isn't sending all the data, but not sure why. I was at a baud of 115200, and changed to 128000 to see if that made any difference. It didn't decrease or increase the occurrences, that I could tell. I'm currently not counting the errors or figuring how often they occur
So what needs to be changed to make it run with less errors? I'm assuming the nano is sending "full" lines of text, as I don't see why it wouldn't. It just seems the serial read in the script isn't getting all the data for some reason.
2) In Grafana, I have some graphs for Volts/Current/Watts. What I would like to do is have a few queries that display the kWh over time. The time I would like to be static [1h,12h, 24h, 1month, 6month, 1yr] for example, or possibly event he ability to change that on the fly using only 1 panel. And also include the costs of power over time.
My current results for the past day look like this:
I'd like to do something like this guy did:https://fablab.dyn.nerd2nerd.org/grafana/d/000000001/fablab-power?refresh=1m&orgId=1
I have a questions here.
1) I'm having an issue with receiving full lines from the serial monitor. I've coded up the python script to recognize the errors and keep going. I get no errors sent to the DB now.
Code:
print "Connecting to", host, "using", database, "&", measurement, "with user:", username
port = '/dev/ttyUSB0'
#baud = 115200
baud = 128000
ser = serial.Serial(port, baud, timeout=0)
client = InfluxDBClient(host=host, username=username, password=password, database=database, use_udp=True, udp_port=8089, )
def send_udp(payload):
return client.write_points(payload)
cntr = 1
max = 200000
timersleep = float(.125)
refreshUpdate = 125
while cntr <= max:
lines = ser.readlines()
if cntr % 5 == 0:
print "Interation: ", cntr, " of ", max
starttime = time.time() * 1000
previous = float(0)
for line in lines:
line = line.rstrip("\n\r")
line = line.split(", ")
if len(line) == 7:
millis, volt, current1, watt1, current2, watt2, totalwatt = line
if previous == 0:
previous = float(millis)
newtime = starttime + float(millis) - float(previous)
previous = float(millis)
payload = [{ "measurement": measurement,
"time": int(newtime),
"fields": {
"volt": float(volt),
"current1": float(current1),
"watt1": float(watt1),
"current2": float(current2),
"watt2": float(watt2),
"totalwatt": float(totalwatt)
}
}]
# print payload
send_udp(payload)
else:
print("=============================================")
print("Iteration:", cntr)
print(" - Line not proper length of elements:", len(line))
print(" - line:", line)
print("=============================================")
# We sleep to allow the nano to dump data to the serial buffer
time.sleep(timersleep)
cntr += 1
ser.close()
I will randomly get these results:
Code:
Interation: 105365 of 200000
Interation: 105370 of 200000
Interation: 105375 of 200000
Interation: 105380 of 200000
=============================================
('Iteration:', 105383)
(' - Line not proper length of elements:', 5)
(' - line:', ['13411972', '118.21', '7.37', '870.75', ''])
=============================================
=============================================
('Iteration:', 105384)
(' - Line not proper length of elements:', 3)
(' - line:', ['16.85', '1991.74', '2862.49'])
=============================================
Interation: 105385 of 200000
Interation: 105390 of 200000
=============================================
('Iteration:', 105390)
(' - Line not proper length of elements:', 5)
(' - line:', ['13412865', '118.14', '7.27', '858.34', ''])
=============================================
=============================================
('Iteration:', 105391)
(' - Line not proper length of elements:', 3)
(' - line:', ['16.87', '1993.34', '2851.68'])
=============================================
Code:
void setup()
{
Serial.begin(128000); //115200);
.......
}
void loop()
{
String buf;
unsigned long currentTimer = millis();
static unsigned long lastUpdate;
static unsigned long lastSerialUpdate;
static unsigned long counter;
float currentLine1 = emon1.Irms; //extract Irms into Variable
float voltageLine1 = emon1.Vrms; //extract Vrms into Variable
float currentLine2 = emon2.Irms; //extract Irms into Variable
float voltageLine2 = emon2.Vrms; //extract Vrms into Variable
float totalWatts;
totalWatts = voltageLine1 * currentLine1;
totalWatts += voltageLine2 * currentLine2;
if (currentTimer - lastSerialUpdate > refreshSerial)
{
buf = String(currentTimer) + ", " +
String(voltageLine1) + ", " +
String(currentLine1) + ", " +
String(currentLine1 * voltageLine1) + ", " +
String(currentLine2) + ", " +
String(currentLine2 * voltageLine1) + ", " +
String((currentLine1 * voltageLine1) + (currentLine2 * voltageLine1));
// String(voltageLine2) + ", " +
Serial.println(buf);
lastSerialUpdate = currentTimer;
}
....
}
2) In Grafana, I have some graphs for Volts/Current/Watts. What I would like to do is have a few queries that display the kWh over time. The time I would like to be static [1h,12h, 24h, 1month, 6month, 1yr] for example, or possibly event he ability to change that on the fly using only 1 panel. And also include the costs of power over time.
My current results for the past day look like this:
I'd like to do something like this guy did:https://fablab.dyn.nerd2nerd.org/grafana/d/000000001/fablab-power?refresh=1m&orgId=1