Basement Alert System (BAS)

Project Motivation and Description

The Fulford and Sons group are home renters that have a basement on their premises. The basement is only accessible from outside of the home. Because of this, periodically checking on the condition of the basement can be spaced out, let alone inconvenient. Also, flooding or mold can be happening while we are unaware that these things are going on down there. This is exactly what happened to us on multiple occasions.

The Fulford and Sons have a furnace that is used in delivering heat to the home. The furnace has a condensation pump that takes water out of the furnace and into a container. The water is then delivered from the container to outside of the home. This container can overflow if things are not functioning correctly, for example, frozen lines. This past winter, the lines froze, and the basement had large amounts of water dispersed throughout it. If left unattended for several days, the overflow will cause significant damage, especially to anything stored down there.

The BAS is designed to alert the home owner/renter of any threatening situations that can occur in the basement. The system contains temperature, humidity, gas, and water level sensors. The BAS also houses a LPC1768 microcontroller that takes the sensor's readings and transmits the data to a local sever web page on the LAN connection of the home. If anything serious occurs, the mbed will use the Huzzah WIFI module to activate an action on the IFTTT website. This action is a text message sent to the home owner/renter indicating which condition is developing. The BAS will produce an alert sound through a simple speaker and amplifier for the water level condition only.


/media/uploads/jfulford6/block.png Block Diagram of BAS

System Devices with Pinouts

HTU21D-F
Adafruit HTU21D-F

MbedHTU21D-F
VoutVin
GNDGND
p10SCL
p9SDA

MiCS5524
Adafruit MiCS5524

MbedMiCS5524
VU5V
GNDGND
p15A0

Huzzah Wifi
Huzzah WIFI

MbedHuzzah WIFI5V Power Supply
V++
GNDGND-
p16RST
p17TX
p18RX

SD Card Reader
SD Card Reader

MbedSD Card Reader
VoutVCC
GNDGND
p5DI
p6DO
p7SCK
p8CS

Class-D Amplifier

MbedTPA2005D1Speaker
VUPWR+
GNDPWR-
p18IN+
GNDIN-
OUT++
OUT--


System Demo


BAS response when water level is too high

BAS response when temperature is too high.

/media/uploads/jfulford6/webpage.png
Web page created by BAS

ALERT!!
Alert text sent from IFTTT.com

System Code

Import programBasement_Alert_System

Send text alert with Water level, Temp, and Gas Level

main.cpp

#include "mbed.h"
#include "ESP8266_Huzzah.h"
#include "HTU21D.h"
#include "rtos.h"
#include "wave_player.h"
#include "SDFileSystem.h"
#include "Server.h"
 
 
 
 
HTU21D temphumid(p9, p10); //Temp humid sensor || SDA, SCL
AnalogIn gas_lvl(A0);
AnalogIn water_lvl(A1);
 
SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD card card reader
 
AnalogOut DACout(p18); // used to play sound on speaker
//wave player plays a *.wav file to D/A and a PWM
wave_player waver(&DACout);
 
 
 
float sample_ftemp=0.0;
float sample_humid=0.0;
float gas_density=0.0;
float gas_density_init = 0.0;
float gasDiff = 0.2;
float water_level=0.0;
float critWater = 0.10; // critical water level
bool soundOn = false;
 
bool tempAlerted = false;
bool gasAlerted = false;
bool humidAlerted = false;
bool waterAlerted = false;
 
Mutex mutex;
Mutex thMutex;
Mutex wifi;
Mutex waterMutex;
Thread *(sound);
 
DigitalOut gasLed(p11);
DigitalOut waterLed(p21);
DigitalOut tempLed(p22);
DigitalOut humidLed(p23);
Timer tt; 
 
//========= Set up ESP8266 to act as server/client =========//
void ESP8266_config()
{
    
    reset=0; //hardware reset for 8266
    pc.baud(9600);  // set what you want here depending on your terminal program speed
    pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
    wait(0.5);
    reset=1;
    timeout=2;
    getreply();
 
    esp.baud(9600);   // change this to the new ESP8266 baudrate if it is changed at any time.
    //ESPsetbaudrate();   // include this routine to set a different ESP8266 baudrate 
    ESPconfig();        // nclude Config to set the ESP8266 configuration  
    pc.baud(9600);
    
}
 
//======================== THREADS =========================//
 
void gas_thread(void const *arg)
{
     while(1)
     {    
        while(soundOn) Thread::yield(); // This helps the sound file for water
         Thread::wait(60);            // level play clearly
         
         /*
         mutex.lock();
         pc.printf("Gas Density: %3f\r\n", gas_density);
         mutex.unlock();
         */
         thMutex.lock();
         gas_density = gas_lvl.read(); 
         thMutex.unlock();
         if (gas_density_init - gas_density > gasDiff)
         {
             gasLed = 1;
             if (!gasAlerted)
             {
                wifi.lock();
                IFTTTgas();
                wifi.unlock();
                gasAlerted = true;
             }
         }
         else
         {
             gasAlerted = false;
             gasLed = 0;
         }
     }
}
 
void temp_thread(void const *arg)
{
    while(1)
    {
        
        while(soundOn) Thread::yield();
        thMutex.lock();
        sample_ftemp = temphumid.sample_ftemp();
        Thread::wait(60);
        thMutex.unlock();
        
        
        /*
        mutex.lock();
        pc.printf("Temperature: %d F\n\r", sample_ftemp);
        mutex.unlock();
        */
        
        if (sample_ftemp > 80 || sample_ftemp < 40)
        {
            tempLed = 1;
            if (sample_ftemp < 40)
            {
                if (!tempAlerted)
                {
                    tempAlerted = true;
                    wifi.lock();
                    IFTTTlowTemp(); // Send three messages back to back;
                    wifi.unlock();
                }
            }
            else
            {
                if (!tempAlerted)
                {
                    tempAlerted = true;
                    wifi.lock();
                    IFTTThighTemp();
                    wifi.unlock();
                }
            }       
        }
        else
        {
            tempLed = 0;
            tempAlerted = false;
        }
    }
}
 
void humid_thread(void const *arg)
{
    while(1)
    {
        while(soundOn) Thread::yield();
        thMutex.lock();
        sample_humid = temphumid.sample_humid();
        Thread::wait(60);
        thMutex.unlock();
        
        /*
        mutex.lock();
        pc.printf("Humidity: %d %%\n\r", sample_humid);
        mutex.unlock();
        */
        
        if (sample_humid > 70)
        {
            humidLed = 1;
            if (!humidAlerted)
            {
                wifi.lock();
                IFTTThumid();
                wifi.unlock();
                humidAlerted = true;
            }
        }
        else
        {
            humidAlerted = false;
            humidLed = 0;
        }
    }
}
 
void water_thread(void const *arg)
{
    while(1)
    {
        
        thMutex.lock();
        water_level = water_lvl.read();
        thMutex.unlock();
        
        
        /*
        mutex.lock();        
        printf("Water Level: %3.2f\r\n",water_level);
        mutex.unlock();
        */
        
        if (water_level > critWater)
        {
            waterLed = 1;
 
            if (!waterAlerted)
            {
                wifi.lock();
                IFTTTwater();
                wifi.unlock();
                waterAlerted = true;
            }
            
            //Thread::wait(100);
            FILE *wave_file;
            sound->set_priority(osPriorityHigh);
            soundOn = true;
            
            wave_file=fopen("/sd/mydir/Red_Alert.wav","r");
            waver.play(wave_file);
            
            soundOn = false;
            sound->set_priority(osPriorityNormal);
            Thread::wait(1000);
            fclose(wave_file);
        }
        else
        {
            waterLed = 0;
            waterAlerted = false;
        }
    }
}
              
        
int main()
{   
 
    ESP8266_config();
    tt.start();
    if (time(NULL) < 1420070400) {
        setRTC();
    }
    
    gas_density_init = gas_lvl.read(); // Get inititial gas density in basement
    sample_ftemp = temphumid.sample_ftemp();
    wait_ms(60);
    sample_humid = temphumid.sample_humid();
    wait_ms(60);
    setServerValues(sample_ftemp,abs(gas_density_init - gas_density),sample_humid,water_lvl.read());
    startserver();
    
    /*  
    IFTTTwater();
    IFTTThighTemp();
    IFTTTlowTemp();
    IFTTThumid();
    IFTTTgas();  */
    
    Thread thread1(gas_thread);
    Thread thread2(temp_thread);
    Thread thread3(humid_thread);
    Thread thread4(water_thread);
    
    sound = &thread4;
    
    // continuosly get AP list and IP
    while(1) 
    {
       
       if(tt.read() > 5) { //let 5 seconds pass before updating
           
           setServerValues(sample_ftemp,gas_density - gas_density_init,sample_humid,water_level);
           gettime();
           getgas();
           gettemp();
           gethumid();
           getwater();
           wifi.lock();
           update_values();
           wifi.unlock();
           tt.reset();
           tt.start();        
       }
    
       while(soundOn) Thread::yield(); 
       //sleep();   
    }
}


Please log in to post comments.