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
Diff: data_logger.cpp
- Revision:
- 2:5c9125d3ddae
- Child:
- 3:221997836268
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_logger.cpp Fri Jul 21 01:56:38 2017 +0000 @@ -0,0 +1,97 @@ +//An embedded server sending sensor data to a client side process running on a PC for logging and analysis +//TODO: Add in error handling and timeouts to deal with dropped connections and compile flags for testing + +#include "data_logger.h" + +EthernetInterface eth; +NTPClient ntp; +TimeInterface time_b; +Timer t; +time_t timestamp; //Built-in time class instance +TCPSocketServer server; +TCPSocketConnection client; +RtosTimer periodic_report(&pressure_report), *ppr; +EventQueue queue1; +EventQueue queue2; +Thread t1; +Thread t2; +Mutex m; + +//setup +DigitalOut led1(LED1); +AnalogIn pressIn(A0); +InterruptIn sig(p5); + +//RtosTimer Timer(send_payload,osTimerPeriodic); + +// main() runs in its own thread in the OS +// (note the calls to Thread::wait below for delays) + +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"); + } + printf("board is up"); + + timestamp = time(NULL); + queue1.call_every(RATE,&pressure_report); + t1.start(callback(&queue1, &EventQueue::dispatch_forever)); //Start the thread for periodic basline value reporting + sig.rise(queue2.event(&cycle_time_isr_rise)); + sig.fall(queue2.event(&cycle_time_isr_fall)); + t2.start(callback(&queue2, &EventQueue::dispatch_forever)); //Start the cycle time and peak value calculation thread + + +//printf("Closing the connection"); +//client.close(); +//eth.disconnect(); + +} + +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 + /*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(); + + } + +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_press>%d</p_press></payload>",time_b.ctime(×tamp), cycle_time, p_press); + printf(appbuffer); + sprintf(appbuffer,"\0"); //Nullify the string + m.unlock(); //Unlock the mutex + } + +void pressure_report(void) { + m.lock(); //Attempt to lock the mutex here + sprintf(appbuffer,"<payload><time>%s</time><id_press>%d</id_press></payload>",time_b.ctime(×tamp), id_press); + printf(appbuffer); + sprintf(appbuffer,"\0"); //Nullify the buffer + + m.unlock(); //Unlock the mutex + } + + +