It collects environmental data and send it to the ESP module also able to receive the configuration message.

Dependencies:   BMP280 DHT mbed

Committer:
thinkfire
Date:
Tue Feb 07 20:16:43 2017 +0000
Revision:
0:085d20f0ca45
Debugging Process going smoothly...

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thinkfire 0:085d20f0ca45 1 #include "mbed.h"
thinkfire 0:085d20f0ca45 2 #include "BMP280.h"
thinkfire 0:085d20f0ca45 3 #include "DHT.h"
thinkfire 0:085d20f0ca45 4 #include "stdio.h"
thinkfire 0:085d20f0ca45 5
thinkfire 0:085d20f0ca45 6 Timer txTimer, mqTimer;
thinkfire 0:085d20f0ca45 7 Serial device(p9, p10);
thinkfire 0:085d20f0ca45 8 BMP280 bmpObj(p28, p27);
thinkfire 0:085d20f0ca45 9 DHT dhtObj(p21,DHT11);
thinkfire 0:085d20f0ca45 10 AnalogIn mqObj(p20);
thinkfire 0:085d20f0ca45 11
thinkfire 0:085d20f0ca45 12 char sensorConf[4];
thinkfire 0:085d20f0ca45 13 char msg[60];
thinkfire 0:085d20f0ca45 14
thinkfire 0:085d20f0ca45 15 bool tmpFlag = true; //temperature sensor flag
thinkfire 0:085d20f0ca45 16 bool presFlag = true; //pressure sensor flag
thinkfire 0:085d20f0ca45 17 bool co2Flag = true; //co2 sensor flag
thinkfire 0:085d20f0ca45 18 bool humFlag = true; //humidity sensor flag
thinkfire 0:085d20f0ca45 19
thinkfire 0:085d20f0ca45 20 bool rxFlag = false; //recieve packet flag
thinkfire 0:085d20f0ca45 21
thinkfire 0:085d20f0ca45 22 int bmpTmp,bmpPres,dhtHum,dhtTmp; //stores bmp280 temperature and sensor value
thinkfire 0:085d20f0ca45 23 int avgTmp; //average of bmpTmp and dhtTmp
thinkfire 0:085d20f0ca45 24 uint16_t mqCo2;
thinkfire 0:085d20f0ca45 25
thinkfire 0:085d20f0ca45 26 //enable or desable sensors
thinkfire 0:085d20f0ca45 27 void setSensors(void);
thinkfire 0:085d20f0ca45 28
thinkfire 0:085d20f0ca45 29 //get temperature and pressure values from bmp280
thinkfire 0:085d20f0ca45 30 void bmpGetValue(void);
thinkfire 0:085d20f0ca45 31
thinkfire 0:085d20f0ca45 32 //get temperature and humidity value from dht11
thinkfire 0:085d20f0ca45 33 void dhtGetValue(void);
thinkfire 0:085d20f0ca45 34
thinkfire 0:085d20f0ca45 35 //get airquality value from mq135
thinkfire 0:085d20f0ca45 36 void mqGetValue(void);
thinkfire 0:085d20f0ca45 37
thinkfire 0:085d20f0ca45 38
thinkfire 0:085d20f0ca45 39 void rx_interrupt()
thinkfire 0:085d20f0ca45 40 {
thinkfire 0:085d20f0ca45 41 int i = 0;
thinkfire 0:085d20f0ca45 42
thinkfire 0:085d20f0ca45 43 while(device.readable()){
thinkfire 0:085d20f0ca45 44 sensorConf[i++]= device.getc();
thinkfire 0:085d20f0ca45 45 i++;
thinkfire 0:085d20f0ca45 46 }
thinkfire 0:085d20f0ca45 47
thinkfire 0:085d20f0ca45 48 sensorConf[i] = '\0';
thinkfire 0:085d20f0ca45 49 rxFlag = true;
thinkfire 0:085d20f0ca45 50
thinkfire 0:085d20f0ca45 51 return;
thinkfire 0:085d20f0ca45 52 }
thinkfire 0:085d20f0ca45 53
thinkfire 0:085d20f0ca45 54 int main()
thinkfire 0:085d20f0ca45 55 {
thinkfire 0:085d20f0ca45 56 //device.attach(&rx_interrupt,device.RxIrq);
thinkfire 0:085d20f0ca45 57
thinkfire 0:085d20f0ca45 58 bmpObj.initialize(); //initialize bmp280 sensor
thinkfire 0:085d20f0ca45 59
thinkfire 0:085d20f0ca45 60 txTimer.start(); //start transmit timer
thinkfire 0:085d20f0ca45 61 mqTimer.start(); //start airquality timer
thinkfire 0:085d20f0ca45 62
thinkfire 0:085d20f0ca45 63 while (true) {
thinkfire 0:085d20f0ca45 64 bmpGetValue(); //get value from bmp280 sensor
thinkfire 0:085d20f0ca45 65 dhtGetValue(); //get value from dht11 sensor
thinkfire 0:085d20f0ca45 66
thinkfire 0:085d20f0ca45 67 if (mqTimer.read_ms()>2000) {
thinkfire 0:085d20f0ca45 68 mqGetValue();
thinkfire 0:085d20f0ca45 69 mqTimer.reset();
thinkfire 0:085d20f0ca45 70 }
thinkfire 0:085d20f0ca45 71
thinkfire 0:085d20f0ca45 72 if(rxFlag) {
thinkfire 0:085d20f0ca45 73 setSensors();
thinkfire 0:085d20f0ca45 74 rxFlag = false;
thinkfire 0:085d20f0ca45 75 }
thinkfire 0:085d20f0ca45 76
thinkfire 0:085d20f0ca45 77 if(bmpTmp!=0 && dhtTmp!=0) {
thinkfire 0:085d20f0ca45 78
thinkfire 0:085d20f0ca45 79 avgTmp = (bmpTmp + dhtTmp)/2;
thinkfire 0:085d20f0ca45 80 }
thinkfire 0:085d20f0ca45 81
thinkfire 0:085d20f0ca45 82 if(txTimer.read_ms()>8000) {
thinkfire 0:085d20f0ca45 83 if(device.writeable()) {
thinkfire 0:085d20f0ca45 84 sprintf(msg,"TMP %d PRS d% CO2 d% HUM %d ",avgTmp,bmpPres,mqCo2,dhtHum);
thinkfire 0:085d20f0ca45 85 device.puts(msg);
thinkfire 0:085d20f0ca45 86 }
thinkfire 0:085d20f0ca45 87 txTimer.reset();
thinkfire 0:085d20f0ca45 88 }
thinkfire 0:085d20f0ca45 89
thinkfire 0:085d20f0ca45 90 wait(0.5);
thinkfire 0:085d20f0ca45 91 }
thinkfire 0:085d20f0ca45 92 }
thinkfire 0:085d20f0ca45 93
thinkfire 0:085d20f0ca45 94 void bmpGetValue(void)
thinkfire 0:085d20f0ca45 95 {
thinkfire 0:085d20f0ca45 96 if(tmpFlag) {
thinkfire 0:085d20f0ca45 97 bmpTmp = bmpObj.getTemperature();
thinkfire 0:085d20f0ca45 98 }
thinkfire 0:085d20f0ca45 99 if(presFlag) {
thinkfire 0:085d20f0ca45 100 bmpPres = bmpObj.getPressure();
thinkfire 0:085d20f0ca45 101 }
thinkfire 0:085d20f0ca45 102 }
thinkfire 0:085d20f0ca45 103
thinkfire 0:085d20f0ca45 104 void dhtGetValue(void)
thinkfire 0:085d20f0ca45 105 {
thinkfire 0:085d20f0ca45 106 int s = dhtObj.readData();
thinkfire 0:085d20f0ca45 107 wait(0.5);
thinkfire 0:085d20f0ca45 108 if (s == ERROR_NONE) {
thinkfire 0:085d20f0ca45 109 if(humFlag) {
thinkfire 0:085d20f0ca45 110 dhtHum = dhtObj.ReadHumidity();
thinkfire 0:085d20f0ca45 111 }
thinkfire 0:085d20f0ca45 112 if(tmpFlag) {
thinkfire 0:085d20f0ca45 113 dhtTmp = dhtObj.ReadTemperature(CELCIUS);
thinkfire 0:085d20f0ca45 114 }
thinkfire 0:085d20f0ca45 115 }
thinkfire 0:085d20f0ca45 116 }
thinkfire 0:085d20f0ca45 117
thinkfire 0:085d20f0ca45 118 void mqGetValue()
thinkfire 0:085d20f0ca45 119 {
thinkfire 0:085d20f0ca45 120 if(co2Flag) {
thinkfire 0:085d20f0ca45 121 mqCo2 = mqObj.read_u16();
thinkfire 0:085d20f0ca45 122 }
thinkfire 0:085d20f0ca45 123 }
thinkfire 0:085d20f0ca45 124
thinkfire 0:085d20f0ca45 125 void setSensors(void)
thinkfire 0:085d20f0ca45 126 {
thinkfire 0:085d20f0ca45 127 if(sensorConf[0]=='T') {
thinkfire 0:085d20f0ca45 128 if(sensorConf[1]=='0') {
thinkfire 0:085d20f0ca45 129 tmpFlag = false;
thinkfire 0:085d20f0ca45 130 avgTmp = 0;
thinkfire 0:085d20f0ca45 131 } else {
thinkfire 0:085d20f0ca45 132 tmpFlag = true;
thinkfire 0:085d20f0ca45 133 }
thinkfire 0:085d20f0ca45 134 } else if(sensorConf[0]=='P') {
thinkfire 0:085d20f0ca45 135 if(sensorConf[1]=='0') {
thinkfire 0:085d20f0ca45 136 presFlag = false;
thinkfire 0:085d20f0ca45 137 bmpPres = 0;
thinkfire 0:085d20f0ca45 138 } else {
thinkfire 0:085d20f0ca45 139 presFlag = true;
thinkfire 0:085d20f0ca45 140 }
thinkfire 0:085d20f0ca45 141 } else if(sensorConf[0]=='C') {
thinkfire 0:085d20f0ca45 142 if(sensorConf[1]=='0') {
thinkfire 0:085d20f0ca45 143 co2Flag = false;
thinkfire 0:085d20f0ca45 144 mqCo2 = 0;
thinkfire 0:085d20f0ca45 145 } else {
thinkfire 0:085d20f0ca45 146 co2Flag = true;
thinkfire 0:085d20f0ca45 147 }
thinkfire 0:085d20f0ca45 148 } else if(sensorConf[0]=='H') {
thinkfire 0:085d20f0ca45 149 if(sensorConf[1]=='0') {
thinkfire 0:085d20f0ca45 150 humFlag = false;
thinkfire 0:085d20f0ca45 151 dhtHum = 0;
thinkfire 0:085d20f0ca45 152 } else {
thinkfire 0:085d20f0ca45 153 humFlag = true;
thinkfire 0:085d20f0ca45 154 }
thinkfire 0:085d20f0ca45 155 }
thinkfire 0:085d20f0ca45 156 }
thinkfire 0:085d20f0ca45 157