-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
195 lines (147 loc) · 4.36 KB
/
main.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
"""
main.py
Copyright (C) 2020 Tomas Hlavacek ([email protected])
Tests / examples for sim driver
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <http://www.gnu.org/licenses/>.
"""
import usys
import uasyncio
import machine
import uselect
import sim
# Disable PIN:
# AT+CPIN="1234"
# AT+CLCK="SC",0,"1234"
DEBUG = True
CONFIG = {
'MQTT_APN': 'internet',
'MQTT_CLIENTID': 'clientid',
'MQTT_BROKER': 'mqtt.example.com',
'MQTT_USER': 'username',
'MQTT_PASS': 'xyzabcdefgh',
'MODEM_POWER_PIN': 4,
'MODEM_RESET_PIN': 5,
'MODEM_RX_PIN': 26,
'MODEM_TX_PIN': 27,
'NTP_SERVER': 'ntp.nic.cz',
}
# Utils for debugging
def d(msg):
if DEBUG:
print(msg)
lastasc2 = ''
async def readline(prompt=""):
global lastasc2
cin = uasyncio.StreamReader(usys.stdin)
ss = uio.StringIO()
ss.seek(0)
ss_cur = 0
usys.stdout.write("%s" % prompt)
while True:
ch = await cin.read(1)
asc2 = ord(ch)
if 31 < asc2 < 127: # ASCII printable characters
ss.seek(ss_cur)
ss.write(ch)
ss_cur += 1
usys.stdout.write(ch)
#elif asc2 == 127: # DEL
elif asc2 == 8: # BS
ss_cur -= 1 if ss_cur > 0 else 0
usys.stdout.write(ch)
elif asc2 == 13 or asc2 == 10: # CR|LF
if lastasc2 == 13 or lastasc2 == 10: # avoid duplicate LF from mpfshell
continue
else:
lastasc2 = asc2
usys.stdout.write(b'\n')
break
lastasc2 = asc2
ss.seek(0)
return ss.read(ss_cur)
async def shell(uplink, data):
global DEBUG
while True:
l = await readline("# ")
l = l.strip()
try:
if l == 'help':
print("Help - Available commands:")
print(" help")
print(" exit")
print(" status")
print(" simd | simdebug")
print(" debug")
print(" undebug")
print(" uplinkstop")
print(" uplinkstart")
print(" getsms")
print(" delsms <smsid>")
print(" sendsms <telnumber> <message>")
print(" reset")
elif l == 'exit':
usys.exit(0)
elif l == 'status':
try:
print("Uplink: %s" % str(await uplink.get_status()))
except Exception as e:
print("Uplink failed to retreive status!")
usys.print_exception(e)
print("Data:")
print(str(data))
elif l == 'data':
print("Data:")
print(str(data))
elif l == 'simd' or l == 'simdebug':
try:
sim = SIM(CONFIG['MODEM_POWER_PIN'], CONFIG['MODEM_RESET_PIN'], CONFIG['MODEM_RX_PIN'], CONFIG['MODEM_TX_PIN'])
await sim.init()
print("GSM running, enter 'stop' to exit GSM shell")
await sim.debug_shell()
except:
raise
finally:
await sim.deinit()
elif l == 'debug':
DEBUG = True
elif l == 'undebug':
DEBUG = False
elif l == 'uplinkstop':
print(await uplink.stop())
elif l == 'uplinkstart':
print(uplink.start(data))
elif l.startswith('getsms'):
print(await uplink.sim.get_sms())
elif l.startswith('delsms'):
_,smsid = l.split(' ', 1)
print(await uplink.sim.del_sms(int(smsid)))
elif l.startswith('sendsms'):
_,tel,msg = l.split(' ', 2)
print(await uplink.sim.send_sms(tel, msg))
elif l == 'reset':
machine.reset()
else:
if l != "":
print("unknown command")
except Exception as e:
usys.print_exception(e)
async def main():
print("Press ctrl-c in next 10 seconds to stop the main.py script")
await uasyncio.sleep(10)
print("Starting up beetle")
uplink = sim.MQTTUplink(CONFIG)
data = {}
tdog = uasyncio.create_task(dog(uplink, data))
uplink.start(data)
tshell = uasyncio.create_task(shell(uplink, data))
print("All tasks running")
await uasyncio.gather(uplink.outt, )
uasyncio.run(main())