* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了

Dependencies:   mbed

Committer:
lixianyu
Date:
Thu Jun 23 11:16:14 2016 +0000
Revision:
0:740c1eb2df13
Child:
1:e34100dd6532
* AM2321?????????2s????i2c?????; * SimpleTimer??bug?????????????????????????; * Blynk??bug??????????????; * ?????????

Who changed what in which revision?

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