BLYNK TEST

Dependencies:   mbed Blynk

Revision:
2:6cd3b0947188
Parent:
1:0e75de2a5d21
Child:
3:4cd9171ba989
--- a/sensor.h	Mon Jun 13 02:21:11 2016 +0000
+++ b/sensor.h	Wed Jun 15 03:08:40 2016 +0000
@@ -1,15 +1,15 @@
 //#include <SoftwareSerial.h>
-//#include <AM2321.h>
 #include "MicroduinoPinNames.h"
+#include "AM2321.h"
 
-#define INTERVAL_pm25             200
-unsigned long pm25_time = 0;
+extern Serial pc;
+#define INTERVAL_pm25    200
 
 //SoftwareSerial pmSerial(4,5);   //PM2.5传感器通讯软串口
-Serial pmSerial(D4, D5); //rx,tx
+Serial pmSerial(D5, D4); //tx,rx
 AnalogIn gLight(A0);
 AnalogIn gCH4(A2);
-//AM2321 am2321;
+AM2321 am2321;
 
 float sensor_tem,sensor_hum,sensor_light,Sensor_etoh;
 
@@ -20,9 +20,16 @@
     return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
 }
 
+void PM25_init(void)
+{
+    //pc.printf("Enter PM25_init\r\n");
+    pmSerial.baud(2400);
+}
+
 // 读取PM2.5传感器
-float PM25()
+void PM25()
 {
+    //pc.printf("Enter PM25()\r\n");
     int data_s = 0;    //串口接收数据
     int num = -1;      //串口接收数据计数
     int sum = 0;       //校验和
@@ -30,12 +37,16 @@
     float dustDensity = 0;  //PM2.5浓度
 
     //pmSerial.begin(2400);   //首先启动软串口
-    pmSerial.baud(2400);
+    //pmSerial.baud(2400);
     //pmSerial.flush();       //清空串口缓存
 
     while(1) {
+        //pc.printf("PM25:while(1)\r\n");
         //if(pmSerial.available() > 0) { //串口缓存有数据
         if (pmSerial.readable() > 0) {
+        //if ((data_s = pmSerial.getc()) != 0) {
+            //myled = 0;
+            //pc.printf("readable() > 0\r\n");
             //data_s = pmSerial.read();   //读取串口缓存数据
             data_s = pmSerial.getc();
             if(data_s == 0xAA) {         //得到数据帧起始位
@@ -56,29 +67,40 @@
         }
     }
     //pmSerial.end();     //关闭软串口
-    return dustDensity; //返回值
+    //return dustDensity; //返回值
+    sensorPM25 = dustDensity;
+    //pc.printf("sensorPM25 = %f\r\n", sensorPM25);
 }
 
 // 读取光照传感器
 void updateLight()
 {
     //sensor_light = map(analogRead(A0), 0, 1023, 0, 255);
-    sensor_light = map(gLight.read_u16(), 0, 65535, 0, 255);
+    float lvf = gLight;
+    uint16_t lt = gLight.read_u16();
+    //pc.printf("light = %d, lvf = %f\r\n", lt, lvf);
+    sensor_light = map(lt, 0, 65535, 0, 255);// 这里和Arduino不一样,不知道为什么
 }
 
 // 读取甲醛传感器
 void updateCH4()
 {
     //Sensor_etoh = map(analogRead(A2), 0, 1023, 0, 30);
-    Sensor_etoh = map(gCH4.read_u16(), 0, 65535, 0, 30);
+    uint16_t ch4 = gCH4.read_u16();
+    Sensor_etoh = map(ch4, 0, 65535, 0, 30);
+    //pc.printf("CH4 = %d\r\n", ch4);
 }
 
 // 读取温湿度传感器
 void updateTempHumi()
 {
-#if 0
+    static uint32_t cot = 0;
+    cot++;
+    
+    #if 1
     am2321.read();
     sensor_tem = am2321.temperature / 10.0;
     sensor_hum = am2321.humidity / 10.0;
-#endif
+    #endif
+    //pc.printf("%u:temp = %.1f, hum = %.1f\r\n",cot, sensor_tem, sensor_hum);
 }