update Listen2Arduino.py for influxDB2, add data.txt timestamp in status display

This commit is contained in:
Ryan Tang 2024-02-27 18:54:59 -05:00
parent be3c8d0ef5
commit ff6314b12c
2 changed files with 55 additions and 4 deletions

View File

@ -2,12 +2,30 @@
import serial
import os
import influxdb_client
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
with open('/home/pi02/influx_token.txt', 'r') as f:
first_line = f.readline()
#token = os.environ.get("INFLUXDB_TOKEN")
token = first_line
org = "FSUFoxLab"
ip = "https://fsunuc.physics.fsu.edu/influx/"
write_client = influxdb_client.InfluxDBClient(url=ip, token=token, org=org)
bucket = "testing"
write_api = write_client.write_api(write_options=SYNCHRONOUS)
print(token)
ser = serial.Serial('/dev/ttyACM0', 9600, timeout=1)
ser.reset_input_buffer()
outFile = "/home/pi02/data.txt"
cmd='curl -s -XPOST "http://fsunuc.physics.fsu.edu:8086/write?db=interlock" --data-binary @/home/pi02/data.txt'
# cmd='curl -s -XPOST "http://fsunuc.physics.fsu.edu:8086/write?db=interlock" --data-binary @/home/pi02/data.txt'
OPEN = 0
CLOSED = 1
@ -73,9 +91,12 @@ while True:
list.append(component)
# print(line)
point = Point("Measurement")
point2 = Point("Status")
# for key, value in format_value.items():
# print(f"{key} value={value}")
for key, value in format_value.items():
# print(f"{key} value={value}")
point.field(key, value)
with open(outFile, "w") as file:
for key, value in format_value.items():
@ -88,6 +109,9 @@ while True:
file.write(f"hv value=1\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=1\n")
point2.field("preaccl", 1).field("valve1", 1).field("valve2", 1).field("hv", 1).field("boiler", 1).field("ion",1)
if value == "2":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=1\n")
@ -95,6 +119,9 @@ while True:
file.write(f"hv value=0\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=1\n")
point2.field("preaccl", 0).field("valve1", 1).field("valve2", 1).field("hv", 0).field("boiler", 1).field("ion",1)
if value == "3":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=0\n")
@ -102,6 +129,9 @@ while True:
file.write(f"hv value=0\n")
file.write(f"boiler value=1\n")
file.write(f"ion value=0\n")
point2.field("preaccl", 0).field("valve1", 0).field("valve2", 0).field("hv", 0).field("boiler", 1).field("ion",0)
if value == "4":
file.write(f"preaccl value=0\n")
file.write(f"valve1 value=0\n")
@ -110,8 +140,17 @@ while True:
file.write(f"boiler value=0\n")
file.write(f"ion value=0\n")
point2.field("preaccl", 0).field("valve1", 0).field("valve2", 0).field("hv", 0).field("boiler", 0).field("ion",0)
for i in range(16):
if int(list[i]) != input_normal[i]:
file.write(f"#{i}--{in_txt[i]}\n")
os.system(cmd)
try:
# os.system(cmd)
print(point)
print(point2)
write_api.write(bucket=bucket, org=org, record=point)
write_api.write(bucket=bucket, org=org, record=point2)
except:
print("problem pushing data")

View File

@ -11,6 +11,9 @@
#include <sstream>
#include <vector>
#include <iomanip> // for std::setprecision
#include <sys/stat.h>
#include <unistd.h>
#include <ctime>
const std::string error_txt[16] = {
"Reset Button", //0
@ -72,6 +75,7 @@ bool ionizer = true ;
std::vector<int> error_code;
bool isTritiumError = false;
bool isVaccumError = false;
char timeBuffer[80];
void readStatus(){
std::ifstream file("data.txt"); // Replace "your_file.txt" with your actual file path
@ -126,6 +130,13 @@ void readStatus(){
file.close();
struct stat fileStat;
stat("data.txt", &fileStat);
time_t lastModifiedTime = fileStat.st_mtime;
struct tm* timeinfo = localtime(&lastModifiedTime);
strftime(timeBuffer, sizeof(timeBuffer), "data.txt : %Y-%m-%d %H:%M:%S", timeinfo);
if( statusReading == 0 || statusReading == 1 ){ // startup or normal
preAccel = true;
valve1 = true;
@ -260,6 +271,7 @@ int main() {
textDisplay( "SubPump : " + FormatDouble(subPumpReading*1000) + " mA", 300, 40, blueColor);
textDisplay("update every 5 sec.", 1200, 670, blackColor);
textDisplay(timeBuffer, 0, 670, blackColor);
// Update the screen
SDL_RenderPresent(renderer);