-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfishpi.py
executable file
·146 lines (87 loc) · 2.56 KB
/
fishpi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/python3
#encoding:utf-8
#
# Author Dan Pancamo
# Description: Get the distance to the fish
# HARDWARE
# Rasapberry pi0w https://amzn.to/32Le5c6
# adafruit ultrasonic 4884
# Updates
# 11/19/2020 New FishpI
from gpiozero import CPUTemperature
import psutil
import graphyte
import time
import os
import serial
import statistics
#GLOBALS
disthigh = 0
distlow = 1000
distavg = 0
count = 0
medianDistance = 0
CpuUtil = 0
GRAPHITE="localhost"
HOT="hot.pancamo.com"
myws="KTXOLIVI"
mywslocation="PancamoPointPier"
graphyte.init(GRAPHITE, prefix='wu.KTXOLIVI.PI2')
sender1 = graphyte.Sender(GRAPHITE, prefix='wu.KTXOLIVI.PI2')
#sender2 = graphyte.Sender(HOT, prefix='wu.KTXOLIVI.PI2')
SERIAL_PORT = "/dev/serial0" # or "COM4" or whatever
serialport = serial.Serial(SERIAL_PORT, 9600)
# This function measures a distance
def read_me007ys(ser, timeout = 1.0):
ts = time.time()
buf = bytearray(3)
idx = 0
while True:
# Option 1, we time out while waiting to get valid data
if time.time() - ts > timeout:
raise RuntimeError("Timed out waiting for data")
c = ser.read(1)[0]
#print(c)
if idx == 0 and c == 0xFF:
buf[0] = c
idx = idx + 1
elif 0 < idx < 3:
buf[idx] = c
idx = idx + 1
else:
chksum = sum(buf) & 0xFF
if chksum == c:
return (buf[1] << 8) + buf[2]
idx = 0
return None
def toGraphite(Distance,CpuUtil):
cpu = CPUTemperature()
FishDist = ((Distance*0.0393701)/12)
CpuTemp = (cpu.temperature * (9/5)) + 32
#print("FishDist=",FishDist)
#print("----------sender 1---------------------")
sender1.send('FishDist', FishDist)
sender1.send('CpuTemp',CpuTemp)
sender1.send('CpuUtil',CpuUtil)
#print("----------sender 2---------------------")
#sender2.send('WaterLevelHigh', WaterLevelHigh)
#sender2.send('WaterLevelLow', WaterLevelLow)
#sender2.send('WaterLevelAvg', WaterLevelAvg)
#sender2.send('WaterLevelMedian', WaterLevelMedian)
#sender2.send('WaveHeight', WaveHeight)
#sender2.send('CpuTemp',CpuTemp)
#sender2.send('CpuUtil',CpuUtil)
return
if __name__ == '__main__':
try:
while True:
distance = read_me007ys(serialport)
#print("CURRENT = %.1f in" % (distance*0.0393701))
if distance > 0:
Cpu = psutil.cpu_percent(percpu=True)
CpuUtil = Cpu[0]
toGraphite(distance,CpuUtil)
#time.sleep(.3)
# Reset by pressing CTRL + C
except KeyboardInterrupt:
print("Measurement stopped by User")