YW51 module library for calculating PM2.5

Dependencies:   SoftSerial

Files at this revision

API Documentation at this revision

Comitter:
bojun0220
Date:
Thu Mar 07 08:19:32 2019 +0000
Parent:
0:1abc1344f5eb
Commit message:
Using software serial instead of serial

Changed in this revision

SoftSerial.lib Show annotated file Show diff for this revision Revisions of this file
YW51.cpp Show annotated file Show diff for this revision Revisions of this file
YW51.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SoftSerial.lib	Thu Mar 07 08:19:32 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/Sissors/code/SoftSerial/#a0029614de72
--- a/YW51.cpp	Tue Feb 19 14:24:36 2019 +0000
+++ b/YW51.cpp	Thu Mar 07 08:19:32 2019 +0000
@@ -22,7 +22,7 @@
  */
 #include "YW51.h"
 
-YW51::YW51(PinName tx, PinName rx, int Baud) : _YW51(tx, rx)
+YW51::YW51(PinName rx, int Baud) : _YW51(NC, rx)
 {
     _YW51.baud(Baud);
 }
@@ -30,14 +30,13 @@
 int YW51::readData()
 {
     double vo;
-
     getline();
     if(msg[0]+ msg[1]+ msg[2]+ msg[3] == msg[4]) {
         vo = (msg[0] * 256.0 + msg[1])* 2.5 / 1024.0;
         pm = findK(vo) * vo;
-        return 1;
+        return 0;
     } else {
-        return 0;
+        return 1;
     }
 }
 
--- a/YW51.h	Tue Feb 19 14:24:36 2019 +0000
+++ b/YW51.h	Thu Mar 07 08:19:32 2019 +0000
@@ -20,7 +20,33 @@
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  * THE SOFTWARE.
  */
+ 
+ /*Example:
 #include "mbed.h"
+#include "YW51.h"
+#include <string>
+Serial pc(USBTX, USBRX);
+
+YW51 dev(D2, 2400);
+int main(){
+    pc.baud(9600);
+    int err;
+    pc.printf("YW-51 Testing Program!\r\n");
+    pc.printf("---------------------------\r\n");
+    while(1){
+        err = dev.readData();
+        if(err == 0){
+            pc.printf("PM2.5 = %fug/m3 \r\n", dev.ShowPM());
+            //pc.printf("%d", dev.PMStage(dev.ShowPM()));
+            }else{
+                pc.printf("Data Error!\r\n");
+            }
+        pc.printf("----------------------------\r\n");
+        }   
+}
+ */
+#include "mbed.h"
+#include "SoftSerial.h"//Software Serial implemented by Erik Oliman
 
 #ifndef MBED_YW51_H
 #define MBED_YW51_H
@@ -29,14 +55,14 @@
 {
 
 public:
-    YW51(PinName _tx, PinName _rx, int Baud);
+    YW51(PinName _rx, int Baud);
     int readData();
     double ShowPM();
     int PMStage(double pm);
 
 
 private:
-    Serial _YW51;
+    SoftSerial _YW51;
     int msg[6];
     double pm;
     int findK(double vo);