-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathallsky_ina3221.py
164 lines (147 loc) · 5.44 KB
/
allsky_ina3221.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
import sys
import board
import allsky_shared as s
from barbudor_ina3221.full import *
metaData = {
"name": "Current/voltage monitoring",
"description": "Monitors current and voltage using an ina3221",
"module": "allsky_ina3221",
"version": "v1.0.1",
"events": [
"periodic"
],
"experimental": "true",
"arguments":{
"i2caddress": "",
"c1enable": "false",
"c1name": "",
"c2enable": "false",
"c2name": "",
"c3enable": "false",
"c3name": "",
"extradatafilename": "allskyina3221.json"
},
"argumentdetails": {
"i2caddress": {
"required": "false",
"description": "I2C Address",
"help": "Override the standard i2c address for a device. NOTE: This value must be hex i.e. 0x40"
},
"c1enable" : {
"required": "false",
"description": "Enable Channel 1",
"help": "Enable channel 1 on the sensor",
"type": {
"fieldtype": "checkbox"
}
},
"c1name" : {
"required": "false",
"description": "Channel 1 name",
"help": "Name of the channel 1 allsky overlay variable"
},
"c2enable" : {
"required": "false",
"description": "Enable Channel 2",
"help": "Enable channel 2 on the sensor",
"type": {
"fieldtype": "checkbox"
}
},
"c2name" : {
"required": "false",
"description": "Channel 2 name",
"help": "Name of the channel 2 allsky overlay variable"
},
"c3enable" : {
"required": "false",
"description": "Enable Channel 3",
"help": "Enable channel 3 on the sensor",
"type": {
"fieldtype": "checkbox"
}
},
"c3name" : {
"required": "false",
"description": "Channel 3 name",
"help": "Name of the channel 3 allsky overlay variable"
},
"extradatafilename": {
"required": "true",
"description": "Extra Data Filename",
"tab": "Extra Data",
"help": "The name of the file to create with the voltage/current data for the overlay manager"
}
},
"businfo": [
"i2c"
]
}
def debugOutput(sensorType, temperature, humidity, dewPoint, heatIndex, pressure, relHumidity, altitude):
s.log(1,f"INFO: Sensor {sensorType} read. Temperature {temperature} Humidity {humidity} Relative Humidity {relHumidity} Dew Point {dewPoint} Heat Index {heatIndex} Pressure {pressure} Altitude {altitude}")
def readChannel(ina3221, channel):
ina3221.enable_channel(channel)
busVoltage = ina3221.bus_voltage(channel)
shuntVoltage = ina3221.shunt_voltage(channel)
current = ina3221.current(channel)
voltage = round(busVoltage + shuntVoltage,2)
current = round(abs(current),3)
s.log(4, f"INFO: Channel {channel} read, voltage {voltage}, current {current}. Bus Voltage {busVoltage}, Shunt Voltage {shuntVoltage}")
return voltage, current
def ina3221(params, event):
result = "Ina3221 read ok"
try:
c1enabled = params["c1enable"]
c1name = params["c1name"].upper()
c2enabled = params["c2enable"]
c2name = params["c2name"].upper()
c3enabled = params["c3enable"]
c3name = params["c3name"].upper()
extradatafilename = params['extradatafilename']
i2cBus = board.I2C()
ina3221 = INA3221(i2cBus)
if INA3221.IS_FULL_API:
ina3221.update(reg=C_REG_CONFIG,
mask=C_AVERAGING_MASK |
C_VBUS_CONV_TIME_MASK |
C_SHUNT_CONV_TIME_MASK |
C_MODE_MASK,
value=C_AVERAGING_128_SAMPLES |
C_VBUS_CONV_TIME_8MS |
C_SHUNT_CONV_TIME_8MS |
C_MODE_SHUNT_AND_BUS_CONTINOUS)
extraData = {}
extraData[f"AS_{c1name}VOLTAGE"] = "N/A"
extraData[f"AS_{c1name}CURRENT"] = "N/A"
extraData[f"AS_{c2name}VOLTAGE"] = "N/A"
extraData[f"AS_{c2name}CURRENT"] = "N/A"
extraData[f"AS_{c3name}VOLTAGE"] = "N/A"
extraData[f"AS_{c3name}CURRENT"] = "N/A"
if c1enabled:
voltage, current = readChannel(ina3221,1)
extraData[f"AS_{c1name}VOLTAGE"] = str(voltage)
extraData[f"AS_{c1name}CURRENT"] = str(current)
if c2enabled:
voltage, current = readChannel(ina3221,2)
extraData[f"AS_{c2name}VOLTAGE"] = str(voltage)
extraData[f"AS_{c2name}CURRENT"] = str(current)
if c3enabled:
voltage, current = readChannel(ina3221,3)
extraData[f"AS_{c3name}VOLTAGE"] = str(voltage)
extraData[f"AS_{c3name}CURRENT"] = str(current)
s.saveExtraData(extradatafilename,extraData)
except Exception as e:
eType, eObject, eTraceback = sys.exc_info()
s.log(0, f'ERROR: ina3221 failed on line {eTraceback.tb_lineno} - {e}')
return result
def ina3221_cleanup():
moduleData = {
"metaData": metaData,
"cleanup": {
"files": {
"allskyina3221.json"
},
"env": {}
}
}
s.cleanupModule(moduleData)