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
data_logger.cpp@4:a6ff97cfb57a, 2017-09-16 (annotated)
- Committer:
- thedude35
- Date:
- Sat Sep 16 22:15:56 2017 -0400
- Branch:
- USB-logging
- Revision:
- 4:a6ff97cfb57a
- Parent:
- 3:221997836268
- Child:
- 7:e9d4a4972e50
Removed erroneous lines
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:5c9125d3ddae | 32 | eth.setName("DL-test"); //set a hostname |
thedude35 | 2:5c9125d3ddae | 33 | eth.connect(); //Bring up the ethernet interface |
thedude35 | 2:5c9125d3ddae | 34 | printf("\nServer IP Address is %s\n", eth.getIPAddress()); |
thedude35 | 2:5c9125d3ddae | 35 | server.bind(SERVER_PORT); |
thedude35 | 2:5c9125d3ddae | 36 | server.listen(); //Wait for the client to connect |
thedude35 | 2:5c9125d3ddae | 37 | server.accept(client); |
thedude35 | 2:5c9125d3ddae | 38 | printf("Connection from: %s\n", client.get_address()); |
thedude35 | 2:5c9125d3ddae | 39 | if (ntp.setTime("0.pool.ntp.org") == 0) //If time retrieval is successful |
thedude35 | 2:5c9125d3ddae | 40 | { |
thedude35 | 2:5c9125d3ddae | 41 | printf("Set time successfully\r\n"); |
thedude35 | 2:5c9125d3ddae | 42 | |
thedude35 | 2:5c9125d3ddae | 43 | } |
thedude35 | 2:5c9125d3ddae | 44 | else |
thedude35 | 2:5c9125d3ddae | 45 | { |
thedude35 | 2:5c9125d3ddae | 46 | printf("Error\r\n"); |
thedude35 | 2:5c9125d3ddae | 47 | } |
thedude35 | 2:5c9125d3ddae | 48 | printf("board is up"); |
thedude35 | 2:5c9125d3ddae | 49 | |
thedude35 | 2:5c9125d3ddae | 50 | timestamp = time(NULL); |
thedude35 | 3:221997836268 | 51 | queue1.call_every(RATE,&idle_report); |
thedude35 | 2:5c9125d3ddae | 52 | t1.start(callback(&queue1, &EventQueue::dispatch_forever)); //Start the thread for periodic basline value reporting |
thedude35 | 2:5c9125d3ddae | 53 | sig.rise(queue2.event(&cycle_time_isr_rise)); |
thedude35 | 2:5c9125d3ddae | 54 | sig.fall(queue2.event(&cycle_time_isr_fall)); |
thedude35 | 2:5c9125d3ddae | 55 | t2.start(callback(&queue2, &EventQueue::dispatch_forever)); //Start the cycle time and peak value calculation thread |
thedude35 | 2:5c9125d3ddae | 56 | |
thedude35 | 2:5c9125d3ddae | 57 | } |
thedude35 | 2:5c9125d3ddae | 58 | |
thedude35 | 2:5c9125d3ddae | 59 | void cycle_time_isr_rise(void) { |
thedude35 | 2:5c9125d3ddae | 60 | t.start(); //Start the timer |
thedude35 | 2:5c9125d3ddae | 61 | m.lock(); //Lock the mutex to prevent the baseline reporting thread from writing to the application buffer |
thedude35 | 2:5c9125d3ddae | 62 | /*for(i=0;i<1023;i++) { |
thedude35 | 2:5c9125d3ddae | 63 | samples[i] = pressureIn.read(); |
thedude35 | 2:5c9125d3ddae | 64 | //add a mean and peak pressure calculation here and you may want to slow down the sample rate |
thedude35 | 2:5c9125d3ddae | 65 | i++; |
thedude35 | 2:5c9125d3ddae | 66 | }*/ |
thedude35 | 2:5c9125d3ddae | 67 | //p_press = pressIn.read(); |
thedude35 | 3:221997836268 | 68 | p_strain = strainCalc(); //Calculate strain while the machine is crushing |
thedude35 | 2:5c9125d3ddae | 69 | } |
thedude35 | 2:5c9125d3ddae | 70 | |
thedude35 | 2:5c9125d3ddae | 71 | void cycle_time_isr_fall(void) { |
thedude35 | 2:5c9125d3ddae | 72 | t.stop(); //Stop the timer |
thedude35 | 2:5c9125d3ddae | 73 | cycle_time = t.read(); |
thedude35 | 2:5c9125d3ddae | 74 | t.reset(); //reset the timer |
thedude35 | 3:221997836268 | 75 | sprintf(appbuffer,"<payload><time>%s</time><cy_time>%f</cy_time><p_strain>%f</p_strain></payload>",time_b.ctime(×tamp), cycle_time, p_strain); |
thedude35 | 2:5c9125d3ddae | 76 | printf(appbuffer); |
thedude35 | 2:5c9125d3ddae | 77 | sprintf(appbuffer,"\0"); //Nullify the string |
thedude35 | 2:5c9125d3ddae | 78 | m.unlock(); //Unlock the mutex |
thedude35 | 2:5c9125d3ddae | 79 | } |
thedude35 | 2:5c9125d3ddae | 80 | |
thedude35 | 3:221997836268 | 81 | void idle_report(void) { |
thedude35 | 2:5c9125d3ddae | 82 | m.lock(); //Attempt to lock the mutex here |
thedude35 | 3:221997836268 | 83 | id_strain = strainCalc(); //Calculate strain while the machine is idle |
thedude35 | 3:221997836268 | 84 | sprintf(appbuffer,"<payload><time>%s</time><id_strain>%f</id_strain></payload>",time_b.ctime(×tamp), id_strain); |
thedude35 | 2:5c9125d3ddae | 85 | printf(appbuffer); |
thedude35 | 2:5c9125d3ddae | 86 | sprintf(appbuffer,"\0"); //Nullify the buffer |
thedude35 | 2:5c9125d3ddae | 87 | |
thedude35 | 2:5c9125d3ddae | 88 | m.unlock(); //Unlock the mutex |
thedude35 | 2:5c9125d3ddae | 89 | } |
thedude35 | 3:221997836268 | 90 | |
thedude35 | 3:221997836268 | 91 | float strainCalc(void) { |
thedude35 | 3:221997836268 | 92 | float deltaR = strainIn/CURRENT; //Calculate Delta R |
thedude35 | 3:221997836268 | 93 | float strain = (1/GAUGE_FACTOR)*(deltaR/NOMINAL_R); //Calculate strain, see equation reference in docs |
thedude35 | 3:221997836268 | 94 | |
thedude35 | 3:221997836268 | 95 | return strain; |
thedude35 | 3:221997836268 | 96 | } |
thedude35 | 2:5c9125d3ddae | 97 |