Daniel StarkelAnh Huy
Published © MIT

Sleep Better

Monitor your room temperature, make use of a fan and a portable heat's pad to bring you the sleep you deserve!

BeginnerProtip4 hours1,687
Sleep Better

Things used in this project

Hardware components

Photon
Particle Photon
×2
Relay (generic)
×2
Room Fan
×1
BME280 Pressure Temperature Sensor
×1
portable heat pad
×1
Extension Cord
×2

Software apps and online services

ThingSpeak API
ThingSpeak API
Maker service
IFTTT Maker service

Story

Read more

Schematics

Relay-Photon-BME 280 Connection

Second photon- Relay simple connection

Relay_Photon_BME280

Relay-Photon

Code

BME 280- Heater control code

Arduino
/***************************************************************************
  This is a library for the BME280 humidity, temperature & pressure sensor
  Designed specifically to work with the Adafruit BME280 Breakout
  ----> http://www.adafruit.com/products/2650
  These sensors use I2C or SPI to communicate, 2 or 4 pins are required
  to interface.
  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!
  Written by Limor Fried & Kevin Townsend for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
  This file was modified by Markus Haack (https://github.com/mhaack)
  in order to work with Particle Photon & Core.
  Then further modified by Mac McAlpine https://github.com/macsboost
  Last modified on Nov 21st, 2016 by Huy Dien (hdien@uncc.edu)
 ***************************************************************************/
 
//#include "Adafruit_BME280/Adafruit_Sensor.h"
#include "Adafruit_BME280/Adafruit_BME280.h"

#define BME_SCK D4
#define BME_MISO D3
#define BME_MOSI D2
#define BME_CS D5

#define SEALEVELPRESSURE_HPA (1013.25)

//This Sketch measure differential pressure across the air filter in the house HVAC

/*                                    +-----+
 *                          +----------| USB |----------+
 *                          |          +-----+       *  |
 *                          | [ ] VIN           3V3 [ ] |
 *                          | [ ] GND           RST [ ] |
 *                          | [ ] TX           VBAT [ ] |
 *                          | [ ] RX  [S]   [R] GND [ ] |
 *                          | [ ] WKP            D7 [*] |
 *                          | [ ] DAC +-------+  D6 [*] |
 *                          | [ ] A5  |   *   |  D5 [*] |
 *                          | [ ] A4  |Photon |  D4 [*] |P1 PWR
 *                          | [ ] A3  |       |  D3 [*] |P2 PWR
 *                          | [ ] A2  +-------+  D2 [*] |
 *                      P1  | [*] A1             D1 [*] |SCL BME 0x76
 *                      P2  | [*] A0             D0 [*] |SDA BME 0x76
 *                          |                           |
 *                           \    []         [______]  /
 *                            \_______________________/
 *
 *
 */

/*
   This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   230400-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/

const String key = "<3JGM1YJR7EURDENQ>";
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO,  BME_SCK);

Timer timer(14232, checkEnvironment); // call this function every 10 seconds
Timer timer2(9901, serialDebug); // call this function every 10 seconds
Timer timer3(10123, checkEnvironment2);
int led = D7; //set d7 become led indicater
int out1 = D3; //outlet signal pin
bool toggle = true; //toggle to turn relay on and off

int caladdress = 0;
double atticTemp = 0;
double baro = 0;
double humidity = 0;


//ApplicationWatchdog wd(5000, System.reset); //5 second application watchdog
SYSTEM_MODE(SEMI_AUTOMATIC);



ApplicationWatchdog wd(60000, System.reset);


void setup() {
    pinMode(led, OUTPUT);// innitialize output pin
    pinMode(out1, OUTPUT); 
    Spark.function("relaycontrol",relaycontrol);//name the function
  Serial.begin(230400);
  Serial.println(F("BME280 test"));

  /*if (!bme.begin(0x76)) {  //0x76 is the i2c address of the sensor
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);
  }*/
  
  
  Particle.publish("DEBUG", "starting...");

  if (bme.begin(0x76)) {  //0x76 is the i2c address of the sensor
    Particle.publish("DEBUG", "starting the environment timer...");
    timer.start(); //initialize timer
    timer2.start();//initialize timer2
    timer3.start();//initialize timer2
  }
  else {
    Particle.publish("WARN", "Could not find a valid BMP280 sensor, check wiring!");
  }

  Particle.publish("DEBUG", "started!");
 
  Particle.connect(); 
  Particle.variable("bme280temp", atticTemp);
  Particle.variable("bme280baro", baro);
  Particle.variable("bme280humid", humidity);
}

void loop() {
    digitalWrite(D7,!digitalRead(D7)); //blink D7
    wd.checkin(); //watchdog checkin
    
    if (System.buttonPushed() > 2000) { //trigger a wifi disconnect if you hold for over 2 seconds.
        //RGB.color(255, 255, 0); // YELLOW
        if (Particle.connected() == false) {  //if not connected, delay, 5000ms before attempting reconnect.  without delay was causing gps to fail.
            Serial.println("Connecting to wifi");
		    Particle.connect();
		    delay(1000);
        } else {
            //Particle.disconnect();
            WiFi.off();
            delay(1000);
        }   
    }
}



void serialDebug() {
    Serial.print("Temperature = ");
    Serial.print(bme.readTemperature());
    Serial.println(" *C");

    Serial.print("Pressure = ");

    Serial.print(bme.readPressure() / 100.0F);
    Serial.println(" hPa");

    Serial.print("Approx. Altitude = ");
    Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
    Serial.println(" m");

    Serial.print("Humidity = ");
    Serial.print(bme.readHumidity());
    Serial.println(" %");

    Serial.println();
}

void checkEnvironment() {

  Particle.publish("RSSI", String(WiFi.RSSI()));
  //Particle.publish("DEBUG", "checking the environment...");
  atticTemp = cToF(bme.readTemperature());
  Particle.publish("environment/temperature", String(atticTemp));
  Particle.publish("thingSpeakWrite_A0", "{ \"1\": \"" + String(atticTemp) + "\", \"k\": \"3JGM1YJR7EURDENQ\" }", 60, PRIVATE);
}

void checkEnvironment2() {

  baro = pToHg(bme.readPressure());
  Particle.publish("environment/pressure", String(baro));
  Particle.publish("environment/altitude", String(bme.readAltitude(SEALEVELPRESSURE_HPA)));
  humidity = bme.readHumidity();
  Particle.publish("environment/humidity", String(humidity));
}

float cToF(float c) {
  return c * 9/5 + 32;
}

float pToHg(float p) {
  return p/3389.39;
}


int relaycontrol(String command){
    if (command == "on") {
        toggle == true;
        digitalWrite(led, HIGH);
        digitalWrite(out1, HIGH);
        return 1;
    }
    else if(command =="off"){
        toggle = false;
        digitalWrite(out1, LOW);
        digitalWrite(led, LOW);
        return 0;
    }
    else if (command == "toggle"){
        if (toggle== true){
            digitalWrite(led, LOW);
            digitalWrite(out1, LOW);
            toggle = false;
            return 0;
            
        }
        else{
            digitalWrite(led, HIGH);
            digitalWrite(out1, HIGH);
            toggle = true;
            return 1;
        }
        
    }
    else { 
        return -1;
        
    }
}

Fan's Relay control code

Arduino
int light = D3;     //pin to control the outlet
int out2 = D5;
int led = D7;       //pin to turn on the built in led, this is just to see if the program works 
                    //when the relay is not used. Could be removed. 
bool toggle = true; //variable to keep track of current position of the relay
void setup() {
    pinMode(light, OUTPUT);     //pin D3 is initialized as a output pin 
    pinMode(led, OUTPUT);       //pin D7 is initialized as a output pin. 
    pinMode(out2, OUTPUT);
    //Spark.function is used to turn on or off the relay using the internet
    Spark.function("lightTimer",lightTimer);
    //The name inside the quotes is the name that will be visible when creating a recipe in IFTTT. This  
    // will call the lightTimer function. 
}

void loop() {
//nothing to loop
}

int lightTimer(String command){//command will be the string passed when this function is called from IFTTT
    if(command =="on"){             // turn on the relay
        toggle = true;
        digitalWrite(light, HIGH);
        digitalWrite(led, HIGH);
        digitalWrite(out2, LOW);
        return 1;
    }
    else if (command =="on2"){      // turn on the second relay
    digitalWrite(out2, HIGH);
    digitalWrite(led, HIGH);
    digitalWrite(light, LOW);
    return 1;
    }
    else if(command =="off"){       //turn off the relay
        toggle = false;
        digitalWrite(light, LOW);
        digitalWrite(led, LOW);
        digitalWrite(out2, LOW);
        return 0;
    }
    else if(command == "toggle"){   //toggle between on and off, using the DO button app. 
        if(toggle == true){         //turn off the relay since its already on
            digitalWrite(light, LOW);
            digitalWrite(led, LOW);
            toggle = false;
            return 0;
        }
        else{                       //turn on the relay since its already off
            digitalWrite(light, HIGH); 
            digitalWrite(led, HIGH);
            toggle = true;
            return 1;
        }
    }
    else{                       //other inputs return -1
        return -1;
    }
}

Credits

Daniel Starkel

Daniel Starkel

1 project • 1 follower
Anh Huy

Anh Huy

2 projects • 2 followers

Comments

Add projectSign up / Login