![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
FRDM-K64F, Avnet M14A2A, Grove Shield, to create smart home system. In use with AT&Ts M2x & Flow.
Dependencies: mbed FXOS8700CQ MODSERIAL
Diff: main.cpp
- Revision:
- 84:fc8c9b39723a
- Parent:
- 77:c65eae5b9958
- Child:
- 85:0cf65ceb4492
--- a/main.cpp Mon Dec 11 21:51:32 2017 +0000 +++ b/main.cpp Wed Mar 06 21:11:49 2019 +0000 @@ -23,6 +23,9 @@ #include "cell_modem.h" #include "hardware.h" +#include "DHT.h" +float current_temp = 0.0f, rel_humid = 0.0f; + I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes MODSERIAL mdm(PTD3, PTD2, 4096, 4096); @@ -205,8 +208,24 @@ } } //parse_JSON +//DigitalIn btn_temp_up(D4); +//DigitalIn btn_temp_down(D8); +DigitalIn btn_silence(D7); +DigitalIn motion(D3); +DigitalOut buzzer(D5); + +AnalogIn pot_temp_set(A3); +DHT sensor(D4, DHT11); + int main() { static unsigned ledOnce = 0; + int error = 0; + float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f; + float value = 0.0f; + float temp_setting = 0.0f, diff = 0.0f; + int mycolor = 0x0; + bool alarm = false; + //delay so that the debug terminal can open after power-on reset: wait (5.0); pc.baud(115200); @@ -234,6 +253,12 @@ OneMsTicker.attach(OneMsFunction, 0.001f) ; // Send and receive data perpetually + + mycolor = 0x0; + SetLedColor(mycolor); + + buzzer = 0; + while(1) { #ifdef USE_VIRTUAL_SENSORS ProcessUsbInterface(); @@ -252,8 +277,83 @@ ledOnce = 1; SetLedColor(0x2); //Green } - parse_JSON(&myJsonResponse[0]); + //parse_JSON(&myJsonResponse[0]); } } //bTimerExpiredFlag + + //--------------------------------begin new code-------------------------------- + error = sensor.readData(); + if (0 == error) + { + c = sensor.ReadTemperature(CELCIUS); + f = sensor.ReadTemperature(FARENHEIT); + k = sensor.ReadTemperature(KELVIN); + h = sensor.ReadHumidity(); + dp = sensor.CalcdewPoint(c, h); + dpf = sensor.CalcdewPointFast(c, h); + //printf("Temperature in Kelvin: %4.2f, Celcius: %4.2f, Farenheit %4.2f\n", k, c, f); + //printf("Humidity is %4.2f, Dewpoint: %4.2f, Dewpoint fast: %4.2f\n", h, dp, dpf); + } + else + { + //printf("Error: %d\n", error); + } + + value = pot_temp_set; + //printf("Slide location %3.6f\r\n", value); + temp_setting = value * 40.0f + 50.0f; + printf(WHT "Thermostat is set to %3.2f degrees.\r\n", temp_setting); + + //current_temp and rel_humid are assigned in sensors.cpp + printf(WHT "Current temperature is %3.2f degrees.\r\n", current_temp); + printf(WHT "Relative humidity is %3.2f percent.\r\n", rel_humid); + + printf(WHT "\r\n"); + + diff = current_temp - temp_setting; + if ((diff > -2.0f && diff < 2.0f && rel_humid < 40.0f) && mycolor != 0x0) //if temperature is near optimal, and it is not humid, turn off heat and AC. + { + mycolor = 0x0; + SetLedColor(mycolor); + } + if ((diff > -2.0f && diff < 2.0f && rel_humid > 40.0f) && mycolor != 0x2) //if temperature is near optimal, and it is humid, use fan only. + { + mycolor = 0x2; + SetLedColor(mycolor); + } + if (diff > 4.0f && mycolor != 0x4) //if it is too hot, turn on AC. + { + mycolor = 0x4; + SetLedColor(mycolor); + } + if (diff < -4.0f && mycolor != 0x1) //if it is too cold, turn on heat. + { + mycolor = 0x1; + SetLedColor(mycolor); + } + + //if motion sensor detects motion, sound an alarm + if ((motion == 1) && !alarm) + { + alarm = true; + printf(WHT "--------------------------------\r\n"); + printf(WHT "Alert! Home intrusion detected!\r\n"); + printf(WHT "--------------------------------\r\n"); + printf(WHT "\r\n"); + } + if ((btn_silence == 1) && alarm) + { + alarm = false; + buzzer = 0; + printf(WHT "Alarm disengaged. Resuming normal operation.\r\n"); + printf(WHT "\r\n"); + } + if (alarm) + { + buzzer = !buzzer; + } + + wait(1.0); + } //forever loop }