YW51 module library for calculating PM2.5

Dependencies:   SoftSerial

Committer:
bojun0220
Date:
Thu Mar 07 08:19:32 2019 +0000
Revision:
1:9b8788ea7c85
Parent:
0:1abc1344f5eb
Using software serial instead of serial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bojun0220 0:1abc1344f5eb 1 /* YW51 module Library
bojun0220 0:1abc1344f5eb 2 *
bojun0220 0:1abc1344f5eb 3 * Copyright (c) 2019, 黃博鈞, National Taiwan University
bojun0220 0:1abc1344f5eb 4 *
bojun0220 0:1abc1344f5eb 5 * Permission is hereby granted, free of charge, to any person obtaining a copy
bojun0220 0:1abc1344f5eb 6 * of this software and associated documentation files (the "Software"), to deal
bojun0220 0:1abc1344f5eb 7 * in the Software without restriction, including without limitation the rights
bojun0220 0:1abc1344f5eb 8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
bojun0220 0:1abc1344f5eb 9 * copies of the Software, and to permit persons to whom the Software is
bojun0220 0:1abc1344f5eb 10 * furnished to do so, subject to the following conditions:
bojun0220 0:1abc1344f5eb 11 *
bojun0220 0:1abc1344f5eb 12 * The above copyright notice and this permission notice shall be included in
bojun0220 0:1abc1344f5eb 13 * all copies or substantial portions of the Software.
bojun0220 0:1abc1344f5eb 14 *
bojun0220 0:1abc1344f5eb 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bojun0220 0:1abc1344f5eb 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bojun0220 0:1abc1344f5eb 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
bojun0220 0:1abc1344f5eb 18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bojun0220 0:1abc1344f5eb 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
bojun0220 0:1abc1344f5eb 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
bojun0220 0:1abc1344f5eb 21 * THE SOFTWARE.
bojun0220 0:1abc1344f5eb 22 */
bojun0220 0:1abc1344f5eb 23 #include "YW51.h"
bojun0220 0:1abc1344f5eb 24
bojun0220 1:9b8788ea7c85 25 YW51::YW51(PinName rx, int Baud) : _YW51(NC, rx)
bojun0220 0:1abc1344f5eb 26 {
bojun0220 0:1abc1344f5eb 27 _YW51.baud(Baud);
bojun0220 0:1abc1344f5eb 28 }
bojun0220 0:1abc1344f5eb 29
bojun0220 0:1abc1344f5eb 30 int YW51::readData()
bojun0220 0:1abc1344f5eb 31 {
bojun0220 0:1abc1344f5eb 32 double vo;
bojun0220 0:1abc1344f5eb 33 getline();
bojun0220 0:1abc1344f5eb 34 if(msg[0]+ msg[1]+ msg[2]+ msg[3] == msg[4]) {
bojun0220 0:1abc1344f5eb 35 vo = (msg[0] * 256.0 + msg[1])* 2.5 / 1024.0;
bojun0220 0:1abc1344f5eb 36 pm = findK(vo) * vo;
bojun0220 1:9b8788ea7c85 37 return 0;
bojun0220 0:1abc1344f5eb 38 } else {
bojun0220 1:9b8788ea7c85 39 return 1;
bojun0220 0:1abc1344f5eb 40 }
bojun0220 0:1abc1344f5eb 41 }
bojun0220 0:1abc1344f5eb 42
bojun0220 0:1abc1344f5eb 43 double YW51::ShowPM()
bojun0220 0:1abc1344f5eb 44 {
bojun0220 0:1abc1344f5eb 45 return pm;
bojun0220 0:1abc1344f5eb 46
bojun0220 0:1abc1344f5eb 47 }
bojun0220 0:1abc1344f5eb 48 int YW51::findK(double vo)
bojun0220 0:1abc1344f5eb 49 {
bojun0220 0:1abc1344f5eb 50 if(vo <= 0.045) {
bojun0220 0:1abc1344f5eb 51 return 200;
bojun0220 0:1abc1344f5eb 52 } else if(vo > 0.046 && vo < 0.048) {
bojun0220 0:1abc1344f5eb 53 return 400;
bojun0220 0:1abc1344f5eb 54 } else if(vo > 0.049 && vo < 0.051) {
bojun0220 0:1abc1344f5eb 55 return 600;
bojun0220 0:1abc1344f5eb 56 } else if(vo > 0.052 && vo < 0.054) {
bojun0220 0:1abc1344f5eb 57 return 750;
bojun0220 0:1abc1344f5eb 58 } else if(vo > 0.055 && vo < 0.058) {
bojun0220 0:1abc1344f5eb 59 return 900;
bojun0220 0:1abc1344f5eb 60 } else if(vo > 0.059 && vo < 0.064) {
bojun0220 0:1abc1344f5eb 61 return 1000;
bojun0220 0:1abc1344f5eb 62 } else if(vo > 0.065 && vo < 0.070) {
bojun0220 0:1abc1344f5eb 63 return 1250;
bojun0220 0:1abc1344f5eb 64 } else if(vo > 0.071 && vo < 0.075) {
bojun0220 0:1abc1344f5eb 65 return 1400;
bojun0220 0:1abc1344f5eb 66 } else if(vo > 0.076 && vo < 0.080) {
bojun0220 0:1abc1344f5eb 67 return 1700;
bojun0220 0:1abc1344f5eb 68 } else if(vo > 0.081 && vo < 0.085) {
bojun0220 0:1abc1344f5eb 69 return 1800;
bojun0220 0:1abc1344f5eb 70 } else if(vo > 0.086 && vo < 0.090) {
bojun0220 0:1abc1344f5eb 71 return 1900;
bojun0220 0:1abc1344f5eb 72 } else if(vo > 0.091 && vo < 0.100) {
bojun0220 0:1abc1344f5eb 73 return 2000;
bojun0220 0:1abc1344f5eb 74 } else if(vo > 0.101 && vo < 0.110) {
bojun0220 0:1abc1344f5eb 75 return 2200;
bojun0220 0:1abc1344f5eb 76 } else if(vo >= 0.111) {
bojun0220 0:1abc1344f5eb 77 return 3000;
bojun0220 0:1abc1344f5eb 78 }
bojun0220 0:1abc1344f5eb 79 }
bojun0220 0:1abc1344f5eb 80 int YW51::PMStage(double pm)
bojun0220 0:1abc1344f5eb 81 {
bojun0220 0:1abc1344f5eb 82 if(pm <= 35) {
bojun0220 0:1abc1344f5eb 83 return 1;//good
bojun0220 0:1abc1344f5eb 84 } else if(pm > 35 && pm <= 53 ) {
bojun0220 0:1abc1344f5eb 85 return 2; //medium
bojun0220 0:1abc1344f5eb 86 } else if(pm > 53 && pm <= 70 ) {
bojun0220 0:1abc1344f5eb 87 return 3;//bad
bojun0220 0:1abc1344f5eb 88 } else {
bojun0220 0:1abc1344f5eb 89 return 4;//very bad
bojun0220 0:1abc1344f5eb 90 }
bojun0220 0:1abc1344f5eb 91 }
bojun0220 0:1abc1344f5eb 92 void YW51::getline()
bojun0220 0:1abc1344f5eb 93 {
bojun0220 0:1abc1344f5eb 94 while(_YW51.getc() != 170); // wait for the start of a line
bojun0220 0:1abc1344f5eb 95 for(int i = 0; i < 6; i++) {
bojun0220 0:1abc1344f5eb 96 msg[i] = _YW51.getc();
bojun0220 0:1abc1344f5eb 97 if(msg[i] == 255) {
bojun0220 0:1abc1344f5eb 98 return;
bojun0220 0:1abc1344f5eb 99 }
bojun0220 0:1abc1344f5eb 100 }
bojun0220 0:1abc1344f5eb 101 error("Overflowed message limit");
bojun0220 0:1abc1344f5eb 102 }
bojun0220 0:1abc1344f5eb 103