Sends out data over serial in such a format that it can be collected in packets by an esp8266.
Dependencies: MCP32082Two mbed
Revision 0:09958db7e6fb, committed 2016-07-25
- Comitter:
- mmellor
- Date:
- Mon Jul 25 14:49:45 2016 +0000
- Commit message:
- Finalish code for use with UDP packet creation on the esp8266 side. This code sends out the data every 1ms but every X sets of data it adds a termination character to the end of the sensor data so that the esp8266 can tell that packet is ended
Changed in this revision
diff -r 000000000000 -r 09958db7e6fb MCP3208.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MCP3208.lib Mon Jul 25 14:49:45 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/mmellor/code/MCP32082Two/#e44491917262
diff -r 000000000000 -r 09958db7e6fb main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Jul 25 14:49:45 2016 +0000 @@ -0,0 +1,88 @@ +#include "mbed.h" +#include "mcp3208.h" + +//Packet Strategy One for UDP +//Send out data at 1ms as normal +//after specified numReads send out data with +//terminated character +//On UDP/ESP8266 side read until you see the termination character + +int readCounter = 1; +int numReads = 45; //This value is numDataSetsInPacket in matlab and arduino code +int counterNum = 0; + +MCP3208 input1(dp2, dp1, dp6, dp9); + +Serial pc(dp16,dp15); + +Ticker datalog; //Create the timer object + +char datastr0[5]; +char datastr1[5]; +char datastr2[5]; +char datastr3[5]; +char datastr4[5]; +char datastr5[5]; + + +// Credit: Erik Olieman +void intToString(char *buffer, int value) +{ + int temp; + temp = value / 1000; + buffer[0] = temp + '0'; + value = value - temp * 1000; + + temp = value / 100; + buffer[1] = temp + '0'; + value = value - temp * 100; + + temp = value / 10; + buffer[2] = temp + '0'; + value = value - temp * 10; + + temp = value / 1; + buffer[3] = temp + '0'; + value = value - temp * 1; + + buffer[4] = '\0'; +} + +bool tickerActivated = false; + +void log_data(){ + tickerActivated = true; +} + +int main(){ + pc.baud(921600); //Set baud rate + pc.printf("Working!!\n\r"); + + datalog.attach_us(&log_data,1000); // 1000us = 1ms + + while(1) { + intToString(datastr0,input1.binary(0)); + intToString(datastr1,input1.binary(1)); + intToString(datastr2,input1.binary(2)); + intToString(datastr3,input1.binary(3)); + intToString(datastr4,input1.binary(4)); + intToString(datastr5,input1.binary(5)); + + if(tickerActivated == true) { //This is true every 1ms + tickerActivated = false; + if(readCounter == numReads){ + readCounter = 1; + counterNum = counterNum + 1; + if(counterNum >= 9999) counterNum = 0; + pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); + } + else{ + readCounter++; + counterNum = counterNum + 1; + if(counterNum >= 9999) counterNum = 0; + + pc.printf("%s,%s,%s,%s,%s,%s,",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5); + } + } + } +}
diff -r 000000000000 -r 09958db7e6fb mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Mon Jul 25 14:49:45 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/f9eeca106725 \ No newline at end of file