-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
736 lines (610 loc) · 15.1 KB
/
main.cpp
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/**
* @file main.cpp
*
* @brief Lora Node SDK Sample
*
* example show how to set/save config and read/send sensor data via LoRa periodically;
*
* @author AdvanWISE
*/
#include "mbed.h"
#include "node_api.h"
#define NODE_AUTOGEN_APPKEY
#define NODE_SENSOR_CONFIGURATION_LOAD 1///<Enable: Configuration load from flash, disable: set by user, default enable
#define NODE_SENSOR_TEMP_HUM_ENABLE 1 ///< Enable or disable TEMP/HUM sensor report, default disable
#define NODE_SENSOR_CO2_VOC_ENABLE 0 ///< Enable or disable CO2/VOC sensor report, default disable
#define NODE_GPIO_ENABLE 1 ///< Enable or disable GPIO report, default enable
#define NODE_DEBUG(x,args...) node_printf_to_serial(x,##args)
#define NODE_DEEP_SLEEP_MODE_SUPPORT 0 ///< Flag to Enable/Disable deep sleep mode
#define NODE_ACTIVE_PERIOD_IN_SEC 10 ///< Period time to read/send sensor data >= 3sec
#define NODE_RXWINDOW_PERIOD_IN_SEC 3 ///< Rx windown time
#define NODE_ACTIVE_TX_PORT 1 ///< Lora Port to send data
Serial debug_serial(PA_9, PA_10); ///< Debug serial port
#if NODE_GPIO_ENABLE
///< Control downlink GPIO0
static DigitalOut led(PA_8);
static unsigned int GPIO0;
#endif
typedef enum
{
NODE_STATE_INIT, ///< Node init state
NODE_STATE_LOWPOWER = 1, ///< Node low power state
NODE_STATE_ACTIVE, ///< Node active state
NODE_STATE_TX, ///< Node tx state
NODE_STATE_RX, ///<Node rx state
NODE_STATE_RX_DONE, ///< Node rx done state
}node_state_t;
struct node_api_ev_rx_done node_rx_done_data;
volatile node_state_t node_state = NODE_STATE_INIT;
static char node_class=1;
static char node_op_mode=1;
static char node_sps_state=0;
#if NODE_SENSOR_TEMP_HUM_ENABLE
static unsigned int node_sensor_temp_hum=0; ///<Temperature and humidity sensor global
#endif
#if NODE_SENSOR_CO2_VOC_ENABLE
static unsigned int node_sensor_voc_co2=0; ///<Voc and CO2 sensor global
#endif
I2C i2c(PC_1, PC_0); ///<i2C define
/** @brief print message via serial
*
* @param format message to print
* @returns 0 on success
*/
int node_printf_to_serial(const char * format, ...)
{
unsigned int i;
va_list ap;
char buf[512 + 1];
memset(buf, 0, 512+1);
va_start(ap, format);
vsnprintf(buf, sizeof(buf), (char *)format, ap);
va_end(ap);
for(i=0; i < strlen(buf); i++)
{
debug_serial.putc(buf[i]);
}
return 0;
}
#if NODE_SENSOR_CO2_VOC_ENABLE
/** @brief TVOC and CO2 sensor read
*
*/
unsigned int iaq_core_sensor(void) {
static unsigned int vc = 0;
char data_read[9];
i2c.read(0xB5, data_read, 9, 1);
if(data_read[2]==0x0||data_read[2]==0x10)
{
//NODE_DEBUG(" IAQ status: %x \n\r", data_read[2]);
//NODE_DEBUG(" CO2: %d ppm \n\r", (data_read[0] << 8 | data_read[1]));
//NODE_DEBUG(" TVOC: %d ppb \n\r", (data_read[7] << 8 | data_read[8]));
vc = (data_read[0]<<24|data_read[1]<<16|data_read[7]<<8|data_read[8]);
}
return vc;
}
/** @brief TVOC and CO2 sensor thread
*
*/
static void node_sensor_voc_co2_thread(void const *args)
{
char data_write[1];
data_write[0]=0x06;//0x2;
i2c.write(0xe0, data_write, 1, 0); // i2c expander enable channel_1 and ch2,no stop
while(1)
{
node_sensor_voc_co2=( unsigned int)iaq_core_sensor();
Thread::wait(2000);//need more than 2 sec to read
}
}
#endif
#if NODE_SENSOR_TEMP_HUM_ENABLE
/** @brief Temperature and humidity sensor read
*
*/
static unsigned int hdc1510_sensor(void)
{
char data_write[3];
char data_read[4];
#define HDC1510_REG_TEMP 0x0
#define HDC1510_ADDR 0x80
data_write[0]=HDC1510_REG_TEMP;
i2c.write(HDC1510_ADDR, data_write, 1, 1);
Thread::wait(50);
i2c.read(HDC1510_ADDR, data_read, 4, 0);
float tempval = (float)((data_read[0] << 8 | data_read[1]) * 165.0 / 65536.0 - 40.0);
/*Temperature*/
int ss = tempval*100;
unsigned int yy=0;
//printf("Temperature: %.2f C\r\n",tempval );
/*Humidity*/
float hempval = (float)((data_read[2] << 8 | data_read[3]) * 100.0 / 65536.0);
yy=hempval*100;
// printf("Humidity: %.2f %\r\n",hempval);
return (yy<<16)|ss;
}
/** @brief Temperature and humidity sensor thread
*
*/
static void node_sensor_temp_hum_thread(void const *args)
{
int cnt=0;
while(1)
{
cnt++;
Thread::wait(10);
if(cnt==100)
{
cnt=0;
node_sensor_temp_hum=(unsigned int )hdc1510_sensor();
}
}
}
#endif
/** @brief node tx procedure done
*
*/
int node_tx_done_cb(void)
{
node_state=NODE_STATE_LOWPOWER;
return 0;
}
/** @brief node got rx data
*
*/
int node_rx_done_cb(struct node_api_ev_rx_done *rx_done_data)
{
memset(&node_rx_done_data,0,sizeof(struct node_api_ev_rx_done));
memcpy(&node_rx_done_data,rx_done_data,sizeof(struct node_api_ev_rx_done));
node_state=NODE_STATE_RX_DONE;
return 0;
}
/** @brief node got beacon
*
*/
int node_beacon_cb(unsigned char sps)
{
switch(sps)
{
case 0:
//NODE_DEBUG("Beacon CB: LOT\r\n",nodeApiDeviceSpsEnabled());
if(!nodeApiDeviceSpsEnabled())
{
node_state=NODE_STATE_ACTIVE;
}
break;
case 1:
//NODE_DEBUG("Beacon CB: SPS\r\n");
node_state=NODE_STATE_ACTIVE;
break;
case 2:
//NODE_DEBUG("Beacon CB: LOT(SPS not supported)\r\n");
if(!nodeApiDeviceSpsEnabled())
{
node_state=NODE_STATE_ACTIVE;
}
break;
}
node_sps_state=sps;
return 0;
}
/** @brief An example to show version
*
*/
void node_show_version()
{
char buf_out[256];
unsigned short ret=NODE_API_OK;
memset(buf_out, 0, 256);
ret=nodeApiGetVersion(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("Version=%s\r\n", buf_out);
}
}
/** @brief An example to set node config
*
*/
void node_set_config()
{
char deveui[32]={};
char devaddr[16]={};
char appkey[33]={};
if(nodeApiGetFuseDevEui(deveui,16)!=NODE_API_OK)
{
NODE_DEBUG("Get fuse DevEui failed\r\n");
return;
}
strcpy(devaddr,&deveui[8]);
nodeApiSetDevAddr(devaddr);
sprintf(appkey,"%s%s",deveui,deveui);
nodeApiSetAppKey(appkey);
/*User configuration*/
//nodeApiSetAppEui("00000000000000ab");
//nodeApiSetAppKey("00000000000000000000000000000011");
//nodeApiSetNwkSKey("00000000000000000000000000000011");
//nodeApiSetAppSKey("00000000000000000000000000000011");
//nodeApiSetDevActMode("2");
//nodeApiSetDevOpMode("1");
//nodeApiSetDevClass("3");
//nodeApiSetDevAdvwiseDataRate("4");
//nodeApiSetDevAdvwiseFreq("923300000");
//nodeApiSetDevAdvwiseTxPwr("20");
}
/** @brief An example to get node config
*
*/
void node_get_config()
{
char buf_out[256];
unsigned short ret=NODE_API_OK;
memset(buf_out, 0, 256);
ret=nodeApiGetFuseDevEui(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevEui=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetAppEui(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("AppEui=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetAppKey(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("AppKey=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetDevAddr(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevAddr=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetNwkSKey(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("NwkSKey=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetAppSKey(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG( "AppSKey=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetDevOpMode(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevOpMode=%s\r\n", buf_out);
node_op_mode=atoi(buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetDevActMode(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevActMode=%s\r\n", buf_out);
}
memset(buf_out, 0, 256);
ret=nodeApiGetDevClass(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevClass=%s\r\n", buf_out);
node_class=atoi(buf_out);
}
if(node_op_mode==1||node_op_mode==2)
{
memset(buf_out, 0, 256);
ret=nodeApiGetDevAdvwiseDataRate(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevAdvwiseDataRate=%s\r\n", buf_out);
}
if(node_op_mode==1)
{
memset(buf_out, 0, 256);
ret=nodeApiGetDevAdvwiseFreq(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevAdvwiseFreq=%sHz\r\n", buf_out);
}
}
}
memset(buf_out, 0, 256);
ret=nodeApiGetDevAdvwiseTxPwr(buf_out, 256);
if(ret==NODE_API_OK)
{
NODE_DEBUG("DevAdvwiseTxPwr=%sdBm\r\n", buf_out);
}
}
/** @brief Read sensor data
*
* A simple sample to generate sensor data, user should implement read sensor data
* @param data sensor_data
* @returns data_length
*/
unsigned char node_get_sensor_data (char *data)
{
unsigned char len=0;
unsigned char sensor_data[32];
memset(sensor_data,0x0,sizeof(sensor_data));
#if NODE_SENSOR_TEMP_HUM_ENABLE
sensor_data[len+2]=0x1;
len++; // temperature
sensor_data[len+2]=0x3;
len++; // len:3 bytes
sensor_data[len+2]=0x0;
len++; //0 is positive, 1 is negative
sensor_data[len+2]=(node_sensor_temp_hum>>8)&0xff;
len++;
sensor_data[len+2]=node_sensor_temp_hum&0xff;
len++;
sensor_data[len+2]=0x2;
len++; // humidity
sensor_data[len+2]=0x2;
len++; // len:2 bytes
sensor_data[len+2]=(node_sensor_temp_hum>>24)&0xff;
len++;
sensor_data[len+2]=(node_sensor_temp_hum>>16)&0xff;
len++;
#endif
#if NODE_SENSOR_CO2_VOC_ENABLE
sensor_data[len+2]=0x3;
len++; // CO2
sensor_data[len+2]=0x2;
len++; // len:2 bytes
sensor_data[len+2]=(node_sensor_voc_co2>>24)&0xff;
len++;
sensor_data[len+2]=(node_sensor_voc_co2>>16)&0xff;
len++;
sensor_data[len+2]=0x4;
len++; // TVOC
sensor_data[len+2]=0x2;
len++; // len:2 bytes
sensor_data[len+2]=(node_sensor_voc_co2>>8)&0xff;
len++;
sensor_data[len+2]=node_sensor_voc_co2&0xff;
len++;
#endif
#if NODE_GPIO_ENABLE
sensor_data[len+2]=0x5;
len++; // GPIO
sensor_data[len+2]=0x1;
len++; // len:1 bytes
sensor_data[len+2]=GPIO0;
len++;
#endif
#if ((!NODE_SENSOR_TEMP_HUM_ENABLE)&&(!NODE_SENSOR_CO2_VOC_ENABLE)&&(!NODE_GPIO_ENABLE))
return 0;
#else
//header
sensor_data[0]=len;
sensor_data[1]=0xc; //publish
memcpy(data, sensor_data,len+2);
return len+2;
#endif
}
/** @brief An loop to read and send sensor data via LoRa periodically
*
*/
void node_state_loop()
{
static unsigned char join_state=0;/*0:init state, 1:not joined, 2: joined*/
nodeApiSetTxDoneCb(node_tx_done_cb);
nodeApiSetRxDoneCb(node_rx_done_cb);
node_state=NODE_STATE_LOWPOWER;
if(node_op_mode==4)
{
NODE_DEBUG("WISE link 2.0 \r\n");
nodeApiSetBeaconCb(node_beacon_cb);
}
while(1)
{
if(nodeApiJoinState()==0)
{
if(join_state==2)
NODE_DEBUG("LoRa is not joined.\r\n");
#if NODE_DEEP_SLEEP_MODE_SUPPORT
nodeApiSetDevSleepRTCWakeup(1);
#else
Thread::wait(1000);
#endif
join_state=1;
continue;
}
else
{
if(join_state<=1)
{
node_class=nodeApiDeviceClass();
NODE_DEBUG("LoRa Joined.\r\n");
node_state=NODE_STATE_LOWPOWER;
}
join_state=2;
}
switch(node_state)
{
case NODE_STATE_LOWPOWER:
{
if(node_class==3||node_op_mode==4)
{
static unsigned int count=0;
Thread::wait(10);
if(node_state!=NODE_STATE_RX_DONE)
{
if(count%(NODE_ACTIVE_PERIOD_IN_SEC*100)==0)
if(node_op_mode!=4)
node_state=NODE_STATE_ACTIVE;
}
count++;
}
else
{
Thread::wait(NODE_RXWINDOW_PERIOD_IN_SEC*1000);
/*Receive RX while sleep*/
if(node_state==NODE_STATE_RX_DONE)
continue;
else
{
#if NODE_DEEP_SLEEP_MODE_SUPPORT
nodeApiSetDevSleepRTCWakeup(NODE_ACTIVE_PERIOD_IN_SEC-NODE_RXWINDOW_PERIOD_IN_SEC);
#else
Thread::wait((NODE_ACTIVE_PERIOD_IN_SEC-NODE_RXWINDOW_PERIOD_IN_SEC)*1000);
#endif
node_state=NODE_STATE_ACTIVE;
}
}
}
break;
case NODE_STATE_ACTIVE:
{
int i=0,ret=0;
unsigned char frame_len=0;
char frame[64]={};
frame_len=node_get_sensor_data(frame);
if(frame_len==0)
{
node_state=NODE_STATE_LOWPOWER;
break;
}
if(node_sps_state==1)
ret=nodeApiSendDataHighPri(NODE_ACTIVE_TX_PORT, frame, frame_len);
else
ret=nodeApiSendData(NODE_ACTIVE_TX_PORT, frame, frame_len);
if(ret==0)
{
NODE_DEBUG("TX: ");
for(i=0;i<frame_len;i++)
{
NODE_DEBUG("%02X ",frame[i]);
}
NODE_DEBUG("\n\r");
node_state=NODE_STATE_TX;
}
else
{
NODE_DEBUG("TX: Forbidden!\n\r ");
node_state=NODE_STATE_LOWPOWER;
}
}
break;
case NODE_STATE_TX:
break;
case NODE_STATE_RX:
break;
case NODE_STATE_RX_DONE:
{
if(node_rx_done_data.data_len!=0)
{
int i=0;
int j=0;
char print_buf[512];
memset(print_buf, 0, sizeof(print_buf));
NODE_DEBUG("RX: ");
for(i=0;i<node_rx_done_data.data_len;i++)
{
NODE_DEBUG("%02X ", node_rx_done_data.data[i]);
}
NODE_DEBUG("(Length: %d, Port%d)", node_rx_done_data.data_len,node_rx_done_data.data_port);
NODE_DEBUG("\n\r");
//
// Downlink data handling
// Data port of downlink is the same as uplinlk Tag ID in TLV format
//
#if NODE_GPIO_ENABLE
if(node_rx_done_data.data_port==5 && node_rx_done_data.data_len==1)
{
if (node_rx_done_data.data[0] == '1')
{
led=1;
GPIO0=1;
}
else
{
led=0;
GPIO0=0;
}
}
#endif // NODE_GPIO_ENABLE
}
node_state=NODE_STATE_LOWPOWER;
break;
}
default:
break;
}
}
}
DigitalOut testled2(PB_10);
DigitalOut powerLed(PA_8);
DigitalOut statusLed(PC_8);
/** @brief Main function
*/
int main ()
{
/*Create sensor thread*/
#if NODE_SENSOR_TEMP_HUM_ENABLE
Thread *p_node_sensor_temp_hum_thread;
#endif
#if NODE_SENSOR_CO2_VOC_ENABLE
Thread *p_node_sensor_co2_thread;
#endif
/* Init carrier board, must be first step */
nodeApiInitCarrierBoard();
debug_serial.baud(115200);
nodeApiInit(&debug_serial, &debug_serial);
#if NODE_SENSOR_TEMP_HUM_ENABLE
p_node_sensor_temp_hum_thread=new Thread(node_sensor_temp_hum_thread);
#endif
#if NODE_SENSOR_CO2_VOC_ENABLE
p_node_sensor_co2_thread=new Thread(node_sensor_voc_co2_thread);
#endif
node_show_version();
#if 0
/*
* Init configuration at beginning
*/
nodeApiLoadCfg();
node_set_config();
#ifdef NODE_AUTOGEN_APPKEY
char deveui[17]={};
if(nodeApiGetFuseDevEui(deveui,16)==NODE_API_OK)
{
char appkey[33]={};
sprintf(appkey,"%s%s",deveui,deveui);
nodeApiSetAppKey(appkey);
}
#endif
/* Apply to module */
nodeApiApplyCfg();
/* Start Lora */
nodeApiStartLora();
node_get_config();
Thread::wait(1000);
#if (!NODE_SENSOR_TEMP_HUM_ENABLE)
while(1)
{
Thread::wait(1000);
}
#else
/*
* Node state loop
*/
node_state_loop();
#endif
/*Never reach here*/
return 0;
#else
// main() runs in its own thread in the OS
int a=0;
powerLed=1;
while (true) {
testled2 = !testled2;
statusLed = !statusLed;
debug_serial.printf("Wise 1510 - I/F board test - %d\r\n",a++);
wait(1);
}
#endif
}