Simple code for multiple sensor test using K64f

Dependencies:   mbed MQ2 FXOS8700Q DHT TextLCD ESP8266

Files at this revision

API Documentation at this revision

Comitter:
abhishek_gupta
Date:
Thu Mar 25 10:44:23 2021 +0000
Parent:
4:aa3e25ccc05d
Commit message:
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

Changed in this revision

FXOS8700Q.lib Show annotated file Show diff for this revision Revisions of this file
MQ2.lib Show annotated file Show diff for this revision Revisions of this file
TextLCD.lib Show annotated file Show diff for this revision Revisions of this file
hcsr04.cpp Show annotated file Show diff for this revision Revisions of this file
hcsr04.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r aa3e25ccc05d -r a981584597fb FXOS8700Q.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FXOS8700Q.lib	Thu Mar 25 10:44:23 2021 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Freescale/code/FXOS8700Q/#aee7dea904e2
diff -r aa3e25ccc05d -r a981584597fb MQ2.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQ2.lib	Thu Mar 25 10:44:23 2021 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/azazeal88/code/MQ2/#c0ebeb4b47b6
diff -r aa3e25ccc05d -r a981584597fb TextLCD.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TextLCD.lib	Thu Mar 25 10:44:23 2021 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/simon/code/TextLCD/#308d188a2d3a
diff -r aa3e25ccc05d -r a981584597fb hcsr04.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.cpp	Thu Mar 25 10:44:23 2021 +0000
@@ -0,0 +1,98 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+
+#include "hcsr04.h"
+
+
+HCSR04::HCSR04(PinName TrigPin,PinName EchoPin):
+    trigger(TrigPin), echo(EchoPin)
+{
+    pulsetime.stop();
+    pulsetime.reset();
+    echo.rise(this,&HCSR04::isr_rise);
+    echo.fall(this,&HCSR04::isr_fall);
+    trigger=0;
+}
+
+HCSR04::~HCSR04()
+{
+}
+
+void HCSR04::isr_rise(void)
+{
+    pulsetime.start();
+}
+void HCSR04::start(void)
+{
+    trigger=1;
+    wait_us(10);
+    trigger=0;
+}
+
+void HCSR04::isr_fall(void)
+{
+    pulsetime.stop();
+    pulsedur = pulsetime.read_us();
+    distance= (pulsedur*343)/20000;
+    pulsetime.reset();
+}
+
+void HCSR04::rise (void (*fptr)(void))
+{
+    echo.rise(fptr);
+}
+void HCSR04::fall (void (*fptr)(void))
+{
+    echo.fall(fptr);
+}
+
+unsigned int HCSR04::get_dist_cm()
+{
+    return distance;
+}
+unsigned int HCSR04::get_pulse_us()
+{
+    return pulsedur;
+}
+
+
+
+/*******************************************************
+   Here is a sample code usage
+********************************************************* 
+#include "hcsr04.h"
+HCSR04  usensor(p25,p6);
+int main()
+{
+    unsigned char count=0;
+    while(1) {
+        usensor.start();
+        wait_ms(500); 
+        dist=usensor.get_dist_cm();
+        lcd.cls();
+        lcd.locate(0,0);
+        lcd.printf("cm:%ld",dist );
+ 
+        count++;
+        lcd.locate(0,1);
+        lcd.printf("Distance =%d",count);
+        
+    }
+*/
\ No newline at end of file
diff -r aa3e25ccc05d -r a981584597fb hcsr04.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/hcsr04.h	Thu Mar 25 10:44:23 2021 +0000
@@ -0,0 +1,68 @@
+/* Copyright (c) 2013 Prabhu Desai
+ * pdtechworld@gmail.com
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
+ * and associated documentation files (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge, publish, distribute,
+ * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all copies or
+ * substantial portions of the Software.
+ *
+ * For more details on the sensor :
+ * http://www.elecfreaks.com/store/hcsr04-ultrasonic-sensor-distance-measuring-module-p-91.html?zenid=pgm8pgnvaodbe36dibq5s1soi3
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
+ * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef MBED_HCSR04_H
+#define MBED_HCSR04_H
+
+#include "mbed.h"
+
+/** HCSR04 Class(es)
+ */
+
+class HCSR04
+{
+public:
+    /** Create a HCSR04 object connected to the specified pin
+    * @param pin i/o pin to connect to
+    */
+    HCSR04(PinName TrigPin,PinName EchoPin);
+    ~HCSR04();
+
+    /** Return the distance from obstacle in cm
+    * @param distance in cms and returns -1, in case of failure
+    */
+    unsigned int get_dist_cm(void);
+    /** Return the pulse duration equal to sonic waves travelling to obstacle and back to receiver.
+    * @param pulse duration in microseconds.
+    */
+    unsigned int get_pulse_us(void);
+    /** Generates the trigger pulse of 10us on the trigger PIN.
+    */
+    void start(void );
+    void isr_rise(void);
+    void isr_fall(void);
+    void fall (void (*fptr)(void));
+    void rise (void (*fptr)(void));
+
+
+
+private:
+
+    Timer pulsetime;
+    DigitalOut  trigger;
+    InterruptIn echo;
+    unsigned int pulsedur;
+    unsigned int distance;
+};
+
+#endif
\ No newline at end of file
diff -r aa3e25ccc05d -r a981584597fb main.cpp
--- 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