PIR sensor with Groove buzzer

Dependencies:   ESP8266

main.cpp

Committer:
omar1646
Date:
2021-03-22
Revision:
0:e1efa9331623

File content as of revision 0:e1efa9331623:

// This program will represent an intrusion detection systesm that will, it's simply a secuirty..
/// system for homes to which will combat against any home burglary. It consists of a infrared motion sensor
/// and grove buzzer. The infrared IC will be able to detect human presense and the grove buzzer will buzz and 
/// emitt an alarming sound. This sound will human presence and will scare off any outside interference. The program also 
/// will have live detectin update via thingspeak.


#include "mbed.h"
Serial pc(USBTX, USBRX); // tx, rx
InterruptIn motion(D0);
DigitalOut buzzer(D3);
DigitalOut Red(LED2);
void wifi_send(void);                    //Connect and Push Data Channel to Cloud Server

int num = 0;
int distance1, distance2;
float dist_remaining1, dist_percent1, dist_remaining2, dist_percent2;
char snd[255],rcv[1000];                    //snd: send command to ESP8266
                                            //rcv: receive response from ESP8266

Serial pc(USBTX,USBRX);                     //Serial Communication with PC
ESP8266 wifi(PTC17, PTC16, 115200);         //Tx Pin:PTC17; Rx Pin:PTC17; Baud rate:115200

int motion_detected = 0;
 
void irq_handler(void)
{
    motion_detected = 1;
}

int main () 
{
    pc.baud(115200);
    
    
    pc.printf("Initial Setup\r\n");
    wifi.SetMode(1);                        //Set ESP mode to 1
    wifi.RcvReply(rcv, 1000);               //Receive a response from ESP
    pc.printf("%s\r", rcv);

    pc.printf("Connecting to WiFi\r\n");    //AP Setup Initialization
    wifi.Join("SSID", "PWD");//Put your Wifi SSID followed by Password WiFi_SSID WiFi_Password
    wifi.RcvReply(rcv, 1000);
    pc.printf("%s\n", rcv);
    wait(8);
    
    wifi.GetIP(rcv);                        //Obtains an IP address from the AP
    
    while (1) 
    {
        wifi_send();
        
        RLed = 1;
        GLed = 1;
        BLed = 0;
        wait(2.0f);
    }
}

void wifi_send(void) 
{
    while(num<1000000000000)
    {
        num=num+1;
        pc.printf("\nCloud Sync Instance #: %d\n\r", num);
        pc.printf("Syncing Data with Cloud, Please Wait.\n\r");
        
        while(1) 
    {
        if(motion_detected) 
        {
            cnt++;
            motion_detected = 0;
            pc.printf("Hello! I've detected %d times since reset\r\n", cnt);
            Red=0;
            //wait(5);
            buzzer= on;
            Red=1;
            wait(5);
        } else{
            pc.printf("did not detect\r\n");
        }
        
        //Sending Data to the Cloud Server via ESP8266 WiFi Module
        strcpy(snd,"AT+CIPMUX=0\n\r");        //AT+CIPMUX: Enabling Single Channel Mode
        wifi.SendCMD(snd);
        wait(1);
        wifi.RcvReply(rcv, 1000);
        wait(1);
        
    //Establish TCP connection w/ Cloud Server
        sprintf(snd,"AT+CIPSTART=4,\"TCP\",\"%s\",80\n",CloudIP);
        wait(1);
        wifi.RcvReply(rcv, 1000);
        wait(1);
        
    //Set length of the data that will be sent
        strcpy(snd,"AT+CIPSEND=100\n\r");
        wifi.SendCMD(snd);
        pc.printf("%s\r", rcv);
        wait(1);
        wifi.RcvReply(rcv, 1000);
        pc.printf("%s\r", rcv);
        wait(1);
        }