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