Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python/Grafana Coding
#1
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.
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'])
=============================================
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

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;
  }
....
}
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...1m&orgId=1
Proceed with caution. Knowledge is Power! Literally! Cool 
Knowledge is Power; Absolute Knowledge is Absolutely Shocking!
Certified 18650 Cell Reclamation Technician

Please come join in general chit-chat and randomness at Discord Chat (channels: general, 3d-printing, linux&coding, 18650, humor, ...)
(this chat is not directly affiliated with SecondLifeStorage; VALID email req'd)
Reply


Messages In This Thread
Python/Grafana Coding - by Korishan - 09-28-2018, 03:48 PM
RE: Python/Grafana Coding - by Sean - 09-28-2018, 04:02 PM
RE: Python/Grafana Coding - by Korishan - 09-28-2018, 04:12 PM
RE: Python/Grafana Coding - by daromer - 09-28-2018, 04:50 PM
RE: Python/Grafana Coding - by jesusangel - 09-28-2018, 07:51 PM
RE: Python/Grafana Coding - by not2bme - 09-28-2018, 08:21 PM
RE: Python/Grafana Coding - by jesusangel - 09-28-2018, 08:37 PM
RE: Python/Grafana Coding - by Korishan - 09-28-2018, 09:56 PM
RE: Python/Grafana Coding - by Korishan - 09-28-2018, 09:59 PM
RE: Python/Grafana Coding - by Korishan - 09-28-2018, 11:25 PM
RE: Python/Grafana Coding - by jesusangel - 09-29-2018, 07:49 AM
RE: Python/Grafana Coding - by not2bme - 09-29-2018, 05:08 AM
RE: Python/Grafana Coding - by daromer - 09-29-2018, 09:39 AM
RE: Python/Grafana Coding - by Korishan - 09-29-2018, 11:45 AM
RE: Python/Grafana Coding - by not2bme - 09-29-2018, 12:40 PM
RE: Python/Grafana Coding - by daromer - 09-29-2018, 06:10 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 12:29 AM
RE: Python/Grafana Coding - by daromer - 09-30-2018, 08:18 AM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 12:26 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 05:19 PM
RE: Python/Grafana Coding - by daromer - 09-30-2018, 06:21 PM
RE: Python/Grafana Coding - by completelycharged - 09-30-2018, 06:33 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 07:18 PM
RE: Python/Grafana Coding - by completelycharged - 09-30-2018, 07:46 PM
RE: Python/Grafana Coding - by daromer - 09-30-2018, 09:35 PM
RE: Python/Grafana Coding - by completelycharged - 09-30-2018, 10:06 PM
RE: Python/Grafana Coding - by daromer - 09-30-2018, 10:11 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 10:15 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 10:18 PM
RE: Python/Grafana Coding - by daromer - 09-30-2018, 10:30 PM
RE: Python/Grafana Coding - by completelycharged - 09-30-2018, 10:53 PM
RE: Python/Grafana Coding - by Korishan - 09-30-2018, 11:41 PM
RE: Python/Grafana Coding - by daromer - 10-01-2018, 05:13 AM
RE: Python/Grafana Coding - by Korishan - 10-03-2018, 04:39 AM
RE: Python/Grafana Coding - by completelycharged - 10-03-2018, 01:08 PM
RE: Python/Grafana Coding - by Korishan - 10-03-2018, 01:18 PM
RE: Python/Grafana Coding - by completelycharged - 10-03-2018, 01:36 PM
RE: Python/Grafana Coding - by Korishan - 10-04-2018, 02:45 AM
RE: Python/Grafana Coding - by completelycharged - 10-04-2018, 03:23 PM
RE: Python/Grafana Coding - by daromer - 10-04-2018, 03:31 PM
RE: Python/Grafana Coding - by completelycharged - 10-04-2018, 04:41 PM
RE: Python/Grafana Coding - by daromer - 10-04-2018, 04:57 PM
RE: Python/Grafana Coding - by Korishan - 10-04-2018, 05:49 PM
RE: Python/Grafana Coding - by daromer - 10-04-2018, 06:02 PM
RE: Python/Grafana Coding - by daromer - 10-04-2018, 07:04 PM
RE: Python/Grafana Coding - by completelycharged - 10-05-2018, 11:23 AM
RE: Python/Grafana Coding - by Korishan - 10-05-2018, 12:22 PM
RE: Python/Grafana Coding - by completelycharged - 10-05-2018, 12:35 PM
RE: Python/Grafana Coding - by Korishan - 10-05-2018, 01:41 PM
RE: Python/Grafana Coding - by daromer - 10-05-2018, 02:20 PM
RE: Python/Grafana Coding - by completelycharged - 10-05-2018, 03:08 PM
RE: Python/Grafana Coding - by daromer - 10-05-2018, 05:41 PM
RE: Python/Grafana Coding - by Korishan - 10-06-2018, 02:52 AM
RE: Python/Grafana Coding - by daromer - 10-06-2018, 03:16 AM
RE: Python/Grafana Coding - by Korishan - 10-06-2018, 03:21 AM
RE: Python/Grafana Coding - by daromer - 10-07-2018, 01:33 PM
RE: Python/Grafana Coding - by Korishan - 10-07-2018, 04:15 PM
RE: Python/Grafana Coding - by daromer - 10-07-2018, 04:42 PM
RE: Python/Grafana Coding - by completelycharged - 10-07-2018, 08:17 PM
RE: Python/Grafana Coding - by daromer - 10-07-2018, 08:29 PM
RE: Python/Grafana Coding - by Korishan - 10-08-2018, 12:28 AM
RE: Python/Grafana Coding - by Korishan - 10-08-2018, 12:48 AM
RE: Python/Grafana Coding - by daromer - 10-08-2018, 05:38 AM
RE: Python/Grafana Coding - by Korishan - 10-08-2018, 09:50 AM
RE: Python/Grafana Coding - by daromer - 10-08-2018, 04:27 PM
RE: Python/Grafana Coding - by Korishan - 10-12-2018, 03:02 AM
RE: Python/Grafana Coding - by daromer - 10-12-2018, 05:00 AM
RE: Python/Grafana Coding - by Korishan - 10-12-2018, 02:11 PM
RE: Python/Grafana Coding - by daromer - 10-12-2018, 02:58 PM
RE: Python/Grafana Coding - by Korishan - 10-12-2018, 09:57 PM
RE: Python/Grafana Coding - by daromer - 10-13-2018, 07:12 AM
RE: Python/Grafana Coding - by Korishan - 10-13-2018, 12:02 PM
RE: Python/Grafana Coding - by daromer - 10-13-2018, 05:38 PM
RE: Python/Grafana Coding - by Korishan - 10-13-2018, 09:41 PM
RE: Python/Grafana Coding - by daromer - 10-14-2018, 05:22 AM
RE: Python/Grafana Coding - by Korishan - 10-14-2018, 12:43 PM
RE: Python/Grafana Coding - by daromer - 10-14-2018, 01:54 PM
RE: Python/Grafana Coding - by Korishan - 10-14-2018, 02:30 PM
RE: Python/Grafana Coding - by daromer - 10-14-2018, 02:55 PM
RE: Python/Grafana Coding - by Korishan - 10-14-2018, 03:53 PM
RE: Python/Grafana Coding - by daromer - 10-14-2018, 04:52 PM
RE: Python/Grafana Coding - by completelycharged - 02-02-2019, 09:36 PM
RE: Python/Grafana Coding - by JakekeKe9 - 09-02-2020, 10:50 PM

Forum Jump:


Users browsing this thread: 3 Guest(s)