IOT Cooler - ECE 4180 Design Project

Description

This is a Cooler that is hooked up through a BlueSmirf Bluetooth Module and also has a small MP3 player attached. The Cooler has the capabilities of reading in temperature data through the TMP36 analog sensor and can output the data to the micro LCD. The temperature data is also sent to an Android application and MP3 player controls are also used through the Bluetooth module. The pushbuttons on the board correspond to play/stop, Forward through Playlist, Reverse through playlist, and increase volume.

Components

mBeduLCD
Vu+5v
GNDGND
p27RX
p28TX
p30RES
mBedBlueTooth
VoutVCC
GNDGND
p14TX
p13RX
mBedSD Card System
p9CD
p6DO
GNDGND
p7SCK
VoutVCC
p5DI
p8CS
mBedTMP36
VoutVs
GNDGND
p15Vout
mBed2N3904
p18B
GNDC
Speaker+E
Speaker-GND
mBed Push Buttons
PinDetect p12Play/Stop
PinDetect p16Forward
PinDetect p17Reverse
PinDetect p20Volume

Project Picture

/media/uploads/anevil14/img_0360.jpg

Project Demo Video

Data Sheet & Product Pages

2N3904

https://www.sparkfun.com/datasheets/Components/2N3904.pdf

TMP36

http://www.analog.com/media/en/technical-documentation/data-sheets/TMP35_36_37.pdf

BlueTooth Module

https://www.sparkfun.com/products/12582

micro SD Card

https://www.sparkfun.com/products/544

micro LCD

https://www.sparkfun.com/products/11377

Import Code

Import programECE4180_FP

IOT Cooler that has an integrated MP3 Player attached

Android Code

/media/uploads/anevil14/smarter_than_you_-embedded_project-.zip

Cooler Code

Code Code for main.cpp

#include "mbed.h"
#include "TMP36.h"
#include "PinDetect.h"
#include "SDFileSystem.h"
#include "SystemState.h"
#include "wave_player.h"
#include "WavDis.h"
#include "time.h"
#include <vector>
#include <string>
#include <stdio.h>

using std::string;

PinDetect fwdButton(p17, PullUp);
PinDetect revButton(p16, PullUp);
PinDetect playButton(p12, PullUp);
PinDetect volumeButton(p20, PullUp);

Serial rn42(p13,p14);  // Bluetooth Module
Serial pc(USBTX, USBRX);

SDFileSystem sd(p5, p6, p7, p8, p9, "sd"); //SD card extra pin p9

AnalogOut DACout(p18);
wave_player waver(&DACout);

float Current_temp = 0.0;
float Current_temp1 = 0.0;
float Current_temp2 = 0.0;

TMP36 myTMP36(p15);
Timer t;

const char* cmds[] = {"MUSIC:UP", "MUSIC:STOP", "MUSIC:PLAY", "MUSIC:NEXT", "MUSIC:PREV", "MUSIC:DOWN"};
SystemState currentState = stop;
string currentSong;
string fileDirectory;
int currentIndex;
int librarySize;
int volumeControl;
bool playStop;
float const rate = 1.0;
string fileName = "";

WavDis display;


vector<string> filenames; //filenames are stored in a vector string

//PROTOCOL:
// ex) TEMPERATURE:30   --> reading of 30 degrees
//
bool writeLine (char* towrite, int length)
{
    if (rn42.writeable()) {
        for (int i = 0; i < length; i++) {
            if (towrite[i] == NULL ) {
                break;
            }
            pc.printf("|%c",towrite[i]);
            rn42.putc(towrite[i]);
        }
        rn42.putc('\n');
        rn42.putc(0);
        return true;
    }

    return false;
}


bool readLine (char* buffer, int sizebuffer)
{
    char tmpRead = 0;

    if (rn42.readable() == 0){
        return false;   
    }
    int i;
    for (i= 0 ; i< sizebuffer-1; i++) {

        //if (rn42.readable()) {
            tmpRead = rn42.getc();
        //} else {
          //  break;
        //}

        if (tmpRead == '\n' || tmpRead == 0) {
            break;
        }
        buffer[i] = tmpRead;
        if (i == 0 && tmpRead == 0) {
            return false;
        }
    }

    buffer[i] = 0;
    if (i == 0 ) {
        return false;
    }

    return true;

}
/*
void Rx_interrupt(void) {
    pc.printf("I blocked you bitch");
    pc.putc(rn42.getc());
     pc.printf("jk");
}*/

//UPDATE NECESSARY VARIABLES EACH TIME A BUTTON EVEN OCCURS
void Update(int index, SystemState state)
{
    currentState = state;
    currentIndex = index;
    currentSong = filenames[currentIndex];
    display.UpdateState(currentState);
    display.UpdateSong(currentSong);
    display.UpdateVolume(15-volumeControl);
    Current_temp = (1.8*myTMP36.read())+32;
    display.UpdateTemp(Current_temp);
    switch(currentState) {
        case stop:
            playStop = false;
            break;
        case play_state:
            playStop = true;
            break;
    }
}


void getWaveFiles()
{
    if (!sd.SDInserted()) {
        while (!sd.SDInserted()) {
            //check for sd card until true
            display.UpdateState(Need_SD);
            printf("SD Card Not Found\n");
            wait(1);
        }
        wait(2);
        sd.disk_initialize();
        wait(2);
    }

    wait(2);
    DIR *dp;
    struct dirent *dirp;
    dp = opendir(fileDirectory.c_str());

    while((dirp = readdir(dp)) != NULL) {
        filenames.push_back(string(dirp->d_name));
    }
    // print filename strings from vector using an iterator
    for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) {
        printf("%s\n\r",(*it).c_str());
    }

    librarySize = filenames.size();
    if (librarySize == 0) {
        printf("Library Empty\n\n");
        return;
    }

    fileDirectory.append("/");

    Update(0,stop);
}



void fwdButton_Event(void)
{
    if (currentState == stop) {
        currentIndex++; //INCREMENT CURRENT INDEX (0 TO NUMBER OF SONGS -1)
        if(currentIndex >= librarySize) {
            currentIndex = 0;
        }
        Update(currentIndex,currentState);
    }
}
void revButton_Event(void)
{
    if (currentState == stop) {
        currentIndex--; //DECREMENT CURRENT INDEX (0 TO NUMBER OF SONGS -1)
        if(currentIndex < 0) {
            currentIndex = (librarySize - 1);
        }
        Update(currentIndex,currentState);
    }
}

void playButton_Event(void)
{

    switch (currentState) {
        case stop:
            Update(currentIndex, play_state);
            break;
        case play_state:
            Update(currentIndex, stop);
            break;
    }
}
void volumeUpButton_Event(void)
{
    if ((volumeControl >= 0) && (volumeControl < 15)){
        volumeControl++;
    }
     display.UpdateVolume(15-volumeControl);
}
void volumeButton_Event(void)
{
    if(volumeControl >= 15) {
        volumeControl = 0;
    } else {
        volumeControl++;
    }
     display.UpdateVolume(15-volumeControl);
}

void volumeDownButton_Event(void)
{
    if(volumeControl > 0) {
        volumeControl--;
    }
     display.UpdateVolume(15-volumeControl);
}

int main()
{

    // BlueTooth Module
    rn42.baud(19600);
    pc.baud(9600);

    // MP3 Player
    currentState = stop;
    currentSong = "";
    librarySize = 0;
    volumeControl = 0;
    currentIndex = -1;
    fileDirectory = "/sd/myMusic";
    char output[50];
    string temp;
    bool didRead = false;
    char buf [100];

    fwdButton.attach_asserted(&fwdButton_Event);
    fwdButton.setSampleFrequency();
    revButton.attach_asserted(&revButton_Event);
    revButton.setSampleFrequency();
    playButton.attach_asserted(&playButton_Event);
    playButton.setSampleFrequency();
    volumeButton.attach_asserted(&volumeButton_Event);
    volumeButton.setSampleFrequency();
    //rn42.attach(&Rx_interrupt, Serial::RxIrq);

    getWaveFiles();
    pc.printf("init");

    while(1) {
        switch (currentState) {
            case stop:
                break;
            case play_state:
                string fileName = "";
                fileName.append(fileDirectory + currentSong);
                printf("%s\n\r",fileName.c_str());
                FILE *wave_file;
                wave_file=fopen(fileName.c_str(),"r");

                waver.play(wave_file, &playStop, &volumeControl);

                fclose(wave_file);
                Update(currentIndex,stop);
                break;
        }
        wait(0.1);
        t.start();

        if (t.read() >= rate) {
            Current_temp = (1.8*myTMP36.read())+32;
            Current_temp1 = (1.8*myTMP36.read())+32;
            Current_temp2 = (1.8*myTMP36.read())+32;
            Current_temp = (Current_temp + Current_temp1 + Current_temp2) / 3.00;
            display.UpdateTemp(Current_temp);
            snprintf(output,50,"%.2f", Current_temp);
            writeLine (output, 50);
            t.reset();
            t.start();
        }


        didRead = readLine(buf, 100);
        pc.printf("(%s)\n",buf);
        if (didRead) {
            if (strcmp(buf,cmds[0]) == 0) {
                pc.printf("Volume Increase");
                volumeUpButton_Event();
            }
            if (strcmp(buf,cmds[1]) == 0) {
                pc.printf("STOP");
                playButton_Event();
            }
            if (strcmp(buf,cmds[2]) == 0) {
                pc.printf("PLAY");
                playButton_Event();
            }
            if (strcmp(buf,cmds[3]) == 0) {
                pc.printf("FORWARD");
                fwdButton_Event();
            }
            if (strcmp(buf,cmds[4]) == 0) {
                pc.printf("REVERSE");
                revButton_Event();
            }
            if (strcmp(buf,cmds[5]) == 0) {
                pc.printf("DOWN");
                volumeDownButton_Event();
            }

        }
    }

}


Please log in to post comments.