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@4:12dfd9a8ee76, 2017-08-07 (annotated)
- Committer:
- Roietronics
- Date:
- Mon Aug 07 23:36:40 2017 +0000
- Revision:
- 4:12dfd9a8ee76
- Parent:
- 3:1ba96949e2fd
- Child:
- 5:7cabc36d4352
Debug trace;
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 | 4:12dfd9a8ee76 | 10 | #define VERSION "1.2" |
| Roietronics | 4:12dfd9a8ee76 | 11 | |
| Roietronics | 4:12dfd9a8ee76 | 12 | |
| Roietronics | 4:12dfd9a8ee76 | 13 | #ifdef MDOT |
| Roietronics | 4:12dfd9a8ee76 | 14 | #define |
| Roietronics | 4:12dfd9a8ee76 | 15 | #define |
| Roietronics | 4:12dfd9a8ee76 | 16 | #define |
| Roietronics | 4:12dfd9a8ee76 | 17 | #define |
| Roietronics | 4:12dfd9a8ee76 | 18 | #endif |
| Roietronics | 4:12dfd9a8ee76 | 19 | |
| Roietronics | 4:12dfd9a8ee76 | 20 | #ifdef FREEDOM |
| Roietronics | 4:12dfd9a8ee76 | 21 | #endif |
| Roietronics | 4:12dfd9a8ee76 | 22 | |
| Roietronics | 0:8326629a1b97 | 23 | #define TRACE_MODE |
| Roietronics | 0:8326629a1b97 | 24 | |
| Roietronics | 0:8326629a1b97 | 25 | //Turn on trace logging to the PC USB port |
| Roietronics | 0:8326629a1b97 | 26 | #ifdef TRACE_MODE |
| Roietronics | 0:8326629a1b97 | 27 | //Log level need to be set one level higher than the high level to be output |
| Roietronics | 3:1ba96949e2fd | 28 | #define LOG_LEVEL 4 |
| Roietronics | 0:8326629a1b97 | 29 | #define DEBUG |
| Roietronics | 0:8326629a1b97 | 30 | #endif |
| Roietronics | 0:8326629a1b97 | 31 | |
| Roietronics | 3:1ba96949e2fd | 32 | |
| Roietronics | 0:8326629a1b97 | 33 | const uint8_t* measureCommand = "68014057"; |
| Roietronics | 0:8326629a1b97 | 34 | const uint8_t* stopCommand = "68012077"; |
| Roietronics | 0:8326629a1b97 | 35 | |
| Roietronics | 0:8326629a1b97 | 36 | #define OK 0xA5A5 |
| Roietronics | 0:8326629a1b97 | 37 | #define BAD 0x9696 |
| Roietronics | 0:8326629a1b97 | 38 | |
| Roietronics | 0:8326629a1b97 | 39 | #define MESSAGE_LEN 32 |
| Roietronics | 0:8326629a1b97 | 40 | //sensor measurement cycle in millseconds |
| Roietronics | 0:8326629a1b97 | 41 | #define MEASURE_DELAY 1500 |
| Roietronics | 0:8326629a1b97 | 42 | |
| Roietronics | 3:1ba96949e2fd | 43 | Serial pc(USBTX, USBRX, 115200); |
| Roietronics | 3:1ba96949e2fd | 44 | |
| Roietronics | 0:8326629a1b97 | 45 | //Use USB debug pott for the debug stream |
| Roietronics | 0:8326629a1b97 | 46 | #ifdef DEBUG |
| Roietronics | 3:1ba96949e2fd | 47 | uint32_t debugLevel = LOG_LEVEL; |
| Roietronics | 3:1ba96949e2fd | 48 | #else |
| Roietronics | 3:1ba96949e2fd | 49 | uint32_t debugLevel = 0; |
| Roietronics | 0:8326629a1b97 | 50 | #endif |
| Roietronics | 0:8326629a1b97 | 51 | |
| Roietronics | 3:1ba96949e2fd | 52 | void logInfo(char* text) { |
| Roietronics | 3:1ba96949e2fd | 53 | if ( debugLevel > 0 ) { |
| Roietronics | 3:1ba96949e2fd | 54 | pc.printf("\n%s\n", text ); |
| Roietronics | 3:1ba96949e2fd | 55 | } |
| Roietronics | 3:1ba96949e2fd | 56 | return; |
| Roietronics | 3:1ba96949e2fd | 57 | } |
| Roietronics | 3:1ba96949e2fd | 58 | |
| Roietronics | 3:1ba96949e2fd | 59 | void logTrace(uint8_t data) { |
| Roietronics | 3:1ba96949e2fd | 60 | if (debugLevel > 2 ){ pc.putc( data ); |
| Roietronics | 3:1ba96949e2fd | 61 | } |
| Roietronics | 3:1ba96949e2fd | 62 | return; |
| Roietronics | 3:1ba96949e2fd | 63 | } |
| Roietronics | 3:1ba96949e2fd | 64 | |
| Roietronics | 3:1ba96949e2fd | 65 | |
| Roietronics | 3:1ba96949e2fd | 66 | //Use second UART to communicate witb the Honneywell sensor |
| Roietronics | 0:8326629a1b97 | 67 | //Default UART setting are 8,N,1 |
| Roietronics | 4:12dfd9a8ee76 | 68 | Serial sensor(D1, D0, 9600); |
| Roietronics | 0:8326629a1b97 | 69 | |
| Roietronics | 0:8326629a1b97 | 70 | Timer t; |
| Roietronics | 0:8326629a1b97 | 71 | |
| Roietronics | 3:1ba96949e2fd | 72 | |
| Roietronics | 0:8326629a1b97 | 73 | //synchrnous serial read and writes |
| Roietronics | 0:8326629a1b97 | 74 | //Mbed 5 Serial class only supports single character read and writes or |
| Roietronics | 0:8326629a1b97 | 75 | // async buffer reads abd wrutes |
| Roietronics | 0:8326629a1b97 | 76 | void readBuffer(uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 77 | { |
| Roietronics | 3:1ba96949e2fd | 78 | logInfo( "Receiving Data from sensor"); |
| Roietronics | 0:8326629a1b97 | 79 | uint8_t* pointer = buffer; |
| Roietronics | 0:8326629a1b97 | 80 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 81 | while(1) |
| Roietronics | 0:8326629a1b97 | 82 | { |
| Roietronics | 0:8326629a1b97 | 83 | if(sensor.readable()) |
| Roietronics | 0:8326629a1b97 | 84 | { |
| Roietronics | 0:8326629a1b97 | 85 | *pointer++ = sensor.getc(); |
| Roietronics | 3:1ba96949e2fd | 86 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 87 | counter--; |
| Roietronics | 0:8326629a1b97 | 88 | } |
| Roietronics | 4:12dfd9a8ee76 | 89 | if(counter == 0) { |
| Roietronics | 0:8326629a1b97 | 90 | return; |
| Roietronics | 0:8326629a1b97 | 91 | } |
| Roietronics | 0:8326629a1b97 | 92 | } |
| Roietronics | 0:8326629a1b97 | 93 | } |
| Roietronics | 0:8326629a1b97 | 94 | |
| Roietronics | 0:8326629a1b97 | 95 | void writeBuffer(const uint8_t* buffer, uint16_t count) |
| Roietronics | 0:8326629a1b97 | 96 | { |
| Roietronics | 3:1ba96949e2fd | 97 | logInfo( "Sending Data to sensor"); |
| Roietronics | 0:8326629a1b97 | 98 | uint8_t* pointer = (uint8_t*)buffer; |
| Roietronics | 0:8326629a1b97 | 99 | uint16_t counter = count; |
| Roietronics | 0:8326629a1b97 | 100 | while(1) |
| Roietronics | 0:8326629a1b97 | 101 | { |
| Roietronics | 0:8326629a1b97 | 102 | if(sensor.writeable()) |
| Roietronics | 0:8326629a1b97 | 103 | { |
| Roietronics | 3:1ba96949e2fd | 104 | logTrace(*pointer); |
| Roietronics | 0:8326629a1b97 | 105 | sensor.putc(*pointer++); |
| Roietronics | 0:8326629a1b97 | 106 | counter--; |
| Roietronics | 0:8326629a1b97 | 107 | } |
| Roietronics | 4:12dfd9a8ee76 | 108 | if(counter == 0) { |
| Roietronics | 0:8326629a1b97 | 109 | return; |
| Roietronics | 0:8326629a1b97 | 110 | } |
| Roietronics | 0:8326629a1b97 | 111 | } |
| Roietronics | 0:8326629a1b97 | 112 | } |
| Roietronics | 0:8326629a1b97 | 113 | |
| Roietronics | 4:12dfd9a8ee76 | 114 | //Validate the received mesurements checksum |
| Roietronics | 0:8326629a1b97 | 115 | |
| Roietronics | 0:8326629a1b97 | 116 | bool checkValue(uint8_t *thebuf, uint8_t leng) |
| Roietronics | 0:8326629a1b97 | 117 | { |
| Roietronics | 0:8326629a1b97 | 118 | bool receiveflag = false; |
| Roietronics | 0:8326629a1b97 | 119 | uint16_t receiveSum=0; |
| Roietronics | 0:8326629a1b97 | 120 | |
| Roietronics | 0:8326629a1b97 | 121 | for(int i=0; i<(leng-2); i++){ |
| Roietronics | 0:8326629a1b97 | 122 | receiveSum=receiveSum+thebuf[i]; |
| Roietronics | 0:8326629a1b97 | 123 | } |
| Roietronics | 0:8326629a1b97 | 124 | receiveSum=receiveSum + 0x42; |
| Roietronics | 0:8326629a1b97 | 125 | |
| Roietronics | 0:8326629a1b97 | 126 | if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the debug data |
| Roietronics | 0:8326629a1b97 | 127 | { |
| Roietronics | 0:8326629a1b97 | 128 | receiveSum = 0; |
| Roietronics | 0:8326629a1b97 | 129 | receiveflag = true; |
| Roietronics | 0:8326629a1b97 | 130 | } |
| Roietronics | 0:8326629a1b97 | 131 | return receiveflag; |
| Roietronics | 0:8326629a1b97 | 132 | } |
| Roietronics | 0:8326629a1b97 | 133 | |
| Roietronics | 0:8326629a1b97 | 134 | //Extract the 1 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 135 | uint16_t transmitPM01(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 136 | { |
| Roietronics | 0:8326629a1b97 | 137 | uint16_t PM01Val; |
| Roietronics | 0:8326629a1b97 | 138 | PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 139 | return PM01Val; |
| Roietronics | 0:8326629a1b97 | 140 | } |
| Roietronics | 0:8326629a1b97 | 141 | |
| Roietronics | 0:8326629a1b97 | 142 | //Extract the 2.5 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 143 | uint16_t transmitPM2_5(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 144 | { |
| Roietronics | 0:8326629a1b97 | 145 | uint16_t PM2_5Val; |
| Roietronics | 0:8326629a1b97 | 146 | PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 147 | return PM2_5Val; |
| Roietronics | 0:8326629a1b97 | 148 | } |
| Roietronics | 0:8326629a1b97 | 149 | |
| Roietronics | 0:8326629a1b97 | 150 | //Extract the 10 micron particle count from the messaage |
| Roietronics | 0:8326629a1b97 | 151 | uint16_t transmitPM10(uint8_t *thebuf) |
| Roietronics | 0:8326629a1b97 | 152 | { |
| Roietronics | 0:8326629a1b97 | 153 | uint16_t PM10Val; |
| Roietronics | 0:8326629a1b97 | 154 | PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 155 | return PM10Val; |
| Roietronics | 0:8326629a1b97 | 156 | } |
| Roietronics | 0:8326629a1b97 | 157 | |
| Roietronics | 0:8326629a1b97 | 158 | int main() |
| Roietronics | 0:8326629a1b97 | 159 | { |
| Roietronics | 0:8326629a1b97 | 160 | uint8_t dataBuffer[32]; |
| Roietronics | 0:8326629a1b97 | 161 | uint16_t response; |
| Roietronics | 0:8326629a1b97 | 162 | uint16_t PM01Value=0; //define PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 163 | uint16_t PM2_5Value=0; //define PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 164 | uint16_t PM10Value=0; //define PM10 value of the air detector module |
| Roietronics | 4:12dfd9a8ee76 | 165 | |
| Roietronics | 4:12dfd9a8ee76 | 166 | pc.printf("Starting Honeywell Dust Sesor App version %s", VERSION); |
| Roietronics | 0:8326629a1b97 | 167 | |
| Roietronics | 0:8326629a1b97 | 168 | //Send start command to the sensor |
| Roietronics | 0:8326629a1b97 | 169 | writeBuffer(measureCommand, 4); |
| Roietronics | 0:8326629a1b97 | 170 | //Wait for sensors response |
| Roietronics | 0:8326629a1b97 | 171 | //while(!sensor.readable()); |
| Roietronics | 0:8326629a1b97 | 172 | readBuffer(dataBuffer, 2); |
| Roietronics | 0:8326629a1b97 | 173 | response = dataBuffer[0] << 8 || dataBuffer[1]; |
| Roietronics | 0:8326629a1b97 | 174 | |
| Roietronics | 0:8326629a1b97 | 175 | switch(response) { |
| Roietronics | 0:8326629a1b97 | 176 | case OK: |
| Roietronics | 0:8326629a1b97 | 177 | logInfo("Sensor Auto Measurement Started"); |
| Roietronics | 0:8326629a1b97 | 178 | break; |
| Roietronics | 0:8326629a1b97 | 179 | case BAD: |
| Roietronics | 0:8326629a1b97 | 180 | logInfo("Sensor rejected Start Measurement Commmand"); |
| Roietronics | 0:8326629a1b97 | 181 | return -1; |
| Roietronics | 0:8326629a1b97 | 182 | default: |
| Roietronics | 0:8326629a1b97 | 183 | logInfo("Communication Error: Invalid Sensor Response"); |
| Roietronics | 0:8326629a1b97 | 184 | return -2; |
| Roietronics | 0:8326629a1b97 | 185 | } |
| Roietronics | 0:8326629a1b97 | 186 | t.start(); |
| Roietronics | 4:12dfd9a8ee76 | 187 | |
| Roietronics | 0:8326629a1b97 | 188 | //Start continous loop |
| Roietronics | 4:12dfd9a8ee76 | 189 | uint32_t count = 0; |
| Roietronics | 4:12dfd9a8ee76 | 190 | while(count++ < 4) |
| Roietronics | 0:8326629a1b97 | 191 | { |
| Roietronics | 0:8326629a1b97 | 192 | if(sensor.getc() == 0x42){ //start to read when detect 0x42 |
| Roietronics | 0:8326629a1b97 | 193 | readBuffer(dataBuffer,MESSAGE_LEN -1); |
| Roietronics | 0:8326629a1b97 | 194 | |
| Roietronics | 0:8326629a1b97 | 195 | if(dataBuffer[0] == 0x4d){ |
| Roietronics | 4:12dfd9a8ee76 | 196 | if(checkValue(dataBuffer,MESSAGE_LEN-1)){ |
| Roietronics | 0:8326629a1b97 | 197 | PM01Value=transmitPM01(dataBuffer); //count PM1.0 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 198 | PM2_5Value=transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 199 | PM10Value=transmitPM10(dataBuffer); //count PM10 value of the air detector module |
| Roietronics | 0:8326629a1b97 | 200 | } |
| Roietronics | 0:8326629a1b97 | 201 | } |
| Roietronics | 0:8326629a1b97 | 202 | } |
| Roietronics | 0:8326629a1b97 | 203 | |
| Roietronics | 0:8326629a1b97 | 204 | char input = pc.getc(); |
| Roietronics | 0:8326629a1b97 | 205 | if(input == 'Q' || input == 'q') |
| Roietronics | 0:8326629a1b97 | 206 | { |
| Roietronics | 0:8326629a1b97 | 207 | t.stop(); |
| Roietronics | 0:8326629a1b97 | 208 | writeBuffer(stopCommand, 4); |
| Roietronics | 0:8326629a1b97 | 209 | logInfo("Exit request received"); |
| Roietronics | 0:8326629a1b97 | 210 | return 0; |
| Roietronics | 0:8326629a1b97 | 211 | } |
| Roietronics | 0:8326629a1b97 | 212 | static unsigned long OledTimer= t.read_ms(); |
| Roietronics | 0:8326629a1b97 | 213 | if (t.read_ms() - OledTimer >=1000) |
| Roietronics | 0:8326629a1b97 | 214 | { |
| Roietronics | 0:8326629a1b97 | 215 | OledTimer= t.read_ms(); |
| Roietronics | 0:8326629a1b97 | 216 | |
| Roietronics | 1:8cad8f7852d4 | 217 | pc.printf("PM1.0: %d ug/m3", PM01Value); |
| Roietronics | 0:8326629a1b97 | 218 | // debug.print(PM01Value); |
| Roietronics | 0:8326629a1b97 | 219 | // debug.println(" ug/m3"); |
| Roietronics | 0:8326629a1b97 | 220 | |
| Roietronics | 1:8cad8f7852d4 | 221 | pc.printf("PM2.5: %d ug/m3", PM2_5Value); |
| Roietronics | 0:8326629a1b97 | 222 | // debug.print(PM2_5Value); |
| Roietronics | 0:8326629a1b97 | 223 | // debug.println(" ug/m3"); |
| Roietronics | 0:8326629a1b97 | 224 | |
| Roietronics | 1:8cad8f7852d4 | 225 | pc.printf("PM10: %d ug/m3", PM10Value); |
| Roietronics | 0:8326629a1b97 | 226 | // debug.print(PM10Value); |
| Roietronics | 0:8326629a1b97 | 227 | // debug.println(" ug/m3"); |
| Roietronics | 1:8cad8f7852d4 | 228 | pc.printf("\n"); |
| Roietronics | 0:8326629a1b97 | 229 | |
| Roietronics | 0:8326629a1b97 | 230 | } |
| Roietronics | 0:8326629a1b97 | 231 | } |
| Roietronics | 4:12dfd9a8ee76 | 232 | pc.printf("Honeywell Dust Sensor exiting"); |
| Roietronics | 0:8326629a1b97 | 233 | } |