kh

Dependencies:   Adafruit_SGP30_mbed mbed CCS811 mbed-rtos ALPHASENSE Adafruit_ADS1015_ BME680

Committer:
etiene32
Date:
Wed Mar 06 14:03:41 2019 +0000
Revision:
0:43070b2dfc87
,hb

Who changed what in which revision?

UserRevisionLine numberNew contents of line
etiene32 0:43070b2dfc87 1 #include "HPMA115S0.h"
etiene32 0:43070b2dfc87 2
etiene32 0:43070b2dfc87 3 HPMA115S0::HPMA115S0(Serial* serial)
etiene32 0:43070b2dfc87 4 {
etiene32 0:43070b2dfc87 5 this->serial = serial;
etiene32 0:43070b2dfc87 6 serial->baud(9600);
etiene32 0:43070b2dfc87 7 pm10_count = 0;
etiene32 0:43070b2dfc87 8 pm2_5_count = 0;
etiene32 0:43070b2dfc87 9 }
etiene32 0:43070b2dfc87 10
etiene32 0:43070b2dfc87 11 float HPMA115S0::getPM10()
etiene32 0:43070b2dfc87 12 {
etiene32 0:43070b2dfc87 13 return pm10_count;
etiene32 0:43070b2dfc87 14 }
etiene32 0:43070b2dfc87 15
etiene32 0:43070b2dfc87 16 float HPMA115S0::getPM2_5()
etiene32 0:43070b2dfc87 17 {
etiene32 0:43070b2dfc87 18 return pm2_5_count;
etiene32 0:43070b2dfc87 19 }
etiene32 0:43070b2dfc87 20
etiene32 0:43070b2dfc87 21 void HPMA115S0::read()
etiene32 0:43070b2dfc87 22 {
etiene32 0:43070b2dfc87 23 uint8_t msg[4]={0x68,0x01,0x04,0x93};
etiene32 0:43070b2dfc87 24 uint8_t ack[8]={0,0,0,0,0,0,0,0};
etiene32 0:43070b2dfc87 25 printf("%d %d %d %d %d %d\r\n",ack[0],ack[1],ack[2],ack[3],ack[4],ack[5]);
etiene32 0:43070b2dfc87 26 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 27 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 28 if(serial->readable()==1){
etiene32 0:43070b2dfc87 29 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 30 for(int i=0;i<8;i++){
etiene32 0:43070b2dfc87 31 ack[i]=serial->getc();
etiene32 0:43070b2dfc87 32 }
etiene32 0:43070b2dfc87 33 }
etiene32 0:43070b2dfc87 34 printf("%d %d %d %d %d %d %d %d\r\n",ack[0],ack[1],ack[2],ack[3],ack[4],ack[5],ack[6],ack[7]);
etiene32 0:43070b2dfc87 35 pm10_count = ((ack[6] << 8) + ack[7]);
etiene32 0:43070b2dfc87 36 pm2_5_count = ((ack[4] << 8) + ack[5]);
etiene32 0:43070b2dfc87 37 }
etiene32 0:43070b2dfc87 38
etiene32 0:43070b2dfc87 39 void HPMA115S0::start()
etiene32 0:43070b2dfc87 40 {
etiene32 0:43070b2dfc87 41 uint8_t msg[4]={0x68,0x01,0x01,0x96};
etiene32 0:43070b2dfc87 42 uint8_t ack[2]={0,0};
etiene32 0:43070b2dfc87 43 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 44 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 45 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 46 if(serial->readable()==1){
etiene32 0:43070b2dfc87 47 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 48 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 49 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 50 }
etiene32 0:43070b2dfc87 51 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 52 }
etiene32 0:43070b2dfc87 53
etiene32 0:43070b2dfc87 54
etiene32 0:43070b2dfc87 55 void HPMA115S0::stop()
etiene32 0:43070b2dfc87 56 {
etiene32 0:43070b2dfc87 57 uint8_t msg[4]={0x68,0x01,0x02,0x95};
etiene32 0:43070b2dfc87 58 uint8_t ack[2]={0,0};
etiene32 0:43070b2dfc87 59 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 60 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 61 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 62 if(serial->readable()==1){
etiene32 0:43070b2dfc87 63 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 64 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 65 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 66 }
etiene32 0:43070b2dfc87 67 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 68 }
etiene32 0:43070b2dfc87 69
etiene32 0:43070b2dfc87 70
etiene32 0:43070b2dfc87 71 void HPMA115S0::set_customer_coef()
etiene32 0:43070b2dfc87 72 {
etiene32 0:43070b2dfc87 73 uint8_t msg[5]={0x68,0x01,0x08,0x64, 0x2A};
etiene32 0:43070b2dfc87 74 uint8_t ack[2]={0,0};
etiene32 0:43070b2dfc87 75 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 76 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 77 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 78 if(serial->readable()==1){
etiene32 0:43070b2dfc87 79 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 80 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 81 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 82 }
etiene32 0:43070b2dfc87 83 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 84 }
etiene32 0:43070b2dfc87 85
etiene32 0:43070b2dfc87 86
etiene32 0:43070b2dfc87 87 void HPMA115S0::read_customer_coef()
etiene32 0:43070b2dfc87 88 {
etiene32 0:43070b2dfc87 89 uint8_t msg[4]={0x68,0x01,0x10,0x87};
etiene32 0:43070b2dfc87 90 uint8_t ack[2]={0,0};
etiene32 0:43070b2dfc87 91 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 92 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 93 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 94 if(serial->readable()==1){
etiene32 0:43070b2dfc87 95 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 96 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 97 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 98 }
etiene32 0:43070b2dfc87 99 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 100 }
etiene32 0:43070b2dfc87 101
etiene32 0:43070b2dfc87 102
etiene32 0:43070b2dfc87 103 void HPMA115S0::stop_autosend()
etiene32 0:43070b2dfc87 104 {
etiene32 0:43070b2dfc87 105 uint8_t msg[4]={0x68,0x01,0x20,0x77};
etiene32 0:43070b2dfc87 106 char ack[2]={0,0};
etiene32 0:43070b2dfc87 107 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 108 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 109 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 110 if(serial->readable()==1){
etiene32 0:43070b2dfc87 111 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 112 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 113 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 114 }
etiene32 0:43070b2dfc87 115 printf("%d %d\r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 116 }
etiene32 0:43070b2dfc87 117
etiene32 0:43070b2dfc87 118 void HPMA115S0::enable_autosend()
etiene32 0:43070b2dfc87 119 {
etiene32 0:43070b2dfc87 120 uint8_t msg[4]={0x68,0x01,0x40,0x57};
etiene32 0:43070b2dfc87 121 uint8_t ack[2]={0,0};
etiene32 0:43070b2dfc87 122 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 123 serial->write(msg, sizeof(msg), NULL);
etiene32 0:43070b2dfc87 124 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 125 if(serial->readable()==1){
etiene32 0:43070b2dfc87 126 //serial->read(ack, sizeof(ack), NULL);
etiene32 0:43070b2dfc87 127 ack[0]=serial->getc();
etiene32 0:43070b2dfc87 128 ack[1]=serial->getc();
etiene32 0:43070b2dfc87 129 }
etiene32 0:43070b2dfc87 130 printf("%d %d \r\n",ack[0],ack[1]);
etiene32 0:43070b2dfc87 131 }
etiene32 0:43070b2dfc87 132
etiene32 0:43070b2dfc87 133 void HPMA115S0::read_autosend()
etiene32 0:43070b2dfc87 134 {
etiene32 0:43070b2dfc87 135 printf("yo\r\n");
etiene32 0:43070b2dfc87 136 while(serial->readable()==0){}
etiene32 0:43070b2dfc87 137 if(serial->readable()==1){
etiene32 0:43070b2dfc87 138 serial->read(buffer, PACKET_SIZE, NULL);
etiene32 0:43070b2dfc87 139 for(int i=1;i<33;i++){
etiene32 0:43070b2dfc87 140 printf("%d ",buffer[i]);
etiene32 0:43070b2dfc87 141 }
etiene32 0:43070b2dfc87 142 if(buffer[1]==0x42){
etiene32 0:43070b2dfc87 143 if(buffer[2]==0x4d){
etiene32 0:43070b2dfc87 144 pm10_count = ((buffer[9] << 8) + buffer[10]);
etiene32 0:43070b2dfc87 145 pm2_5_count = ((buffer[7] << 8) + buffer[8]);
etiene32 0:43070b2dfc87 146 }
etiene32 0:43070b2dfc87 147 }
etiene32 0:43070b2dfc87 148 }
etiene32 0:43070b2dfc87 149 }