Skip to content

Instantly share code, notes, and snippets.

@Olikonsti
Created November 5, 2022 20:01
Show Gist options
  • Save Olikonsti/d1f89309fe2924bff58f4cd7fe81db54 to your computer and use it in GitHub Desktop.
Save Olikonsti/d1f89309fe2924bff58f4cd7fe81db54 to your computer and use it in GitHub Desktop.
import random
import RPi.GPIO as GPIO
import time
import serial
import mysql.connector
ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=1)
ser.reset_input_buffer()
mydb = mysql.connector.connect(
host="mysql",
user="*",
password="*",
database="SolarData"
)
mycursor = mydb.cursor(buffered=True)
mydb.commit()
SAMPLES = 150
while True:
#ser.write(b"\n")
sample = 0
vb_sum = 0
cl_sum = 0
vp_sum = 0
cp_sum = 0
while sample < SAMPLES:
ser_response = ser.readline().decode('utf-8').rstrip()
print(ser_response)
if ser_response != "":
sample += 1
exec("global dic; dic = " + ser_response)
vb_sum += dic["vb"]
cl_sum += dic["cl"]
vp_sum += dic["vp"]
cp_sum += dic["cp"]
vb = round(vb_sum/sample, 3)
cl = round(cl_sum/sample, 3)
vp = round(vp_sum/sample, 3)
cp = round(cp_sum/sample, 3)
pl = round(cl*vb, 3)
pp = round(cp*vp, 3)
pb = round(pp-pl, 3)
print("DATA: vb:", vb, " cl:", cl, " vp:", vp, " cp:", cp, " pl:", pl, " pp:", pp)
mycursor.execute(f"INSERT INTO active (PP, PL, CP, VP, CL, VB, PB) VALUES ({pp}, {pl}, {cp}, {vp}, {cl}, {vb}, {pb})")
mydb.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment