質問編集履歴

1

個人情報を消しました.

2021/06/18 06:57

投稿

raspypy
raspypy

スコア247

test CHANGED
File without changes
test CHANGED
@@ -32,252 +32,10 @@
32
32
 
33
33
  ##プログラム
34
34
 
35
- ```python3.6
35
+ import ambient
36
36
 
37
- import lora
38
-
39
- import ast
40
-
41
- import time
42
-
43
- import struct
44
-
45
- import sys
46
-
47
- import ambient
37
+ am1 = ambient.Ambient(channelId1, writeKey1)
48
38
 
49
39
 
50
40
 
51
- # (bw, sf, timeout)
52
-
53
- mode = [
54
-
55
- (3, 12, 5), (3, 11, 5), (3, 10, 4), (3, 9, 3), (3, 8, 2), (3, 7, 2),
56
-
57
- (4, 12, 5), (4, 11, 4), (4, 10, 3), (4, 9, 3), (4, 8, 2), (4, 7, 2),
58
-
59
- (5, 12, 4), (5, 11, 3), (5, 10, 2), (5, 9, 2), (5, 8, 2), (5, 7, 2),
60
-
61
- (6, 12, 3), (6, 11, 3), (6, 10, 2), (6, 9, 2), (6, 8, 2), (6, 7, 2)
62
-
63
- ]
64
-
65
-
66
-
67
- channelId1 = 37537
68
-
69
- writeKey1 = '17eb678e47248e04'
70
-
71
- channelId2 = 37538
72
-
73
- writeKey2 = 'c1ebd40ef25434f2'
74
-
75
- channelId3 = 37539
76
-
77
- writeKey3 = '2e4b2507527c6e1f'
78
-
79
- channelId4 = 37540
80
-
81
- writeKey4 = '662fed41f01a2148'
82
-
83
-
84
-
85
- am1 = ambient.Ambient(channelId1, writeKey1)
86
-
87
- am2 = ambient.Ambient(channelId2, writeKey2)
88
-
89
- am3 = ambient.Ambient(channelId3, writeKey3)
90
-
91
- am4 = ambient.Ambient(channelId4, writeKey4)
92
-
93
-
94
-
95
- lr = lora.LoRa()
96
-
97
-
98
-
99
- def printable(l):
100
-
101
- x = struct.unpack(str(len(l)) + 'b', l)
102
-
103
- y = ''
104
-
105
- for i in range(len(x)):
106
-
107
- if x[i] >= 0:
108
-
109
- y = y + chr(x[i])
110
-
111
- return y
112
-
113
-
114
-
115
- def sendcmd(cmd):
116
-
117
- # print(cmd)
118
-
119
- lr.write(cmd)
120
-
121
- t = time.time()
122
-
123
- while (True):
124
-
125
- if (time.time() - t) > 5:
126
-
127
- print('panic: %s' % cmd)
128
-
129
- exit()
130
-
131
- line = lr.readline()
132
-
133
- if 'OK' in printable(line):
134
-
135
- # print(line)
136
-
137
- return True
138
-
139
- elif 'NG' in printable(line):
140
-
141
- # print(line)
142
-
143
- return False
144
-
145
-
146
-
147
- def setMode(bw, sf):
148
-
149
- lr.write('config\r\n')
150
-
151
- lr.s.flush()
152
-
153
- time.sleep(0.2)
154
-
155
- lr.reset()
156
-
157
- time.sleep(1.5)
158
-
159
-
160
-
161
- line = lr.readline()
162
-
163
- while not ('Mode' in printable(line)):
164
-
165
- line = lr.readline()
166
-
167
- if len(line) > 0:
168
-
169
- print(line)
170
-
171
-
172
-
173
- sendcmd('2\r\n')
174
-
175
- sendcmd('bw %d\r\n' % bw)
176
-
177
- sendcmd('sf %d\r\n' % sf)
178
-
179
- sendcmd('q 2\r\n')
180
-
181
- sendcmd('w\r\n')
182
-
183
-
184
-
185
- lr.reset()
186
-
187
- print('LoRa module set to new mode')
188
-
189
- time.sleep(1)
190
-
191
- sys.stdout.flush()
192
-
193
-
194
-
195
- while (True):
196
-
197
- rssi = [None] * len(mode)
198
-
199
- latlng = ()
200
-
201
- for i in range(len(mode)):
202
-
203
- print('setMode(bw: %d, sf: %d)' % (mode[i][0], mode[i][1]))
204
-
205
- setMode(mode[i][0], mode[i][1])
206
-
207
-
208
-
209
- t = None if i == 0 else mode[i][2]
210
-
211
- timeout = False
212
-
213
- start = time.time()
214
-
215
- while (True):
216
-
217
- while (True):
218
-
219
- line = lr.readline(t)
220
-
221
- # print(line)
222
-
223
- # sys.stdout.flush()
224
-
225
- if len(line) == 0: # TIMEOUT
226
-
227
- timeout = True
228
-
229
- break
230
-
231
- if len(line) >= 14: # 'rssi(4bytes),pan id(4bytes),src id(4bytes),\r\n'で14バイト
232
-
233
- break
234
-
235
- if timeout == True:
236
-
237
- rssi[i] = None
238
-
239
- print('TIMEOUT')
240
-
241
- break;
242
-
243
- data = lr.parse(line) # 'rssi(4bytes),pan id(4bytes),src id(4bytes),laglng=(12バイト,12バイト)\r\n', ペイロード34バイト
244
-
245
- print(data)
246
-
247
- if 'loc=' in data[3]:
248
-
249
- loc = ast.literal_eval(data[3].split('=')[1])
250
-
251
- rssi[i] = data[0]
252
-
253
- latlng = loc
254
-
255
- s = mode[i][2] - (time.time() - start)
256
-
257
- # print('sleep: ' + str(s))
258
-
259
- if i != 0 and s > 0:
260
-
261
- time.sleep(s)
262
-
263
- break
264
-
265
-
266
-
267
- print(rssi)
268
-
269
- print(latlng)
270
-
271
- sys.stdout.flush()
272
-
273
-
274
-
275
- am1.send({'d1': rssi[5], 'd2': rssi[4], 'd3': rssi[3], 'd4': rssi[2], 'd5': rssi[1], 'd6': rssi[0], 'lat': latlng[0], 'lng': latlng[1]})
276
-
277
- am2.send({'d1': rssi[11], 'd2': rssi[10], 'd3': rssi[9], 'd4': rssi[8], 'd5': rssi[7], 'd6': rssi[6], 'lat': latlng[0], 'lng': latlng[1]})
278
-
279
- am3.send({'d1': rssi[17], 'd2': rssi[16], 'd3': rssi[15], 'd4': rssi[14], 'd5': rssi[13], 'd6': rssi[12], 'lat': latlng[0], 'lng': latlng[1]})
280
-
281
- am4.send({'d1': rssi[23], 'd2': rssi[22], 'd3': rssi[21], 'd4': rssi[20], 'd5': rssi[19], 'd6': rssi[18], 'lat': latlng[0], 'lng': latlng[1]})
282
-
283
41
  ```