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:
Sat Sep 16 21:05:00 2017 +0000
Revision:
3:221997836268
Parent:
2:5c9125d3ddae
Child:
4:a6ff97cfb57a
Updated readme

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 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 //printf("Closing the connection");
thedude35 2:5c9125d3ddae 59 //client.close();
thedude35 2:5c9125d3ddae 60 //eth.disconnect();
thedude35 2:5c9125d3ddae 61
thedude35 2:5c9125d3ddae 62 }
thedude35 2:5c9125d3ddae 63
thedude35 2:5c9125d3ddae 64 void cycle_time_isr_rise(void) {
thedude35 2:5c9125d3ddae 65 t.start(); //Start the timer
thedude35 2:5c9125d3ddae 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 3:221997836268 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 3:221997836268 80 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 2:5c9125d3ddae 81 printf(appbuffer);
thedude35 2:5c9125d3ddae 82 sprintf(appbuffer,"\0"); //Nullify the string
thedude35 2:5c9125d3ddae 83 m.unlock(); //Unlock the mutex
thedude35 2:5c9125d3ddae 84 }
thedude35 2:5c9125d3ddae 85
thedude35 3:221997836268 86 void idle_report(void) {
thedude35 2:5c9125d3ddae 87 m.lock(); //Attempt to lock the mutex here
thedude35 3:221997836268 88 id_strain = strainCalc(); //Calculate strain while the machine is idle
thedude35 3:221997836268 89 sprintf(appbuffer,"<payload><time>%s</time><id_strain>%f</id_strain></payload>",time_b.ctime(&timestamp), id_strain);
thedude35 2:5c9125d3ddae 90 printf(appbuffer);
thedude35 2:5c9125d3ddae 91 sprintf(appbuffer,"\0"); //Nullify the buffer
thedude35 2:5c9125d3ddae 92
thedude35 2:5c9125d3ddae 93 m.unlock(); //Unlock the mutex
thedude35 2:5c9125d3ddae 94 }
thedude35 3:221997836268 95
thedude35 3:221997836268 96 float strainCalc(void) {
thedude35 3:221997836268 97 float deltaR = strainIn/CURRENT; //Calculate Delta R
thedude35 3:221997836268 98 float strain = (1/GAUGE_FACTOR)*(deltaR/NOMINAL_R); //Calculate strain, see equation reference in docs
thedude35 3:221997836268 99
thedude35 3:221997836268 100 return strain;
thedude35 3:221997836268 101 }
thedude35 2:5c9125d3ddae 102