mDot / Mbed OS Honneywell_Dust_Simple
Revision:
0:8326629a1b97
Child:
1:8cad8f7852d4
diff -r 000000000000 -r 8326629a1b97 Main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Main.cpp	Tue Aug 01 13:40:48 2017 +0000
@@ -0,0 +1,195 @@
+/* Run Honneywell Dust Sensor in continous Sampling Mode on a mDot
+   Version 1.0
+   Steve Mylroie Roitronic August 1st 2017
+   @C Copyright Global Quality Corp
+*/
+
+#include "mbed.h"
+#include "mDot.h"
+#include "MTSLog.h"
+#include "stdlib.h"
+#include <Stream.h>
+
+#define TRACE_MODE
+
+//Turn on trace logging to the PC USB port
+#ifdef TRACE_MODE
+//Log level need to be set one level higher than the high level to be output
+#define LOG_LEVEL mts::MTSLog::TRACE_LEVEL + 1
+#define DEBUG
+#endif
+
+const uint8_t* measureCommand = "68014057";
+const uint8_t* stopCommand    = "68012077";
+
+#define OK   0xA5A5
+#define BAD  0x9696
+
+#define MESSAGE_LEN 32
+//sensor measurement cycle in millseconds
+#define MEASURE_DELAY 1500 
+
+//Use USB debug pott for the debug stream
+#ifdef DEBUG
+Serial pc(USBTX, USBRX, 115200);
+#endif
+
+//Use second debug to communicate witb the Honneywell sensor
+//Default UART setting are 8,N,1
+Serial sensor(PA_2, PA_3 , 9600);
+
+Timer t;
+
+//synchrnous serial read and writes
+//Mbed 5 Serial class only supports single character read and writes or
+// async buffer reads abd wrutes
+void readBuffer(uint8_t* buffer, uint16_t count)
+{
+    uint8_t* pointer = buffer;
+    uint16_t counter = count;
+    while(1)
+    {
+        if(sensor.readable())
+        {
+            *pointer++ = sensor.getc();
+            counter--;
+            }
+        if(count == 0) {
+            return;
+            }
+        }
+    }
+    
+void writeBuffer(const uint8_t* buffer, uint16_t count)
+{
+    uint8_t* pointer = (uint8_t*)buffer;
+    uint16_t counter = count;
+    while(1)
+    {
+        if(sensor.writeable())
+        {
+            sensor.putc(*pointer++);
+            counter--;
+            }
+        if(count == 0) {
+            return;
+            }
+        }
+    } 
+
+//Va;idate the received mesurements checksum
+
+bool checkValue(uint8_t *thebuf, uint8_t leng)
+{  
+  bool receiveflag = false;
+  uint16_t receiveSum=0;
+
+  for(int i=0; i<(leng-2); i++){
+  receiveSum=receiveSum+thebuf[i];
+  }
+  receiveSum=receiveSum + 0x42;
+ 
+  if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1]))  //check the debug data 
+  {
+    receiveSum = 0;
+    receiveflag = true;
+  }
+  return receiveflag;
+}
+
+//Extract the 1 micron particle count from the messaage
+uint16_t transmitPM01(uint8_t *thebuf)
+{
+  uint16_t PM01Val;
+  PM01Val=((thebuf[3]<<8) + thebuf[4]); //count PM1.0 value of the air detector module
+  return PM01Val;
+}
+
+//Extract the 2.5 micron particle count from the messaage
+uint16_t transmitPM2_5(uint8_t *thebuf)
+{
+  uint16_t PM2_5Val;
+  PM2_5Val=((thebuf[5]<<8) + thebuf[6]);//count PM2.5 value of the air detector module
+  return PM2_5Val;
+  }
+
+//Extract the 10 micron particle count from the messaage
+uint16_t transmitPM10(uint8_t *thebuf)
+{
+  uint16_t PM10Val;
+  PM10Val=((thebuf[7]<<8) + thebuf[8]); //count PM10 value of the air detector module  
+  return PM10Val;
+}
+
+int main()
+{
+    uint8_t dataBuffer[32];
+    uint16_t response;
+    uint16_t PM01Value=0;     //define PM1.0 value of the air detector module
+    uint16_t PM2_5Value=0;        //define PM2.5 value of the air detector module
+    uint16_t PM10Value=0;         //define PM10 value of the air detector module
+
+    //Send start command to the sensor
+    writeBuffer(measureCommand, 4);
+    //Wait for sensors response
+    //while(!sensor.readable());
+    readBuffer(dataBuffer, 2);
+    response = dataBuffer[0] << 8 || dataBuffer[1];
+
+    switch(response) {
+        case OK:
+        logInfo("Sensor Auto Measurement Started");
+        break;
+        case BAD:
+        logInfo("Sensor rejected Start Measurement Commmand");
+        return -1;
+        default:
+        logInfo("Communication Error: Invalid Sensor Response");
+        return -2;
+    }
+    t.start();
+    
+    //Start continous loop 
+    while(1)
+    {
+       if(sensor.getc() == 0x42){    //start to read when detect 0x42
+            readBuffer(dataBuffer,MESSAGE_LEN -1);
+        
+            if(dataBuffer[0] == 0x4d){
+              if(checkValue(dataBuffer,MESSAGE_LEN)){
+                PM01Value=transmitPM01(dataBuffer); //count PM1.0 value of the air detector module
+                PM2_5Value=transmitPM2_5(dataBuffer);//count PM2.5 value of the air detector module
+                PM10Value=transmitPM10(dataBuffer); //count PM10 value of the air detector module 
+              }
+          }           
+        }
+        
+        char input = pc.getc();
+        if(input == 'Q' || input == 'q')
+        { 
+            t.stop();
+            writeBuffer(stopCommand, 4);
+            logInfo("Exit request received");
+            return 0;
+        }
+        static unsigned long OledTimer= t.read_ms();  
+        if (t.read_ms() - OledTimer >=1000) 
+        {
+          OledTimer= t.read_ms(); 
+          
+          printf("PM1.0: %d ug/m3", PM01Value);  
+//          debug.print(PM01Value);
+//          debug.println("  ug/m3");            
+        
+          printf("PM2.5: %d ug/m3", PM2_5Value);  
+//          debug.print(PM2_5Value);
+//          debug.println("  ug/m3");     
+          
+          printf("PM10: %d ug/m3", PM10Value);  
+//          debug.print(PM10Value);
+//          debug.println("  ug/m3");   
+          printf("\n");
+            
+        }
+    }
+ }
\ No newline at end of file