Simple code for multiple sensor test using K64f

Dependencies:   mbed MQ2 FXOS8700Q DHT TextLCD ESP8266

Committer:
abhishek_gupta
Date:
Thu Mar 25 10:44:23 2021 +0000
Revision:
5:a981584597fb
Parent:
4:aa3e25ccc05d
This code represents the use of multiple sensors in a simple way in order to create a small scale smart home system which runs on its own

Who changed what in which revision?

UserRevisionLine numberNew contents of line
siddharthp 0:da7e87322045 1 #include "mbed.h"
abhishek_gupta 5:a981584597fb 2 #include "TextLCD.h"
abhishek_gupta 5:a981584597fb 3 #include "MQ2.h"
siddharthp 0:da7e87322045 4 #include "DHT.h"
abhishek_gupta 5:a981584597fb 5 #include "hcsr04.h"
abhishek_gupta 5:a981584597fb 6 #include "FXOS8700Q.h"
abhishek_gupta 5:a981584597fb 7 AnalogIn sensor(A0);
abhishek_gupta 5:a981584597fb 8 DigitalOut Red(LED1);
abhishek_gupta 5:a981584597fb 9 DigitalOut Green(LED2);
abhishek_gupta 5:a981584597fb 10 DigitalOut Blue(LED3);
abhishek_gupta 5:a981584597fb 11 DigitalOut ledPin( PTD3 );
abhishek_gupta 5:a981584597fb 12 DigitalOut buzzer(D13);
abhishek_gupta 5:a981584597fb 13 MQ2 mq2(A1);
abhishek_gupta 5:a981584597fb 14 DHT sensor1(D2, DHT11);
abhishek_gupta 5:a981584597fb 15 AnalogIn loudness(A3);
abhishek_gupta 5:a981584597fb 16 DigitalIn sw2(SW2);
abhishek_gupta 5:a981584597fb 17 DigitalIn sw3(SW3);
abhishek_gupta 5:a981584597fb 18 HCSR04 usensor(D10,D11);
abhishek_gupta 5:a981584597fb 19 I2C i2c(PTE25, PTE24);
abhishek_gupta 5:a981584597fb 20 unsigned int dist;
siddharthp 0:da7e87322045 21
abhishek_gupta 5:a981584597fb 22 FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);
abhishek_gupta 5:a981584597fb 23 TextLCD lcd(D8, D9, D4, D5 ,D6 ,D7);
abhishek_gupta 5:a981584597fb 24 Serial pc(USBTX, USBRX);
abhishek_gupta 5:a981584597fb 25 int main() {
abhishek_gupta 5:a981584597fb 26 motion_data_units_t acc_data;
abhishek_gupta 5:a981584597fb 27 float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
abhishek_gupta 5:a981584597fb 28 acc.enable();
abhishek_gupta 5:a981584597fb 29 float s= 0.0f ,l=0.0f;
abhishek_gupta 5:a981584597fb 30 mq2.begin(); // 'Calibrate' sensor
abhishek_gupta 5:a981584597fb 31 int a,b,e;
abhishek_gupta 5:a981584597fb 32 float h = 0.0f, c = 0.0f, x = 0.0f, y = 0.0f;
abhishek_gupta 5:a981584597fb 33 float val , R;
abhishek_gupta 5:a981584597fb 34 ledPin=0;
abhishek_gupta 5:a981584597fb 35 Red=1;
abhishek_gupta 5:a981584597fb 36 Blue=1;
abhishek_gupta 5:a981584597fb 37 Green=1;
abhishek_gupta 5:a981584597fb 38 while(1) {
abhishek_gupta 5:a981584597fb 39 acc.getAxis(acc_data);
abhishek_gupta 5:a981584597fb 40 acc.getX(faX);
abhishek_gupta 5:a981584597fb 41 acc.getY(faY);
abhishek_gupta 5:a981584597fb 42 acc.getZ(faZ);
abhishek_gupta 5:a981584597fb 43 a= mq2.readCO();
abhishek_gupta 5:a981584597fb 44 b= mq2.readSmoke();
abhishek_gupta 5:a981584597fb 45 e = mq2.readLPG();
abhishek_gupta 5:a981584597fb 46 s= loudness.read();
abhishek_gupta 5:a981584597fb 47 l = (s*100000);
abhishek_gupta 5:a981584597fb 48 val = sensor.read();
abhishek_gupta 5:a981584597fb 49 R = (1023-val)*10/val;
abhishek_gupta 5:a981584597fb 50 printf("Sensor reading: %2.2f\r\n", R/1000);
abhishek_gupta 5:a981584597fb 51 sensor1.readData();
abhishek_gupta 5:a981584597fb 52 x = sensor1.ReadTemperature(CELCIUS);
abhishek_gupta 5:a981584597fb 53 c = x/36;
abhishek_gupta 5:a981584597fb 54 y = sensor1.ReadHumidity();
abhishek_gupta 5:a981584597fb 55 h = y/15;
abhishek_gupta 5:a981584597fb 56 printf("Temperature is %0.00f, Humidity is %0.00f\r\n",c,h);
abhishek_gupta 5:a981584597fb 57 if(R>40000)
abhishek_gupta 5:a981584597fb 58 {
abhishek_gupta 5:a981584597fb 59 lcd.cls();
abhishek_gupta 5:a981584597fb 60 lcd.printf("Lights ON");
abhishek_gupta 5:a981584597fb 61 printf("Turing on Indoor Light\r\n");
abhishek_gupta 5:a981584597fb 62 ledPin = 1;
abhishek_gupta 5:a981584597fb 63 }
abhishek_gupta 5:a981584597fb 64 else {
abhishek_gupta 5:a981584597fb 65 lcd.cls();
abhishek_gupta 5:a981584597fb 66 lcd.printf("Lights OFF");
abhishek_gupta 5:a981584597fb 67 printf("Optimal Lighting Indoor\r\n");
abhishek_gupta 5:a981584597fb 68 ledPin = 0;
abhishek_gupta 5:a981584597fb 69 }
abhishek_gupta 5:a981584597fb 70 if(c>20){
abhishek_gupta 5:a981584597fb 71 printf("Room is Hot and Humid at %0.00f and %0.00f\r\n",c,h);
abhishek_gupta 5:a981584597fb 72 wait(1);
abhishek_gupta 5:a981584597fb 73 lcd.cls();
abhishek_gupta 5:a981584597fb 74 lcd.printf("Room is Hot and Humid");
abhishek_gupta 5:a981584597fb 75 Red = !Red;
abhishek_gupta 5:a981584597fb 76 wait(1);
abhishek_gupta 5:a981584597fb 77 }
abhishek_gupta 5:a981584597fb 78 else {
abhishek_gupta 5:a981584597fb 79
abhishek_gupta 5:a981584597fb 80 wait(1);
abhishek_gupta 5:a981584597fb 81 lcd.cls();
abhishek_gupta 5:a981584597fb 82 lcd.printf("Good Temperature and Humidity");
abhishek_gupta 5:a981584597fb 83 Red = 1;
abhishek_gupta 5:a981584597fb 84 wait(1);
abhishek_gupta 5:a981584597fb 85 }
abhishek_gupta 5:a981584597fb 86 if(e||a||b>0)
siddharthp 0:da7e87322045 87 {
abhishek_gupta 5:a981584597fb 88 wait(1);
abhishek_gupta 5:a981584597fb 89 pc.printf("Gas Detected\r\n");
abhishek_gupta 5:a981584597fb 90 lcd.cls();
abhishek_gupta 5:a981584597fb 91 lcd.printf("Gas Detected");
abhishek_gupta 5:a981584597fb 92 Green = !Green;
abhishek_gupta 5:a981584597fb 93 wait(1);
abhishek_gupta 5:a981584597fb 94 }
abhishek_gupta 5:a981584597fb 95 else{
abhishek_gupta 5:a981584597fb 96 wait(1);
abhishek_gupta 5:a981584597fb 97 pc.printf("NoGas Detected\r\n");
abhishek_gupta 5:a981584597fb 98 lcd.cls();
abhishek_gupta 5:a981584597fb 99 lcd.printf("No Gas Detected");
abhishek_gupta 5:a981584597fb 100 Green = 1;
abhishek_gupta 5:a981584597fb 101 wait(1);
abhishek_gupta 5:a981584597fb 102 }
abhishek_gupta 5:a981584597fb 103 wait(1);
abhishek_gupta 5:a981584597fb 104 pc.printf("................................\r\n");
abhishek_gupta 5:a981584597fb 105 if(l<3600)
abhishek_gupta 5:a981584597fb 106 {wait(1);
abhishek_gupta 5:a981584597fb 107 printf("Music too loud\r\n");
abhishek_gupta 5:a981584597fb 108 lcd.cls();
abhishek_gupta 5:a981584597fb 109 lcd.printf("Music is Loud");
abhishek_gupta 5:a981584597fb 110 }
abhishek_gupta 5:a981584597fb 111 else{wait(1);
abhishek_gupta 5:a981584597fb 112 printf("Sound is Good\r\n");
abhishek_gupta 5:a981584597fb 113 lcd.cls();
abhishek_gupta 5:a981584597fb 114 lcd.printf("Sound is Good");
abhishek_gupta 5:a981584597fb 115 }
abhishek_gupta 5:a981584597fb 116 wait(1.0);
abhishek_gupta 5:a981584597fb 117 wait(.5);
abhishek_gupta 5:a981584597fb 118 if(faX<0){
abhishek_gupta 5:a981584597fb 119 wait(.2);
abhishek_gupta 5:a981584597fb 120 Blue = 0;
abhishek_gupta 5:a981584597fb 121 printf("<<<<<<<<System is Moved in X-Axis>>>>>>>\r\n");
abhishek_gupta 5:a981584597fb 122 wait(.5);
abhishek_gupta 5:a981584597fb 123 lcd.cls();
abhishek_gupta 5:a981584597fb 124 lcd.printf("System Moved");
abhishek_gupta 5:a981584597fb 125 lcd.locate(0,1);
abhishek_gupta 5:a981584597fb 126 lcd.printf("X-Axis");
abhishek_gupta 5:a981584597fb 127 wait(.5);}
abhishek_gupta 5:a981584597fb 128 else if(faY>0)
abhishek_gupta 5:a981584597fb 129 {
abhishek_gupta 5:a981584597fb 130 wait(.2);
abhishek_gupta 5:a981584597fb 131 Blue = 0;
abhishek_gupta 5:a981584597fb 132 printf("<<<<<<<<<<System is Moved in Y-Axis>>>>>>>>\r\n");
abhishek_gupta 5:a981584597fb 133 wait(.5);
abhishek_gupta 5:a981584597fb 134 lcd.cls();
abhishek_gupta 5:a981584597fb 135 lcd.printf("System Moved");
abhishek_gupta 5:a981584597fb 136 lcd.locate(0,1);
abhishek_gupta 5:a981584597fb 137 lcd.printf("Y-Axis");
abhishek_gupta 5:a981584597fb 138 wait(.5);}
abhishek_gupta 5:a981584597fb 139 else if(faZ<1){
abhishek_gupta 5:a981584597fb 140 wait(.2);
abhishek_gupta 5:a981584597fb 141 Blue = 0;
abhishek_gupta 5:a981584597fb 142 wait(.5);
abhishek_gupta 5:a981584597fb 143 printf("<<<<<<<<System is Moved in Z-Axis>>>>>>>>>>\r\n");
abhishek_gupta 5:a981584597fb 144 wait(.5);
abhishek_gupta 5:a981584597fb 145 lcd.cls();
abhishek_gupta 5:a981584597fb 146 lcd.printf("System Moved");
abhishek_gupta 5:a981584597fb 147 lcd.locate(0,1);
abhishek_gupta 5:a981584597fb 148 lcd.printf("Z-Axis");
abhishek_gupta 5:a981584597fb 149 wait(.5);}
abhishek_gupta 5:a981584597fb 150 else {
abhishek_gupta 5:a981584597fb 151 Blue = 1;
abhishek_gupta 5:a981584597fb 152 wait(.5);
abhishek_gupta 5:a981584597fb 153 printf("<<<<<System Safe>>>>>>\r\n");
abhishek_gupta 5:a981584597fb 154 lcd.cls();
abhishek_gupta 5:a981584597fb 155 lcd.printf("System Safe");
abhishek_gupta 5:a981584597fb 156 wait(.5);}
abhishek_gupta 5:a981584597fb 157
abhishek_gupta 5:a981584597fb 158
abhishek_gupta 5:a981584597fb 159 }}
abhishek_gupta 5:a981584597fb 160
abhishek_gupta 5:a981584597fb 161