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