-
Notifications
You must be signed in to change notification settings - Fork 0
/
RepeaterPi.py
225 lines (175 loc) · 6.94 KB
/
RepeaterPi.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
from influxdb import InfluxDBClient
from subprocess import check_output
import configparser
import time
import serial
import sys
__version__ = "2.6"
__author__ = "Erich Ellsworth"
__contact__ = "[email protected]"
# Need to tell where to find the cfg if running from systemd
config_file = '/root/RepeaterPi/config.ini'
if len(sys.argv) > 1 and sys.argv[1] == "--test":
config_file = './config_example.ini'
config = configparser.ConfigParser()
config.read(config_file)
# Config stuff
repeater_location = config['Basic']['repeater_location']
repeater_name = config['Basic']['repeater_name']
serial_port = config['Basic']['serial_port']
poll_time_sec = int(config['Basic']['poll_time_sec'])
main_cal = config['calibration']['main_cal']
amplifier_cal = config['calibration']['amplifier_cal']
fwd_pwr_cal = config['calibration']['pwr_fwd']
rev_pwr_cal = config['calibration']['pwr_rev']
# InfluxDB Login
hostname = config['Grafana']['hostname']
port = config['Grafana']['port']
username = config['Grafana']['username']
password = config['Grafana']['password']
database = config['Grafana']['database']
# Setup InfluxDB client
client = InfluxDBClient(hostname, port, username, password, database)
# Load serial library
serialPort = serial.Serial()
# average is 0, most recent 1, least recent 0
serialdata = ""
arduinoData = [0, 0, 0, 0, 0, 0] # 0 temp, 1 main, 2 amp, 3 fwd, 4 rev, 5 5v ref
tempHistory = [0, 0, 0, 0, 0, 0]
voltage = [0, 0, 0, 0]
x = 0
tx = True
startup = True
# Intro message
print("RepeaterPi %sv by KG5KEY on %s\n" % (__version__, repeater_name))
print("Copyright (C) 2017 Erich Ellsworth\n" +
"Contact: " + __contact__)
# gets the data from the ADC and converts it to raw voltage
def getVoltage(channel):
return (float(arduinoData[channel]) * (float(arduinoData[5]) * .001) / float(1023))
#return float((float(arduinoData[channel]) * 5.0 / float(1023)))
def scaleVoltage(channel):
rv = (getVoltage(channel) * (61/11))
if rv < 1:
rv = 0
return rv
def scaleWattage(channel):
return ((75 * getVoltage(channel)) / 3.3)
# calculates temp
def calcTemp(channel):
temp = round(float(((((getVoltage(channel) * 1000) - 500) / 10) * 9 / 5 + 32)), 2)
if abs(temp - tempHistory[0]) > 4:
temp = tempHistory[0]
return float(temp)
def getPiTemp():
cputemp = check_output(['/opt/vc/bin/vcgencmd', 'measure_temp'])
cputemp = str(cputemp[5:9])
for char in "b'":
cputemp = cputemp.replace(char,'')
return cputemp
def genTelemetry():
print("--------------------------------------")
print("Telemetry for " + str(time.asctime(time.localtime(time.time()))))
print("Temperature: " + str(tempHistory[0]) + " Pi Temp: " + str(getPiTemp()))
print("Primary: " + str(voltage[0]) + "v Amplifier: " + str(voltage[1]) + "v")
print("fwd " + str(pwr_fwd) + " rev " + str(pwr_rev))
print("Arduino Data: " + str(arduinoData))
print("x = " + str(x) + " tx: " + str(tx))
def calibrateTemp(channel):
temp = round(float(((((getVoltage(channel) * 1000) - 500) / 10) * 9 / 5 + 32)), 2)
return ([temp, temp, temp, temp, temp, temp])
def getSerialData():
serialdata = str(serialPort.readline())
for char in "b'rn":
serialdata = serialdata.replace(char,'')
for char in "\\":
serialdata = serialdata.replace(char,'')
return(serialdata.split(","))
def tempAverage(var):
tempHistory[5] = tempHistory[4]
tempHistory[4] = tempHistory[3]
tempHistory[3] = tempHistory[2]
tempHistory[2] = tempHistory[1]
tempHistory[1] = var
tempHistory[0] = round((tempHistory[1] + tempHistory[2] +
tempHistory[3] + tempHistory[4] + tempHistory[5]) / 5, 2)
return tempHistory
def updateDashboard():
json_body = [
{
"measurement": repeater_location,
"tags": {
"use": "null",
},
"fields": {
"temp": calcTemp(0),
"temp_pi": float(getPiTemp()),
"v_main": voltage[0],
"v_amp": voltage[1],
"arduino": float('%.5s' % str(float(arduinoData[5]) * float(.001))),
"pwr_fwd": pwr_fwd,
"pwr_rev": pwr_rev,
}
}
]
print("Sending data to " + hostname)
client.write_points(json_body)
print("Done sending data.")
if len(sys.argv) > 1:
if sys.argv[1] == "--copyright":
print("\nThis program is free software: you can redistribute it and/or modify\n" +
"it under the terms of the GNU General Public License as published by\n" +
"the Free Software Foundation, either version 3 of the License, or\n" +
"(at your option) any later version.\n\n" +
"This program is distributed in the hope that it will be useful,\n" +
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" +
"GNU General Public License for more details.\n\n" +
"You should have received a copy of the GNU General Public License\n" +
"along with this program. If not, see <http://www.gnu.org/licenses/>.")
if sys.argv[1] == "--test":
print("\n--- Start Report ---")
print("Python: " + sys.version)
print("PySerial: " + str(serial.__version__))
print("RepeaterPi: " + str(__version__))
print("argv: " + str(sys.argv))
print("--- End of Report ---")
sys.exit(0)
print("To view the copyright information, run RepeaterPi.py with the " +
"--copyright argument.")
if __name__ == '__main__':
print("\nStarting RepeaterPi service...")
# Serial setup
serialPort.baudrate = 9600
serialPort.port = serial_port
serialPort.open()
x = 0
startup = True
print("Reading data from Arduino, this may take up to 15 seconds.")
arduinoData = getSerialData()
tempHistory = calibrateTemp(0)
voltage[0] = (round(float(scaleVoltage(1)) * float(main_cal), 2))
voltage[1] = (round(float(scaleVoltage(2)) * float(amplifier_cal), 2))
voltage[2] = voltage[0]
voltage[3] = voltage[1]
pwr_fwd = (scaleWattage(3) * float(fwd_pwr_cal))
pwr_rev = (scaleWattage(4) * float(rev_pwr_cal))
updateDashboard()
startup = False
while True:
arduinoData = getSerialData()
tempAverage(calcTemp(0))
voltage[0] = (round(float(scaleVoltage(1)) * float(main_cal), 2))
voltage[1] = (round(float(scaleVoltage(2)) * float(amplifier_cal), 2))
pwr_fwd = (scaleWattage(3) * float(fwd_pwr_cal))
pwr_rev = (scaleWattage(4) * float(rev_pwr_cal))
genTelemetry()
if abs(voltage[0] - voltage[2]) > .3 or abs(voltage[1] - voltage[3]) > .3 or x > ((900 / poll_time_sec) - 1) or tx or pwr_fwd > 1:
updateDashboard()
voltage[2] = voltage[0]
voltage[3] = voltage[1]
x = 0
else:
x += 1
tx = pwr_fwd > 1
time.sleep(poll_time_sec)