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.
Dependencies: K30_I2C libxDot-mbed5
Fork of K30_I2C_Program by
Revision 3:2ab969b31f6b, committed 2018-09-14
- Comitter:
- bjenkins
- Date:
- Fri Sep 14 03:17:31 2018 +0000
- Parent:
- 2:680fc2690d10
- Commit message:
- Update PM code
Changed in this revision
--- a/HPM/hpm.cpp Fri Sep 07 02:54:05 2018 +0000
+++ b/HPM/hpm.cpp Fri Sep 14 03:17:31 2018 +0000
@@ -1,21 +1,28 @@
#include "mbed.h"
#include "hpm.h"
-
+
HPM::HPM(Serial* device, Serial* debug): _device(device), _debug(debug)
{
- _device->baud(9600);
- wait(0.1);
+
}
bool HPM::init(void)
{
+ _debug->printf("Initialising the PM sensor\r\n");
+ wait(1.0);
+
+ _debug->printf("PM baud rate set to 9600\r\n");
+ _device->baud(9600);
+ wait(0.1);
+
// Stop autosend
if (!this->stop_autosend())
{
_debug->printf("Stop autosend failed\r\n");
return 0;
}
+ _debug->printf("Stop autosend success\r\n");
// Start fan
if (!this->start_measurement())
@@ -23,69 +30,81 @@
_debug->printf("Start measurement failed\r\n");
return 0;
}
+ _debug->printf("Start measurement success\r\n");
return 1;
}
bool HPM::start_measurement(void)
{
+ clear_rx_buffer();
+
// First, we send the command
unsigned char start_measurement[] = {0x68, 0x01, 0x01, 0x96 };
for (int i=0; i<sizeof(start_measurement); i++)
{
+ while (!_device->writeable()); // wait until we can write to the port
_device->putc(start_measurement[i]);
}
- //Then we wait for the response
- while (!_device->readable()) {} // wait until chars arrive
- char read1 = _device->getc();
- char read2 = _device->getc();
-
- // Test the response
- if ((read1 == 0xA5) && (read2 == 0xA5)){
- return 1; // ACK
- }
- else if ((read1 == 0x96) && (read2 == 0x96))
- {
- _debug->printf ("NACK\r\n");
- return 0; // NACK
- }
- else return 0;
+ //Then we wait for the response
+ while (!_device->readable()) {} // wait until chars arrive
+ char read1 = _device->getc();
+ while (!_device->readable()) {} // wait until chars arrive
+ char read2 = _device->getc();
+
+ // Test the response
+ if ((read1 == 0xA5) && (read2 == 0xA5)){
+ return 1; // ACK
+ }
+ else if ((read1 == 0x96) && (read2 == 0x96))
+ {
+ _debug->printf ("NACK\r\n");
+ return 0; // NACK
+ }
+ else return 0;
}
bool HPM::stop_measurement(void)
{
+ clear_rx_buffer();
+
// First, we send the command
unsigned char stop_measurement[] = {0x68, 0x01, 0x01, 0x95 };
for (int i=0; i<sizeof(stop_measurement); i++)
{
+ while (!_device->writeable()); // wait until we can write to the port
_device->putc(stop_measurement[i]);
}
- //Then we wait for the response
- while (!_device->readable()) {} // wait until chars arrive
- char read1 = _device->getc();
- char read2 = _device->getc();
-
- // Test the response
- if ((read1 == 0xA5) && (read2 == 0xA5)){
- return 1; // ACK
- }
- else if ((read1 == 0x96) && (read2 == 0x96))
- {
- _debug->printf ("NACK\r\n");
- return 0; // NACK
- }
- else return 0;
+ //Then we wait for the response
+ while (!_device->readable()) {} // wait until chars arrive
+ char read1 = _device->getc();
+ while(!_device->readable());
+ char read2 = _device->getc();
+
+ // Test the response
+ if ((read1 == 0xA5) && (read2 == 0xA5)){
+ return 1; // ACK
+ }
+ else if ((read1 == 0x96) && (read2 == 0x96))
+ {
+ _debug->printf ("NACK\r\n");
+ return 0; // NACK
+ }
+ else return 0;
}
bool HPM::read_measurement (long int &PM25, long int &PM10)
{
+ clear_rx_buffer();
+
// Send the command 0x68 0x01 0x04 0x93
unsigned char read_particle[] = {0x68, 0x01, 0x04, 0x93 };
for (int i=0; i<sizeof(read_particle); i++)
{
+ while (!_device->writeable()); // wait until we can write to the port
_device->putc(read_particle[i]);
}
@@ -145,61 +164,87 @@
return 1;
}
}
+
+ _debug->printf ("HEAD=%X, LEN=%X\r\n", HEAD, LEN);
return 0;
}
bool HPM::stop_autosend(void)
{
+ clear_rx_buffer();
+
// Stop auto send
unsigned char stop_autosend[] = {0x68, 0x01, 0x20, 0x77 };
for (int i=0; i<sizeof(stop_autosend); i++)
{
+ while (!_device->writeable()); // wait until we can write to the port
_device->putc(stop_autosend[i]);
}
-
- //Then we wait for the response
- while(!_device->readable());
- char read1 = _device->getc();
- char read2 = _device->getc();
-
- // Test the response
- if ((read1 == 0xA5) && (read2 == 0xA5)){
- // ACK
- return 1;
- }
- else if ((read1 == 0x96) && (read2 == 0x96))
- {
- // NACK
- _debug->printf ("NACK\r\n");
- return 0;
- }
- else return 0;
+
+ //Then we wait for the response
+ while(!_device->readable());
+ char read1 = _device->getc();
+ while(!_device->readable());
+ char read2 = _device->getc();
+
+ // Test the response
+ if ((read1 == 0xA5) && (read2 == 0xA5)){
+ // ACK
+ return 1;
+ }
+ else if ((read1 == 0x96) && (read2 == 0x96))
+ {
+ // NACK
+ _debug->printf ("NACK\r\n");
+ return 0;
+ }
+ else
+ {
+ _debug->printf ("stop_autosend() output is %X%X\r\n", read1, read2);
+ return 0;
+ }
}
bool HPM::start_autosend(void)
{
+ clear_rx_buffer();
+
// Start auto send
unsigned char start_autosend[] = {0x68, 0x01, 0x40, 0x57 };
for (int i=0; i<sizeof(start_autosend); i++)
{
+ while (!_device->writeable()); // wait until we can write to the port
_device->putc(start_autosend[i]);
}
- //Then we wait for the response
- while(!_device->readable());
- char read1 = _device->getc();
- char read2 = _device->getc();
- // Test the response
- if ((read1 == 0xA5) && (read2 == 0xA5)){
- // ACK
- return 1;
- }
- else if ((read1 == 0x96) && (read2 == 0x96))
- {
- // NACK
- _debug->printf ("NACK\r\n");
- return 0;
-
- }
- else return 0;
-}
\ No newline at end of file
+ //Then we wait for the response
+ while(!_device->readable());
+ char read1 = _device->getc();
+ while(!_device->readable());
+ char read2 = _device->getc();
+ // Test the response
+ if ((read1 == 0xA5) && (read2 == 0xA5)){
+ // ACK
+ return 1;
+ }
+ else if ((read1 == 0x96) && (read2 == 0x96))
+ {
+ // NACK
+ _debug->printf ("NACK\r\n");
+ return 0;
+ }
+ else
+ {
+ _debug->printf ("start_autosend() output is %X%X\r\n", read1, read2);
+ return 0;
+ }
+}
+
+void HPM::clear_rx_buffer(void)
+{
+ // ensure the Rx buffer is empty. clear out the crap
+ while (_device->readable())
+ {
+ unsigned char dummy = _device->getc();
+ }
+}
--- a/HPM/hpm.h Fri Sep 07 02:54:05 2018 +0000
+++ b/HPM/hpm.h Fri Sep 14 03:17:31 2018 +0000
@@ -15,10 +15,10 @@
bool start_autosend(void);
private:
+ void clear_rx_buffer(void);
+
Serial* _device;
Serial* _debug;
- //long int _PM25;
- //long int _PM10;
};
#endif
\ No newline at end of file
--- a/K30_I2C.lib Fri Sep 07 02:54:05 2018 +0000 +++ b/K30_I2C.lib Fri Sep 14 03:17:31 2018 +0000 @@ -1,1 +1,1 @@ -https://os.mbed.com/teams/Hux-Connect/code/K30_I2C/#ceb528ccf75f +https://os.mbed.com/teams/Hux-Connect/code/K30_I2C/#2cbf3018e5bb
--- a/main.cpp Fri Sep 07 02:54:05 2018 +0000
+++ b/main.cpp Fri Sep 14 03:17:31 2018 +0000
@@ -2,9 +2,9 @@
#include "mDot.h"
#include "ChannelPlans.h"
-//#define TEST_HPM // particle
+//#define TEST_HPM // PM2.5 / PM10
//#define TEST_MICS_VZ_89TE // voc
-#define TEST_SPEC_3SP_CO // CO
+//#define TEST_SPEC_3SP_CO // CO
#define TEST_K30 // CO2 sensor
#ifdef TEST_HPM
@@ -27,7 +27,6 @@
mDot* dot = NULL;
Serial pc (USBTX, USBRX); // xDot USB interface
Serial device (UART_TX, UART_RX); // xDot UART to sensors
-bool need5v = false;
#ifdef TEST_HPM
HPM hpm = HPM::HPM(&device, &pc);
@@ -47,7 +46,7 @@
#endif
#ifdef TEST_K30
- K30_I2C k30(I2C_SDA, I2C_SCL, 0x68<<1); // xDot dev board
+ K30_I2C k30(I2C_SDA, I2C_SCL, 0x68<<1);
int co2 = 0;
#endif
@@ -75,15 +74,11 @@
pc.printf("done\r\n");
}
-int main (){
-
+int main ()
+{
// Initialising
blueLED = true;
orangeLED = false;
-
- //lora::ChannelPlan * plan = new lora::ChannelPlan_AS923();
- //dot = mDot::getInstance(plan);
- //dot->setAdr(true);
pc.baud(57600); // start debug port
pc.printf("************************\r\n");
@@ -93,7 +88,16 @@
#ifdef TEST_HPM
turnOn5v();
- b_response = hpm.init();
+ do {
+ b_response = hpm.init();
+ if (!b_response)
+ {
+ pc.printf("Initialisation of PM sensor failed\r\n");
+ wait(1.0);
+ }
+ } while (!b_response);
+ pc.printf("Initialisation of PM sensor success\r\n");
+
#endif
#ifdef TEST_MICS_VZ_89TE
@@ -126,7 +130,7 @@
b_response = hpm.read_measurement(PM25, PM10);
if (b_response)
{
- pc.printf ("PM2.5 = %d ug/m3, PM10 = %d ug/m3\r\n", PM25, PM10);
+ pc.printf ("PM2.5 = %d ug/m3, PM10 = %d ug/m3\r", PM25, PM10);
}
else
{
@@ -149,8 +153,7 @@
#endif
#ifdef TEST_K30
- int rc_check;
- rc_check = k30.readCO2(co2);
+ int rc_check = k30.readCO2(co2);
if (rc_check)
pc.printf("K30 CO2:%d ppm\r\n",co2);
else
