This repository has been archived by the owner on Dec 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
demo.ble.lora.rm1xx.sb
681 lines (597 loc) · 25.3 KB
/
demo.ble.lora.rm1xx.sb
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//******************************************************************************
// Copyright (c) 2016-2017, Laird
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
//
// SPDX-License-Identifier:ISC
//
//******************************************************************************
//
// This application demos using BLE to read data from a sensor and transmit the
// data using LoRa. Before running, a BL600/BL652/BT900 will need to have the
// "temp.server.notify.bl600" application loaded and running or a TI Sensortag
// CC2650 needs to be turned on
//
// Then the ABP LoRa keys need to be loaded to the RM1xx module as described in
// the "LoRaWAN Keys and IDs Overview" document on the Laird RM1xx website,
// https://www.lairdtech.com/products/rm1xx-lora-modules and the module
// restarted before running the application.
//
// Note that this application requires ABP and would require some changes to
// utilise OTAA.
//
// Once the application starts, it will scan for the BLE sensor, connect to it
// and join the LoRa network, then every 60 seconds the last received reading
// will be send to the LoRa network. Pressing button 2 will exit the application
//
//******************************************************************************
//******************************************************************************
// Definitions
//******************************************************************************
#include "RM1xx-defs.h"
#include "lib/ble.sblib"
//Set this to 0 to disable all debugging messages
#define ENABLE_DEBUG_PRINTS 1
//Number of connections
#define NUM_OF_CONNS 1
//Status bits
#define BLE_CONNECTED 1
#define LORA_CONNECTED 2
//Types of devices
#define TI_SENSORTAG_SENSOR 1
#define LAIRD_TEMP_SENSOR 2
//Base UUID of optical service
#define BASE_UUID "\F0\00\AA\70\04\51\40\00\B0\00\00\00\00\00\00\00"
//Base UUID of Laird service
#define BASE_UUID_LAIRD "\56\9a\00\00\b8\7f\49\0c\92\cb\11\ba\5e\a5\16\7c"
//Sibling UUID of optical read characteristic
#define OPT_READ_SIBLING_UUID 0xAA71
//Sibling UUID of optical configuration characteristic
#define OPT_CONFIG_SIBLING_UUID 0xAA72
//Sibling UUID of temperature characteristic
#define LAIRD_TEMP_SIBLING_UUID 0x2A6E
//Notification UUID
#define NOTIFICATION_UUID 0x2902
//Time between sensor updates to be sent via LoRa (in ms)
#define LORA_SEND_DATA_INTERVAL 60000
//******************************************************************************
// Register Error Handler as early as possible
//******************************************************************************
sub HandlerOnErr()
print "\n OnErr - ";GetLastError();"\n"
endsub
onerror next HandlerOnErr
//******************************************************************************
// Debugging resource as early as possible
//******************************************************************************
sub AssertRC(byval rc as integer, byval tag as integer)
if rc!=0 then
print "Failed with ";integer.h' rc;" at tag ";tag;"\n"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgMsgVal(byval msg$ as string, byval vl as integer)
if (ENABLE_DEBUG_PRINTS!=0) then
print msg$;" ";vl;"\n"
endif
endsub
//==============================================================================
//==============================================================================
sub DbgMsg(byval msg$ as string)
if (ENABLE_DEBUG_PRINTS!=0) then
print msg$;"\n"
endif
endsub
//******************************************************************************
// Global Variable Declarations
//******************************************************************************
dim rc
dim looprc
dim sensorType //Type of device that the RM1xx is connected to
dim PrevAds$[6] //Previous adverts (size of 6 adverts)
dim PrevAdCount //Number of previous adverts
dim hc[NUM_OF_CONNS+1] //Contains connection handles
dim conns //Number of connections
dim hcVsp //Handle of connection to use for VSP service
dim LastReading$ //Most recent reading from the Luminosity/Temperature sensor
dim status //Current status of BLE and LoRa links
DIM OptHndl //Handle of the optical value characteristic
DIM OptHndlDesc //Handle of the optical value characteristic descriptor
DIM OptConfigHndl //Handle of the optical value configuration chracteristic
DIM TmpHndl //Handle of the temperature characteristic
DIM CAction //Current action of state machine
DIM currentsize
DIM maxsize
//******************************************************************************
// Initialise Global Variable
//******************************************************************************
PrevAdCount = 0
//******************************************************************************
// Function and Subroutine definitions
//******************************************************************************
//==============================================================================
//Release all connection handles
//==============================================================================
sub InitConnHandles()
dim z
for z=0 to (NUM_OF_CONNS)
hc[z]=-1
next
endsub
//==============================================================================
//==============================================================================
function AcqConnHandle(hConn)
dim z
for z=1 to (NUM_OF_CONNS)
if hc[z] == -1 then
hc[z]=hConn
exitfunc z
endif
next
endfunc 0
//==============================================================================
//==============================================================================
function RelConnHandle(hConn)
dim z
for z=1 to (NUM_OF_CONNS)
if hc[z] == hConn then
hc[z]=-1
exitfunc z
endif
next
endfunc 0
//==============================================================================
//==============================================================================
sub ShowConnParms(nCtx as integer)
dim intrvl,sprvto,slat
rc = BleGetCurConnParms(nCtx,intrvl,sprvto,slat)
AssertRC(rc, 184)
if rc==0 then
DbgMsgVal("Conn Interval",intrvl)
DbgMsgVal("Conn Supervision Timeout",sprvto)
DbgMsgVal("Conn Slave Latency",slat)
DbgMsg("\n")
endif
endsub
//==============================================================================
//==============================================================================
FUNCTION RemoveZeros(Data$) AS STRING
//This function removes leading 0s from the front of a string
dim i, Done, TmpStr$ : i = 0 : Done = 0
WHILE (i < strlen(Data$))
TmpStr$ = MID$(Data$, i, 1)
IF (STRCMP(TmpStr$, "0") != 0) THEN
//Other number found - cut string to this length and mark as finished
TmpStr$ = RIGHT$(Data$, strlen(Data$)-i)
i = strlen(Data$)
Done = 1
ENDIF
i = i+1
ENDWHILE
IF (Done == 0) THEN
//Other number not found in provided string
TmpStr$ = "0"
ENDIF
ENDFUNC TmpStr$
//******************************************************************************
// Handler definitions
//******************************************************************************
//==============================================================================
// This handler is called when data has arrived at the serial port
//==============================================================================
function HndlrUartRxCmd()
endfunc 1
//==============================================================================
// This handler is called when there is an advert timeout
//==============================================================================
function HndlrBlrAdvTimOut()
print "Adverts timeout\n"
endfunc 1
//==============================================================================
// This handler is called when there is an advert report waiting to be read
//==============================================================================
function HndlrAdvRpt()
DIM periphAddr$, advData$, nRssi, ADval$, TmpStr$, TmpStr2$, TmpVal
//Read all cached advert reports
looprc = BleScanGetAdvReport(periphAddr$, advData$, TmpVal, nRssi)
while (looprc == 0)
//Check if this advert was received recently
TmpStr$ = periphAddr$ + advData$
TmpVal = 0
while (TmpVal < PrevAdCount)
if (strcmp(TmpStr$, PrevAds$[TmpVal]) == 0) then
//Already seen this advert, ignore it
TmpVal = 254
endif
TmpVal = TmpVal+1
endwhile
if (TmpVal != 255) then
//Advert not seen before - get device name
rc = BLEGETADBYTAG(advData$, 8, TmpStr$)
if (rc == 0) then
//Name found at index 0x08
PRINT TmpStr$;"\n"
else
//Name not found at index 0x08, check 0x09
rc = BLEGETADBYTAG(advData$, 9, TmpStr$)
if (rc == 0) then
//Name found at index 0x09
PRINT TmpStr$;"\n"
endif
endif
if (strcmp(TmpStr$, "CC2650 SensorTag")==0) then
sensorType = TI_SENSORTAG_SENSOR
//Connect to the SensorTag
print "Found the TI SensorTag. Connecting...\n"
rc = BleConnect(periphAddr$, 5000, 25000, 750000, 5000000)
AssertRC(rc, 272)
else
rc = BLEGETADBYTAG(advData$, 7, TmpStr$)
if (rc == 0) then
//Complete list of 128-bit UUIDs present
dim RevTmpStr$
dim i, l
i = strlen(TmpStr$)-1
l = 0
while (i >= 0)
//Reverse the order of the string
rc = strsetchr(RevTmpStr$, strgetchr(TmpStr$, i), l)
i = i-1
l = l+1
endwhile
//Compare the 128-bit UUID with the desired 128-bit UUID
if (strcmp(RevTmpStr$, BASE_UUID_LAIRD) == 0) then
//Found the target device
sensorType = LAIRD_TEMP_SENSOR
//Connect to the temperature sensor
print "Found the Laird temperature sensor. Connecting...\n"
rc = BleConnect(periphAddr$, 5000, 25000, 750000, 5000000)
AssertRC(rc, 297)
endif
endif
endif
//Output BT addr and RSSI
PRINT strhexize$(periphAddr$);", RSSI: ";nRssi;"\n"
//Go through all tags and print them out
PRINT "Advertising data [hex]:\n"
TmpVal = 0
while (TmpVal <= 0xff)
if (TmpVal == 8) then
//Skip name tags
TmpVal = 10
endif
rc = BLEGETADBYTAG(advData$, TmpVal, TmpStr$)
if (rc == 0) then
//Output tag
TmpStr2$ = ""
SPRINT #TmpStr2$,INTEGER.H'TmpVal
TmpStr2$ = RemoveZeros(TmpStr2$)
PRINT " Length: ";strlen(TmpStr$);", Type: 0x";TmpStr2$;", Value: ";STRHEXIZE$(TmpStr$);"\n"
endif
TmpVal = TmpVal+1
endwhile
//Newline
PRINT "\n"
//Check if the array is too large
if (PrevAdCount > 4) then
//Array too big - clear it
TmpVal = 0
while (TmpVal < 6)
PrevAds$[TmpVal] = ""
TmpVal = TmpVal+1
endwhile
PrevAdCount = 0
endif
//Add this adverts to the recent list
TmpStr$ = periphAddr$ + advData$
PrevAds$[PrevAdCount] = TmpStr$
PrevAdCount = PrevAdCount+1
endif
looprc = BleScanGetAdvReport(periphAddr$, advData$, TmpVal, nRssi)
endwhile
endfunc 1
//==============================================================================
//==============================================================================
function HndlrFindChar(hConn,cProp,hVal,hiUuid)
//Callback for finding a characteristic
dim uHndS$
dim uHndS, uHndC
print "EVFINDCHAR("
print "hConn=";integer.h' hConn;",hIncUuid=";integer.h' hiUuid
print ",hVal=";hVal;",Props=";integer.h' cProp;")\n"
if (CAction == 0) then
//Result for finding first characteristic
if (sensorType == TI_SENSORTAG_SENSOR) then
//TI Sensortag
CAction = 1
OptHndl = hVal
uHndS$ = BASE_UUID
uHndS = BleHandleUuid128(uHndS$)
uHndC = BleHandleUuidSibling(uHnds, OPT_CONFIG_SIBLING_UUID)
rc = BleGattcFindChar(hConn, uHndS, 0, uHndC, 0)
AssertRC(rc, 368)
elseif (sensorType == LAIRD_TEMP_SENSOR) then
//Laird temperature sensor
CAction = 2
TmpHndl = hVal
uHndS$ = BASE_UUID_LAIRD
uHndS = BleHandleUuid128(uHndS$)
uHndC = BleHandleUuidSibling(uHnds, LAIRD_TEMP_SIBLING_UUID)
rc = BLEGATTCFINDDESC(hConn, uHndS, 0, uHndC, 0, BleHandleUuid16(NOTIFICATION_UUID), 0)
AssertRC(rc, 377)
endif
elseif (CAction == 1) then
//Result for finding second characteristic
CAction = 2
OptConfigHndl = hVal
uHndS$ = BASE_UUID
uHndS = BleHandleUuid128(uHndS$)
uHndC = BleHandleUuidSibling(uHnds, OPT_READ_SIBLING_UUID)
rc = BLEGATTCFINDDESC(hConn, uHndS, 0, uHndC, 0, BleHandleUuid16(NOTIFICATION_UUID), 0)
AssertRC(rc, 387)
endif
endfunc 1
//==============================================================================
// This handler is called when there is a LoRa TX Complete event
//==============================================================================
function HndlrLoRaTxComp()
print "LoRa TX Complete Event\n"
endfunc 1
//==============================================================================
// This handler is called when the LoRa Join procedure starts
//==============================================================================
function HndlrLoRaJoining()
print "Attempting to join the LoRa network\n"
endfunc 1
//==============================================================================
// This handler is called when the LoRa Join Process completes successfully
//==============================================================================
function HndlrLoRaJoined()
print "Joined the LoRa network\n"
status = status+LORA_CONNECTED
//Start the timer for sending data
TimerStart(0, LORA_SEND_DATA_INTERVAL, 1)
endfunc 1
//==============================================================================
// This handler is called when there is a BLE message
//==============================================================================
function HndlrBleMsg(BYVAL nMsgId AS INTEGER, BYVAL nCtx AS INTEGER)
dim hz
dim atHandle, at$
dim rc,uHndl,sIdx,cIdx,baseUuid$,uHndS$,uHndS,uHndC
select nMsgId
case BLE_EVBLEMSGID_CONNECT
//Connected to BLE device
hz = AcqConnHandle(nCtx)
conns = conns + 1
print " --- Connect: (";integer.h' nCtx;") handle=";hz;"\n"
if hz == 0 then
//Could not store the handle so drop the connection
rc = BleDisconnect(nCtx)
print " DROPPED"
else
//accepted so display connection parameters
ShowConnParms(nCtx)
//always assume most recent connection for VSP
hcVsp = hc[hz]
endif
//First action
CAction = 0
if (sensorType == TI_SENSORTAG_SENSOR) then
//Search for TI sensortag optical read characteristic
uHndS$ = BASE_UUID
uHndS = BleHandleUuid128(uHndS$)
uHndC = BleHandleUuidSibling(uHnds, OPT_READ_SIBLING_UUID)
else
//Search for Laird temperature sensor characteristic
uHndS$ = BASE_UUID_LAIRD
uHndS = BleHandleUuid128(uHndS$)
uHndC = BleHandleUuidSibling(uHnds, LAIRD_TEMP_SIBLING_UUID)
endif
//Find the characteristic
rc = BleGattcFindChar(nCtx, uHndS, 0, uHndC, 0)
AssertRC(rc, 458)
status = status+BLE_CONNECTED
if status != (LORA_CONNECTED + BLE_CONNECTED) then
//Join the LoRa network using ABP
rc = LORAMACJoin(LORAMAC_JOIN_BY_PERSONALIZATION)
AssertRC(rc, 464)
endif
case BLE_EVBLEMSGID_DISCONNECT
//BLE device disconnected
TimerCancel(0)
hz = RelConnHandle(nCtx)
conns = conns - 1
print " --- Disconnect: (";integer.h' nCtx;") handle=";hz;"\n"
status = status-BLE_CONNECTED
case BLE_EVBLEMSGID_NEW_BOND
DbgMsg(" +++ New Bond")
case BLE_EVBLEMSGID_UPDATED_BOND
DbgMsg(" +++ Updated Bond")
case BLE_EVBLEMSGID_ENCRYPTED
DbgMsg(" +++ Encrypted Connection")
case BLE_EVBLEMSGID_CONN_TO_BONDED_MASTER
DbgMsgVal(" -- Conn to Bonded Master ",nCtx)
case BLE_EVBLEMSGID_AUTH_KEY_REQUEST
DbgMsgVal(" +++ Auth Key Request, type=",nCtx)
case BLE_EVBLEMSGID_CONN_PARMS_UPDATE
ShowConnParms(nCtx)
case BLE_EVBLEMSGID_CONN_PARMS_UPDATE_FAIL
DbgMsg(" ??? Conn Parm Negotiation FAILED")
case else
DbgMsgVal("Unknown Ble Msg", nMsgId)
endselect
endfunc 1
//==============================================================================
//Handler to service button 2 pressed
//==============================================================================
function HndlrBtn2Pr()
//Exit application
print "Button 2 pressed\n"
endfunc 0
//==============================================================================
//==============================================================================
function HndlrFindDesc(hConn, hDesc)
//Result for finding descriptor
print "EVFINDDESC("
print "hConn=";integer.h' hConn;",hDesc=";hDesc;")\n"
if (CAction == 2) then
if (hDesc != 0) then
//Descriptor has been found
CAction = 3
OptHndlDesc = hDesc
dim at$
at$="\01\00"
print"Enable notification.\n"
rc = BleGattcWrite(hc[1],hDesc,at$)
AssertRC(rc, 525)
else
//Descriptor was not found
rc = BleDisconnect(hConn)
print "Required descriptor not found, disconnecting...\n"
endif
endif
endfunc 1
//==============================================================================
//==============================================================================
function HndlrAttrWrite(hConn,aHndl,nSts)
//Result for writing GATT attribute
print "EVATTRWRITE(hConn=";integer.h' hConn;",handle=";aHndl;",status=";nSts;")\n"
if (CAction == 3) then
CAction = 4
if (sensorType == TI_SENSORTAG_SENSOR) then
//Enable the TI sensortag's optical sensor
dim at$ : at$="\01"
rc = BleGattcWrite(hc[1], OptConfigHndl, at$)
AssertRC(rc, 545)
endif
endif
endfunc 1
//==============================================================================
//==============================================================================
function HndlrAttrNtfyCmd()
//Result for GATT characteristic notification
dim hConn,hAttr,att$,dscd
dim lumoVal
print "EVATTRNOTIFY()\n"
do
rc = BleGattcNotifyRead(hConn,hAttr,att$,dscd)
if rc==0 then
print " >BleGattcNotifyRead("
print "hConn=";integer.h' hConn;",handle=";hAttr;",Dumped=";dscd
print ",data=";StrHexize$(att$);")\n"
if (sensorType == TI_SENSORTAG_SENSOR && hAttr == OptHndl) then
//Luminosity sensor notificaion
LastReading$ = StrHexize$(att$)
elseif (sensorType == LAIRD_TEMP_SENSOR && hAttr == TmpHndl) then
//Temperature sensor notification
print "Temp is ";strescape$(att$);"\n"
endif
endif
dowhile rc==0
endfunc 1
//==============================================================================
//==============================================================================
function HndlrConnTO()
//Restart scanning for BLE devices
rc = BleScanStart(0,0)
AssertRC(rc, 581)
endfunc 1
//==============================================================================
//==============================================================================
function HndlrTmr0()
//Timer for sending BLE sensor data over LoRa
if status == (LORA_CONNECTED + BLE_CONNECTED) then
//BLE sensor and LoRa connected
rc = LoramacQueryTxPossible(strlen(LastReading$),currentsize,maxsize)
if rc == 0 then
rc = LORAMACTxData(2,LastReading$, 0)
if rc != 0 then
Print "Failed to send packet.\n"
else
print "Transmitting data packet over LoRa\n"
endif
else
Print "Packet too large\n"
Print "size ";strlen(LastReading$);" max ";currentsize;"\n"
endif
AssertRC(rc, 602)
endif
endfunc 1
//******************************************************************************
// Equivalent to main() in C
//******************************************************************************
//------------------------------------------------------------------------------
// Enable synchronous event handlers
//------------------------------------------------------------------------------
ONEVENT EVUARTRX CALL HndlrUartRxCmd
ONEVENT EVGPIOCHAN0 CALL HndlrBtn2Pr
ONEVENT EVTMR0 CALL HndlrTmr0
//BLE events
ONEVENT EVBLEMSG CALL HndlrBleMsg
ONEVENT EVBLE_CONN_TIMEOUT CALL HndlrConnTO
ONEVENT EVBLE_ADV_TIMEOUT CALL HndlrBlrAdvTimOut
ONEVENT EVBLE_ADV_REPORT CALL HndlrAdvRpt
ONEVENT EVFINDCHAR CALL HndlrFindChar
ONEVENT EVFINDDESC CALL HndlrFindDesc
ONEVENT EVATTRWRITE CALL HndlrAttrWrite
ONEVENT EVATTRNOTIFY CALL HndlrAttrNtfyCmd
//LoRa events
ONEVENT EVLORAMACTXCOMPLETE CALL HndlrLoRaTxComp
ONEVENT EVLORAMACJOINING CALL HndlrLoRaJoining
ONEVENT EVLORAMACJOINED CALL HndlrLoRaJoined
//------------------------------------------------------------------------------
// Initialise Services and then wait for events
//------------------------------------------------------------------------------
sub Initialise()
LastReading$ = ""
InitConnHandles()
status = 0
endsub
//==============================================================================
//==============================================================================
sub Startup()
dim uuid$
dim uHndl
rc = GpioBindEvent(0, 5, 1) //Channel 0, bind to high-to-low on GPIO pin 5
AssertRC(rc, 647)
//Open the GATT client
rc = BleGattcOpen(0, 0)
AssertRC(rc, 651)
//Register base uuids with the underlying stack, otherwise the services with the
//128bit uuid's will be delivered with a uuid handle == FF000000 == UNKNOWN
uuid$ = BASE_UUID
uHndl = BleHandleUuid128(uuid$)
uuid$ = "569a0000b87f490c92cb11ba5ea5167c"
uuid$ = StrDehexize$(uuid$)
uHndl = BleHandleUuid128(uuid$)
uuid$ = "569a2a10b87f490c92cb11ba5ea5167c"
uuid$ = StrDehexize$(uuid$)
uHndl = BleHandleUuid128(uuid$)
uuid$ = "569a2a6eb87f490c92cb11ba5ea5167c"
uuid$ = StrDehexize$(uuid$)
uHndl = BleHandleUuid128(uuid$)
//Start scanning for BLE devices
rc = BleScanStart(0, 0)
AssertRC(rc, 669)
endsub
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
Initialise()
Startup()
//------------------------------------------------------------------------------
// Wait for a synchronous event.
// An application can have multiple <WaitEvent> statements
//------------------------------------------------------------------------------
WaitEvent