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

Committer:
thedude35
Date:
Sun Sep 17 22:00:00 2017 +0000
Branch:
USB-logging
Revision:
8:e16130691c3b
Parent:
7:e9d4a4972e50
Syntax cleanup

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedude35 2:5c9125d3ddae 1 //An embedded server sending sensor data to a client side process running on a PC for logging and analysis
thedude35 2:5c9125d3ddae 2 //TODO: Add in error handling and timeouts to deal with dropped connections and compile flags for testing
thedude35 2:5c9125d3ddae 3
thedude35 2:5c9125d3ddae 4 #include "data_logger.h"
thedude35 2:5c9125d3ddae 5
thedude35 2:5c9125d3ddae 6 EthernetInterface eth;
thedude35 2:5c9125d3ddae 7 NTPClient ntp;
thedude35 2:5c9125d3ddae 8 TimeInterface time_b;
thedude35 2:5c9125d3ddae 9 Timer t;
thedude35 2:5c9125d3ddae 10 time_t timestamp; //Built-in time class instance
thedude35 2:5c9125d3ddae 11 TCPSocketServer server;
thedude35 2:5c9125d3ddae 12 TCPSocketConnection client;
thedude35 2:5c9125d3ddae 13 EventQueue queue1;
thedude35 2:5c9125d3ddae 14 EventQueue queue2;
thedude35 2:5c9125d3ddae 15 Thread t1;
thedude35 2:5c9125d3ddae 16 Thread t2;
thedude35 2:5c9125d3ddae 17 Mutex m;
thedude35 2:5c9125d3ddae 18
thedude35 2:5c9125d3ddae 19 //setup
thedude35 2:5c9125d3ddae 20 DigitalOut led1(LED1);
thedude35 3:221997836268 21 AnalogIn strainIn(A0);
thedude35 2:5c9125d3ddae 22 InterruptIn sig(p5);
thedude35 2:5c9125d3ddae 23
thedude35 2:5c9125d3ddae 24 //RtosTimer Timer(send_payload,osTimerPeriodic);
thedude35 2:5c9125d3ddae 25
thedude35 2:5c9125d3ddae 26 // main() runs in its own thread in the OS
thedude35 2:5c9125d3ddae 27 // (note the calls to Thread::wait below for delays)
thedude35 2:5c9125d3ddae 28
thedude35 2:5c9125d3ddae 29 int main(void)
thedude35 2:5c9125d3ddae 30 {
thedude35 2:5c9125d3ddae 31 eth.init(); //Use DHCP
thedude35 7:e9d4a4972e50 32 eth.setName("DL-test"); //set a hostname
thedude35 7:e9d4a4972e50 33 eth_cxn_status = eth.connect(); //Attempt an ethernet connection and save the status, add retries here in the future
thedude35 7:e9d4a4972e50 34 if(eth_cxn_status == 0) {
thedude35 7:e9d4a4972e50 35 //Bring up the ethernet interface
thedude35 7:e9d4a4972e50 36 printf("Detected Ethernet connection, entering network logging mode \n Server IP Address is %s\n", eth.getIPAddress());
thedude35 7:e9d4a4972e50 37 server.bind(SERVER_PORT);
thedude35 7:e9d4a4972e50 38 server.listen(); //Wait for the client to connect
thedude35 7:e9d4a4972e50 39 server.accept(client);
thedude35 7:e9d4a4972e50 40 printf("Connection from: %s\n", client.get_address());
thedude35 7:e9d4a4972e50 41 if (ntp.setTime("0.pool.ntp.org") == 0) //If time retrieval is successful
thedude35 7:e9d4a4972e50 42 {
thedude35 7:e9d4a4972e50 43 printf("Set time successfully\r\n");
thedude35 7:e9d4a4972e50 44
thedude35 7:e9d4a4972e50 45 }
thedude35 7:e9d4a4972e50 46 else
thedude35 7:e9d4a4972e50 47 {
thedude35 7:e9d4a4972e50 48 printf("Unable to set time, error\r\n");
thedude35 7:e9d4a4972e50 49 }
thedude35 8:e16130691c3b 50 }
thedude35 7:e9d4a4972e50 51 else {
thedude35 7:e9d4a4972e50 52 printf("Entering USB logging mode");
thedude35 8:e16130691c3b 53 }
thedude35 2:5c9125d3ddae 54
thedude35 2:5c9125d3ddae 55 timestamp = time(NULL);
thedude35 3:221997836268 56 queue1.call_every(RATE,&idle_report);
thedude35 2:5c9125d3ddae 57 t1.start(callback(&queue1, &EventQueue::dispatch_forever)); //Start the thread for periodic basline value reporting
thedude35 2:5c9125d3ddae 58 sig.rise(queue2.event(&cycle_time_isr_rise));
thedude35 2:5c9125d3ddae 59 sig.fall(queue2.event(&cycle_time_isr_fall));
thedude35 2:5c9125d3ddae 60 t2.start(callback(&queue2, &EventQueue::dispatch_forever)); //Start the cycle time and peak value calculation thread
thedude35 2:5c9125d3ddae 61
thedude35 2:5c9125d3ddae 62 }
thedude35 2:5c9125d3ddae 63
thedude35 2:5c9125d3ddae 64 void cycle_time_isr_rise(void) {
thedude35 7:e9d4a4972e50 65 t.start(); //Start the timer
thedude35 7:e9d4a4972e50 66 m.lock(); //Lock the mutex to prevent the baseline reporting thread from writing to the application buffer
thedude35 2:5c9125d3ddae 67 /*for(i=0;i<1023;i++) {
thedude35 2:5c9125d3ddae 68 samples[i] = pressureIn.read();
thedude35 2:5c9125d3ddae 69 //add a mean and peak pressure calculation here and you may want to slow down the sample rate
thedude35 2:5c9125d3ddae 70 i++;
thedude35 2:5c9125d3ddae 71 }*/
thedude35 2:5c9125d3ddae 72 //p_press = pressIn.read();
thedude35 7:e9d4a4972e50 73 p_strain = strainCalc(); //Calculate strain while the machine is crushing
thedude35 2:5c9125d3ddae 74 }
thedude35 2:5c9125d3ddae 75
thedude35 2:5c9125d3ddae 76 void cycle_time_isr_fall(void) {
thedude35 2:5c9125d3ddae 77 t.stop(); //Stop the timer
thedude35 2:5c9125d3ddae 78 cycle_time = t.read();
thedude35 2:5c9125d3ddae 79 t.reset(); //reset the timer
thedude35 8:e16130691c3b 80 if(eth_cxn_status == 0) {
thedude35 7:e9d4a4972e50 81 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);
thedude35 8:e16130691c3b 82 }
thedude35 7:e9d4a4972e50 83 else {
thedude35 7:e9d4a4972e50 84 printf("Cycle time is: %f \n Strain detected during cycle is: %f% \r", cycle_time, p_strain);
thedude35 8:e16130691c3b 85 }
thedude35 2:5c9125d3ddae 86 sprintf(appbuffer,"\0"); //Nullify the string
thedude35 2:5c9125d3ddae 87 m.unlock(); //Unlock the mutex
thedude35 7:e9d4a4972e50 88 };
thedude35 2:5c9125d3ddae 89
thedude35 3:221997836268 90 void idle_report(void) {
thedude35 2:5c9125d3ddae 91 m.lock(); //Attempt to lock the mutex here
thedude35 3:221997836268 92 id_strain = strainCalc(); //Calculate strain while the machine is idle
thedude35 8:e16130691c3b 93 if (eth_cxn_status == 0) { //Log data based on the available connection
thedude35 7:e9d4a4972e50 94 sprintf(appbuffer,"<payload><time>%s</time><id_strain>%f</id_strain></payload>",time_b.ctime(&timestamp), id_strain);
thedude35 7:e9d4a4972e50 95 }
thedude35 7:e9d4a4972e50 96 else {
thedude35 7:e9d4a4972e50 97 printf("Strain while machine is idle: %f%",id_strain );
thedude35 8:e16130691c3b 98 }
thedude35 7:e9d4a4972e50 99
thedude35 2:5c9125d3ddae 100 sprintf(appbuffer,"\0"); //Nullify the buffer
thedude35 2:5c9125d3ddae 101 m.unlock(); //Unlock the mutex
thedude35 2:5c9125d3ddae 102 }
thedude35 3:221997836268 103
thedude35 3:221997836268 104 float strainCalc(void) {
thedude35 3:221997836268 105 float deltaR = strainIn/CURRENT; //Calculate Delta R
thedude35 3:221997836268 106 float strain = (1/GAUGE_FACTOR)*(deltaR/NOMINAL_R); //Calculate strain, see equation reference in docs
thedude35 3:221997836268 107 return strain;
thedude35 3:221997836268 108 }
thedude35 2:5c9125d3ddae 109