-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjet-testing.py
74 lines (62 loc) · 1.71 KB
/
jet-testing.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
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import time
import os
import glob
from mqtt_client.publisher import Publisher
import json
#Setup Temp Sensor
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + w1_slave'
# Setup ADC Sensor
i2c = busio.I2C(board.SCL, board.SDA)
ads = ADS.ADS1115(i2c)
ads.gain = 1
chan = AnalogIn(ads, ADS.P0)
chan2 = AnalogIn(ads,ADS.P1)
# Setup Pubber
pubber = Publisher(client_id="jet-pubber")
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
def publish_temp_status():
temp_c, temp_f = readTemp()
message = {
'temp_c' : temp_c,
'temp_f': temp_f,
}message
app_json = json.dumps(message)
pubber.publish("/status/temp",app_json)
print(message)
def publish_adc_status():
jet1_amps = ((chan.voltage - 2.47) / 0.013)
jet2_amps = ((chan2.voltage - 2.47) / 0.013)
message = {
'jet1_amps': jet1_amps,
'jet2_amps': jet2_amps
}
print(json.dumps(message))
app_json = json.dumps(message)
pubber.publish("/status/adc",app_json)
while(True):
#publish_temp_status()
publish_adc_status()
time.sleep(0.1)