/
device.py
163 lines (128 loc) · 4.96 KB
/
device.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
import os
import machine
from machine import Timer
from ubinascii import hexlify
from terkin.radio import NetworkManager
class TerkinDevice:
def __init__(self, name=None, version=None, settings=None):
self.name = name
self.version = version
self.settings = settings
# Keep track of time since boot.
self.chrono = Timer.Chrono()
self.chrono.start()
self.networking = None
self.wdt = None
self.rtc = None
def start_networking(self):
self.tlog('Starting networking')
self.networking = NetworkManager(self.settings)
# Start WiFi
self.networking.start_wifi()
# Wait for network interface to come up.
self.networking.wait_for_nic()
# Inform about networking status.
#self.networking.print_status()
def start_wdt(self):
"""
The WDT is used to restart the system when the application crashes and
ends up into a non recoverable state. After enabling, the application
must "feed" the watchdog periodically to prevent it from expiring and
resetting the system.
"""
# https://docs.pycom.io/firmwareapi/pycom/machine/wdt.html
print('INFO: Starting the watchdog timer (WDT)')
from machine import WDT
# Enable it with a specified timeout.
# TODO: Use values from configuration settings here.
self.wdt = WDT(timeout=5000)
self.wdt.feed()
def feed_wdt(self):
if self.wdt is not None:
self.wdt.feed()
def start_rtc(self):
"""
The RTC is used to keep track of the date and time.
"""
# https://docs.pycom.io/firmwareapi/pycom/machine/rtc.html
# https://medium.com/@chrismisztur/pycom-uasyncio-installation-94931fc71283
import time
from machine import RTC
self.rtc = RTC()
# TODO: Use values from configuration settings here.
self.rtc.ntp_sync("pool.ntp.org", 360)
while not self.rtc.synced():
time.sleep_ms(50)
print(self.rtc.now())
def run_gc(self):
"""
Run a garbage collection.
https://docs.pycom.io/firmwareapi/micropython/gc.html
"""
import gc
gc.collect()
def start_telemetry(self):
self.tlog('Starting telemetry')
telemetry_targets = self.settings.get('telemetry.targets')
print('Telemetry targets:', telemetry_targets)
if len(telemetry_targets) > 1:
print('WARNING: Will only use first telemetry target (FIXME)')
# TODO: Iterate all telemetry targets to submit measurement data to multiple destinations.
telemetry_target = telemetry_targets[0]
telemetry_address = telemetry_target['address']
# Create a "Node API" telemetry client object
from terkin.telemetry import TelemetryNode, TelemetryTopologies
self.telemetry = TelemetryNode(
telemetry_target['endpoint'],
address={
"realm": telemetry_address['realm'],
"network": telemetry_address['network'],
"gateway": telemetry_address['gateway'],
"node": telemetry_address['node'],
},
# TODO: Use topology from configuration settings.
topology=TelemetryTopologies.KotoriWanTopology
)
# Setup telemetry object
self.telemetry.setup()
def enable_serial(self):
# Disable these two lines if you don't want serial access.
uart = machine.UART(0, 115200)
os.dupterm(uart)
def elapsed(self):
return self.chrono.read()
def tlog(self, message):
now = self.elapsed()
print('[{}] {}'.format(now, message))
def print_bootscreen(self):
"""
Print bootscreen.
This contains important details about your device
and the operating system running on it.
"""
# TODO: Maybe move to TerkinDatalogger.
# Program name and version.
title = '{} {}'.format(self.name, self.version.decode())
print('=' * len(title))
print(title)
print('=' * len(title))
# Machine runtime information.
print('CPU freq {} MHz'.format(machine.freq() / 1000000))
print('Device id {}'.format(hexlify(machine.unique_id()).decode()))
print()
# System memory info (in bytes)
machine.info()
print()
# TODO: Python runtime information.
"""
>>> import os; os.uname()
(sysname='FiPy', nodename='FiPy', release='1.20.0.rc7', version='v1.9.4-2833cf5 on 2019-02-08', machine='FiPy with ESP32', lorawan='1.0.2', sigfox='1.0.1')
"""
runtime_info = os.uname()
for key in dir(runtime_info):
if key == '__class__':
continue
value = getattr(runtime_info, key)
print('{:8}: {}'.format(key, value))
print()
# TODO: Program authors, contributors and credits.