This repository has been archived by the owner on Sep 26, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWaspUtils.cpp
1501 lines (1290 loc) · 29.4 KB
/
WaspUtils.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2017 Libelium Comunicaciones Distribuidas S.L.
* http://www.libelium.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 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 Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Version: 3.1
* Design: David Gascon
* Implementation: Alberto Bielsa, David Cuartielles, Yuri Carmona
*/
#ifndef __WPROGRAM_H__
#include "WaspClasses.h"
#endif
#include <inttypes.h>
/*
* Constructor
*/
WaspUtils::WaspUtils (void)
{
}
/* setLED(led, state) - set the specified LED to the specified state(ON or OFF)
*
* It sets the specified LED to the specified state(ON or OFF)
*/
void WaspUtils::setLED(uint8_t led, uint8_t state)
{
pinMode(led,OUTPUT);
if(state==LED_ON)
{
digitalWrite(led,HIGH);
}
if(state==LED_OFF)
{
digitalWrite(led,LOW);
}
}
/* setExternalLED(led, state) - set external LED to the specified state(ON or OFF)
*
* It sets external LED to the specified state(ON or OFF)
*/
void WaspUtils::setExternalLED(uint8_t state)
{
pinMode(LED1,OUTPUT);
if(state==LED_ON)
{
digitalWrite(LED1,HIGH);
}
if(state==LED_OFF)
{
digitalWrite(LED1,LOW);
}
}
/* getLED(led) - gets the state of the specified LED
*
* It gets the state of the specified LED
*/
uint8_t WaspUtils::getLED(uint8_t led)
{
return digitalRead(led);
}
/* getExternalLED(led) - gets the state of the specified LED
*
* It gets the state of external LED
*/
uint8_t WaspUtils::getExternalLED()
{
return digitalRead(LED1);
}
/* blinkLEDs(time) - blinks LED, with the specified time of blinking
*
* It bliks LED0 and LED1, with the specified time of blinking
*/
void WaspUtils::blinkLEDs(uint16_t time)
{
setLED(LED0,LED_ON);
setLED(LED1,LED_ON);
delay(time);
setLED(LED0,LED_OFF);
setLED(LED1,LED_OFF);
delay(time);
}
/* blinkRedLED(time, num) - blinks the red LED once during 200ms
*/
void WaspUtils::blinkRedLED()
{
blinkRedLED( 200, 1 );
}
/* blinkRedLED(time, num) - blinks the red LED once during 'time' milliseconds
*/
void WaspUtils::blinkRedLED( uint16_t time )
{
blinkRedLED( time, 1 );
}
/* blinkRedLED(time, num) - blinks the red LED for 'num' times, and during
* 'time' milliseconds
*/
void WaspUtils::blinkRedLED( uint16_t time, uint8_t num )
{
for( int i = 0; i < num ; i++ )
{
setLED(LED0,LED_ON);
delay(time);
setLED(LED0,LED_OFF);
delay(time);
}
}
/* blinkGreenLED(time, num) - blinks the green LED once during 200ms
*/
void WaspUtils::blinkGreenLED()
{
blinkGreenLED( 200, 1 );
}
/* blinkGreenLED(time, num) - blinks the green LED once during 'time' milliseconds
*/
void WaspUtils::blinkGreenLED( uint16_t time )
{
blinkGreenLED( time, 1);
}
/* blinkGreenLED(time, num) - blinks the green LED for 'num' times, and during
* 'time' milliseconds
*/
void WaspUtils::blinkGreenLED( uint16_t time, uint8_t num )
{
for( int i = 0; i < num ; i++ )
{
setLED(LED1,LED_ON);
delay(time);
setLED(LED1,LED_OFF);
delay(time);
}
}
/* externalLedBlink(time) - blinks external LED, with the specified time of blinking
*
* It bliks LED1, with the specified time of blinking
*/
void WaspUtils::externalLEDBlink(uint16_t time)
{
setLED(LED1,LED_ON);
delay(time);
setLED(LED1,LED_OFF);
delay(time);
}
/* map(x,in_min,in_max,out_min,out_max) - maps 'x' from the read range to the specified range
*
* It maps 'x' from the read range to the specified range
*
* 'in_min' and 'in_max' are the entry range read from the sensor
* 'out_min' and 'out_max' are the desired output range
*/
long WaspUtils::map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
/* setMux( MUX_LOW, MUX_HIGH) - sets multiplexer on UART_1 to the desired combination
*
* It sets MUX to the desired combination.
*
* --------------
* MUX1_0 = 0 & MUX1_1 = 1 ---> GPS MODULE
* MUX1_0 = 1 & MUX1_1 = 1 ---> GPRS MODULE
* MUX1_0 = 1 & MUX1_1 = 0 ---> AUX1 MODULE
* MUX1_0 = 0 & MUX1_1 = 0 ---> AUX2 MODULE
*/
void WaspUtils::setMux(uint8_t MUX_LOW, uint8_t MUX_HIGH)
{
pinMode(MUX1_PW, OUTPUT);
pinMode(MUX1_0, OUTPUT);
pinMode(MUX1_1, OUTPUT);
digitalWrite(MUX1_PW, HIGH);
digitalWrite(MUX1_0, MUX_LOW);
digitalWrite(MUX1_1, MUX_HIGH);
}
/* setMuxGPS() - sets multiplexer on UART_1 to the desired combination (0,1)
*
* It sets multiplexer on UART_1 to the desired combination (0,1) to enable GPS module
*
* --------------
* MUX1_0 = 0 & MUX1_1 = 1 ---> GPS MODULE
* MUX1_0 = 1 & MUX1_1 = 1 ---> GPRS MODULE
* MUX1_0 = 1 & MUX1_1 = 0 ---> AUX1 MODULE
* MUX1_0 = 0 & MUX1_1 = 0 ---> AUX2 MODULE
*/
void WaspUtils::setMuxGPS()
{
pinMode(MUX1_PW, OUTPUT);
pinMode(MUX1_0, OUTPUT);
pinMode(MUX1_1, OUTPUT);
digitalWrite(MUX1_PW, HIGH);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, HIGH);
}
/* setMuxSocket1() - sets multiplexer on UART_1 to the desired combination (1,1)
*
* It sets multiplexer on UART_1 to the desired combination (1,1) to enable SOCKET1
*
* --------------
* MUX1_0 = 0 & MUX1_1 = 1 ---> GPS MODULE
* MUX1_0 = 1 & MUX1_1 = 1 ---> GPRS MODULE
* MUX1_0 = 1 & MUX1_1 = 0 ---> AUX1 MODULE
* MUX1_0 = 0 & MUX1_1 = 0 ---> AUX2 MODULE
*/
void WaspUtils::setMuxSocket1()
{
// check RTC int pin to disable line in order to communicate
if (digitalRead(RTC_INT_PIN_MON) == HIGH)
{
if (RTC.isON == 0)
{
RTC.ON();
RTC.clearAlarmFlag();
RTC.OFF();
}
else
{
RTC.clearAlarmFlag();
}
}
// disable interruptions when using UART1
detachInterrupt(RXD1_PIN);
detachInterrupt(TXD1_PIN);
pinMode(MUX1_PW, OUTPUT);
pinMode(MUX1_0, OUTPUT);
pinMode(MUX1_1, OUTPUT);
digitalWrite(MUX1_PW, HIGH);
digitalWrite(MUX1_0, HIGH);
digitalWrite(MUX1_1, HIGH);
}
/* setMuxAux1() - sets multiplexer on UART_1 to the desired combination (1,1)
*
* It sets multiplexer on UART_1 to the desired combination (1,1) to enable Aux1 module
*
* --------------
* MUX1_0 = 0 & MUX1_1 = 1 ---> GPS MODULE
* MUX1_0 = 1 & MUX1_1 = 1 ---> GPRS MODULE
* MUX1_0 = 1 & MUX1_1 = 0 ---> AUX1 MODULE
* MUX1_0 = 0 & MUX1_1 = 0 ---> AUX2 MODULE
*/
void WaspUtils::setMuxAux1()
{
pinMode(MUX1_PW, OUTPUT);
pinMode(MUX1_0, OUTPUT);
pinMode(MUX1_1, OUTPUT);
digitalWrite(MUX1_PW, HIGH);
digitalWrite(MUX1_0, HIGH);
digitalWrite(MUX1_1, LOW);
}
/* setMuxAux2() - sets multiplexer on UART_1 to the desired combination (1,1)
*
* It sets multiplexer on UART_1 to the desired combination (1,1) to enable Aux2 module
*
* --------------
* MUX1_0 = 0 & MUX1_1 = 1 ---> GPS MODULE
* MUX1_0 = 1 & MUX1_1 = 1 ---> GPRS MODULE
* MUX1_0 = 1 & MUX1_1 = 0 ---> AUX1 MODULE
* MUX1_0 = 0 & MUX1_1 = 0 ---> AUX2 MODULE
*/
void WaspUtils::setMuxAux2()
{
pinMode(MUX1_PW, OUTPUT);
pinMode(MUX1_0, OUTPUT);
pinMode(MUX1_1, OUTPUT);
digitalWrite(MUX1_PW, HIGH);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, LOW);
}
/* setMuxUSB() - set multiplexer on UART_0 to USB
*
*/
void WaspUtils::setMuxUSB()
{
if (_boot_version >= 'G')
{
pinMode(MUX0_PW,OUTPUT);
digitalWrite(MUX0_PW,HIGH);
pinMode(MUX_USB_XBEE,OUTPUT);
digitalWrite(MUX_USB_XBEE,LOW);
}
else
{
pinMode(MUX_PW,OUTPUT);
digitalWrite(MUX_PW,HIGH);
pinMode(MUX_USB_XBEE,OUTPUT);
digitalWrite(MUX_USB_XBEE,LOW);
}
}
/* muxOFF() - switch off the multiplexer on UART_0 and UART_1
*
*/
void WaspUtils::muxOFF()
{
if (_boot_version >= 'G')
{
pinMode(MUX1_PW,OUTPUT);
pinMode(MUX0_PW,OUTPUT);
pinMode(MUX_USB_XBEE,OUTPUT);
pinMode(MUX1_0,OUTPUT);
pinMode(MUX1_1,OUTPUT);
digitalWrite(MUX1_PW,LOW);
digitalWrite(MUX0_PW,LOW);
digitalWrite(MUX_USB_XBEE,LOW);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, LOW);
}
else
{
pinMode(MUX_PW,OUTPUT);
pinMode(MUX_USB_XBEE,OUTPUT);
pinMode(MUX1_0,OUTPUT);
pinMode(MUX1_1,OUTPUT);
digitalWrite(MUX_PW,LOW);
digitalWrite(MUX_USB_XBEE,LOW);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, LOW);
}
}
/* muxOFF1() - switch off the multiplexer on UART_1
*
*/
void WaspUtils::muxOFF1()
{
if (_boot_version >= 'G')
{
pinMode(MUX1_PW,OUTPUT);
pinMode(MUX1_0,OUTPUT);
pinMode(MUX1_1,OUTPUT);
digitalWrite(MUX1_PW,LOW);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, LOW);
}
else
{
// previous generation -> switch mux to Aux2
Utils.setMuxAux2();
}
}
/* muxOFF0() - switch off the multiplexer on UART_0
*
*/
void WaspUtils::muxOFF0()
{
if (_boot_version >= 'G')
{
pinMode(MUX0_PW,OUTPUT);
pinMode(MUX_USB_XBEE,OUTPUT);
digitalWrite(MUX0_PW,LOW);
digitalWrite(MUX_USB_XBEE,LOW);
}
else
{
pinMode(MUX_PW,OUTPUT);
pinMode(MUX_USB_XBEE,OUTPUT);
pinMode(MUX1_0,OUTPUT);
pinMode(MUX1_1,OUTPUT);
digitalWrite(MUX_PW,LOW);
digitalWrite(MUX_USB_XBEE,LOW);
digitalWrite(MUX1_0, LOW);
digitalWrite(MUX1_1, LOW);
}
}
/* setMuxSocket0() - set multiplexer on UART_0 to SOCKET0
*
*/
void WaspUtils::setMuxSocket0()
{
if (_boot_version >= 'G')
{
pinMode(MUX0_PW,OUTPUT);
digitalWrite(MUX0_PW,HIGH);
pinMode(MUX_USB_XBEE,OUTPUT);
digitalWrite(MUX_USB_XBEE,HIGH);
}
else
{
pinMode(MUX_PW,OUTPUT);
digitalWrite(MUX_PW,HIGH);
pinMode(MUX_USB_XBEE,OUTPUT);
digitalWrite(MUX_USB_XBEE,HIGH);
}
}
/* readEEPROM(address) - reads from the EEPROM specified address
*
* It reads from the EEPROM specified address
*
* EEPROM has 4096 Bytes of memory
*/
uint8_t WaspUtils::readEEPROM(int address)
{
return eeprom_read_byte((unsigned char *) address);
}
/* writeEEPROM(address,value) - writes the specified value into the specified address
*
* It writes the specified value into the specified address
*
* EEPROM has 4096 Bytes of memory
*/
void WaspUtils::writeEEPROM(int address, uint8_t value)
{
if( address >= EEPROM_START )
{
eeprom_write_byte((uint8_t*) address, value);
}
}
/*
* setID (moteID) - store mote ID to EEPROM
*
* This function stores the mote Identifier in EEPROM[147-162] addresses
*/
void WaspUtils::setID(char* moteID)
{
// set zeros in EEPROM addresses
for( int i=0 ; i<16 ; i++ )
{
eeprom_write_byte( (uint8_t*)(i+EEPROM_FRAME_MOTEID), 0x00);
}
// set the mote ID to EEPROM memory
for( int i=0 ; i<16 ; i++ )
{
eeprom_write_byte( (uint8_t*)(i+EEPROM_FRAME_MOTEID), moteID[i] );
// break if end of string
if( moteID[i] == '\0')
{
break;
}
}
}
/*
* setAuthKey (moteID) - store the authentication key to EEPROM
*
* This function stores the authentication key in EEPROM[107-114] addresses
*/
void WaspUtils::setAuthKey(char* authkey)
{
// set zeros in EEPROM addresses
for(int i=0;i<8;i++)
{
eeprom_write_byte((unsigned char *) i+EEPROM_OTA_AUTHKEY, 0x00);
}
// set the authentication key to EEPROM memory
for(int i=0;i<8;i++)
{
eeprom_write_byte((unsigned char *) i+EEPROM_OTA_AUTHKEY, authkey[i]);
// break if end of string
if( authkey[i] == '\0')
{
break;
}
}
}
/* readSerialChip() - reads the Waspmote unique serial identifier chip several
* times and compare if always is equal
*
*/
unsigned long WaspUtils::readSerialChip()
{
int data[64];
int aux_48[48];
unsigned long id = 0;
int attempts = 2;
unsigned long id_array[attempts];
unsigned long ID_ERROR = 0xFFFFFFFF;
unsigned long seed = 1;
for(int index = 0; index < attempts; index++)
{
// init seed
seed = 1;
WaspOneWire OneWire(LED1);
Utils.setLED(LED0,LED_OFF);
Utils.setLED(LED1,LED_OFF);
// init variable
id_array[index] = 0;
// Powering the serial ID chip
pinMode(LED0, OUTPUT);
digitalWrite(LED0,HIGH);
// reset DS2411 chip
OneWire.resetSerialID();
Utils.setLED(LED1,LED_ON);
delay(10);
// ask the ID number
OneWire.write(0x33,0);
delay(50);
// read the ID number
for (int i = 63; i >= 0;i--)
{
data[i] = OneWire.read_bit();
delay(1);
}
for (int i = 8;i < 56;i++)
{
aux_48[i-8]=data[i];
}
// convert from the array to integer
for(int i=(48-1);i>=0;i--)
{
if(aux_48[i] == 1)
{
id_array[index] += seed;
}
seed = seed * 2;
}
if (id_array[index] == ID_ERROR)
{
id_array[index] = 0;
}
// Powering off the serial ID chip
digitalWrite(LED0,LOW);
Utils.setLED(LED0,LED_OFF);
Utils.setLED(LED1,LED_OFF);
}
for(int index = 0; index < attempts-1; index++)
{
if( id_array[index] != id_array[index+1] )
{
return 0;
}
}
// give value
id = id_array[0];
return id;
}
/* readSerialID() - reads the Waspmote unique serial identifier
*
* It reads the Waspmote unique serial identifier
*/
unsigned long WaspUtils::readSerialID()
{
uint8_t error;
unsigned long eeprom_id;
unsigned long id;
if (_boot_version >= 'G')
{
eeprom.ON();
error = eeprom.readSerialNumber();
if (error == 0)
{
_serial_id[0] = eeprom._response[0];
_serial_id[1] = eeprom._response[1];
_serial_id[2] = eeprom._response[2];
_serial_id[3] = eeprom._response[3];
_serial_id[4] = eeprom._response[4];
_serial_id[5] = eeprom._response[5];
_serial_id[6] = eeprom._response[6];
_serial_id[7] = eeprom._response[7];
return 0;
}
else
{
memset( (uint8_t*)_serial_id, 0x00, sizeof(_serial_id));
return 1;
}
}
else
{
id = readSerialChip();
if( id == 0 )
{
// get eeprom serial id (latest Waspmote batches)
eeprom_id = Utils.getSerialEEPROM();
// check correct value of serial id
// -> 0x0A0A0A0A is a wrong value for default Waspmote EEPROM
// -> 0xFFFFFFFF is a wrong value
if( (eeprom_id != 0x0A0A0A0A) && (eeprom_id !=0xFFFFFFFF) )
{
id = eeprom_id;
}
else
{
id = 0;
}
}
_serial_id[0] = 0x00;
_serial_id[1] = 0x00;
_serial_id[2] = 0x00;
_serial_id[3] = 0x00;
_serial_id[4] = (id>>24) & 0xFF;
_serial_id[5] = (id>>16) & 0xFF;
_serial_id[6] = (id>>8) & 0xFF;
_serial_id[7] = (id>>0) & 0xFF;
return id;
}
}
/* setSerialEEPROM() - sets the Waspmote unique serial identifier to EEPROM
*
* The serial id to be stored is specified as input
*/
bool WaspUtils::setSerialEEPROM( unsigned long serial )
{
// Store to EEPROM
eeprom_write_dword( (uint32_t*)EEPROM_SERIALID_START, serial );
// check EEPROM
if( serial == getSerialEEPROM() )
{
return true;
}
return false;
}
/* getSerialEEPROM() - gets the Waspmote unique serial identifier from EEPROM
*
*/
unsigned long WaspUtils::getSerialEEPROM()
{
return eeprom_read_dword( (uint32_t*)EEPROM_SERIALID_START );
}
/* readTempDS1820() - reads the DS1820 temperature sensor
*
* It reads the DS1820 temperature sensor
*/
float WaspUtils::readTempDS1820(uint8_t pin)
{
// check if it is necessary to turn on
// the generic 3v3 power supply
bool flag = WaspRegister |= REG_3V3;
if (flag == false)
{
PWR.setSensorPower(SENS_3V3,SENS_ON);
delay(10);
}
WaspOneWire OneWireTemp(pin);
byte data[12];
byte addr[8];
delay(25);
if ( !OneWireTemp.search(addr))
{
//no more sensors on chain, reset search
OneWireTemp.reset_search();
return -1000;
}
if ( WaspOneWire::crc8( addr, 7) != addr[7])
{
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28)
{
return -1000;
}
OneWireTemp.reset();
OneWireTemp.select(addr);
OneWireTemp.write(0x44,1); // start conversion, with parasite power on at the end
delay(1000);
OneWireTemp.reset();
OneWireTemp.select(addr);
OneWireTemp.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) // we need 9 bytes
{
data[i] = OneWireTemp.read();
}
OneWireTemp.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
// check if it is necessary to turn off
// the generic 3v3 power supply
if (flag == false)
{
PWR.setSensorPower(SENS_3V3,SENS_OFF);
}
return TemperatureSum;
}
/*
* It converts a hexadecimal number stored in an array to a string (8 Byte
* numbers). This function is used by the XBee module library in order to
* convert mac addresses
*
*/
void WaspUtils::hex2str(uint8_t* number, char* macDest)
{
hex2str(number,macDest,8);
}
/*
* It converts a hexadecimal number stored in an array to a string (length is an
* input parameter). This function is used by the XBee module library in order to
* convert mac addresses
*
*/
void WaspUtils::hex2str(uint8_t* number, char* macDest, uint8_t length)
{
uint8_t aux_1=0;
uint8_t aux_2=0;
for(int i=0;i<length;i++)
{
aux_1=number[i]/16;
aux_2=number[i]%16;
if (aux_1<10)
{
macDest[2*i]=aux_1+'0';
}
else{
macDest[2*i]=aux_1+('A'-10);
}
if (aux_2<10){
macDest[2*i+1]=aux_2+'0';
}
else{
macDest[2*i+1]=aux_2+('A'-10);
}
}
macDest[length*2]='\0';
}
/* readTemperature() - reads the temperature sensor
*
* It reads the temperature sensor
*/
float WaspUtils::readTemperature()
{
// variables
int sensor_value = 0;
float sensor_volts = 0;
float temperature = 0.0;
// Powering the sensor
PWR.setSensorPower(SENS_3V3,SENS_ON);
delay(1000);
// Reading the analog value
sensor_value = analogRead(ANALOG6);
delay(100);
sensor_value = analogRead(ANALOG6);
delay(100);
PWR.setSensorPower(SENS_3V3,SENS_OFF);
// Calculating the volts
sensor_volts = float(sensor_value) * 3.300 / 1023;
// Calculating the temperature value
temperature = (sensor_volts * 100) - 50;
return temperature;
}
/* readHumidity() - reads the humidity sensor
*
* It reads the humidity sensor
*/
uint8_t WaspUtils::readHumidity()
{
// variables
int sensor_value = 0;
float sensor_volts = 0;
int humidity = 0;
// Powering the sensor
PWR.setSensorPower(SENS_3V3,SENS_ON);
delay(15000);
// Reading the analog value
sensor_value = analogRead(ANALOG7);
delay(100);
sensor_value = analogRead(ANALOG7);
delay(100);
PWR.setSensorPower(SENS_3V3,SENS_OFF);
// Calculating the volts
sensor_volts = float(sensor_value) * 3.300 / 1023;
// Calculating the light value
humidity = int((sensor_volts * 100) / 3);
return humidity;
}
/* readLight() - reads the light sensor
*
* It reads the light sensor
*/
uint8_t WaspUtils::readLight()
{
// variables
int sensor_value = 0;
float sensor_volts = 0;
int light = 0;
// Powering the sensor
PWR.setSensorPower(SENS_3V3,SENS_ON);
// Reading the analog value
sensor_value = analogRead(ANALOG5);
delay(100);
sensor_value = analogRead(ANALOG5);
PWR.setSensorPower(SENS_3V3,SENS_OFF);
// Calculating the volts
sensor_volts = float(sensor_value) * 3.300 / 1023;
// Calculating the light value
light = int((sensor_volts * 100) / 3.3);
return light;
}
/*
Function: Coverts a long to a number stored in an array
*/
uint8_t WaspUtils::long2array(long num, char* numb)
{
long aux=num;
uint8_t i=0;
if( num<0 )
{
num = ~(num);
num+=1;
numb[i]='-';
i++;
}
aux=num;
while(aux>=10)
{
aux=aux/10;
i++;
}
numb[i+1]='\0';
aux=num;
while(aux>=10)
{
numb[i]=aux%10 + '0';
aux=aux/10;
i--;
}
numb[i]=aux + '0';
return i;
}
/*
* Function: Converts a string to an hex number
*
*/
uint8_t WaspUtils::str2hex(char* str)
{
int aux=0, aux2=0;
if( (*str>='0') && (*str<='9') )
{
aux=*str++-'0';
}
else if( (*str>='A') && (*str<='F') )
{
aux=*str++-'A'+10;
}
else if( (*str>='a') && (*str<='f') )
{
aux=*str++-'a'+10;
}
if( (*str>='0') && (*str<='9') )
{
aux2=*str-'0';
}
else if( (*str>='A') && (*str<='F') )
{
aux2=*str-'A'+10;
}
else if( (*str>='a') && (*str<='f') )
{
aux2=*str-'a'+10;
}
return aux*16+aux2;
}
/*
* Function: Converts a string to an array of bytes
* For example: If the input string is defined as:
* char input[] = "";
* The output string is array -> 23576173706D6F74655F50726F23
*/
uint16_t WaspUtils::str2hex(char* str, uint8_t* array)
{
// get length in bytes (half of ASCII characters)