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-datasource4. 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:
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