File tree 2 files changed +54
-1
lines changed
2 files changed +54
-1
lines changed Original file line number Diff line number Diff line change
1
+ from machine import ADC
2
+ import ujson
3
+ import utime
4
+
5
+
6
+ class ACAnalogSensor :
7
+
8
+ def __init__ (self , data ):
9
+ self .index = str (data ["index" ])
10
+ self .serial_no = data ["serial_no" ]
11
+ self .gpio = int (data ["gpio" ])
12
+ self .type = data ["type" ]
13
+ self .window = 10
14
+ self .current_size = 0.0
15
+ self .values = []
16
+ self .adc = ADC (self .gpio )
17
+ self .response_dict = {"serial_no" : str (self .serial_no ), "index" : str (self .index ), "type" : self .type }
18
+ bin = ujson .dumps (self .response_dict ) + "\n "
19
+ self .response_dict_binary = bytes (bin .encode ('ascii' ))
20
+
21
+ def add_value (self ):
22
+
23
+ if self .current_size >= self .window :
24
+ self .values .pop (0 )
25
+
26
+ x = 0
27
+ for i in range (2000 ):
28
+ temp = self .adc .read_u16 ()
29
+ if temp > x :
30
+ x = temp
31
+ utime .sleep_us (10 )
32
+
33
+ self .values .append (x )
34
+
35
+ if self .current_size < self .window :
36
+ self .current_size = self .current_size + 1.0
37
+
38
+ def get_average_value (self ):
39
+ x = 0.0
40
+ for element in self .values :
41
+ x = x + element
42
+ return x / self .current_size
43
+
44
+ def update_response_dict (self ):
45
+ self .response_dict ["values" ] = str (self .values )
46
+ self .response_dict ["averaged" ] = str (self .get_average_value ())
47
+ bin = ujson .dumps (self .response_dict ) + "\n "
48
+ self .response_dict_binary = bytes (bin .encode ('ascii' ))
49
+ return self .response_dict
Original file line number Diff line number Diff line change 1
1
from machine import ADC , Pin
2
2
from AnalogSensor import AnalogSensor
3
+ from ACAnalogSensor import ACAnalogSensor
3
4
from Pump import Pump
4
5
import ujson
5
6
import sys
@@ -37,6 +38,9 @@ def init():
37
38
if v ['type' ] == 'sensor' :
38
39
analog_sensor = AnalogSensor (v )
39
40
io_components [str (analog_sensor .index )] = analog_sensor
41
+ elif v ['type' ] == 'acsensor' :
42
+ analog_sensor = ACAnalogSensor (v )
43
+ io_components [str (analog_sensor .index )] = analog_sensor
40
44
elif v ['type' ] == 'pump' :
41
45
pump = Pump (v )
42
46
io_components [str (pump .index )] = pump
@@ -83,7 +87,7 @@ def loop():
83
87
print (e )
84
88
finally :
85
89
for sensor in io_components .values ():
86
- if sensor .type == 'sensor' :
90
+ if sensor .type == 'sensor' or sensor . type == 'acsensor' :
87
91
sensor .add_value ()
88
92
sensor .update_response_dict ()
89
93
led .toggle ()
You can’t perform that action at this time.
0 commit comments