add all sensors used
Dependencies: mbed QEI-1 nRF24L01P xiugai
Revision 7:dc221ddd4588, committed 2020-12-01
- Comitter:
- brainliang
- Date:
- Tue Dec 01 01:38:26 2020 +0000
- Parent:
- 6:7db9b13ece76
- Commit message:
- peixun_template
Changed in this revision
--- a/main.cpp Sun Nov 03 08:26:46 2019 +0000 +++ b/main.cpp Tue Dec 01 01:38:26 2020 +0000 @@ -4,82 +4,17 @@ #include <string> typedef bool boolean; typedef std::string String; -#include "QEI.h" -#include "converters.h" -int Position; -float Error; -int Output; -float Error_Last; -float P; -float Error_int; -float I; -float Error_diff; -float D; - -QEI qei_PA_0(PA_0,PA_1,NC,13,QEI::X4_ENCODING); -Serial Serial_2(PA_2,PA_3); -PwmOut myServoPB_0(PB_0); -PwmOut myServoPB_1(PB_1); -Ticker tick241376; - -void PID_Caculation() { -Error = 0 - Position; -Error_diff = Error - Error_Last; -Error_Last = Error; -Error_int = Error_int + Error; -if (Error_int > 100) { -Error_int = 100; -} else if (Error_int < -100) { -Error_int = -100; -} -if (Error > -10 && Error < 10) { -Output = 0; -} else { -Output = P * Error + (I * Error_int + D * Error_diff); -} -if (Output > 100) { -Output = 100; -} else if (Output < -100) { -Output = -100; -} -} - -void Set_speed() { -if (Output >= 0) { -myServoPB_0.period_ms(20); -myServoPB_0.pulsewidth_us((200 * Output)); -myServoPB_1.period_ms(20); -myServoPB_1.pulsewidth_us(0); -} else if (Output < 0) { -myServoPB_0.period_ms(20); -myServoPB_0.pulsewidth_us(0); -myServoPB_1.period_ms(20); -myServoPB_1.pulsewidth_us((-200 * Output)); -} -} - -void tick241376_handle() { -Position = Position + qei_PA_0.getPulses(); -qei_PA_0.reset(); -Serial_2.printf("%d\n",_p(Position)); -PID_Caculation(); -Set_speed(); -} - +DigitalOut myDigitalOutPA_0(PC_13); int main() { -Serial_2.baud(9600); - -tick241376.attach(&tick241376_handle,0.05); -Position = 0; -qei_PA_0.reset(); -P = 1; -I = 0; -D = 1; while (true) { +myDigitalOutPA_0.write(HIGH); +wait_ms(1000); +//myDigitalOutPA//_0.write(LOW); +//wait_ms(1000); } } \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensors/examples/Example_for_sr501.cpp Tue Dec 01 01:38:26 2020 +0000 @@ -0,0 +1,113 @@ +#include "sensors.h" +#include "mbed.h" +#include "esp8266.h" +#include "useful_func.h" + +Serial ser2usb(PA_9, PA_10, 9600); +DigitalOut myled(PC_13); + +//�����豸���ƣ����ڶ��ĺͷ�����Ϣ /********************************/ +const char device[] = "living_room"; /* �����Ľڵ����� */ + /*------------------------------*/ +//�������д�����, ÿ��һ��, ÿ�������ֵ�λ���������, ��λһ�㲻д +// ������������Ĵ��������ƻ�������������, ����: lightIntencity +const char* sensors[][2] = { /*------------------------------*/ + "sr501", "", /* �������Ӵ����� */ + "report", "", /*------------------------------*/ + NULL, NULL //last line must end with empty pointer +}; + +//��������ִ����, ÿ��һ��, ÿ�������ֲ���������������� +// ����������ִ��������, ����: fan +const char* actuators[][2] = { /*------------------------------*/ + "command", "", /* ��������ִ���� */ + "dat", "", /********************************/ + "light", "", + NULL, NULL //last line must end with empty pointer +}; + +int main() +{ + // ���������� + sr501 x(PA_0); + + // ����ִ���� + DigitalOut light(PA_13); + + /*----- ��ʼ���� -----*/ + // connect to wifi + ser2usb.printf("Initializing...\r\n"); + Esp8266 client(PB_10, PB_11, "iot_b827eb8fb527", "7c02b50b");// TX pin / RX pin / SSID / Password + + // connect to server + ser2usb.printf("connecting to mqtt server...\r\n"); + mqtt_client_init(client, device, sensors, actuators); + + // done report + ser2usb.printf("--Initialization done.--\r\n"); + + + /*----- �������� -----*/ + // declare buff + char actuator_name[32], control_value[32]; + + + bool sr501_status = false; + + + /*----- ������ѭ�� -----*/ + while (1) + { + // looking for new command + if (client.get_control_cmd(actuator_name, control_value)) + { + ser2usb.printf("Received %s %s\r\n", actuator_name, control_value); + if (strcmp(actuator_name, "command") == 0) // �յ�command�µ�ָ�� + { + ser2usb.printf("received_a_command\r\n"); + client.publish_value("report", "received_a_command"); + } + else if (strcmp(actuator_name, "dat") == 0) // �յ�dat�µ�ָ�� + { + ser2usb.printf("received_a_data\r\n"); + client.publish_value("report", "received_a_data"); + } + else if (strcmp(actuator_name, "light") == 0) // �յ�light�µ�ָ�� + { + ser2usb.printf("light control\r\n"); + client.publish_value("report", "light_control"); + if (strcmp(control_value, "on") == 0) // �����on + { // ���� + light = 1; + ser2usb.printf("On. \r\n"); + } + else if (strcmp(control_value, "off") == 0) // �����off + { // �ص� + light = 0; + ser2usb.printf("Off. \r\n"); + } + } + else + { + ser2usb.printf("unknow actuator\r\n"); + client.publish_value("report", "unknow_actuator"); + } + + if (x == sr501_status) + { + ; + } + else + { + sr501_status = x.read(); + char sensor_buff[32]; /*++++++++++++++++++++++++++++++++++++++++++*/ + sprintf(sensor_buff, "%d", sr501_status); /* �ϴ����ݷ��� */ + client.publish_value(sensors[0][0], "_upload"); /* �������report�·���_upload��Ϣ */ + wait(0.01); /* ��һ��ʱ����ϴ����������� */ + client.publish_value(sensors[0][0], sensor_buff); /* �ϴ����������ݺ��һ��ʱ�� */ + wait(0.01); /* Ȼ�������report�·���_end��Ϣ */ + client.publish_value(sensors[0][0], "_end"); /*++++++++++++++++++++++++++++++++++++++++++*/ + } + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensors/sensors.cpp Tue Dec 01 01:38:26 2020 +0000 @@ -0,0 +1,728 @@ +#include "sensors.h" + +#ifdef ENABLE_SENSORS + +#include "mbed.h" + +extern Serial ser2usb; +extern DigitalOut myled; +//============================================================SR501 +void sr501::triggered() //触发中断!! +{ +// ser2usb.printf("sr501 Triggered!\r\n"); + status = true; +} +//-------- +sr501::sr501(PinName pSignal) //启动热释电!!! + : status(false), signal(pSignal) +{ + signal.rise(callback(this, &sr501::triggered)); + ser2usb.printf("sr501 start!\r\n"); +} +//------- +bool sr501::operator==(const bool &target) +{ + if(status == target) + { + return true; + } + else + { + return false; + } +} +//------- +void sr501::reset() +{ + status = false; +} +//------- +int sr501::read() +{ + return signal.read(); +} +//------- +#ifdef ENABLE_ESP8266 +void sr501::send_status(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%d", this->read()); + + ser2usb.printf("-----sr501-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + /*++++++++++++++++++++++++++++++++++++++++++*/ + client.publish_value(topic, "_upload"); /* 务必先在topic下发布_upload消息 */ + wait(0.01); /* 等一段时间后上传传感器数据 */ + client.publish_value(topic, buff); /* 上传传感器数据后等一段时间 */ + wait(0.01); /* 然后务必在topic下发布_end消息 */ + client.publish_value(topic, "_end"); /*++++++++++++++++++++++++++++++++++++++++++*/ + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 +//========================================== //BH1750 +BH1750::BH1750(PinName sda,PinName scl) //启动光强!!!!!(默认设置) + :link(sda,scl) +{ + status = true; + char mode[1]={BH1750_CONTINUOUS_HIGH_RES_MODE}; + ser2usb.printf("modifying\r\n"); + while(status) + { + status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false); + wait_ms(10); + } + ser2usb.printf("BH1750 start with default mode!\r\n"); +} +//-------- +BH1750::BH1750(PinName sda,PinName scl,char mode[]) //启动光强!!!!!(自定义设置) + :link(sda,scl) +{ + status = true; + while(status) + { + status = link.write(BH1750_I2CADDR, mode, sizeof(mode), false); + wait_ms(10); + } + ser2usb.printf("BH1750 start with customize mode!\r\n"); +} +//-------- +float BH1750::getlightdata() //读取光强(lux) +{ + status = true; + status = link.read(BH1750_I2CADDR, rawdata, 2, false); + if(!status) + { + float result = ((rawdata[0]<<8)|rawdata[1])/1.2; + return result; + } + else + { + ser2usb.printf("BH1750 read fail!\r\n"); + return -1; + } +} + +#ifdef ENABLE_ESP8266 +void BH1750::send_data(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%.3f", this->getlightdata()); + + ser2usb.printf("-----BH1750-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 +//==============================================MQ-2 +mq::mq(PinName dio) + : status(false), signal(dio),signallevel(PC_13) +{ + signal.fall(callback(this, &mq::triggered)); + ser2usb.printf("mq start!\r\n"); +} +//-------- +mq::mq(PinName dio,PinName aio) + : status(false), signal(dio),signallevel(aio) +{ + signal.fall(callback(this, &mq::triggered)); + ser2usb.printf("mq start!\r\n"); +} +//-------- +void mq::triggered() //触发中断!! +{ + ser2usb.printf("mq Triggered!\r\n"); + status = true; +} +//-------- +bool mq::operator==(const bool &target) +{ + if(status == target) + { + return true; + } + else + { + return false; + } +} +//-------- +float mq::getairdata() +{ + return signallevel.read(); +} +//----------- +void mq::reset() +{ + status = false; +} +int mq::read() +{ + return signal.read(); +} + +#ifdef ENABLE_ESP8266 +void mq::send_data(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%d", this->read()); + + ser2usb.printf("-----mq-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 +//===========================================DHT11 +dht11::dht11(PinName pin) + :datapin(pin) +{ + starttime.start(); + ser2usb.printf("dht11 start!\r\n"); +} +//---------- +int dht11::getdata() +{ + int timeout=10000; + uint8_t data[5]; + uint8_t bit=7; + uint8_t count=0; + + for(int i=0;i<5;i++) + { + data[i]=0; + } + if(starttime.read_ms()<1500){while(starttime.read_ms()<1500){}starttime.stop();} + + datapin.output(); + datapin=0; + wait_ms(19); + datapin=1; + wait_us(30); + datapin.input(); + + while(!datapin) + { + if(timeout--==0){ser2usb.printf("timeout!no reset\r\n");return 0;} + } + timeout=10000; + while(datapin) + { + if(timeout--==0){ser2usb.printf("timeout! no response\r\n");return 0;} + } + timeout=10000; + for(int i=0;i<40;i++) + { + while(!datapin) + { + if(timeout--==0){ser2usb.printf("timeout!\r\n");return 0;} + } + timer.start(); + timeout=10000; + while(datapin) + { + if(timeout--==0){ser2usb.printf("timeout!n\r\n");return 0;} + } + timeout=10000; + long t=timer.read_us(); + timer.stop(); + timer.reset(); + + if(bit==0) + { + if(t>40){data[count]|=(1<<bit);} + bit=7; + count++; + } + else + { + if(t>40) + { + data[count]|=(1<<bit); + } + bit--; + } + } + datapin=1; + if(data[4]==data[0]+data[1]+data[2]+data[3]) + { + //ser2usb.printf("Humidity (%):%f\r\n",(float)data[0]); + //ser2usb.printf("Temperature (oC): %f\r\n",(float)data[2]); + H=data[0]+data[1]/10.0; + T=data[2]+data[3]/10.0; + return 1; + } + else + { + ser2usb.printf("error!\r\ndata0:%d\tdata1:%d\r\ndata2:%d\tdata3:%d\r\ndata4:%d\r\n", + (int)data[0], + (int)data[1], + (int)data[2], + (int)data[3], + (int)data[4]); + return 0; + } +} +//------- +float dht11::gethumidity() +{ + return H; +} +//------- +float dht11::gettemperature() +{ + return T; +} + +#ifdef ENABLE_ESP8266 +void dht11::send_temp(Esp8266 & client, const char * topic) +{ + char buff[32]; + if(this->getdata()) + sprintf(buff, "%.3f", this->gettemperature()); + else + { + ser2usb.printf("dht11 not aviliable\r\n"); + return; + } + + ser2usb.printf("Sending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +void dht11::send_humi(Esp8266 & client, const char * topic) +{ + char buff[32]; + if(this->getdata()) + sprintf(buff, "%.3f", this->gethumidity()); + else + { + ser2usb.printf("dht11 not aviliable\r\n"); + return; + } + + ser2usb.printf("-----dht11-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 +//============================================DS18B20 +DS18B20::DS18B20(PinName pin) + :datapin(pin) +{ + if(start()){ser2usb.printf("DS18B20 started\r\n");} +} +//----------- +int DS18B20::start() +{ + //ser2usb.printf("starting\r\n"); + datapin.output(); + datapin=0; + wait_us(600); + datapin.input(); + datapin.mode(PullUp); + wait_us(70); + while(datapin.read()){return 0;} + //ser2usb.printf("started\r\n"); + wait_us(250); + return 1; +} +//---------- +void DS18B20::writebyte(uint8_t send) +{ + datapin.output(); + datapin=1; + wait_us(20); + for(int i=0;i<8;i++) + { + datapin.output(); + datapin=0; //产生读写时序的起始信号 + wait_us(2); //要求至少1us的延时 + datapin=send & 0x01; //对总线赋值,从最低位开始写起 + wait_us(70);//延时70us,写0在60~120us之间释放,写1的话大于60us均可释放 + datapin=1; //释放总线,为下一次mcu送数据做准备, + send>>=1; //有效数据移动到最低位,2次写数据间隙至少需1us + } + datapin.output(); + datapin=1; + wait_us(70); +} +//----------- +uint8_t DS18B20::readByte() //mcu读一个字节 +{ + uint8_t i,value=0; + for(i=0;i<8;i++) + { + datapin.output(); + datapin=0; //起始信号 + wait_us(2); + datapin.input(); //mcu释放总线 + datapin.mode(PullUp); + if(datapin) + { + value=value|0x80;//保存高电平数据,低电平的话不用保存,移位后默认是0 + } + value>>=1; + wait_us(30); //延时40us + } + return value; +} +//------------ +float DS18B20::transfer(uint8_t h,uint8_t l) +{ + //h=0x01; + //l=0x01; + int flag=(h&0x01)>>7; + if(!flag) + { + float i=(h*256+l)*0.25; + //ser2usb.printf("flag:%d\th:%d\tl:%d\r\n",flag,h,l); + return i; + } + else + { + float i=((~h)*256+(~l))*0.25*-1; + //ser2usb.printf("flag:%d\th:%d\tl:%d\r\n",flag,h,l); + return i; + } +} +//---------- +int DS18B20::getdata() +{ + T=5000; + start(); + writebyte(0xcc); + writebyte(0x44); + wait(2); + + start(); + writebyte(0xcc); + writebyte(0xbe); + uint8_t a=readByte();//l + uint8_t b=readByte();//h + T=transfer(b,a); + + if(T==5000){return 0;} + else{return 1;} +} +//---------- +float DS18B20::gettemperature() +{ + if(getdata()){return T;} + else{ser2usb.printf("get temperature fail!\r\n");return 0;} +} + +#ifdef ENABLE_ESP8266 +void DS18B20::send_temp(Esp8266 & client, const char * topic) +{ + char buff[32]; + if(this->getdata()) + sprintf(buff, "%.3f", this->gettemperature()); + else + { + ser2usb.printf("dht11 not aviliable\r\n"); + return; + } + + ser2usb.printf("-----DS18B20-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 + +//==============================================YL-38 +YL::YL(PinName dio) + : status(false), signal(dio),signallevel(PC_13) +{ + signal.fall(callback(this, &YL::triggered)); + ser2usb.printf("YL start!\r\n"); +} +//-------- +YL::YL(PinName dio,PinName aio) + : status(false), signal(dio),signallevel(aio) +{ + signal.fall(callback(this, &YL::triggered)); + ser2usb.printf("YL start!\r\n"); +} +//-------- +void YL::triggered() //触发中断!! +{ + ser2usb.printf("YL Triggered!\r\n"); + status = true; +} +//-------- +bool YL::operator==(const bool &target) +{ + if(status == target) + { + return true; + } + else + { + return false; + } +} +//-------- +float YL::getairdata() +{ + return signallevel.read(); +} +//----------- +void YL::reset() +{ + status = false; +} +int YL::read() +{ + return signal.read(); +} + +#ifdef ENABLE_ESP8266 +void YL::send_data(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%d", this->read()); + + ser2usb.printf("-----YL-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 +//============================================= +BMP180::BMP180(PinName sda,PinName scl) + :i2c(sda,scl) +{ + OSS=OSS_3; + uint8_t c = readByte(BMP180_ADDRESS, BMP180_WHO_AM_I); + if(c == 0x55) + { + ser2usb.printf("BMP-180 is 0x%x\r\n", c); + ser2usb.printf("BMP-180 should be 0x55\r\n"); + ser2usb.printf("BMP-180 online...\r\n"); + + BMP180Calibration(); + ser2usb.printf("BMP-180 calibration complete...\r\n"); + } + else + { + ser2usb.printf("BMP-180 is 0x%x\r\n", c); + ser2usb.printf("BMP-180 should be 0x55\r\n"); + while(1); // idle here forever + } +} +void BMP180::writeByte(uint8_t address, uint8_t subAddress, uint8_t data) +{ + char data_write[2]; + data_write[0] = subAddress; + data_write[1] = data; + i2c.write(address, data_write, 2, 0); +} + +char BMP180::readByte(uint8_t address, uint8_t subAddress) +{ + char data[1]; // `data` will store the register data + char data_write[1]; + data_write[0] = subAddress; + i2c.write(address, data_write, 1, 1); // no stop + i2c.read(address, data, 1, 0); + return data[0]; +} + +void BMP180::readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest) +{ + char data[14]; + char data_write[1]; + data_write[0] = subAddress; + i2c.write(address, data_write, 1, 1); // no stop + i2c.read(address, data, count, 0); + for(int ii = 0; ii < count; ii++) { + dest[ii] = data[ii]; + } +} + + +// Stores all of the BMP180's calibration values into global variables +// Calibration values are required to calculate temp and pressure +// This function should be called at the beginning of the program +// These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics +void BMP180::BMP180Calibration() +{ + ac1 = readByte(BMP180_ADDRESS, 0xAA) << 8 | readByte(BMP180_ADDRESS, 0xAB); + ac2 = readByte(BMP180_ADDRESS, 0xAC) << 8 | readByte(BMP180_ADDRESS, 0xAD); + ac3 = readByte(BMP180_ADDRESS, 0xAE) << 8 | readByte(BMP180_ADDRESS, 0xAF); + ac4 = readByte(BMP180_ADDRESS, 0xB0) << 8 | readByte(BMP180_ADDRESS, 0xB1); + ac5 = readByte(BMP180_ADDRESS, 0xB2) << 8 | readByte(BMP180_ADDRESS, 0xB3); + ac6 = readByte(BMP180_ADDRESS, 0xB4) << 8 | readByte(BMP180_ADDRESS, 0xB5); + b1 = readByte(BMP180_ADDRESS, 0xB6) << 8 | readByte(BMP180_ADDRESS, 0xB7); + b2 = readByte(BMP180_ADDRESS, 0xB8) << 8 | readByte(BMP180_ADDRESS, 0xB9); + mb = readByte(BMP180_ADDRESS, 0xBA) << 8 | readByte(BMP180_ADDRESS, 0xBB); + mc = readByte(BMP180_ADDRESS, 0xBC) << 8 | readByte(BMP180_ADDRESS, 0xBD); + md = readByte(BMP180_ADDRESS, 0xBE) << 8 | readByte(BMP180_ADDRESS, 0xBF); +} + +// Temperature returned will be in units of 0.1 deg C +long BMP180::BMP180GetTemperature() +{ + long ut = 0; + uint8_t rawData[2] = {0, 0}; + + writeByte(BMP180_ADDRESS, 0xF4, 0x2E); // start temperature measurement + wait_ms(5); + readBytes(BMP180_ADDRESS, 0xF6, 2, &rawData[0]); // read raw temperature measurement + ut = rawData[0]*256+ rawData[1]; + + long x1, x2; + + x1 = (((long)ut - (long)ac6)*(long)ac5) >> 15; + x2 = ((long)mc << 11)/(x1 + md); + b5 = x1 + x2; + + return ((b5 + 8)>>4)/10.0; +} + +// Calculate pressure read calibration values +// b5 is also required so BMP180GetTemperature() must be called first. +// Value returned will be pressure in units of Pa. +long BMP180::BMP180GetPressure() +{ + long up = 0; + writeByte(BMP180_ADDRESS, 0xF4, 0x34 | OSS << 6); // Configure pressure measurement for highest resolution + wait_ms(5+8*OSS); // delay 5 ms at lowest resolution, 29 ms at highest + uint8_t rawData[3] = {0, 0, 0}; + readBytes(BMP180_ADDRESS, 0xF6, 3, &rawData[0]); // read raw pressure measurement of 19 bits + up = (((long) rawData[0] << 16) | ((long)rawData[1] << 8) | rawData[2]) >> (8 - OSS); + + long x1, x2, x3, b3, b6, p; + unsigned long b4, b7; + + b6 = b5 - 4000; + // Calculate B3 + x1 = (b2 * (b6 * b6)>>12)>>11; + x2 = (ac2 * b6)>>11; + x3 = x1 + x2; + b3 = (((((long)ac1)*4 + x3)<<OSS) + 2)>>2; + + // Calculate B4 + x1 = (ac3 * b6)>>13; + x2 = (b1 * ((b6 * b6)>>12))>>16; + x3 = ((x1 + x2) + 2)>>2; + b4 = (ac4 * (unsigned long)(x3 + 32768))>>15; + + b7 = ((unsigned long)(up - b3) * (50000>>OSS)); + if (b7 < 0x80000000) + p = (b7<<1)/b4; + else + p = (b7/b4)<<1; + + x1 = (p>>8) * (p>>8); + x1 = (x1 * 3038)>>16; + x2 = (-7357 * p)>>16; + p += (x1 + x2 + 3791)>>4; + + return p; +} + +#ifdef ENABLE_ESP8266 +void BMP180::send_press(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%ld", this->BMP180GetPressure()); + + ser2usb.printf("-----BMP180-----Sending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} + +void BMP180::send_temp(Esp8266 & client, const char * topic) +{ + char buff[32]; + sprintf(buff, "%ld", this->BMP180GetTemperature()); + + ser2usb.printf("-----BMP180-----\r\nSending message \"%s\" to topic \"%s\"\r\n", buff, topic); + + client.publish_value(topic, "_upload"); + wait(0.01); + client.publish_value(topic, buff); + wait(0.01); + client.publish_value(topic, "_end"); + + ser2usb.printf("Senging data done.\r\n"); +} +#endif // ENABLE_ESP8266 + +//=============================== +GP2Y1010::GP2Y1010(PinName led,PinName measure) + :measurePin(measure),ledPower(led) +{ + samplingTime = 280; + deltaTime = 40; + sleepTime = 9680; + + voMeasured = 0; + calcVoltage = 0; + dustDensity = 0; +} + +float GP2Y1010::getairdata() +{ + ledPower=1; + + wait_us(samplingTime); + + voMeasured = measurePin.read(); + + wait_us(deltaTime); + ledPower=0; + wait_us(sleepTime); + + calcVoltage = voMeasured*5.0; + dustDensity = 0.17*calcVoltage-0.1; + + if ( dustDensity < 0) + { + dustDensity = 0.00; + } + return dustDensity ; +} + +#endif // ENABLE_SENSORS
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sensors/sensors.h Tue Dec 01 01:38:26 2020 +0000 @@ -0,0 +1,258 @@ +#ifndef __SENSORS_H +#define __SENSORS_H + +#ifdef ENABLE_SENSORS + +#include "mbed.h" +#include "esp8266.h" + + + +//========================================== +class sr501 +{ +/***** + * 红外热释电对象 + * 感应输出高电平 +*****/ + private: +// DigitalIn signal; + bool status; + InterruptIn signal; + void triggered(); + public: + sr501(PinName pSignal); + bool operator==(const bool &target); + void reset(); + int read(); + +#ifdef ENABLE_ESP8266 + void send_status(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 + +}; +//========================================= +//+++++++++++++++++++++BH1750+++++++++++++++++++++++++++++ +#define BH1750_I2CADDR 0x46 +#define BH1750_POWER_DOWN 0x00 // No active state +#define BH1750_POWER_ON 0x01 // Wating for measurment command +#define BH1750_RESET 0x07 // Reset data register value - not accepted in POWER_DOWN mode +#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_HIGH_RES_MODE_2 0x11 // Start measurement at 0.5lx resolution. Measurement time is approx 120ms. +#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 // Start measurement at 4lx resolution. Measurement time is approx 16ms. + +// Start measurement at 1lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE 0x20 + +// Start measurement at 0.5lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_HIGH_RES_MODE_2 0x21 + +// Start measurement at 1lx resolution. Measurement time is approx 120ms. +// Device is automatically set to Power Down after measurement. +#define BH1750_ONE_TIME_LOW_RES_MODE 0x23 +//++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +class BH1750 +{ + +/***** +* +*****/ + private: + I2C link; + char rawdata[2]; + bool status; + public: + BH1750(PinName sda,PinName scl); + BH1750(PinName sda,PinName scl,char mode[]); + float getlightdata(); + +#ifdef ENABLE_ESP8266 + void send_data(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 + +}; +//=========================================== +class mq +{ +/***** +* +*****/ + private: + bool status; + InterruptIn signal; + AnalogIn signallevel; + + void triggered(); + public: + mq(PinName dio); + mq(PinName dio,PinName aio); + bool operator==(const bool &target); + float getairdata(); + void reset(); + int read(); + +#ifdef ENABLE_ESP8266 + void send_data(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 + +}; + +//=========================================== +class dht11 +{ +/***** +* +*****/ + private: + Timer timer; + Timer starttime; + DigitalInOut datapin; + float H; + float T; + public: + dht11(PinName pin); + int getdata(); + float gethumidity(); + float gettemperature(); + +#ifdef ENABLE_ESP8266 + void send_temp(Esp8266 & client, const char * topic); + void send_humi(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 + +}; +//==========================================DS18B20 +class DS18B20 +{ +/***** +* +*****/ + private: + DigitalInOut datapin; + uint8_t dat; + float T; + + int start(); + void writebyte(uint8_t send); + uint8_t readByte(); + float transfer(uint8_t h,uint8_t l); + public: + DS18B20(PinName pin); + int getdata(); + float gettemperature(); + +#ifdef ENABLE_ESP8266 + void send_temp(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 + +}; +//==========================================YL-38通用传感 +class YL +{ +/***** +* +*****/ + private: + bool status; + InterruptIn signal; + AnalogIn signallevel; + + void triggered(); + public: + YL(PinName dio); + YL(PinName dio,PinName aio); + bool operator==(const bool &target); + float getairdata(); + void reset(); + int read(); + +#ifdef ENABLE_ESP8266 + void send_data(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 +}; +//======================================== +#define BMP180_ADDRESS 0x77<<1 // I2C address of BMP180, eight bit address on mbed +#define BMP180_WHO_AM_I 0xD0 // WHO_AM_I id of BMP180, should return 0x55 +#define BMP180_RESET 0xE0 +#define BMP180_CONTROL 0xF4 +#define BMP180_OUT_MSB 0xF6 +#define BMP180_OUT_LSB 0xF7 +#define BMP180_OUT_XLSB 0xF8 + +// Set initial input parameters + +enum OSS { // BMP-085 sampling rate + OSS_0 = 0, // 4.5 ms conversion time + OSS_1=1, // 7.5 + OSS_2=2, // 13.5 + OSS_3=3 // 25.5 +}; + + + +class BMP180 { +/***** +* +*****/ +private: + uint8_t OSS; // maximum pressure resolution + + //Set up I2C, (SDA,SCL) + I2C i2c; + + // These are constants used to calculate the temperature and pressure from the BMP-180 sensor + int16_t ac1, ac2, ac3, b1, b2, mb, mc, md, b5; + uint16_t ac4, ac5, ac6; + + void writeByte(uint8_t address, uint8_t subAddress, uint8_t data); + char readByte(uint8_t address, uint8_t subAddress); + void readBytes(uint8_t address, uint8_t subAddress, uint8_t count, uint8_t * dest); + + // Stores all of the BMP180's calibration values into global variables + // Calibration values are required to calculate temp and pressure + // This function should be called at the beginning of the program + // These BMP-180 functions were adapted from Jim Lindblom of SparkFun Electronics + void BMP180Calibration(); +public: + BMP180(PinName sda,PinName scl) ; + // Temperature returned will be in units of 0.1 deg C + long BMP180GetTemperature(); + + // Calculate pressure read calibration values + // b5 is also required so BMP180GetTemperature() must be called first. + // Value returned will be pressure in units of Pa. + long BMP180GetPressure(); + +#ifdef ENABLE_ESP8266 + void send_press(Esp8266 & client, const char * topic); + void send_temp(Esp8266 & client, const char * topic); +#endif // ENABLE_ESP8266 +}; +//================================================ +class GP2Y1010 +{ +/***** +* +*****/ + private: + AnalogIn measurePin ; + DigitalOut ledPower; + unsigned int samplingTime ; + unsigned int deltaTime ; + unsigned int sleepTime ; + + float voMeasured ; + float calcVoltage ; + float dustDensity ; + + public: + GP2Y1010(PinName ledPower,PinName measurePin); + float getairdata(); +}; +//============================================= + +#endif // ENABLE_SENSORS + +#endif // !__SENSORS_H