Browse code

version 1.7 upgrade

Gandolf authored on 10/29/2019 22:18:24
Showing 1 changed files
... ...
@@ -68,7 +68,13 @@
68 68
        - simplified serial data output
69 69
    * v16 released 16 Sep 2017 by J L Owrey
70 70
        - added capability of rebooting via network http request,
71
-         i.e.,    "http://{device_IP_address}/reset"
71
+         i.e., "http://{device IP address}/reset"
72
+   * v17 released 29 Oct 2019 by J L Owrey
73
+       - modified NTP server address user setting to allow fully
74
+         qualified domain names as well as IP addresses.  Default
75
+         NTP address set to "time.nist.gov" per NIST request to use
76
+         (in order to facilitate load balancing) the fully qualified
77
+         domain name instead of individual server IP addresses.
72 78
 */
73 79
 
74 80
 /***  PREPROCESSOR DEFINES  ***/
... ...
@@ -79,8 +85,8 @@
79 85
  Define the header and version number displayed at startup
80 86
  and also by the 'view settings' command.
81 87
 */
82
-#define STARTUP_HEADER "\n\rRadmon v1.6 (c) 2018\n"
83
-#define RADMON_VERSION "v1.6"
88
+#define STARTUP_HEADER "\n\rRadmon v1.7 (c) 2019\n"
89
+#define RADMON_VERSION "v1.7"
84 90
 /*
85 91
  The following define sets the MAC address of the device.  This
86 92
  address is a permanent attribute of the device's Ethernet interface,
... ...
@@ -89,7 +95,7 @@
89 95
  specific instance of the Ethernet shield.  This MAC address should
90 96
  be shown on a label affixed to the device housing.
91 97
 */
92
-#define ETHERNET_MAC_ADDRESS 0x90, 0xA2, 0xDA, 0x0D, 0x84, 0xF6
98
+#define ETHERNET_MAC_ADDRESS 0xNN, 0xNN, 0xNN, 0xNN, 0xNN, 0xNN
93 99
 /*
94 100
  The following defines an APIPA default address in the event that
95 101
  DHCP mode is ON and a DHCP address cannot be obtained.
... ...
@@ -114,14 +120,18 @@
114 120
  time server:
115 121
               time-c-b.nist.gov
116 122
 */
117
-#define DEFAULT_NTP_SERVER_IP_ADDR "132.163.96.3"
123
+#define DEFAULT_NTP_SERVER_ADDR "pool.ntp.org"
118 124
 #define NTP_PORT 8888
119 125
 #define NTP_PACKET_SIZE 48 // NTP time stamp is in the first 48 bytes of the message
120 126
 /*
121 127
  The following defines how often the system clock gets synchronized
122 128
  to network time.
123 129
 */
124
-#define NET_SYNCH_INTERVAL 43200 //number in seconds
130
+#define NET_SYNCH_INTERVAL 21600 //number in seconds - 4 times a day
131
+/*
132
+ Number of retries if first time server request fails.
133
+*/
134
+#define TIME_SERVER_REQUEST_RETRIES 3
125 135
 /*
126 136
  The following defines the size of the buffer space required for the
127 137
  serial data string from the Mighty Ohm Geiger counter.  The serial
... ...
@@ -179,8 +189,8 @@ SoftwareSerial MightyOhmTxOut(5, 6);
179 189
  time, and next synchronization time.
180 190
 */
181 191
 char mightOhmData[MIGHTYOHM_DATA_STRING_LENGTH + 1];
182
-unsigned long lastSerialUpdateTime;
183
-time_t nextClockSynchTime;
192
+unsigned long nextSerialUpdateTime = 0;
193
+time_t nextClockSynchTime = -1;
184 194
 /*
185 195
  Create global variables to store the verbose mode state (ON or OFF)
186 196
  and the IP address mode state (static or DHCP).
... ...
@@ -189,10 +199,10 @@ boolean bVerbose;
189 199
 boolean bUseStaticIP;
190 200
 /*
191 201
  Create and initialize global arrays to hold the current IP address
192
- and the NTP server IP address.
202
+ and the NTP server address.
193 203
 */
194 204
 byte ipAddr[4];
195
-byte ntpIpAddr[4];
205
+char timeServer[32];
196 206
 
197 207
 /*** SYSTEM STARTUP  ***/
198 208
 
... ...
@@ -215,11 +225,13 @@ void setup()
215 225
    Start up the Ethernet interface using either a static or
216 226
    DHCP supplied address (depending on stored system configuration).
217 227
   */
218
-  if(bUseStaticIP)
228
+  if (bUseStaticIP)
219 229
   {
220 230
     Ethernet.begin(mac, ipAddr);
221
-  } else {
222
-    if ( Ethernet.begin(mac) == 0 )
231
+  }
232
+  else
233
+  {
234
+    if (Ethernet.begin(mac) == 0)
223 235
     {
224 236
       /* DHCP not responding so use APIPA address */
225 237
       parseIpAddress(ipAddr, DEFAULT_APIPA_IP_ADDRESS);
... ...
@@ -245,12 +257,6 @@ void setup()
245 257
     Open serial communications to the MightyOhm device.
246 258
   */  
247 259
   MightyOhmTxOut.begin(9600);
248
-   /*
249
-    Initialize initial time for sending out the hearbeat string. Normally
250
-    the system clock will be at approx 3200 msec at this point. So allow
251
-    some additional time for data to accumulate in MightyOhm data buffer.
252
-  */
253
-  lastSerialUpdateTime = -1;
254 260
   /*
255 261
    Initialize MightyOhm data string to empty.
256 262
   */
... ...
@@ -261,17 +267,13 @@ void setup()
261 267
 /*** MAIN LOOP ***/
262 268
 
263 269
 void loop() {
264
-  long currentTime;
265
-
266
-  currentTime = millis();
267
-  
268 270
   /*
269 271
    Check for user keyboard 'c' pressed.  This character switches
270 272
    to command mode.
271 273
   */   
272
-  if ( Serial.available() ) {
274
+  if (Serial.available()) {
273 275
     // get incoming byte
274
-    if(Serial.read() == 'c') {
276
+    if (Serial.read() == 'c') {
275 277
       commandMode();
276 278
     }
277 279
   }
... ...
@@ -280,8 +282,8 @@ void loop() {
280 282
     Poll serial input buffer from MightyOhm for new data and 
281 283
     process received bytes to form a complete data string.
282 284
   */
283
-  while ( MightyOhmTxOut.available() ) {
284
-    processRxByte( MightyOhmTxOut.read() );
285
+  while (MightyOhmTxOut.available()) {
286
+    processRxByte(MightyOhmTxOut.read());
285 287
   }
286 288
   
287 289
   /*
... ...
@@ -289,9 +291,12 @@ void loop() {
289 291
     serial port at regular intervals.
290 292
   */
291 293
   if (bVerbose) {
292
-    if (abs(millis() - lastSerialUpdateTime) > SERIAL_UPDATE_INTERVAL) {
293
-      lastSerialUpdateTime = millis();
294
-      Serial.println( mightOhmData );
294
+    if (millis() > nextSerialUpdateTime) {
295
+      Serial.println(mightOhmData);
296
+      /* 
297
+       Set the time for the next serial update to occur.
298
+      */
299
+      nextSerialUpdateTime = millis() + SERIAL_UPDATE_INTERVAL;
295 300
     }
296 301
   }
297 302
   
... ...
@@ -299,8 +304,13 @@ void loop() {
299 304
    Periodically synchronize local system clock to time
300 305
    provided by NTP time server.
301 306
   */
302
-  if ( now() > nextClockSynchTime ) {
307
+  if (now() > nextClockSynchTime) {
303 308
     synchronizeSystemClock();
309
+    /* 
310
+     Set the time for the next network NTP
311
+     time synchronization to occur.
312
+    */
313
+    nextClockSynchTime = now() + NET_SYNCH_INTERVAL;
304 314
   }
305 315
   
306 316
   /*
... ...
@@ -322,24 +332,24 @@ void synchronizeSystemClock()
322 332
   byte count;
323 333
   
324 334
   Serial.println(F("Synchronizing with network time server..."));
325
-    
326
-  for(count = 0; count < 3; count++)  // Attempt to synchronize 3 times
335
+
336
+  count = 0;
337
+  while (1)  // Attempt to synchronize 3 times
327 338
   {
328
-    if(syncToNetworkTime() == 1)
329
-    {
339
+    if (syncToNetworkTime() == 1) {
330 340
       //  Synchronization successful
331 341
       break;
332 342
     }
333
-    delay(1000);
334
-  } /* end for */ 
335
-  if(count == 3) {
336
-    Serial.println(F("synch failed"));
343
+    if (count == TIME_SERVER_REQUEST_RETRIES) {
344
+      Serial.print(F("synch failed: "));
345
+      break;
346
+    }
347
+    count++;
348
+    delay(2000);
349
+  }
350
+  if (count > 0) {
351
+    Serial.print(count);Serial.println(F(" retries"));
337 352
   }
338
-  /* 
339
-   Set the time for the next network NTP
340
-   time synchronization to occur.
341
-  */
342
-  nextClockSynchTime = now() + NET_SYNCH_INTERVAL;
343 353
   return;
344 354
 }
345 355
 
... ...
@@ -477,8 +487,8 @@ void transmitWebPage(EthernetClient client) {
477 487
                  "p {font: 16px arial, sans-serif;}"
478 488
                  "h2 {font: 24px arial, sans-serif;}</style>" \
479 489
                  "</head><body><h2>Radiation Monitor</h2>" \
480
-                 "<p><a href=\"http://intravisions.com/radmon/\">" \
481
-                 "<i>intravisions.com/radmon</i></a></p>" \
490
+                 "<p><a href=\"http://intravisions.com/\">" \
491
+                 "<i>IntraVisions.com</i></a></p>" \
482 492
                  "<hr>"));
483 493
   /* Data Items */             
484 494
   client.print(F("<pre>UTC &#9;"));
... ...
@@ -541,10 +551,7 @@ void transmitRawData(EthernetClient client) {
541 551
 void transmitErrorPage(EthernetClient client) {
542 552
   client.print(F("<!DOCTYPE HTML>" \
543 553
                  "<html><head><title>Radiation Monitor</title></head>"  \
544
-                 "<body><h2>Invalid Url</h2>"  \
545
-                 "<p>You have requested a service at an unknown " \
546
-                 "url.</p><p>If you think you made this request in error, " \
547
-                 "please disconnect and try your request again.</p>" \
554
+                 "<body><h2>404 Not Found</h2>"  \
548 555
                  "</body></html>"
549 556
                  ));
550 557
 }
... ...
@@ -632,12 +639,12 @@ int syncToNetworkTime()
632 639
    Send an NTP packet to the time server and allow for network lag
633 640
    before checking if a reply is available.
634 641
   */
635
-  sendNTPpacket(packetBuffer);
636
-  delay(2000);  // allow 2000 milli-seconds for network lag
637
-
642
+  sendNTPpacket(timeServer, packetBuffer);
638 643
   /*
639 644
    Wait for response from NTP time server.
640 645
   */
646
+  delay(1000);  // allow 1000 milli-seconds for network lag
647
+
641 648
   if ( Udp.parsePacket() )
642 649
   {  
643 650
     /*
... ...
@@ -676,7 +683,7 @@ int syncToNetworkTime()
676 683
 /*
677 684
   Send an NTP request to the NTP time server.
678 685
 */
679
-void sendNTPpacket( byte* packetBuffer )
686
+void sendNTPpacket( char * serverAddress, byte * packetBuffer )
680 687
 {
681 688
   /*
682 689
    Set all bytes in the buffer to 0.
... ...
@@ -700,7 +707,7 @@ void sendNTPpacket( byte* packetBuffer )
700 707
    All NTP fields have been given values, so now
701 708
    send a packet requesting a timestamp.
702 709
   */ 		
703
-  Udp.beginPacket( ntpIpAddr, 123 ); //NTP requests are to port 123
710
+  Udp.beginPacket( serverAddress, 123 ); //NTP requests are to port 123
704 711
   Udp.write( packetBuffer, NTP_PACKET_SIZE );
705 712
   Udp.endPacket();
706 713
   return;
... ...
@@ -749,7 +756,7 @@ void commandMode()
749 756
         setIP();
750 757
         break;
751 758
       case '3':
752
-        setNTPIP();
759
+        setNTPServer();
753 760
         break;
754 761
       case '4':
755 762
         toggleVerbose();
... ...
@@ -765,6 +772,7 @@ void commandMode()
765 772
          server or to initialize the Ethernet interface
766 773
          with a static IP address.
767 774
         */
775
+        delay(100);
768 776
         software_Reset();
769 777
         return;
770 778
       default:
... ...
@@ -801,9 +809,8 @@ void displaySettings()
801 809
   Serial.println(sBuf);
802 810
   
803 811
   // Display NTP server IP address
804
-  sprintf(sBuf, "%d.%d.%d.%d", ntpIpAddr[0], ntpIpAddr[1], ntpIpAddr[2], ntpIpAddr[3]);
805 812
   Serial.print(F("NTP server: ")); 
806
-  Serial.println(sBuf);
813
+  Serial.println(timeServer);
807 814
 
808 815
   // Display verbose mode setting
809 816
   printVerboseMode();
... ...
@@ -842,21 +849,20 @@ void setIP()
842 849
  carriage return as the first character, then use the
843 850
  default IP address for the NTP server.
844 851
 */
845
-void setNTPIP()
852
+void setNTPServer()
846 853
 {
847
-  char sBuf[16];
854
+  char sBuf[32];
848 855
   
849 856
   Serial.print(F("enter IP (<CR> for default): "));
850
-  getSerialLine(sBuf, 16);
857
+  getSerialLine(sBuf, 32);
851 858
   
852 859
   if (strlen(sBuf) == 0)
853 860
   {
854
-    strcpy(sBuf, DEFAULT_NTP_SERVER_IP_ADDR);
855
-    parseIpAddress(ntpIpAddr, sBuf);
861
+    strcpy(timeServer, DEFAULT_NTP_SERVER_ADDR);
856 862
   }
857 863
   else
858 864
   {
859
-    parseIpAddress(ntpIpAddr, sBuf);
865
+    strcpy(timeServer, sBuf);
860 866
   }
861 867
   Serial.println();
862 868
   return;
... ...
@@ -991,13 +997,20 @@ char* getSerialLine(char* sBuffer, int bufferLength)
991 997
 void writeSettingsToEEPROM()
992 998
 {
993 999
   byte ix;
1000
+  char c;
994 1001
   for (ix = 0; ix < 4; ix++)
995 1002
   {
996 1003
     EEPROM.write(ix, ipAddr[ix]);
997
-    EEPROM.write(ix + 4, ntpIpAddr[ix]);
998 1004
   }
999
-  EEPROM.write(8, bVerbose);
1000
-  EEPROM.write(9, bUseStaticIP);
1005
+  EEPROM.write(4, bVerbose);
1006
+  EEPROM.write(5, bUseStaticIP);
1007
+  ix = 0;
1008
+  while(1) {
1009
+    c = timeServer[ix];
1010
+    EEPROM.write(6 + ix, c);
1011
+    if (c == 0 || ix > 31) break;
1012
+    ix++;
1013
+  }
1001 1014
   return;
1002 1015
 }
1003 1016
 
... ...
@@ -1010,13 +1023,20 @@ void writeSettingsToEEPROM()
1010 1023
 void readSettingsFromEEPROM()
1011 1024
 {
1012 1025
   byte ix;
1026
+  char c;
1013 1027
   for (ix = 0; ix < 4; ix++)
1014 1028
   {
1015 1029
     ipAddr[ix] = EEPROM.read(ix);
1016
-    ntpIpAddr[ix] = EEPROM.read(ix + 4);
1017 1030
   }
1018
-  bVerbose = EEPROM.read(8);
1019
-  bUseStaticIP = EEPROM.read(9);
1031
+  bVerbose = EEPROM.read(4);
1032
+  bUseStaticIP = EEPROM.read(5);
1033
+  ix = 0;
1034
+  while(1) {
1035
+    c = EEPROM.read(6 + ix);
1036
+    timeServer[ix] = c;
1037
+    if (c == 0 || ix > 31) break;
1038
+    ix++;
1039
+  }
1020 1040
   return;
1021 1041
 }
1022 1042
 
... ...
@@ -1046,4 +1066,3 @@ void software_Reset()
1046 1066
   asm volatile ("  jmp 0");
1047 1067
   return; 
1048 1068
 }  
1049
-