BLYNK TEST

Dependencies:   mbed Blynk

Committer:
lixianyu
Date:
Wed Jun 15 03:08:40 2016 +0000
Revision:
2:6cd3b0947188
Parent:
1:0e75de2a5d21
Child:
3:4cd9171ba989
PM2.5????????5V??????????

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lixianyu 0:d8f4c441e032 1 //#include <SoftwareSerial.h>
lixianyu 0:d8f4c441e032 2 #include "MicroduinoPinNames.h"
lixianyu 2:6cd3b0947188 3 #include "AM2321.h"
lixianyu 0:d8f4c441e032 4
lixianyu 2:6cd3b0947188 5 extern Serial pc;
lixianyu 2:6cd3b0947188 6 #define INTERVAL_pm25 200
lixianyu 0:d8f4c441e032 7
lixianyu 0:d8f4c441e032 8 //SoftwareSerial pmSerial(4,5); //PM2.5传感器通讯软串口
lixianyu 2:6cd3b0947188 9 Serial pmSerial(D5, D4); //tx,rx
lixianyu 0:d8f4c441e032 10 AnalogIn gLight(A0);
lixianyu 0:d8f4c441e032 11 AnalogIn gCH4(A2);
lixianyu 2:6cd3b0947188 12 AM2321 am2321;
lixianyu 0:d8f4c441e032 13
lixianyu 1:0e75de2a5d21 14 float sensor_tem,sensor_hum,sensor_light,Sensor_etoh;
lixianyu 0:d8f4c441e032 15
lixianyu 0:d8f4c441e032 16 float sensorPM25;
lixianyu 0:d8f4c441e032 17
lixianyu 0:d8f4c441e032 18 long map(long x, long in_min, long in_max, long out_min, long out_max)
lixianyu 0:d8f4c441e032 19 {
lixianyu 0:d8f4c441e032 20 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
lixianyu 0:d8f4c441e032 21 }
lixianyu 0:d8f4c441e032 22
lixianyu 2:6cd3b0947188 23 void PM25_init(void)
lixianyu 2:6cd3b0947188 24 {
lixianyu 2:6cd3b0947188 25 //pc.printf("Enter PM25_init\r\n");
lixianyu 2:6cd3b0947188 26 pmSerial.baud(2400);
lixianyu 2:6cd3b0947188 27 }
lixianyu 2:6cd3b0947188 28
lixianyu 0:d8f4c441e032 29 // 读取PM2.5传感器
lixianyu 2:6cd3b0947188 30 void PM25()
lixianyu 0:d8f4c441e032 31 {
lixianyu 2:6cd3b0947188 32 //pc.printf("Enter PM25()\r\n");
lixianyu 0:d8f4c441e032 33 int data_s = 0; //串口接收数据
lixianyu 0:d8f4c441e032 34 int num = -1; //串口接收数据计数
lixianyu 0:d8f4c441e032 35 int sum = 0; //校验和
lixianyu 0:d8f4c441e032 36 int cal[6]; //接收数据缓存
lixianyu 0:d8f4c441e032 37 float dustDensity = 0; //PM2.5浓度
lixianyu 0:d8f4c441e032 38
lixianyu 0:d8f4c441e032 39 //pmSerial.begin(2400); //首先启动软串口
lixianyu 2:6cd3b0947188 40 //pmSerial.baud(2400);
lixianyu 0:d8f4c441e032 41 //pmSerial.flush(); //清空串口缓存
lixianyu 0:d8f4c441e032 42
lixianyu 0:d8f4c441e032 43 while(1) {
lixianyu 2:6cd3b0947188 44 //pc.printf("PM25:while(1)\r\n");
lixianyu 0:d8f4c441e032 45 //if(pmSerial.available() > 0) { //串口缓存有数据
lixianyu 0:d8f4c441e032 46 if (pmSerial.readable() > 0) {
lixianyu 2:6cd3b0947188 47 //if ((data_s = pmSerial.getc()) != 0) {
lixianyu 2:6cd3b0947188 48 //myled = 0;
lixianyu 2:6cd3b0947188 49 //pc.printf("readable() > 0\r\n");
lixianyu 0:d8f4c441e032 50 //data_s = pmSerial.read(); //读取串口缓存数据
lixianyu 0:d8f4c441e032 51 data_s = pmSerial.getc();
lixianyu 0:d8f4c441e032 52 if(data_s == 0xAA) { //得到数据帧起始位
lixianyu 0:d8f4c441e032 53 num = 0; //开始计数
lixianyu 0:d8f4c441e032 54 } else if(num >= 0) {
lixianyu 0:d8f4c441e032 55 num++; //读到数据,计数+1
lixianyu 0:d8f4c441e032 56 cal[num-1] = data_s; //数据保存到缓存中
lixianyu 0:d8f4c441e032 57 if(num == 6) { //读到数据帧最后一位
lixianyu 0:d8f4c441e032 58 sum = cal[0] + cal[1] + cal[2] + cal[3]; //计算校验和
lixianyu 0:d8f4c441e032 59 if(sum == cal[4] && cal[5] == 0xFF) { //校验和匹配,数据帧最后一位为0xFF,说明接收的数据帧正常
lixianyu 0:d8f4c441e032 60 dustDensity = (cal[0]*256 + cal[1])*(5.0/1024)*550; //计算PM2.5浓度,单位ug/m3
lixianyu 0:d8f4c441e032 61 } else { //接收的数据不正常
lixianyu 0:d8f4c441e032 62 dustDensity = 0; //浓度清零
lixianyu 0:d8f4c441e032 63 }
lixianyu 0:d8f4c441e032 64 break;
lixianyu 0:d8f4c441e032 65 }
lixianyu 0:d8f4c441e032 66 }
lixianyu 0:d8f4c441e032 67 }
lixianyu 0:d8f4c441e032 68 }
lixianyu 0:d8f4c441e032 69 //pmSerial.end(); //关闭软串口
lixianyu 2:6cd3b0947188 70 //return dustDensity; //返回值
lixianyu 2:6cd3b0947188 71 sensorPM25 = dustDensity;
lixianyu 2:6cd3b0947188 72 //pc.printf("sensorPM25 = %f\r\n", sensorPM25);
lixianyu 0:d8f4c441e032 73 }
lixianyu 0:d8f4c441e032 74
lixianyu 0:d8f4c441e032 75 // 读取光照传感器
lixianyu 0:d8f4c441e032 76 void updateLight()
lixianyu 0:d8f4c441e032 77 {
lixianyu 0:d8f4c441e032 78 //sensor_light = map(analogRead(A0), 0, 1023, 0, 255);
lixianyu 2:6cd3b0947188 79 float lvf = gLight;
lixianyu 2:6cd3b0947188 80 uint16_t lt = gLight.read_u16();
lixianyu 2:6cd3b0947188 81 //pc.printf("light = %d, lvf = %f\r\n", lt, lvf);
lixianyu 2:6cd3b0947188 82 sensor_light = map(lt, 0, 65535, 0, 255);// 这里和Arduino不一样,不知道为什么
lixianyu 0:d8f4c441e032 83 }
lixianyu 0:d8f4c441e032 84
lixianyu 0:d8f4c441e032 85 // 读取甲醛传感器
lixianyu 0:d8f4c441e032 86 void updateCH4()
lixianyu 0:d8f4c441e032 87 {
lixianyu 0:d8f4c441e032 88 //Sensor_etoh = map(analogRead(A2), 0, 1023, 0, 30);
lixianyu 2:6cd3b0947188 89 uint16_t ch4 = gCH4.read_u16();
lixianyu 2:6cd3b0947188 90 Sensor_etoh = map(ch4, 0, 65535, 0, 30);
lixianyu 2:6cd3b0947188 91 //pc.printf("CH4 = %d\r\n", ch4);
lixianyu 0:d8f4c441e032 92 }
lixianyu 0:d8f4c441e032 93
lixianyu 0:d8f4c441e032 94 // 读取温湿度传感器
lixianyu 0:d8f4c441e032 95 void updateTempHumi()
lixianyu 0:d8f4c441e032 96 {
lixianyu 2:6cd3b0947188 97 static uint32_t cot = 0;
lixianyu 2:6cd3b0947188 98 cot++;
lixianyu 2:6cd3b0947188 99
lixianyu 2:6cd3b0947188 100 #if 1
lixianyu 0:d8f4c441e032 101 am2321.read();
lixianyu 0:d8f4c441e032 102 sensor_tem = am2321.temperature / 10.0;
lixianyu 0:d8f4c441e032 103 sensor_hum = am2321.humidity / 10.0;
lixianyu 2:6cd3b0947188 104 #endif
lixianyu 2:6cd3b0947188 105 //pc.printf("%u:temp = %.1f, hum = %.1f\r\n",cot, sensor_tem, sensor_hum);
lixianyu 0:d8f4c441e032 106 }