LowPowerLab Forum

Hardware support => CurrentRanger => Topic started by: mayor on March 30, 2021, 01:53:41 PM

Title: Current Ranger Logging data with Redis and Grafana
Post by: mayor on March 30, 2021, 01:53:41 PM
I have a Grafana stack, and decided to add the RedisTimeSeries data source for CurrentRanger data.

Anyone interested, let me know, I'll clean up and share if so.

(https://i.ibb.co/Yf8py94/Screen-Shot-2021-03-30-at-1-50-31-PM.png)
Title: Re: Logging data
Post by: Felix on March 30, 2021, 06:02:36 PM
I'm interested, and I am sure others would be too  ;)
Title: Re: Logging data
Post by: mayor on March 30, 2021, 07:49:59 PM
Step by step, then

1. Install and start Grafana https://grafana.com/docs/grafana/latest/installation/ (https://grafana.com/docs/grafana/latest/installation/)
2. Install Redis Time Series https://oss.redislabs.com/redistimeseries/ (https://oss.redislabs.com/redistimeseries/)
3. Install Redis Time Series as data source in Grafana https://github.com/RedisGrafana/grafana-redis-datasource (https://github.com/RedisGrafana/grafana-redis-datasource)
4. Set up your Grafana dashboard along the lines of:

(https://i.ibb.co/S7Ww6BJ/Screen-Shot-2021-03-30-at-7-41-56-PM.png)

5. Copy the following to a directory. It's not trying to be clever, and should easily be modifiable to suit your needs:

Code: [Select]
import io
import time
import math
import serial
from redistimeseries.client import Client as RedisTimeSeries
#from redis import Redis

redis_host = 'ubuntu'
redis_port = 6379
redis_key = 'energy'
redis_downsample = redis_key + '_sub'
serialdev = '/dev/cu.usbmodem301'
baud = 230400

rts = RedisTimeSeries(port=redis_port,host=redis_host)
rts.redis.flushdb()
rts.create(key=redis_key, retention_msecs=600000) # keep 10 minutes' worth
rts.create(key=redis_downsample, retention_msecs=3600000) # keep downsamples for 1 hour
rts.createrule(\
source_key=redis_key,\
dest_key=redis_downsample,\
aggregation_type='avg',\
bucket_size_msec=100) # aggregate average over 100 ms for downsamples

ser = serial.Serial(serialdev, baud, timeout=1)
sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser), None, None, newline='\r\n')
sio.write('u')
sio.flush()

# read last few lines of output to determine if usb logging is enabled
i = 0
while i < 4:
    i += 1
    l = sio.readline()
if not l:
    sio.write('u')
    sio.flush()

# log only one data point per millisecond
lastTime = 0

while True:
    t = math.floor(time.time()*1000)
    if t == lastTime:
        continue
    try:
        rts.add(key=redis_key, timestamp=t, value=format(float(sio.readline())*1000,'f'))
        lastTime = t
    except ValueError:
        print('non-data output')

6. pip install redis
7. pip install redistimeseries
8. pip install pyserial

Run the script with python, it should connect to the CurrentRanger and start logging to Redis, and should be viewable in Grafana
Title: Re: Current Ranger Logging data with Redis and Grafana
Post by: Felix on March 31, 2021, 09:15:34 AM
Thank you, looks great!
Title: Re: Current Ranger Logging data with Redis and Grafana
Post by: mayor on April 02, 2021, 08:27:45 PM
Added some useful stats... Very happy with the CurrentRanger so far!

(https://i.ibb.co/Z1RvHWV/Screen-Shot-2021-04-02-at-8-26-09-PM.png)

Title: Re: Current Ranger Logging data with Redis and Grafana
Post by: mayor on April 06, 2021, 10:21:23 AM
Alternate install steps for Grafana/Redis:

1. Install Docker
2.
Code: [Select]
git clone https://github.com/RedisGrafana/grafana-redis-datasource
3.
Code: [Select]
docker-compose up

Title: Re: Current Ranger Logging data with Redis and Grafana
Post by: willemb on July 23, 2022, 12:56:05 PM
exactly what I was looking for.  Thanks for sharing.  Very helpful.