Sends out data over serial in such a format that it can be collected in packets by an esp8266.

Dependencies:   MCP32082Two mbed

Committer:
mmellor
Date:
Mon Jul 25 14:49:45 2016 +0000
Revision:
0:09958db7e6fb
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

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mmellor 0:09958db7e6fb 1 #include "mbed.h"
mmellor 0:09958db7e6fb 2 #include "mcp3208.h"
mmellor 0:09958db7e6fb 3
mmellor 0:09958db7e6fb 4 //Packet Strategy One for UDP
mmellor 0:09958db7e6fb 5 //Send out data at 1ms as normal
mmellor 0:09958db7e6fb 6 //after specified numReads send out data with
mmellor 0:09958db7e6fb 7 //terminated character
mmellor 0:09958db7e6fb 8 //On UDP/ESP8266 side read until you see the termination character
mmellor 0:09958db7e6fb 9
mmellor 0:09958db7e6fb 10 int readCounter = 1;
mmellor 0:09958db7e6fb 11 int numReads = 45; //This value is numDataSetsInPacket in matlab and arduino code
mmellor 0:09958db7e6fb 12 int counterNum = 0;
mmellor 0:09958db7e6fb 13
mmellor 0:09958db7e6fb 14 MCP3208 input1(dp2, dp1, dp6, dp9);
mmellor 0:09958db7e6fb 15
mmellor 0:09958db7e6fb 16 Serial pc(dp16,dp15);
mmellor 0:09958db7e6fb 17
mmellor 0:09958db7e6fb 18 Ticker datalog; //Create the timer object
mmellor 0:09958db7e6fb 19
mmellor 0:09958db7e6fb 20 char datastr0[5];
mmellor 0:09958db7e6fb 21 char datastr1[5];
mmellor 0:09958db7e6fb 22 char datastr2[5];
mmellor 0:09958db7e6fb 23 char datastr3[5];
mmellor 0:09958db7e6fb 24 char datastr4[5];
mmellor 0:09958db7e6fb 25 char datastr5[5];
mmellor 0:09958db7e6fb 26
mmellor 0:09958db7e6fb 27
mmellor 0:09958db7e6fb 28 // Credit: Erik Olieman
mmellor 0:09958db7e6fb 29 void intToString(char *buffer, int value)
mmellor 0:09958db7e6fb 30 {
mmellor 0:09958db7e6fb 31 int temp;
mmellor 0:09958db7e6fb 32 temp = value / 1000;
mmellor 0:09958db7e6fb 33 buffer[0] = temp + '0';
mmellor 0:09958db7e6fb 34 value = value - temp * 1000;
mmellor 0:09958db7e6fb 35
mmellor 0:09958db7e6fb 36 temp = value / 100;
mmellor 0:09958db7e6fb 37 buffer[1] = temp + '0';
mmellor 0:09958db7e6fb 38 value = value - temp * 100;
mmellor 0:09958db7e6fb 39
mmellor 0:09958db7e6fb 40 temp = value / 10;
mmellor 0:09958db7e6fb 41 buffer[2] = temp + '0';
mmellor 0:09958db7e6fb 42 value = value - temp * 10;
mmellor 0:09958db7e6fb 43
mmellor 0:09958db7e6fb 44 temp = value / 1;
mmellor 0:09958db7e6fb 45 buffer[3] = temp + '0';
mmellor 0:09958db7e6fb 46 value = value - temp * 1;
mmellor 0:09958db7e6fb 47
mmellor 0:09958db7e6fb 48 buffer[4] = '\0';
mmellor 0:09958db7e6fb 49 }
mmellor 0:09958db7e6fb 50
mmellor 0:09958db7e6fb 51 bool tickerActivated = false;
mmellor 0:09958db7e6fb 52
mmellor 0:09958db7e6fb 53 void log_data(){
mmellor 0:09958db7e6fb 54 tickerActivated = true;
mmellor 0:09958db7e6fb 55 }
mmellor 0:09958db7e6fb 56
mmellor 0:09958db7e6fb 57 int main(){
mmellor 0:09958db7e6fb 58 pc.baud(921600); //Set baud rate
mmellor 0:09958db7e6fb 59 pc.printf("Working!!\n\r");
mmellor 0:09958db7e6fb 60
mmellor 0:09958db7e6fb 61 datalog.attach_us(&log_data,1000); // 1000us = 1ms
mmellor 0:09958db7e6fb 62
mmellor 0:09958db7e6fb 63 while(1) {
mmellor 0:09958db7e6fb 64 intToString(datastr0,input1.binary(0));
mmellor 0:09958db7e6fb 65 intToString(datastr1,input1.binary(1));
mmellor 0:09958db7e6fb 66 intToString(datastr2,input1.binary(2));
mmellor 0:09958db7e6fb 67 intToString(datastr3,input1.binary(3));
mmellor 0:09958db7e6fb 68 intToString(datastr4,input1.binary(4));
mmellor 0:09958db7e6fb 69 intToString(datastr5,input1.binary(5));
mmellor 0:09958db7e6fb 70
mmellor 0:09958db7e6fb 71 if(tickerActivated == true) { //This is true every 1ms
mmellor 0:09958db7e6fb 72 tickerActivated = false;
mmellor 0:09958db7e6fb 73 if(readCounter == numReads){
mmellor 0:09958db7e6fb 74 readCounter = 1;
mmellor 0:09958db7e6fb 75 counterNum = counterNum + 1;
mmellor 0:09958db7e6fb 76 if(counterNum >= 9999) counterNum = 0;
mmellor 0:09958db7e6fb 77 pc.printf("%s,%s,%s,%s,%s,%s\n\r",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5);
mmellor 0:09958db7e6fb 78 }
mmellor 0:09958db7e6fb 79 else{
mmellor 0:09958db7e6fb 80 readCounter++;
mmellor 0:09958db7e6fb 81 counterNum = counterNum + 1;
mmellor 0:09958db7e6fb 82 if(counterNum >= 9999) counterNum = 0;
mmellor 0:09958db7e6fb 83
mmellor 0:09958db7e6fb 84 pc.printf("%s,%s,%s,%s,%s,%s,",datastr0,datastr1,datastr2,datastr3,datastr4,datastr5);
mmellor 0:09958db7e6fb 85 }
mmellor 0:09958db7e6fb 86 }
mmellor 0:09958db7e6fb 87 }
mmellor 0:09958db7e6fb 88 }