Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Main.cpp@9:1e63726fff9c, 2017-08-14 (annotated)
- Committer:
- Roietronics
- Date:
- Mon Aug 14 19:14:31 2017 +0000
- Revision:
- 9:1e63726fff9c
- Parent:
- 7:5f612ed18852
Version 1.5; Moved debug printf statement causing delay which result in character lose out of read loop
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Roietronics | 1:8cad8f7852d4 | 1 | /* Run Honeywell Dust Sensor in continous Sampling Mode on a mDot |
| Roietronics | 3:1ba96949e2fd | 2 | Version 1.1 |
| Roietronics | 0:8326629a1b97 | 3 | Steve Mylroie Roitronic August 1st 2017 |
| Roietronics | 0:8326629a1b97 | 4 | @C Copyright Global Quality Corp |
| Roietronics | 0:8326629a1b97 | 5 | */ |
| Roietronics | 0:8326629a1b97 | 6 | |
| Roietronics | 0:8326629a1b97 | 7 | #include "mbed.h" |
| Roietronics | 0:8326629a1b97 | 8 | #include "stdlib.h" |
| Roietronics | 0:8326629a1b97 | 9 | |
| Roietronics | 9:1e63726fff9c | 10 | #define VERSION "1.5" |
| Roietronics | 4:12dfd9a8ee76 | 11 | |
| Roietronics | 0:8326629a1b97 | 12 | #define TRACE_MODE |
| Roietronics | 0:8326629a1b97 | 13 | |
| Roietronics | 0:8326629a1b97 | 14 | //Turn on trace logging to the PC USB port |
| Roietronics | 0:8326629a1b97 | 15 | #ifdef TRACE_MODE |
| Roietronics | 0:8326629a1b97 | 16 | //Log level need to be set one level higher than the high level to be output |
| Roietronics | 5:7cabc36d4352 | 17 | #define LOG_LEVEL 2 |
| Roietronics | 0:8326629a1b97 | 18 | #define DEBUG |
| Roietronics | 0:8326629a1b97 | 19 | #endif |
| Roietronics | 0:8326629a1b97 | 20 | |
| Roietronics | 3:1ba96949e2fd | 21 | |
| Roietronics | 0:8326629a1b97 | 22 | const uint8_t* measureCommand = "68014057"; |
| Roietronics | 0:8326629a1b97 | 23 | const uint8_t* stopCommand = "68012077"; |
| Roietronics | 0:8326629a1b97 | 24 | |
| Roietronics | 5:7cabc36d4352 | 25 | //Sensor command response codes |
| Roietronics | 0:8326629a1b97 | 26 | #define OK 0xA5A5 |
| Roietronics | 0:8326629a1b97 | 27 | #define BAD 0x9696 |
| Roietronics | 0:8326629a1b97 | 28 | |
| Roietronics | 5:7cabc36d4352 | 29 | //Auto Mode measurement response lenght |
| Roietronics | 0:8326629a1b97 | 30 | #define MESSAGE_LEN 32 |
| Roietronics | 5:7cabc36d4352 | 31 | |
| Roietronics | 0:8326629a1b97 | 32 | //sensor measurement cycle in millseconds |
| Roietronics | 0:8326629a1b97 | 33 | #define MEASURE_DELAY 1500 |
| Roietronics | 0:8326629a1b97 | 34 | |
| Roietronics | 3:1ba96949e2fd | 35 | Serial pc(USBTX, USBRX, 115200); |
| Roietronics | 3:1ba96949e2fd | 36 | |
| Roietronics | 0:8326629a1b97 | 37 | //Use USB debug pott for the debug stream |
| Roietronics | 0:8326629a1b97 | 38 | #ifdef DEBUG |
| Roietronics | 3:1ba96949e2fd | 39 | uint32_t debugLevel = LOG_LEVEL; |
| Roietronics | 3:1ba96949e2fd | 40 | #else |
| Roietronics | 3:1ba96949e2fd | 41 | uint32_t debugLevel = 0; |
| Roietronics | 0:8326629a1b97 | 42 | #endif |
| Roietronics | 0:8326629a1b97 | 43 | |
| Roietronics | 3:1ba96949e2fd | 44 | void logInfo(char* text) { |
| Roietronics | 3:1ba96949e2fd | 45 | if ( debugLevel > 0 ) { |
| Roietronics | 3:1ba96949e2fd | 46 | pc.printf("\n%s\n", text ); |
| Roietronics | 3:1ba96949e2fd | 47 | } |
| Roietronics | 3:1ba96949e2fd | 48 | return; |
| Roietronics | 5:7cabc36d4352 | 49 | } |
| Roietronics | 3:1ba96949e2fd | 50 | |
| Roietronics | 3:1ba96949e2fd | 51 | void logTrace(uint8_t data) { |
| Roietronics | 3:1ba96949e2fd | 52 | if (debugLevel > 2 ){ pc.putc( data ); |
| Roietronics | 3:1ba96949e2fd | 53 | } |
| Roietronics | 3:1ba96949e2fd | 54 | return; |
| Roietronics | 3:1ba96949e2fd | 55 | } |
| Roietronics | 3:1ba96949e2fd | 56 | |
| Roietronics | 5:7cabc36d4352 | 57 | //Board specfic serial port pin definitions |
| Roietronics | 3:1ba96949e2fd | 58 | |
| Roietronics | 5:7cabc36d4352 | 59 | #ifdef TARGET_Freescale //Freedom Board |
| Roietronics | 5:7cabc36d4352 | 60 | |
| Roietronics | 5:7cabc36d4352 | 61 | #define SENSOR_XMT D1 |
| Roietronics | 5:7cabc36d4352 | 62 | #define SENSOR_RCV D0 |
| Roietronics | 5:7cabc36d4352 | 63 | |
| Roietronics | 5:7cabc36d4352 | 64 | #endif |
| Roietronics | 5:7cabc36d4352 | 65 | |
| Roietronics | 5:7cabc36d4352 | 66 | #ifdef TARGET_MTS_MDOT_F411RE //Multi Tech mDot |
| Roietronics | 5:7cabc36d4352 | 67 | |
| Roietronics | 5:7cabc36d4352 | 68 | #define SENSOR_XMT PA_2 |
| Roietronics | 5:7cabc36d4352 | 69 | #define SENSOR_RCV PA_3 |
| Roietronics | 5:7cabc36d4352 | 70 | |
| Roietronics | 5:7cabc36d4352 | 71 | #endif |
| Roietronics | 5:7cabc36d4352 | 72 | |
| Roietronics | 5:7cabc36d4352 | 73 | //Use a boards second UART to communicate witb the Honneywell sensor |
| Roietronics | 0:8326629a1b97 | 74 | //Default UART setting are 8,N,1 |
| Roietronics | 7:5f612ed18852 | 75 | Serial sensor( SENSOR_XMT, SENSOR_RCV); |
| Roietronics | 0:8326629a1b97 | 76 | |
| Roietronics | 0:8326629a1b97 | 77 | //synchrnous serial read and writes |
| Roietronics | 0:8326629a1b97 | 78 | //Mbed 5 Serial class only supports single character read and writes or |
| Roietronics | 0:8326629a1b97 | 79 | // async buffer reads abd wrutes |
| Roietronics | 0:8326629a1b97 | 80 | void readBuffer(uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 81 | { |
| Roietronics | 0:8326629a1b97 | 82 | uint8_t* pointer = buffer; |
| Roietronics | 0:8326629a1b97 | 83 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 84 | while(1) |
| Roietronics | 0:8326629a1b97 | 85 | { |
| Roietronics | 0:8326629a1b97 | 86 | if(sensor.readable()) |
| Roietronics | 0:8326629a1b97 | 87 | { |
| Roietronics | 9:1e63726fff9c | 88 | *pointer++ = sensor.getc(); |
| Roietronics | 3:1ba96949e2fd | 89 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 90 | counter--; |
| Roietronics | 0:8326629a1b97 | 91 | } |
| Roietronics | 4:12dfd9a8ee76 | 92 | if(counter == 0) { |
| Roietronics | 9:1e63726fff9c | 93 | pc.printf("Retrived Data from Sensor\n"); |
| Roietronics | 0:8326629a1b97 | 94 | return; |
| Roietronics | 0:8326629a1b97 | 95 | } |
| Roietronics | 0:8326629a1b97 | 96 | } |
| Roietronics | 0:8326629a1b97 | 97 | } |
| Roietronics | 0:8326629a1b97 | 98 | |
| Roietronics | 0:8326629a1b97 | 99 | void writeBuffer(const uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 100 | { |
| Roietronics | 5:7cabc36d4352 | 101 | logInfo( "Sending Data to sensor\n"); |
| Roietronics | 0:8326629a1b97 | 102 | uint8_t* pointer = (uint8_t*)buffer; |
| Roietronics | 0:8326629a1b97 | 103 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 104 | while(1) |
| Roietronics | 0:8326629a1b97 | 105 | { |
| Roietronics | 0:8326629a1b97 | 106 | if(sensor.writeable()) |
| Roietronics | 0:8326629a1b97 | 107 | { |
| Roietronics | 3:1ba96949e2fd | 108 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 109 | sensor.putc(*pointer++); |
| Roietronics | 0:8326629a1b97 | 110 | counter--; |
| Roietronics | 0:8326629a1b97 | 111 | } |
| Roietronics | 4:12dfd9a8ee76 | 112 | if(counter == 0) { |
| Roietronics | 0:8326629a1b97 | 113 | return; |
| Roietronics | 0:8326629a1b97 | 114 | } |
| Roietronics | 0:8326629a1b97 | 115 | } |
| Roietronics | 0:8326629a1b97 | 116 | } |
| Roietronics | 0:8326629a1b97 | 117 | |
| Roietronics | 4:12dfd9a8ee76 | 118 | //Validate the received mesurements checksum |
| Roietronics | 0:8326629a1b97 | 119 | |
| Roietronics | 0:8326629a1b97 | 120 | bool checkValue(uint8_t *thebuf, uint8_t leng) |
| Roietronics | 0:8326629a1b97 | 121 | { |
| Roietronics | 0:8326629a1b97 | 122 | bool receiveflag = false; |
| Roietronics | 7:5f612ed18852 | 123 | uint16_t receiveSum = 0; |
| Roietronics | 0:8326629a1b97 | 124 | |
| Roietronics | 6:6aa9d16e27bc | 125 | //Don't include the checksum bytes in the sum |
| Roietronics | 6:6aa9d16e27bc | 126 | for(int i=0; i<(leng-3); i++){ |
| Roietronics | 0:8326629a1b97 | 127 | receiveSum=receiveSum+thebuf[i]; |
| Roietronics | 0:8326629a1b97 | 128 | } |
| Roietronics | 0:8326629a1b97 | 129 | receiveSum=receiveSum + 0x42; |
| Roietronics | 0:8326629a1b97 | 130 | |
| Roietronics | 0:8326629a1b97 | 131 | if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the debug data |
| Roietronics | 0:8326629a1b97 | 132 | { |
| Roietronics | 0:8326629a1b97 | 133 | receiveSum = 0; |
| Roietronics | 0:8326629a1b97 | 134 | receiveflag = true; |
| Roietronics | 0:8326629a1b97 | 135 | } |
| Roietronics | 0:8326629a1b97 | 136 | return receiveflag; |
| Roietronics | 0:8326629a1b97 | 137 | } |
| Roietronics | 0:8326629a1b97 | 138 | |
| Roietronics | 0:8326629a1b97 | 139 | //Extract the 1 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 140 | uint16_t transmitPM01(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 141 | { |
| Roietronics | 0:8326629a1b97 | 142 | uint16_t PM01Val; |
| Roietronics | 0:8326629a1b97 | 143 | PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 144 | return PM01Val; |
| Roietronics | 0:8326629a1b97 | 145 | } |
| Roietronics | 0:8326629a1b97 | 146 | |
| Roietronics | 0:8326629a1b97 | 147 | //Extract the 2.5 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 148 | uint16_t transmitPM2_5(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 149 | { |
| Roietronics | 0:8326629a1b97 | 150 | uint16_t PM2_5Val; |
| Roietronics | 0:8326629a1b97 | 151 | PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 152 | return PM2_5Val; |
| Roietronics | 0:8326629a1b97 | 153 | } |
| Roietronics | 0:8326629a1b97 | 154 | |
| Roietronics | 0:8326629a1b97 | 155 | //Extract the 10 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 156 | uint16_t transmitPM10(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 157 | { |
| Roietronics | 0:8326629a1b97 | 158 | uint16_t PM10Val; |
| Roietronics | 0:8326629a1b97 | 159 | PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 160 | return PM10Val; |
| Roietronics | 0:8326629a1b97 | 161 | } |
| Roietronics | 0:8326629a1b97 | 162 | |
| Roietronics | 0:8326629a1b97 | 163 | int main() |
| Roietronics | 0:8326629a1b97 | 164 | { |
| Roietronics | 5:7cabc36d4352 | 165 | uint8_t dataBuffer[MESSAGE_LEN]; |
| Roietronics | 0:8326629a1b97 | 166 | uint16_t response; |
| Roietronics | 0:8326629a1b97 | 167 | uint16_t PM01Value=0; //define PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 168 | uint16_t PM2_5Value=0; //define PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 169 | uint16_t PM10Value=0; //define PM10 value of the air detector module |
| Roietronics | 4:12dfd9a8ee76 | 170 | |
| Roietronics | 9:1e63726fff9c | 171 | pc.printf("Starting Honeywell Dust Sesor App version %s\n", VERSION); |
| Roietronics | 0:8326629a1b97 | 172 | |
| Roietronics | 0:8326629a1b97 | 173 | //Send start command to the sensor |
| Roietronics | 0:8326629a1b97 | 174 | writeBuffer(measureCommand, 4); |
| Roietronics | 5:7cabc36d4352 | 175 | /* |
| Roietronics | 5:7cabc36d4352 | 176 | Not geting response from sensor - first characters received are measurement data |
| Roietronics | 0:8326629a1b97 | 177 | //Wait for sensors response |
| Roietronics | 0:8326629a1b97 | 178 | //while(!sensor.readable()); |
| Roietronics | 0:8326629a1b97 | 179 | readBuffer(dataBuffer, 2); |
| Roietronics | 0:8326629a1b97 | 180 | response = dataBuffer[0] << 8 || dataBuffer[1]; |
| Roietronics | 0:8326629a1b97 | 181 | |
| Roietronics | 0:8326629a1b97 | 182 | switch(response) { |
| Roietronics | 0:8326629a1b97 | 183 | case OK: |
| Roietronics | 0:8326629a1b97 | 184 | logInfo("Sensor Auto Measurement Started"); |
| Roietronics | 0:8326629a1b97 | 185 | break; |
| Roietronics | 0:8326629a1b97 | 186 | case BAD: |
| Roietronics | 0:8326629a1b97 | 187 | logInfo("Sensor rejected Start Measurement Commmand"); |
| Roietronics | 0:8326629a1b97 | 188 | return -1; |
| Roietronics | 0:8326629a1b97 | 189 | default: |
| Roietronics | 0:8326629a1b97 | 190 | logInfo("Communication Error: Invalid Sensor Response"); |
| Roietronics | 0:8326629a1b97 | 191 | return -2; |
| Roietronics | 0:8326629a1b97 | 192 | } |
| Roietronics | 5:7cabc36d4352 | 193 | */ |
| Roietronics | 4:12dfd9a8ee76 | 194 | |
| Roietronics | 0:8326629a1b97 | 195 | //Start continous loop |
| Roietronics | 9:1e63726fff9c | 196 | while(1) { |
| Roietronics | 9:1e63726fff9c | 197 | if(sensor.getc() == 0x42){ //start to read when detect 0x42 |
| Roietronics | 0:8326629a1b97 | 198 | readBuffer(dataBuffer,MESSAGE_LEN -1); |
| Roietronics | 0:8326629a1b97 | 199 | if(dataBuffer[0] == 0x4d){ |
| Roietronics | 6:6aa9d16e27bc | 200 | if(checkValue(dataBuffer, MESSAGE_LEN-1)){ |
| Roietronics | 7:5f612ed18852 | 201 | PM01Value = transmitPM01(dataBuffer); //count PM1.0 value of the air detector module |
| Roietronics | 7:5f612ed18852 | 202 | PM2_5Value = transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module |
| Roietronics | 7:5f612ed18852 | 203 | PM10Value = transmitPM10(dataBuffer); //count PM10 value of the air detector module |
| Roietronics | 9:1e63726fff9c | 204 | } |
| Roietronics | 6:6aa9d16e27bc | 205 | else { |
| Roietronics | 9:1e63726fff9c | 206 | pc.printf("Message checksum error\n"); |
| Roietronics | 9:1e63726fff9c | 207 | } |
| Roietronics | 6:6aa9d16e27bc | 208 | } |
| Roietronics | 7:5f612ed18852 | 209 | else { |
| Roietronics | 9:1e63726fff9c | 210 | pc.putc(dataBuffer[0]); |
| Roietronics | 7:5f612ed18852 | 211 | pc.printf("Second Character was %x\n", dataBuffer[0]); |
| Roietronics | 7:5f612ed18852 | 212 | } |
| Roietronics | 7:5f612ed18852 | 213 | //Check for exit request |
| Roietronics | 7:5f612ed18852 | 214 | if(pc.readable()) { |
| Roietronics | 7:5f612ed18852 | 215 | char input = pc.getc(); |
| Roietronics | 7:5f612ed18852 | 216 | if(input == 'Q' || input == 'q') |
| Roietronics | 7:5f612ed18852 | 217 | { |
| Roietronics | 7:5f612ed18852 | 218 | writeBuffer(stopCommand, 4); |
| Roietronics | 7:5f612ed18852 | 219 | pc.printf("Exit request received\n"); |
| Roietronics | 7:5f612ed18852 | 220 | return 0; |
| Roietronics | 9:1e63726fff9c | 221 | } |
| Roietronics | 7:5f612ed18852 | 222 | } |
| Roietronics | 9:1e63726fff9c | 223 | // Use MBed wait function instead of Arduino delay loop |
| Roietronics | 9:1e63726fff9c | 224 | wait_ms(1000); |
| Roietronics | 9:1e63726fff9c | 225 | |
| Roietronics | 9:1e63726fff9c | 226 | pc.printf("PM1.0: %d ug/m3\n", PM01Value); |
| Roietronics | 9:1e63726fff9c | 227 | |
| Roietronics | 9:1e63726fff9c | 228 | pc.printf("PM2.5: %d ug/m3\n", PM2_5Value); |
| Roietronics | 9:1e63726fff9c | 229 | |
| Roietronics | 9:1e63726fff9c | 230 | pc.printf("PM10: %d ug/m3\n", PM10Value); |
| Roietronics | 9:1e63726fff9c | 231 | |
| Roietronics | 9:1e63726fff9c | 232 | pc.printf("\n"); |
| Roietronics | 5:7cabc36d4352 | 233 | } |
| Roietronics | 9:1e63726fff9c | 234 | } |
| Roietronics | 9:1e63726fff9c | 235 | } |