Author Topic: Current Ranger Logging data with Redis and Grafana  (Read 6970 times)

mayor

  • NewMember
  • *
  • Posts: 14
Current Ranger Logging data with Redis and Grafana
« 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.


« Last Edit: March 31, 2021, 09:15:24 AM by Felix »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Logging data
« Reply #1 on: March 30, 2021, 06:02:36 PM »
I'm interested, and I am sure others would be too  ;)

mayor

  • NewMember
  • *
  • Posts: 14
Re: Logging data
« Reply #2 on: March 30, 2021, 07:49:59 PM »
Step by step, then

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



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
« Last Edit: March 31, 2021, 08:58:56 AM by mayor »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 6866
  • Country: us
    • LowPowerLab
Re: Current Ranger Logging data with Redis and Grafana
« Reply #3 on: March 31, 2021, 09:15:34 AM »
Thank you, looks great!

mayor

  • NewMember
  • *
  • Posts: 14
Re: Current Ranger Logging data with Redis and Grafana
« Reply #4 on: April 02, 2021, 08:27:45 PM »
Added some useful stats... Very happy with the CurrentRanger so far!




mayor

  • NewMember
  • *
  • Posts: 14
Re: Current Ranger Logging data with Redis and Grafana
« Reply #5 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


willemb

  • NewMember
  • *
  • Posts: 6
Re: Current Ranger Logging data with Redis and Grafana
« Reply #6 on: July 23, 2022, 12:56:05 PM »
exactly what I was looking for.  Thanks for sharing.  Very helpful.