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
- Committer:
- thedude35
- Date:
- 2017-09-16
- Branch:
- USB-logging
- Revision:
- 4:a6ff97cfb57a
- Parent:
- 3:221997836268
- Child:
- 7:e9d4a4972e50
File content as of revision 4:a6ff97cfb57a:
//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; EventQueue queue1; EventQueue queue2; Thread t1; Thread t2; Mutex m; //setup DigitalOut led1(LED1); AnalogIn strainIn(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,&idle_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 } 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(); 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(×tamp), cycle_time, p_strain); printf(appbuffer); 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(×tamp), id_strain); printf(appbuffer); 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; }