Skip to content

Instantly share code, notes, and snippets.

@pingud98
Last active January 10, 2021 20:45
Show Gist options
  • Save pingud98/9e7a7626d1f122ebcbf0642546419959 to your computer and use it in GitHub Desktop.
Save pingud98/9e7a7626d1f122ebcbf0642546419959 to your computer and use it in GitHub Desktop.
Cosmic Pi Parser for Influx
from influxdb import InfluxDBClient
import serial
import uuid
import random
import time
import datetime
import s2cell
dbframe = 0
cosmicdict = {
"DeviceID": 0,
"UTCUnixTime": 0,
"SubSeconds": 0.0,
"TemperatureC": 0.0,
"Humidity": 0.0,
"AccelX": 0.0,
"AccelY": 0.0,
"AccelZ": 0.0,
"MagX": 0.0,
"MagY": 0.0,
"MagZ": 0.0,
"Pressure": 0.0,
"Altitude": 0.0,
"Longitude": 0.0,
"Latitude": 0.0
}
nstimestamp = 0
s2celllocation = 0
print("starting")
ser = serial.Serial(port='/dev/serial0', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1)
print("connected to: " + ser.portstr)
print ("The device ID using uuid1() is : ",end="")
print (uuid.uuid1().int)
cosmicdict['DeviceID']=uuid.uuid1().int
print("DB connection")
client = InfluxDBClient(host='localhost', port=8086)
client.create_database('cosmicpilocal')
data=[]
#ser.write("help\n");
while True:
line = ser.readline();
if line:
#print(line.decode('utf-8'))
line_str = str(line.decode('utf-8'))
data_type = line_str.split(':')[0]
gps_type = line_str.split(',')[0]
#print(data_type)
#print(gps_type)
if data_type in cosmicdict.keys():
data = line_str.split(':')[1].split(';')[0]
cosmicdict[data_type] = float(data)
if data_type == "PPS":
gps_lock_sting = line_str.split(':')[2]
gps_lock_sting = gps_lock_sting.split(';')[0]
if (len(gps_lock_sting) == 1):
cosmicdict[UTCUnixTim] += 1
gps_type = line_str.split(',')[0]
if gps_type == "$GPZDA" or gps_type == "$GNZDA":
if (line_str.count(',') == 6):
g_time_string = line_str.split(',')[1].split('.')[0] # has format hhmmss
hour = int(g_time_string[0:2])
minute = int(g_time_string[2:4])
second = int(g_time_string[4:6])
day = int(line_str.split(',')[2])
month = int(line_str.split(',')[3])
year = int(line_str.split(',')[4])
time_from_gps = datetime.datetime(year,month,day,hour,minute,second,tzinfo=None)
cosmicdict['UTCUnixTime'] = (time_from_gps - datetime.datetime(1970, 1, 1)).total_seconds()
if gps_type == "$GPGGA":
# sanity check
if (line_str.count(',') == 14):
# use this as documentation for the string: http://aprs.gids.nl/nmea/#gga
lat = line_str.split(',')[2]
lat = float(lat[0:2])
minutes = line_str.split(',')[2]
minutes = float(minutes[2:len(minutes)])
lat += minutes / 60.
if line_str.split(',')[3] == 'S':
lat = -lat
lon = line_str.split(',')[4]
lon = float(lon[0:3])
minutes = line_str.split(',')[4]
minutes = float(minutes[3:len(minutes)])
lon += minutes / 60.
if line_str.split(',')[5] == 'W':
lon = -lon
cosmicdict['Latitude'] = lat
cosmicdict['Longitude'] = lon
if data_type == "Event":
if ((line_str.count(':') == 3) and (line_str.count(';') == 1)):
sub_sec_string = line_str.split(':')[2]
sub_sec_string = sub_sec_string.split(';')[0]
if sub_sec_string.count('/') == 1:
# this is the newer format and we need to divide the first number by the second one
divisors = sub_sec_string.split('/')
current_subSeconds = float(divisors[0]) / float(divisors[1])
cosmicdict['SubSeconds'] = current_subSeconds
print(cosmicdict)
#conversion to ns for influx
#add s and ns, then multiply by 1e9
nstimestamp = cosmicdict['UTCUnixTime']+cosmicdict['SubSeconds']
nstimestamp = nstimestamp*1e9
nstimestamp = int(nstimestamp)
s2celllocation = s2cell.lat_lon_to_cell_id(cosmicdict['Latitude'],cosmicdict['Longitude'])
data = []
data.append("{measurement},id={DeviceID} s2_cell_id={s2_cell_id},lat={latitude},lon={longitude},Temp={Temp},Hum={Hum},Accelx={Accelx},Accely={Accely},Accelz={Accelz},Magx={Magx},Magy={Magy},Magz={Magz},Press={Pressx},Alt={Altx} {timestamp}"
.format(measurement='CosmicPiV1.8.1',
DeviceID=cosmicdict['DeviceID'],
s2_cell_id=s2celllocation,
latitude=cosmicdict['Longitude'],
longitude=cosmicdict['Latitude'],
Temp=cosmicdict['TemperatureC'],
Hum=cosmicdict['Humidity'],
Accelx=cosmicdict['AccelX'],
Accely=cosmicdict['AccelY'],
Accelz=cosmicdict['AccelZ'],
Magx=cosmicdict['MagX'],
Magy=cosmicdict['MagY'],
Magz=cosmicdict['MagZ'],
Pressx=cosmicdict['Pressure'],
Altx=cosmicdict['Altitude'],
timestamp=nstimestamp))
print(data)
client.write_points(data, database='cosmicpilocal', time_precision='n', batch_size=1, protocol='line')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment