/
system.py
210 lines (157 loc) · 6.03 KB
/
system.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
# -*- coding: utf-8 -*-
# (c) 2019 Richard Pobering <richard@hiveeyes.org>
# (c) 2019 Andreas Motl <andreas@hiveeyes.org>
# License: GNU General Public License, Version 3
import time
from machine import ADC, enable_irq, disable_irq
from micropython import const
from terkin import logging
log = logging.getLogger(__name__)
# Todo: Make this configurable.
log.setLevel(logging.DEBUG)
class SystemMemoryFree:
"""
Read free memory in bytes.
"""
def read(self):
import gc
value = gc.mem_free()
return {'system.memfree': value}
class SystemTemperature:
"""
Read the internal temperature of the MCU.
- https://docs.micropython.org/en/latest/esp32/quickref.html#general-board-control
- https://github.com/micropython/micropython-esp32/issues/33
- https://github.com/micropython/micropython/pull/3933
- https://github.com/micropython/micropython-esp32/pull/192
- https://github.com/espressif/esp-idf/issues/146
- https://forum.pycom.io/topic/2208/new-firmware-release-1-10-2-b1/4
"""
def read(self):
import machine
rawvalue = machine.temperature()
# Fahrenheit
# 'system.temperature': 57.77778
#temperature = (rawvalue - 32) / 1.8
# Magic
# 'system.temperature': 41.30435
value = rawvalue * (44/23) + (-5034/23)
reading = {'system.temperature': value}
return reading
class SystemBatteryLevel:
"""
Read the battery level by reading the ADC on a pin connected
to a voltage divider, which is pin 16 on the expansion board.
Implementation
==============
Written by Dominik Kapusta <https://github.com/ayoy>. Thanks!
The MIT License (MIT)
Copyright (c) 2018 Dominik Kapusta
- https://kapusta.cc/2018/02/02/air-quality-monitor-revisited/
- https://github.com/ayoy/upython-aq-monitor/blob/lora/lib/adc.py
Documentation
=============
- https://forum.pycom.io/topic/3776/adc-use-to-measure-battery-level-vin-level
- https://docs.pycom.io/firmwareapi/pycom/machine/adc
- https://docs.pycom.io/tutorials/all/adc
"""
# How many times to sample the ADC for making a reading.
adc_sample_count = const(1000)
def __init__(self):
"""
Initialized ADC unit.
"""
# Sum of resistor values.
self.resistor_sum = None
# Resistor between input pin and ground.
self.resistor_pin = None
# ADC channel used for sampling the raw value.
self.adc = ADC(id=0)
def setup(self, settings):
self.resistor_sum = settings.get('sensors.system.vcc.resistor_sum')
self.resistor_pin = settings.get('sensors.system.vcc.resistor_pin')
def read(self):
"""
Acquire vbatt reading by sampling ADC.
"""
assert type(self.resistor_sum) is int, 'Invalid setting for voltage divider resistor value "resistor_sum"'
assert type(self.resistor_pin) is int, 'Invalid setting for voltage divider resistor value "resistor_pin"'
# Power on ADC.
self.adc.init()
log.debug('Reading battery level with voltage divider {}/{}'.format(self.resistor_sum, self.resistor_pin))
# Sample ADC a few times.
adc_channel = self.adc.channel(attn=ADC.ATTN_6DB, pin='P16')
adc_samples = [0.0] * self.adc_sample_count
adc_mean = 0.0
i = 0
irq_state = disable_irq()
while i < self.adc_sample_count:
sample = adc_channel()
adc_samples[i] = sample
adc_mean += sample
i += 1
enable_irq(irq_state)
adc_mean /= self.adc_sample_count
adc_variance = 0.0
for sample in adc_samples:
adc_variance += (sample - adc_mean) ** 2
adc_variance /= (self.adc_sample_count - 1)
raw_voltage = adc_channel.value_to_voltage(4095)
mean_voltage = adc_channel.value_to_voltage(int(adc_mean))
mean_variance = (adc_variance * 10 ** 6) // (adc_mean ** 2)
# log.debug("ADC readings. count=%u:\n%s" %(self.adc_sample_count, str(adc_samples)))
log.debug("SystemBatteryLevel: Mean of ADC readings (0-4095) = %15.13f" % adc_mean)
log.debug("SystemBatteryLevel: Mean of ADC voltage readings (0-%dmV) = %15.13f" % (raw_voltage, mean_voltage))
log.debug("SystemBatteryLevel: Variance of ADC readings = %15.13f" % adc_variance)
log.debug("SystemBatteryLevel: 10**6*Variance/(Mean**2) of ADC readings = %15.13f" % mean_variance)
voltage_millivolt = (adc_channel.value_to_voltage(int(adc_mean))) * self.resistor_sum / self.resistor_pin
voltage_volt = voltage_millivolt / 1000.0
# Shut down ADC channel.
adc_channel.deinit()
log.debug('Battery level: {}'.format(voltage_volt))
reading = {'system.voltage': voltage_volt}
return reading
def power_off(self):
"""
Shut down ADC.
"""
log.info('Turning off ADC')
self.adc.deinit()
class SystemUptime:
"""
Return system time and uptime in seconds.
https://docs.pycom.io/firmwareapi/micropython/utime.html#utimeticksms
"""
start_time = time.time()
def read(self):
now = time.time()
uptime = time.ticks_ms() / 1000.0
runtime = now - self.start_time
reading = {
'system.time': now,
'system.uptime': uptime,
'system.runtime': runtime,
}
return reading
class SystemHallSensor:
"""
- https://forum.pycom.io/topic/3537/internal-hall-sensor-question/2
- https://github.com/micropython/micropython-esp32/pull/211
- https://www.esp32.com/viewtopic.php?t=481
"""
pass
class SystemMemoryPeeker:
"""
``machine.mem32[]``
- https://github.com/micropython/micropython-esp32/pull/192#issuecomment-334631994
"""
pass
class ADC2:
"""
::
adc = machine.ADC(machine.Pin(35))
adc.atten(machine.ADC.ATTN_11DB)
- https://github.com/micropython/micropython-esp32/issues/33
- https://www.esp32.com/viewtopic.php?t=955
"""
pass