* AM2321的取温度间隔得大于2s,否则,i2c会不工作了 * SimpleTimer有个bug,会导致两次快速的读温度,现在读温度函数里加了保护 * Blynk有个bug,会导致无法把数据传到服务器 * 现在可以正常工作了
sensor.h
00001 #include "MicroduinoPinNames.h" 00002 #include "Config.h" 00003 #include "AM2321.h" 00004 00005 extern Serial pc; 00006 extern Timer g_MainTimer; 00007 #define INTERVAL_pm25 200 00008 00009 //SoftwareSerial pmSerial(4,5); //PM2.5传感器通讯软串口 00010 #ifdef OPEN_PM25 00011 Serial pmSerial(D5, D4); //tx,rx 00012 #endif 00013 AnalogIn gLight(A0); 00014 AnalogIn gCH4(A2); 00015 AM2321 am2321; 00016 00017 float sensor_tem,sensor_hum,sensor_light,Sensor_etoh; 00018 00019 float sensorPM25 = 0.6; 00020 00021 long map(long x, long in_min, long in_max, long out_min, long out_max) 00022 { 00023 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; 00024 } 00025 00026 #ifdef OPEN_PM25 00027 void PM25_init(void) 00028 { 00029 //pc.printf("Enter PM25_init\r\n"); 00030 pmSerial.baud(2400); 00031 } 00032 00033 // 读取PM2.5传感器 00034 void PM25() 00035 { 00036 //pc.printf("Enter PM25()\r\n"); 00037 int data_s = 0; //串口接收数据 00038 int num = -1; //串口接收数据计数 00039 int sum = 0; //校验和 00040 int cal[6]; //接收数据缓存 00041 float dustDensity = 0; //PM2.5浓度 00042 00043 //pmSerial.begin(2400); //首先启动软串口 00044 //pmSerial.baud(2400); 00045 //pmSerial.flush(); //清空串口缓存 00046 00047 uint32_t start = g_MainTimer.read_ms(); 00048 while(true) {//避免无限循环 00049 uint32_t nowTime = g_MainTimer.read_ms(); 00050 if (nowTime - start > 100) { 00051 break; 00052 } 00053 //pc.printf("PM25:while(1)\r\n"); 00054 //if(pmSerial.available() > 0) { //串口缓存有数据 00055 if (pmSerial.readable() > 0) { 00056 //if ((data_s = pmSerial.getc()) != 0) { 00057 //myled = 0; 00058 //pc.printf("readable() > 0\r\n"); 00059 //data_s = pmSerial.read(); //读取串口缓存数据 00060 data_s = pmSerial.getc(); 00061 if(data_s == 0xAA) { //得到数据帧起始位 00062 num = 0; //开始计数 00063 } else if(num >= 0) { 00064 num++; //读到数据,计数+1 00065 cal[num-1] = data_s; //数据保存到缓存中 00066 if(num == 6) { //读到数据帧最后一位 00067 sum = cal[0] + cal[1] + cal[2] + cal[3]; //计算校验和 00068 if(sum == cal[4] && cal[5] == 0xFF) { //校验和匹配,数据帧最后一位为0xFF,说明接收的数据帧正常 00069 dustDensity = (cal[0]*256 + cal[1])*(5.0/1024)*550; //计算PM2.5浓度,单位ug/m3 00070 } else { //接收的数据不正常 00071 dustDensity = 0; //浓度清零 00072 } 00073 break; 00074 } 00075 } 00076 } 00077 } 00078 //pmSerial.end(); //关闭软串口 00079 //return dustDensity; //返回值 00080 sensorPM25 = dustDensity; 00081 //pc.printf("sensorPM25 = %f\r\n", sensorPM25); 00082 } 00083 #endif 00084 00085 #if 0 00086 // 读取光照传感器 00087 void updateLight() 00088 { 00089 //sensor_light = map(analogRead(A0), 0, 1023, 0, 255); 00090 //float lvf = gLight; 00091 int lt = (int)(gLight.read()*1024.0); 00092 00093 //pc.printf("light = %d, lvf = %f\r\n", lt, lvf); 00094 sensor_light = map(lt, 0, 1024, 0, 255);// 这里和Arduino不一样,不知道为什么 00095 00096 } 00097 #else 00098 void updateLight(void) 00099 { 00100 uint16_t lt = gLight.read_u16(); 00101 pc.printf("lt0 = 0x%x, ", lt); 00102 lt = lt & 0x03FF;// 10bit 00103 //lt = lt >> 2; 00104 //lt = (lt & 0x0FFF)>>2; 00105 //lt = (lt >> 2) & 0x03FF; 00106 sensor_light = map(lt, 0, 1023, 0, 255); 00107 //sensor_light = map(lt, 0, 65535, 0, 65535); 00108 pc.printf("lt=0x%x, sensor_light=%f\r\n", lt, sensor_light); 00109 sensorPM25 += 1.0; 00110 } 00111 #endif 00112 00113 // 读取甲醛传感器 00114 void updateCH4() 00115 { 00116 //Sensor_etoh = map(analogRead(A2), 0, 1023, 0, 30); 00117 uint16_t ch4 = gCH4.read_u16(); 00118 //ch4 = ch4 & 0x03FF; 00119 //Sensor_etoh = map(ch4, 0, 1023, 0, 30); 00120 Sensor_etoh = map(ch4, 0, 65535, 0, 30); 00121 //pc.printf("CH4 = %d\r\n", ch4); 00122 } 00123 00124 // 读取温湿度传感器 00125 void updateTempHumi(void) 00126 { 00127 static uint32_t lstTime = 0; 00128 uint32_t nowTime = g_MainTimer.read_ms(); 00129 pc.printf("Enter updateTempHumi(), lstTime:%ums, nowTime:%u, cha:%u\r\n", lstTime, nowTime, nowTime - lstTime); 00130 if (nowTime - lstTime < 2000) { 00131 return; 00132 } 00133 am2321.read(); 00134 sensor_tem = am2321.temperature / 10.0; 00135 sensor_hum = am2321.humidity / 10.0; 00136 //pc.printf("%u:temp = %.1f, hum = %.1f\r\n",cot, sensor_tem, sensor_hum); 00137 lstTime = g_MainTimer.read_ms(); 00138 }
Generated on Tue Jul 12 2022 13:01:30 by 1.7.2