Simple code for multiple sensor test using K64f

Dependencies:   mbed MQ2 FXOS8700Q DHT TextLCD ESP8266

Revision:
5:a981584597fb
Parent:
4:aa3e25ccc05d
--- a/main.cpp	Sun Dec 04 21:21:05 2016 +0000
+++ b/main.cpp	Thu Mar 25 10:44:23 2021 +0000
@@ -1,90 +1,161 @@
 #include "mbed.h"
+#include "TextLCD.h"
+#include "MQ2.h"
 #include "DHT.h"
-#include "ESP8266.h"
- 
-DHT sensor(D4, DHT11);
-ESP8266 wifi(PTC17, PTC16, 115200);
-Serial pc(USBTX,USBRX);
-DigitalOut RED(LED1);
-char snd[255],rcv[1000];
-#define IP "184.106.153.149" // thingspeak.com IP Address
-void wifi_send(void);
+#include "hcsr04.h"
+#include "FXOS8700Q.h"
+AnalogIn sensor(A0);
+DigitalOut Red(LED1);
+DigitalOut Green(LED2);
+DigitalOut Blue(LED3);
+DigitalOut ledPin( PTD3 );
+DigitalOut buzzer(D13);
+MQ2 mq2(A1);  
+DHT sensor1(D2, DHT11);
+AnalogIn loudness(A3);
+DigitalIn sw2(SW2);
+DigitalIn sw3(SW3);
+HCSR04  usensor(D10,D11);
+I2C i2c(PTE25, PTE24);
+unsigned int dist;
 
-int main()
-{
-    pc.baud(115200);   
-    pc.printf("SET mode to AP\r\n");
-    wifi.SetMode(1);    // set ESP mode to 1
-    wifi.RcvReply(rcv, 1000);    //receive a response from ESP
-    pc.printf("%s",rcv);    //Print the response onscreen
-    pc.printf("Conneting to Wifi\r\n");
-    wifi.Join("ssid", "password");     // Your wifi username & Password 
-    wifi.RcvReply(rcv, 1000);    //receive a response from ESP
-    pc.printf("%s\n", rcv);    //Print the response onscreen
-    wait(8);     //waits for response from ESP
-    pc.printf("Getting IP\r\n");    //get IP addresss from the connected AP
-    wifi.GetIP(rcv);    //receive an IP address from the AP
-    pc.printf("%s\n", rcv);
-    while (1) 
-    {
-        pc.printf("PLEASE STAY AWAY\r\n");
-        pc.printf("Sending WiFi information\n");
-        wifi_send();
-        RED=1;
-        wait(2.0f);
-        RED=0;
-        wait(1.5f);
-    }    
-}
-void wifi_send(void){
-    int error = 0;
-    float h = 0.0f, c = 0.0f;
-        
-        wait(2.0f);
-        error = sensor.readData();
-        if (0 == error) 
+FXOS8700QAccelerometer acc(i2c, FXOS8700CQ_SLAVE_ADDR1);  
+TextLCD lcd(D8, D9, D4, D5 ,D6 ,D7);
+Serial pc(USBTX, USBRX);
+int main() {
+    motion_data_units_t acc_data;
+    float faX, faY, faZ, fmX, fmY, fmZ, tmp_float;
+    acc.enable();
+    float s= 0.0f ,l=0.0f;
+    mq2.begin();                                                                // 'Calibrate' sensor
+    int a,b,e;
+    float h = 0.0f, c = 0.0f, x = 0.0f, y = 0.0f;
+    float val , R;
+    ledPin=0;
+    Red=1;
+    Blue=1;
+    Green=1;
+    while(1) {
+        acc.getAxis(acc_data);
+        acc.getX(faX);
+        acc.getY(faY);
+        acc.getZ(faZ);
+    a= mq2.readCO();
+    b= mq2.readSmoke();  
+  e = mq2.readLPG();
+        s= loudness.read();
+      l = (s*100000);
+        val = sensor.read();
+        R = (1023-val)*10/val;
+        printf("Sensor reading: %2.2f\r\n", R/1000);
+        sensor1.readData();
+        x   = sensor1.ReadTemperature(CELCIUS);
+        c = x/36;
+        y   = sensor1.ReadHumidity();
+        h = y/15;
+        printf("Temperature is %0.00f, Humidity is %0.00f\r\n",c,h);
+        if(R>40000)
+        {
+        lcd.cls();
+        lcd.printf("Lights ON");
+        printf("Turing on Indoor Light\r\n");
+        ledPin = 1;
+        }
+        else {
+            lcd.cls();
+            lcd.printf("Lights OFF");
+            printf("Optimal Lighting Indoor\r\n");
+            ledPin = 0;
+            }
+        if(c>20){
+            printf("Room is Hot and Humid at %0.00f and %0.00f\r\n",c,h);
+            wait(1);
+            lcd.cls();
+            lcd.printf("Room is Hot and Humid");
+            Red = !Red;
+            wait(1);
+            }
+        else {
+            
+            wait(1);
+            lcd.cls();
+            lcd.printf("Good Temperature and Humidity");
+            Red = 1;
+            wait(1);
+            }
+        if(e||a||b>0)
         {
-            c   = sensor.ReadTemperature(CELCIUS);
-            h   = sensor.ReadHumidity();
-            //printf("Temperature in Celcius: %f\n", c);
-            //printf("Humidity is %f\n", h);
-        } 
-        else 
-        {
-            printf("Error: %d\n", error);
-        }
-  //WIFI updates the Status to Thingspeak servers//
-  strcpy(snd,"AT+CIPMUX=1\n");//Setting WiFi into MultiChannel mode
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wait(2.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);
-  sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",IP); //Initiate connection with THINGSPEAK server 
-  pc.printf(snd);
-  wait(3.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);
-  strcpy(snd,"AT+CIPSEND=4,47\n");    //Send Number of open connections,Characters to send 
-  wifi.SendCMD(snd);
-  pc.printf(snd);
-  wait(2.0);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s\n", rcv);
-  wait(2);    
-  sprintf(snd,"GET http://api.thingspeak.com/update?api_key=**********&field1=%1.3f\n", c); //Post values to thingspeak
-  pc.printf("%s",snd);
-  wifi.SendCMD(snd);
-  wait(2);
-  sprintf(snd,"GET http://api.thingspeak.com/update?api_key=***********&field2=%1.3f\n", h); //Post values to thingspeak
-  pc.printf("%s",snd);
-  wifi.SendCMD(snd);
-  wait(2);
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s", rcv);
-  wifi.SendCMD("AT+CIPCLOSE"); //Close the connection to server
-  wifi.RcvReply(rcv, 1000);
-  pc.printf("%s", rcv);
-}
\ No newline at end of file
+            wait(1);
+            pc.printf("Gas Detected\r\n");
+            lcd.cls();
+            lcd.printf("Gas Detected");
+            Green = !Green; 
+            wait(1);
+            }
+        else{
+            wait(1);
+            pc.printf("NoGas Detected\r\n");
+            lcd.cls();
+            lcd.printf("No Gas Detected");
+            Green = 1;
+            wait(1);
+            }
+            wait(1);
+        pc.printf("................................\r\n");                                           
+        if(l<3600)
+        {wait(1);
+            printf("Music too loud\r\n");
+            lcd.cls();
+            lcd.printf("Music is Loud");
+            }
+        else{wait(1);
+            printf("Sound is Good\r\n");
+            lcd.cls();
+            lcd.printf("Sound is Good");    
+            }
+         wait(1.0);
+         wait(.5);
+        if(faX<0){
+        wait(.2);
+         Blue = 0;
+         printf("<<<<<<<<System is Moved in X-Axis>>>>>>>\r\n");
+         wait(.5);
+            lcd.cls();
+            lcd.printf("System Moved");
+            lcd.locate(0,1);
+             lcd.printf("X-Axis");
+             wait(.5);}
+            else if(faY>0)
+            {
+            wait(.2);
+             Blue = 0;
+             printf("<<<<<<<<<<System is Moved in Y-Axis>>>>>>>>\r\n");
+             wait(.5);
+            lcd.cls();
+            lcd.printf("System Moved");
+            lcd.locate(0,1);
+             lcd.printf("Y-Axis");
+             wait(.5);}           
+        else if(faZ<1){
+         wait(.2);
+         Blue = 0;
+         wait(.5);
+         printf("<<<<<<<<System is Moved in Z-Axis>>>>>>>>>>\r\n");
+         wait(.5);
+            lcd.cls();
+            lcd.printf("System Moved");
+            lcd.locate(0,1);
+             lcd.printf("Z-Axis");
+             wait(.5);}
+         else {
+         Blue = 1;
+         wait(.5);
+         printf("<<<<<System Safe>>>>>>\r\n");
+            lcd.cls();
+            lcd.printf("System Safe");
+             wait(.5);}
+           
+
+}}
+
+   
\ No newline at end of file