-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pointcast.ino
4119 lines (3459 loc) · 106 KB
/
Pointcast.ino
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
/*
A fixed position Safecast device for Radiation measurement.
Copyright (c) 2015, Safecast
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the <organization> nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
History Versions:
2015-04-05 V2.4.9 delay for switching off LEDs
2015-04-07 V2.6.0 merged code with 3G
2015-04-07 V2.6.1 beeper setup and code cleaning(need jumper from D10 in Arduino shield (is pin D27)to A3)
2015-04-08 V2.6.3 setup for measuring voltage on A13
2015-04-08 V2.6.4 made switch for sending to dev or API
2015-04-14 V2.6.6 added heart beat on green LED and reading setup for files
2015-04-19 V2.6.7 added setup files now 1024 byte possible . added setup variables
2015-04-20 V2.6.8 added device ID, startup screen change, header for file format changed to NGRDD
2015-04-23 V2.6.9 added second line to SDcard logging for added status NNXSTS, added menus
2015-04-23 V2.7.0 renamed Pointcast
2015-04-28 V2.7.1 moved startup 3G into send string, battery voltage report corrected for Teensy.
2015-04-30 V2.7.2 Added temperature setup for DS18B20 (disabled at the moment) setup screens
2015-05-05 V2.7.3 Added Joy stick setup. Added Height. Fixed lot/lan sending information on Ethernet
2015-05-08 V2.7.4 Added new screens for setup and error reporting
2015-05-09 V2.7.5 Changed startup name
2015-05-10 V2.7.6 Added Red LED warning on SDcard and Sensor fails, renamed SDcard files and updates headers
2015-05-11 V2.7.7 Updates headers. 3G display and header setup same as Ethernet card
2015-05-12 V2.7.8 Moved SDcard screen before time screen
2015-05-16 V2.7.9 Added Joystick interaction. Key down startup 1/5 of normal time. Prepare joy stick enter key (push down) for other functions.
2015-05-17 V2.8.0 Changed startup screen to be faster with 1 second display if joystick in pressed down. Blink the heartbeat LED for 1 sec.
2015-05-18 V2.8.1 Setup NTP for automatic update time on Ethernet.
2015-05-19 V2.8.2 Fixed first display readings of CPM (was too high)
2015-05-19 V2.8.3 Fixed 3G not starting up due to Ethernet settings.
2015-05-22 V2.8.4 Auto gateway setup (two gateways at the moment)
2015-05-22 V2.8.5 Display RSSI level in 3G.
2015-05-23 V2.8.6 SDCard RSSI level saved in 3G.
2015-05-30 V2.8.7 Menu updated.
2015-05-30 V2.8.8 Menu loop remake
2015-05-31 V2.8.9 Logfile name fix
2015-05-31 V2.9.0 Sending data through indextest_extra.php on 107.161.164.166 with no time-stamping and with extra information
2015-06-04 V2.9.1 Setup for fast sens mode selectable from SDcard with the "trb" option
2015-06-08 V2.9.2 Temperature setup truncated API key, battery/power setup changed.
2015-06-10 V2.9.3 Added skip sdcard init on one time pass. uSv/H changed to uSH..menu stat prepared...
2015-06-17 V2.9.4 Fixed second line temperature and batter logging on SDcard
2015-06-17 V2.9.5 3G second line recoding for SDcard fixes
2015-06-19 V2.9.6 Changed file naming SDCard
2015-06-20 V2.9.7 more fixed 3G second line
2015-06-21 V2.9.8 Switch between API and Dev sending through sdcard.
2015-06-22 V2.9.9 DEV status is displayed on LCD
2015-06-22 V3.0.0 Second line for extra information fix (final)
2015-06-25 V3.0.1 Device Type ID on SDCard and the API setup for Device typing LND7317 is 129 and LND712 is 130
2015-06-25 V3.0.2 Added height for dev to measurements.
2015-06-25 V3.0.3 Fixes for SDcard reading and Network startup
2015-06-25 V3.0.4 RTC tests moved after network startup
2015-07-27 V3.0.5 Setup for kcpm
2015-07-30 V3.0.6 Setup for logic for sending data with deviceType ID
2015-08-15 V3.0.7 Fixed CPM2 bug in display
2015-08-16 V3.0.8 3G RTC and display setup changed
2015-08-18 V3.0.9 Voltage display adjusted for volage drop over D103 lower board
2015-08-18 V3.1.0 Switched Teensy to internal ref mode for voltage measurements
2015-08-21 V3.1.1 Added devicetype_id to send sting inside measurement
2015-08-25 V3.1.2 MACid reading from SDcard and programming
2015-08-25 V3.1.3 SDcard fail lcd display
2015-08-25 V3.1.4 Voltage display direct without diode compensation and 3G displays carrier
2015-08-25 V3.1.5 Fixed always two digit display Ethernet Mac
2015-08-25 V3.1.6 Fixed Ethernet hang bug.
2015-08-25 V3.1.7 Fixed RTC bug.
2015-08-26 V3.1.8 Fixed Ethernet display hang on scroll back bug
2015-08-27 V3.1.9 Fixes for sending Ethernet
2015-09-01 V3.2.0 Fixes for variables listing data.
2015-09-04 V3.2.1 Fixes for failing SDcard data is still send
2015-09-05 V3.2.2 Sending DeviceType_ID
2015-09-06 V3.2.3 3G display setting changed
2015-09-06 V3.2.4 Set audio alarms for failing to connect for 3G and Ethernet on sending
2015-09-12 V3.2.5 Setup fails for Ethernet connections FAIL=1 means sensor 1 fails to send .. FAIL=2 means sensor 2 fails to send ....added delay for 3G sensing between sensors
2015-09-12 V3.2.6 Setup S1peak and S2peak and setup for uptime of sensor
2015-09-15 V3.2.7 Terminal displays last failure
2015-09-19 V3.2.8 Single sensor setup displaying no error on sensor test
2015-09-19 V3.2.9 Reset every week on Saturday 9:30am (UTC)
2015-10-03 V3.3.0 Beep 3 times and some display errors fix
2015-10-09 V3.3.1 Added longer delay for 3G sending second sensor
2015-10-09 V3.3.2 Fixed display failed error 3G.
2015-11-14 V3.3.3 Changed the way 3G connects to APN
2015-11-16 V3.3.4 Fixed display failed error 3G.
2015-11-16 V3.3.5 EEprom clearing on enter of joystick at system screen.
2015-11-17 V3.3.6 Switch off fail led by enter joy switch on main screen
2015-11-21 V3.3.7 Fixed display failed error 3G.
2015-11-24 V3.3.8 Fixed display failed error 3G.
2015-12-03 V3.3.9 Added dose storage for EEProm.
2015-12-09 V3.4.0 Stores total counts in EEprom
2015-12-16 V3.4.1 fixes fails display
2015-12-31 V3.4.2 Ethernet fixes fails display
2016-01-05 V3.4.3 Change parameters in 3GIM for longer timeout of network and setup for 1024 body lenght
2016-01-07 V3.4.4 Changed delays in 3GIM for stability
2016-01-08 V3.4.5 Error messages fixed
2016-01-15 V3.4.6 httpPost fixes for 3G
2016-01-15 V3.4.7 serial confirm messages fix for 3G
2016-01-24 V3.4.8 added code for switching temperature sensor
2016-01-26 V3.4.9 changed 3G send way by restarting 3G module every time send.
2016-02-01 V3.5.0 Added Alarm function.
2016-02-15 V3.5.1 3G fast reset
2016-02-28 V3.5.8 reset after 5 times
2016-03-02 V3.5.9 added code for correct data login name on SDcard.
2016-03-03 V3.6.0 Ethernet detect messages on fail
2016-03-05 V3.6.3 redone menus
2016-03-05 V3.6.4 Fixed time issues.
2016-03-06 V3.6.5 Weekly restart redone
2016-03-08 V3.6.6 clean out code
2016-03-09 V3.6.7 RTC menu
2016-03-11 V3.6.8 Ping to api.safecast.org
2016-03-13 V3.7.1 Redone screen flow
2016-03-13 V3.7.2 ping to Google.com/gw02/gw02 setup
2016-03-14 V3.7.3 setup timing for 3G refresh
2016-03-14 V3.7.4 ping http://safecast-production.s3.amazonaws.com
2016-03-14 V3.7.5 moved countdown and delayed displaying countdown
2016-03-16 V3.7.6 added fails, restarts and create file numbers to EEprom. Added restarts and fails to second line in SDCard
2016-03-17 V3.7.7 file creation counts (logs in stats) added.
2016-03-27 V3.7.8 Display API/DEV toggle implemented , logging new lot to sdcard with stats
2016-03-28 V3.7.9 Fixed after EPROM erase not counting error
2016-04-02 V3.8.0 Sliding window cpm calculation
2016-04-02 V3.8.1 Reading sdcard remove "\r" (windows)
2016-04-19 V3.8.2 push down key delay for menu
2016-04-21 V3.8.3 start counting after all screen of startup are done
2016-04-27 V3.8.4 5 minutes count test
2016-04-28 V3.8.5 fix for Reading sdcard remove "\r\n" (windows)
2016-04-29 V3.8.6 added phone number setup from sdcard for 3G, changed display data to 1 minute, changed NX to 12 and delay to 5000 for 5 seconds measuring, KCPM 2 charater after decimal point
2016-04-30 V3.8.7 fixed new line on new log on sdcard
2016-05-05 V3.8.8 5 minutes display
2016-05-15 V3.8.9 pool.ntp.org as time server dns based
2016-05-15 V3.9.0 Xbee BLEbee setup
2016-05-15 V3.9.1 Menu fixes.
2016-06-09 V3.9.2 Boot delay 3 seconds
2016-06-16 V3.9.3 Changed to 5 minutes count
2016-06-17 V3.9.4 Added Ethernet.maintain() to renew lease before restart
2016-07-04 V3.9.5 reset Wiz820IO modified at startup
2016-07-08 V3.9.6 move week hourly reset in main loop and 3g gim restart first shutdown 3gim module.
2016-07-16 V3.9.7 reset counter just before main loop to avoid wrong first count
2016-07-21 V3.9.8 random minute restart weekly restart
2016-07-27 V3.9.9 fix for DS18S20 to report correctly
2016-07-27 V4.0.0 adjusted display for Ethernet and 3G to display count down in 4 digits and get even spaces for the voltage and temperature.
2016-07-29 V4.0.1 3GIM and Wiz820IO modules reset at startup
2016-07-31 V4.0.2 setup weekly restarts
2016-08-01 V4.0.3 improved weekly restart based on rst value on sdcard
2016-08-05 V4.0.4 daily RTC update
2016-08-10 V4.0.5 moved 3Gpower down/up more sooner in startup. to allow 3GIM module to settle down before accessing it
2016-08-30 V4.0.6 fixed bug with pin assignment of Ethernet
2016-09-09 V4.0.7 fixed retries connect Ethernet
2016-09-09 V4.0.8 added delay between retries Ethernet connect
2016-09-14 V4.0.9 fixed in time sync provider
2016-09-17 V4.1.0 fixed weekly restarts (was not correctly working)
2016-09-17 V4.1.1 fixed Openlog startup detection (was broken in 4.0.9)
2016-09-19 V4.1.2 fixed compiler warnings
2016-09-20 V4.1.3 NTP server timeout retries at 5 minutes.
2016-09-21 V4.1.4 fixes freezes on time update
2016-09-22 V4.1.5 Cleaned up formatting
2016-09-23 V4.1.6 setup NTP check for 15 seconds retries and 300 seconds timeout
2016-09-23 V4.1.7 setup to renew a lease for DHCP every 5 minutes for networks that have short DHCP lease setup
2016-09-24 V4.1.8 conditional setup of the Ethernet.maintain() function to restart the Ethernet connection id a new lease or a rebind can not obtained.
2016-10-16 V4.1.9 Added confirmation for measurement ID from API to be sure data is accepted in the Database.
2016-10-28 V4.2.0 Status data to API.
2016-10-28 V4.2.1 Status data to API with fix for Ethernet.
2016-10-28 V4.2.2 Ethernet reply message of measurement_id of for API setup for displaying on terminal
2016-10-31 V4.2.3 3G cleanup code
2016-11-11 V4.2.4 3G send error message corrected.
2017-02-14 V4.2.5 SD card can now be edited in Windows (ray fixed the import code)
2018-06-02 V4.2.7 .SD card can now be edited in Windows (ray new fix)
contact [email protected]
*/
/**************************************************************************/
// Init
/**************************************************************************/
#define ENABLE_DEBUG
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <EEPROM.h>
#include <limits.h>
#include <SoftwareSerial.h>
#include <Time.h>
#include <TimeLib.h>
#include <TimeAlarms.h>
#include <i2c_t3.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <limits.h>
#include "a3gim.h"
#include "PointcastSetup.h"
#include "PointcastDebug.h"
//setup LCD I2C
#define I2C_ADDR 0x27 // Define I2C Address where the PCF8574 is for LCD2004 form http://www.sainsmart.com the PCF8574A is 0x3f
#define BACKLIGHT_PIN 3
#define En_pin 2
#define Rw_pin 1
#define Rs_pin 0
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
int n = 1;
LiquidCrystal_I2C lcd(I2C_ADDR, En_pin, Rw_pin, Rs_pin, D4_pin, D5_pin, D6_pin, D7_pin);
int green_ledPin = 31;
int red_ledPin = 26;
// 3G signal strengh
int rssi = -125;
//setup Power detection
#define VOLTAGE_PIN A13
#define VOLTAGE_R1 100000
#define VOLTAGE_R2 10000
//sliding windows counting variables
#define NX 60
#define TIME_INTERVAL 5000
#define IS_READY (interruptCounterAvailable())
//setup Onewire for temp sensor
OneWire ds(32); // on pin 10 of extra header(a 4.7K resistor is necessary)
//main menu variable
boolean finished_startup = false;
boolean network_startup = false;
boolean sdcard_startup = false;
boolean pingConnect = false;
#define LINE_SZ 128
// SENT_SZ is used for sending data for 3G
#define SENT_SZ 512
//OLINE_SZ is used for OpenLog buffers
#define OLINE_SZ 512
//GATEWAY_sz is array for gateways
#define GATEWAY_SZ 2
static char obuf[OLINE_SZ];
static char buf[LINE_SZ];
static char buf2[LINE_SZ];
static char strbuffer[32];
static char strbuffer1[32];
char body[512];
char body2[512];
char body3[512];
char STATS[255];
// OpenLog Settings --------------------------------------------------------------
SoftwareSerial OpenLog = SoftwareSerial(0, 1);
SoftwareSerial XbeeSerial = SoftwareSerial(25, 24);
static const int resetOpenLog = 3;
#define OPENLOG_RETRY 500
bool openlog_ready = false;
char logfile_name[13]; // placeholder for filename
bool logfile_ready = false;
static void setupOpenLog();
static void createFile(char *fileName);
static ConfigType config;
static DoseType dose;
PointcastSetup PointcastSetup(OpenLog, config, dose, obuf, OLINE_SZ);
//static
static char VERSION[] = "V4.2.7";
#if ENABLE_3G
static char path[LINE_SZ];
char datedisplay[8];
char coordinate[16];
#endif
#if ENABLE_ETHERNET
static char json_buf[SENT_SZ];
static char json_buf2[SENT_SZ];
bool ethernetfailed;
#endif
//Struct setup
typedef struct
{
unsigned char state;
unsigned char conn_fail_cnt;
} devctrl_t;
static devctrl_t ctrl;
//Const
const char *server = "107.161.164.163";
const char serverName[] = "safecast-production.s3.amazonaws.com";
char character;
const int port = 80;
const int interruptMode = FALLING;
int updateIntervalInMinutes = 1;
const char *header = "Content-Type:application/httpPost$r$n";
const boolean useHTTPS = false; // Use https(true) or http(false)
uint32_t seconds;
#if ENABLE_3G
const int PowerPin = 6;
#endif
#if ENABLE_ETHERNET
//ethernet
byte macAddress[] = { 0x90, 0xA2, 0xDA, 0x0E, 0xE0, 0x5C };
EthernetClient client;
IPAddress localIP (192, 168, 100, 40);
char timeServer[] = "pool.ntp.org";
int resetPin = 9; //
int ethernet_powerdonwPin = A2;
const int timeZone = 0;
boolean timeset_on = false;
EthernetUDP Udp;
unsigned int localPort = 8888; // local port to listen for UDP packets]
char macstr[19];
const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
#endif
//Boolean
boolean getTimeStamp = true;
//Strings
String last_failure = "";
String replysub = "";
String replyserver = "";
String replyserver1 = "";
//int
int MAX_FAILED_CONNS = 5;
int len;
int len2;
int len4;
int conn_fail_cnt;
int NORMAL = 0;
int RESET = 1;
int S1peak;
int S2peak;
int failures;
int left_mins_old = 1;
int ntpcount;
int fail_cnt = 0;
//long
unsigned long elapsedTime(unsigned long startTime);
unsigned long previousMillis = 0;
unsigned long currentmillis = 0;
unsigned long total_time = 0;
unsigned long GetmeasurementReplyReturn;
unsigned long GetmeasurementReplyReturn1;
unsigned long GetmeasurementReplyReturn2;
long display_days = 0;
long display_hours = 0;
long display_mins = 0;
long display_secs = 0;
//long for sliding window
unsigned long shift_reg[NX] = {0};
unsigned long shift_reg2[NX] = {0};
unsigned long reg_index = 0;
unsigned long reg_index2 = 0;
unsigned long total_count = 0;
unsigned long max_count = 0;
unsigned long uptime = 0;
unsigned long _start_time;
unsigned long _delay = TIME_INTERVAL;
unsigned long _count = 0;
unsigned long cpm1 = 0, cpm2 = 0, cpb1 = 0, cpb2 = 0;
unsigned long cpm_gen1()
{
unsigned int i;
unsigned long c_p_m1 = 0;
// sum up
for (i = 0 ; i < NX ; i++)
c_p_m1 += shift_reg[i];
//deadtime compensation (medcom international)
c_p_m1 = (unsigned long)((float)c_p_m1 / (1 - (((float)c_p_m1 * 1.8833e-6))) / 5);
return c_p_m1;
}
unsigned long cpm_gen2()
{
unsigned int i;
unsigned long c_p_m2 = 0;
// sum up
for (i = 0 ; i < NX ; i++)
c_p_m2 += shift_reg2[i];
//deadtime compensation (medcom international)
c_p_m2 = (unsigned long)((float)c_p_m2 / (1 - (((float)c_p_m2 * 1.8833e-6))) / 5);
return c_p_m2;
}
// Interval is how long display wait
int display_interval = 2000;
// Display time on display
bool displayTimeOn;
// Interval is how long LED blinks
int blinkinterval = 50;
// Char
char timestamp[19];
char lat[8];
char lon[9];
char lat_lon_nmea[25];
unsigned char state;
#if ENABLE_3G
char res[a3gsMAX_RESULT_LENGTH + 1];
char res2[a3gsMAX_RESULT_LENGTH + 1];
char res4[a3gsMAX_RESULT_LENGTH + 1];
const int timeZone = 1;
#endif
// Gateway setup
char gateway0[16] ;
char gateway1[16] ;
char *gateway[2];
// Joystick pins setup
const int JOY_A_PIN = 17;
const int JOY_B_PIN = 20;
const int JOY_C_PIN = 21;
const int JOY_D_PIN = 23;
const int JOY_E_PIN = 22;
volatile boolean joyCntA = false;
volatile boolean joyCntB = false;
volatile boolean joyCntC = false;
volatile boolean joyCntD = false;
volatile boolean joyCntE = false;
//WDT timer
IntervalTimer wdTimer;
//reset marco
#define CPU_RESTART_ADDR (uint32_t *)0xE000ED0C
#define CPU_RESTART_VAL 0x5FA0004
#define CPU_RESTART (*CPU_RESTART_ADDR = CPU_RESTART_VAL);
void onReset()
{
CPU_RESTART;
}
// generate checksums for log format
byte len1, chk;
byte len3, chk2;
char checksum(char *s, int N)
{
int i = 0;
char chk = s[0];
for (i = 1; i < N; i++)
chk ^= s[i];
return chk;
}
char checksum2(char *s, int N)
{
int i = 0;
char chk2 = s[0];
for (i = 1; i < N; i++)
chk2 ^= s[i];
return chk2;
}
// Sampling interval (e.g. 60,000ms = 1min)
unsigned long updateIntervalInMillis = 0;
// The next time to feed
unsigned long nextExecuteMillis = 0;
// Event flag signals when a geiger event has occurred
volatile unsigned char eventFlag = 0; // FIXME: Can we get rid of eventFlag and use counts>0?
unsigned long int counts_per_sample;
unsigned long int counts_per_sample2;
// The last connection time to disconnect from the server
// after uploaded feeds
long lastConnectionTime = 0;
// The conversion coefficient from cpm to µSv/h
float conversionCoefficient = 0;
float conversionCoefficient2 = 0;
//Pulse counters
void onPulse()
{
counts_per_sample++;
}
void onPulse2()
{
counts_per_sample2++;
}
//sliding windows setup
void interruptCounterReset()
{
// set start time
_start_time = millis();
// set count to zero (optional)
counts_per_sample = 0;
counts_per_sample2 = 0;
}
int interruptCounterAvailable()
{
// get current time
unsigned long now = millis();
// do basic check for millis overflow
if (now >= _start_time)
return (now - _start_time >= _delay);
else
return (ULONG_MAX + now - _start_time >= _delay);
}
// return current number of counts
unsigned long interruptCounterCount()
{
return counts_per_sample;
}
// return current number of counts
unsigned long interruptCounterCount2()
{
return counts_per_sample2;
}
//Joy Switch
void joyA_Callback()
{
joyCntA = true;
}
void joyB_Callback()
{
joyCntB = true;
}
void joyC_Callback()
{
joyCntC = true;
}
void joyD_Callback()
{
joyCntD = true;
}
void joyE_Callback()
{
joyCntE = true;
}
/**************************************************************************/
// Setup
/**************************************************************************/
// freeRam for Teensy 3.0
uint32_t FreeRam() {
uint32_t stackTop;
uint32_t heapTop;
// current position of the stack.
stackTop = (uint32_t) &stackTop;
// current position of heap.
void* hTop = malloc(1);
heapTop = (uint32_t) hTop;
free(hTop);
// The difference is the free, available ram.
return stackTop - heapTop;
}
void setup() {
//power down and up 3GIM module
#if ENABLE_3G
delay(1000);
pinMode(PowerPin, OUTPUT); digitalWrite(PowerPin, HIGH);
#ifdef ENABLE_DEBUG
Serial.println("3GIM power down");
#endif
delay(1000);
digitalWrite(PowerPin, LOW);
#ifdef ENABLE_DEBUG
Serial.println("3GIM power up");
#endif
delay(3000);
#endif
//power down and up Ethernet module
#if ENABLE_ETHERNET
delay(1000);
pinMode(ethernet_powerdonwPin, OUTPUT); digitalWrite(ethernet_powerdonwPin, HIGH);
#ifdef ENABLE_DEBUG
Serial.println("Ethernet power down");
#endif
delay(1000);
digitalWrite(ethernet_powerdonwPin, LOW);
#ifdef ENABLE_DEBUG
Serial.println("Ethernet power up");
#endif
delay(1000);
// reset Ethernet wiz820iIO
pinMode(resetPin, OUTPUT); digitalWrite(resetPin, HIGH);
#ifdef ENABLE_DEBUG
Serial.println("Ethernet reset");
#endif
delay(1000);
digitalWrite(resetPin, LOW);
#endif
analogReference(INTERNAL);
//Read dose from EEPROM
EEPROM_readAnything(BMRDD_EEPROM_DOSE, dose);
dose.restarts++;
EEPROM_writeAnything(BMRDD_EEPROM_DOSE, dose);
//print last reset message and setup the patting of the dog
delay(100);
printResetType();
//start WDT
wdTimer.begin(KickDog, 10000000); // patt the dog every 10sec
// Load EEPROM settings
PointcastSetup.initialize();
// setup for trouble shooting
updateIntervalInMinutes = config.trb ? 1 : 5;
updateIntervalInMillis = updateIntervalInMinutes * 60 * 1000;
//serial for Xbee
XbeeSerial.begin(9600);
//beep for loud piezo twice
int i = 0;
while (i < 2) {
digitalWrite(28, HIGH);
pinMode(28, OUTPUT);
delay(250);
pinMode(28, INPUT);
delay(250);
i++;
}
//button reset
pinMode(27, INPUT_PULLUP);
attachInterrupt(27, onReset, interruptMode);
// set brightnes
lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE);
lcd.setBacklight(125);
//Joy Switch setup
pinMode(JOY_A_PIN, INPUT_PULLUP);
pinMode(JOY_B_PIN, INPUT_PULLUP);
pinMode(JOY_C_PIN, INPUT_PULLUP);
pinMode(JOY_D_PIN, INPUT_PULLUP);
pinMode(JOY_E_PIN, INPUT_PULLUP);
attachInterrupt(JOY_A_PIN, joyA_Callback, FALLING);
attachInterrupt(JOY_B_PIN, joyB_Callback, FALLING);
attachInterrupt(JOY_C_PIN, joyC_Callback, FALLING);
attachInterrupt(JOY_D_PIN, joyD_Callback, FALLING);
attachInterrupt(JOY_E_PIN, joyE_Callback, FALLING);
//set up the LCD's number of columns and rows:
lcd.begin(20, 4);
//boot delay
delay(3000);
Menu_startup();
}
//End setup
/**************************************************************************/
// Start screen
/**************************************************************************/
void Menu_startup(void) {
displayTimeOn = false;
//setup failure message
#ifdef ENABLE_DEBUG
Serial.print("last_failure =");
Serial.println(config.last_failure);
Serial.print("logs =");
Serial.println(dose.logs);
Serial.print("fails =");
Serial.println(dose.fails);
Serial.print("restarts =");
Serial.println(dose.restarts);
Serial.print("free ram = ");
Serial.println(FreeRam());
#endif
lcd.clear();
// LED on delay (start speed display function by pressing down)
previousMillis = millis();
while ((unsigned long)(millis() - (unsigned long)previousMillis) <= (unsigned long)display_interval) {
if (joyCntA) {
#ifdef ENABLE_DEBUG
Serial.println ("Down");
#endif
joyCntA = !joyCntA; joyCntB = false; lcd.clear(); display_interval = 3000; Menu_system(); return;
}
if (joyCntE) {
#ifdef ENABLE_DEBUG
Serial.println ("Enter");
#endif
joyCntE = !joyCntE; joyCntB = false; joyCntA = false; lcd.clear(); lcd.print("Clearing EEPROM"); eepromclear(); return;
}
// Print startup message to the LCD.
lcd.setCursor(0, 0);
lcd.print("SAFECAST POINTCASTv1");
lcd.setCursor(0, 1);
lcd.print("Firmware :");
lcd.print(VERSION);
lcd.setCursor(0, 2);
lcd.print("Device ID:");
lcd.print(config.devid);
lcd.setCursor(0, 3);
lcd.print("http://safecast.org");
//LED1(green) setup
pinMode(green_ledPin, OUTPUT);
digitalWrite(green_ledPin, HIGH);
digitalWrite(28, LOW);
//LED2(red) setup
pinMode(red_ledPin, OUTPUT);
digitalWrite(red_ledPin, HIGH);
}
//LED off
digitalWrite(red_ledPin, LOW);
digitalWrite(green_ledPin, LOW);
// Sound off
digitalWrite(28, HIGH);
Menu_system();
}
/**************************************************************************/
// System Screen
/**************************************************************************/
void Menu_system(void) {
displayTimeOn = false;
float battery = ((read_voltage(VOLTAGE_PIN)));
float temperature = getTemp();
// Print system message to the LCD.
lcd.clear();
previousMillis = millis() ;
while ((unsigned long)(millis() - (unsigned long)previousMillis) <= (unsigned long)display_interval) {
if (joyCntB) {
#ifdef ENABLE_DEBUG
Serial.println ("Up");
#endif
joyCntB = !joyCntB; joyCntA = false; lcd.clear(); display_interval = 3000; Menu_startup(); return;
}
if (joyCntA) {
#ifdef ENABLE_DEBUG
Serial.println ("Down");
#endif
joyCntA = !joyCntA; joyCntB = false; lcd.clear(); display_interval = 3000; Menu_sdcard(); return;
}
if (joyCntE) {
#ifdef ENABLE_DEBUG
Serial.println ("Enter");
#endif
joyCntE = !joyCntE; joyCntB = false; joyCntA = false; display_interval = 100000;
}
lcd.setCursor(0, 0);
lcd.print("System");
lcd.setCursor(0, 1);
lcd.print("Power:");
if (battery < 4.4) {
lcd.print("BAT");;
} else {
lcd.print("EXT");
}
lcd.setCursor(15, 1);
lcd.print(battery);
lcd.print("V");
lcd.setCursor(0, 2);
lcd.print("Bat:");
int battery1 = ((battery - 2.9) * 100);
if (battery1 < 0) battery1 = 1;
if (battery1 > 100) battery1 = 100;
sprintf_P(strbuffer, PSTR("%02d"), battery1);
lcd.print(strbuffer);
lcd.print("%");
lcd.setCursor(11, 2);
lcd.print("TNSY:");
lcd.print("3.3");
lcd.print("V");
lcd.setCursor(0, 3);
lcd.print("Tmp:");
lcd.print(temperature);
lcd.print("C");
}
Menu_sdcard();
}
/**************************************************************************/
// SDcard Screen
/**************************************************************************/
void Menu_sdcard(void) {
displayTimeOn = false;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("SDCARD ");
//Openlog setup
if (!sdcard_startup) {
OpenLog.begin(9600);
setupOpenLog();
}
if (!openlog_ready) {
lcd.setCursor(8, 0);
lcd.print("FAIL");
// failure message to EEPROM
String last_failure = "FAILsdcard";
last_failure.toCharArray(config.last_failure, 12);
EEPROM_writeAnything(BMRDD_EEPROM_SETUP, config);
//end test
#ifdef ENABLE_DEBUG
Serial.print(config.last_failure);
#endif
delay(2000);
//Red LED on
digitalWrite(26, HIGH);
#ifdef ENABLE_DEBUG
Serial.println();
Serial.println("No SD card.. ");
#endif
}
if (openlog_ready) {
previousMillis = millis() ;
while ((unsigned long)(millis() - (unsigned long)previousMillis) <= (unsigned long)display_interval) {
if (joyCntB) {
#ifdef ENABLE_DEBUG
Serial.println ("Up");
#endif
joyCntB = !joyCntB; joyCntA = false; lcd.clear(); display_interval = 3000; Menu_system(); return;
}
if (joyCntA) {
#ifdef ENABLE_DEBUG
Serial.println ("Down");
#endif
joyCntA = !joyCntA; joyCntB = false; lcd.clear(); display_interval = 3000; Menu_network(); return;
}
if (joyCntE) {
#ifdef ENABLE_DEBUG
Serial.println ("Enter");
#endif
joyCntE = !joyCntE; joyCntB = false; joyCntA = false; display_interval = 100000;
}
if (!sdcard_startup) {
lcd.setCursor(8, 0);
lcd.print(" PASS");
lcd.setCursor(0, 1);
lcd.print("PNTCAST:");
#ifdef ENABLE_DEBUG
Serial.println();
Serial.println("loading setup");
#endif
PointcastSetup.loadFromFile("PNTCAST.TXT");
lcd.print(" PASS");
lcd.setCursor(0, 2);
lcd.print("SENSORS:");
#ifdef ENABLE_DEBUG
Serial.println();
Serial.println("loading sensors setup");
#endif
PointcastSetup.loadFromFile("SENSORS.TXT");
lcd.print(" PASS");
lcd.setCursor(0, 3);
lcd.print("NETWORK:");
#ifdef ENABLE_DEBUG
Serial.println();
Serial.println("loading Network setup");
#endif
PointcastSetup.loadFromFile("NETWORKS.TXT");
lcd.print(" PASS");
sdcard_startup = true;
}
// sdcard display status
lcd.setCursor(8, 0);
lcd.print(" PASS");
lcd.setCursor(0, 1);
lcd.print("PNTCAST:");
lcd.print(" PASS");
lcd.setCursor(0, 2);
lcd.print("SENSORS:");
lcd.print(" PASS");
lcd.setCursor(0, 3);
lcd.print("NETWORK:");
lcd.print(" PASS");
delay(3000);
}
}
// SENSOR 1 setup
if (config.sensor1_enabled) {
conversionCoefficient = 1 / config.sensor1_cpm_factor;
pinMode(14, INPUT_PULLUP);
attachInterrupt(14, onPulse, interruptMode);
}