An embedded server sending sensor information over the network to a remote client side process parsing the data

Dependencies:   EthernetInterface NTPClient TimeInterface WebSocketClient mbed-rtos mbed ST_Events-old

Files at this revision

API Documentation at this revision

Comitter:
thedude35
Date:
Sun Sep 17 17:43:20 2017 -0400
Branch:
USB-logging
Parent:
5:094b0948a2f5
Child:
8:e16130691c3b
Commit message:
Added network connection checking within the reporting functions

Changed in this revision

data_logger.cpp Show annotated file Show diff for this revision Revisions of this file
data_logger.h Show annotated file Show diff for this revision Revisions of this file
--- a/data_logger.cpp	Sun Sep 17 02:32:55 2017 +0000
+++ b/data_logger.cpp	Sun Sep 17 17:43:20 2017 -0400
@@ -29,22 +29,29 @@
 int main(void) 
 {
     eth.init(); //Use DHCP
-    eth.setName("DL-test");         //set a hostname 
-    eth.connect();      //Bring up the ethernet interface
-    printf("\nServer IP Address is %s\n", eth.getIPAddress());
-    server.bind(SERVER_PORT);
-    server.listen();                                                                                          //Wait for the client to connect
-    server.accept(client);
-    printf("Connection from: %s\n", client.get_address());
-    if (ntp.setTime("0.pool.ntp.org") == 0)                                                                  //If time retrieval is successful
-    {
-    printf("Set time successfully\r\n");
-                                                                   
-    }
-    else
-    {
-    printf("Error\r\n");
-    }
+    eth.setName("DL-test");         					//set a hostname
+    eth_cxn_status = eth.connect();						//Attempt an ethernet connection and save the status, add retries here in the future
+    if(eth_cxn_status == 0) {
+    	      //Bring up the ethernet interface
+    	printf("Detected Ethernet connection, entering network logging mode \n Server IP Address is %s\n", eth.getIPAddress());
+    	    server.bind(SERVER_PORT);
+    	    server.listen();                                                                                          //Wait for the client to connect
+    	    server.accept(client);
+    	    printf("Connection from: %s\n", client.get_address());
+    	    if (ntp.setTime("0.pool.ntp.org") == 0)                                                                  //If time retrieval is successful
+    	    {
+    	    printf("Set time successfully\r\n");
+
+    	    }
+    	    else
+    	    {
+    	    printf("Unable to set time, error\r\n");
+    	    }
+    	};
+    else {
+    	 printf("Entering USB logging mode");
+    	 };
+
     printf("board is up");
     
     timestamp = time(NULL);
@@ -57,41 +64,48 @@
 }
 
 void cycle_time_isr_rise(void) { 
-    t.start();                                              //Start the timer
-    m.lock();                                               //Lock the mutex to prevent the baseline reporting thread from writing to the application buffer
+    t.start();                                              											  //Start the timer
+    m.lock();                                               											  //Lock the mutex to prevent the baseline reporting thread from writing to the application buffer
     /*for(i=0;i<1023;i++) {
         samples[i] = pressureIn.read();
         //add a mean and peak pressure calculation here and you may want to slow down the sample rate
         i++;       
         }*/
     //p_press =  pressIn.read();
-    p_strain = strainCalc();                                //Calculate strain while the machine is crushing
+    p_strain = strainCalc();                                											   //Calculate strain while the machine is crushing
     }
 
 void cycle_time_isr_fall(void) {
     t.stop();                                                                                              //Stop the timer
     cycle_time = t.read();
     t.reset();                                                                                             //reset the timer
-    sprintf(appbuffer,"<payload><time>%s</time><cy_time>%f</cy_time><p_strain>%f</p_strain></payload>",time_b.ctime(&timestamp), cycle_time, p_strain);
-    printf(appbuffer);
+    if(eth_cxn_status) == 0 {
+    	sprintf(appbuffer,"<payload><time>%s</time><cy_time>%f</cy_time><p_strain>%f</p_strain></payload>",time_b.ctime(&timestamp), cycle_time, p_strain);
+    };
+    else {
+    	printf("Cycle time is: %f \n Strain detected during cycle is: %f% \r", cycle_time, p_strain);
+    };
     sprintf(appbuffer,"\0");                                                                                //Nullify the string
     m.unlock();                                                                                             //Unlock the mutex
-    }
+    };
 
 void idle_report(void) {
     m.lock();                                                                                               //Attempt to lock the mutex here
     id_strain = strainCalc();                                                                               //Calculate strain while the machine is idle
-    sprintf(appbuffer,"<payload><time>%s</time><id_strain>%f</id_strain></payload>",time_b.ctime(&timestamp), id_strain);
-    printf(appbuffer);       
+    if (eth_cxn_status) == 0 {																				//Log data based on the available connection
+    	sprintf(appbuffer,"<payload><time>%s</time><id_strain>%f</id_strain></payload>",time_b.ctime(&timestamp), id_strain);
+    }
+    else {
+    	printf("Strain while machine is idle: %f%",id_strain );
+    };
+
     sprintf(appbuffer,"\0");                                                                                //Nullify the buffer
-        
     m.unlock();                                                                                             //Unlock the mutex
     }
     
 float strainCalc(void) {
     float deltaR = strainIn/CURRENT;                                                                        //Calculate Delta R
     float strain = (1/GAUGE_FACTOR)*(deltaR/NOMINAL_R);                                                     //Calculate strain, see equation reference in docs
-    
     return strain;
     }
 
--- a/data_logger.h	Sun Sep 17 02:32:55 2017 +0000
+++ b/data_logger.h	Sun Sep 17 17:43:20 2017 -0400
@@ -15,11 +15,12 @@
 int i = 0;
 char appbuffer[105];                    //Application buffer
 int len = 105;                          //Length of the buffer
-float cycle_time;
+float cycle_time;						//Drum crushing cycle time
 int p_press = 0;
 int id_press = 0;
-float p_strain = 0;
-float id_strain = 0;
+float p_strain = 0;						//Peak strain
+float id_strain = 0;					//Idle stain
+int eth_cxn_status;						//Ethernet connection status
 
 
 //int samples[];
@@ -31,7 +32,7 @@
 //ISR on the falling edge of the digital input stopping the cycle timer and restarting the periodic reporting thread
 
 void idle_report(void);
-//sends the XML markup payload while the machine is idle 
+//sends the XML markup payload report while the machine is idle
 
 float strainCalc(void);
 //Calculate the strain from the analog input signal received from the strain gauge