Created
February 27, 2016 17:22
-
-
Save coolya/a8c3b71bf60e1f766f2f to your computer and use it in GitHub Desktop.
Arduino sketch for homedata project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SparkFunBME280.h> | |
#include <Adafruit_HDC1000.h> | |
#include <avr/sleep.h> | |
#include <avr/power.h> | |
#include <avr/wdt.h> | |
BME280 bme; | |
Adafruit_HDC1000 hdc = Adafruit_HDC1000(); | |
volatile int seconds_asleep = 60; | |
ISR(WDT_vect) | |
{ | |
seconds_asleep++; | |
if (seconds_asleep > 60) | |
{ | |
Serial.println("Error: event was not handled!"); | |
} | |
} | |
void enterSleep(void) | |
{ | |
set_sleep_mode(SLEEP_MODE_PWR_SAVE); /* EDIT: could also use SLEEP_MODE_PWR_DOWN for lowest power consumption. */ | |
sleep_enable(); | |
/* Now enter sleep mode. */ | |
sleep_mode(); | |
/* The program will continue from here after the WDT timeout*/ | |
sleep_disable(); /* First thing to do is disable sleep. */ | |
/* Re-enable the peripherals. */ | |
power_all_enable(); | |
} | |
void setup(void) { | |
initBME(); | |
if (!hdc.begin(0x43)) { | |
Serial.println("Couldn't find sensor!"); | |
while (1); | |
} | |
/* Clear the reset flag. */ | |
MCUSR &= ~(1<<WDRF); | |
/* In order to change WDE or the prescaler, we need to | |
* set WDCE (This will allow updates for 4 clock cycles). | |
*/ | |
WDTCSR |= (1<<WDCE) | (1<<WDE); | |
/* set new watchdog timeout prescaler value */ | |
WDTCSR = 1<<WDP2 | 1<<WDP1; /* 1.0 seconds */ | |
/* Enable the WD interrupt (note no reset). */ | |
WDTCSR |= _BV(WDIE); | |
} | |
void initBME(void) { | |
//***Driver settings********************************// | |
//commInterface can be I2C_MODE or SPI_MODE | |
//specify chipSelectPin using arduino pin names | |
//specify I2C address. Can be 0x77(default) or 0x76 | |
//For I2C, enable the following and disable the SPI section | |
bme.settings.commInterface = I2C_MODE; | |
bme.settings.I2CAddress = 0x77; | |
//For SPI enable the following and dissable the I2C section | |
//bme.settings.commInterface = SPI_MODE; | |
//bme.settings.chipSelectPin = 10; | |
//***Operation settings*****************************// | |
//renMode can be: | |
// 0, Sleep mode | |
// 1 or 2, Forced mode | |
// 3, Normal mode | |
bme.settings.runMode = 3; //Normal mode | |
//tStandby can be: | |
// 0, 0.5ms | |
// 1, 62.5ms | |
// 2, 125ms | |
// 3, 250ms | |
// 4, 500ms | |
// 5, 1000ms | |
// 6, 10ms | |
// 7, 20ms | |
bme.settings.tStandby = 0; | |
//filter can be off or number of FIR coefficients to use: | |
// 0, filter off | |
// 1, coefficients = 2 | |
// 2, coefficients = 4 | |
// 3, coefficients = 8 | |
// 4, coefficients = 16 | |
bme.settings.filter = 0; | |
//tempOverSample can be: | |
// 0, skipped | |
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively | |
bme.settings.tempOverSample = 1; | |
//pressOverSample can be: | |
// 0, skipped | |
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively | |
bme.settings.pressOverSample = 1; | |
//humidOverSample can be: | |
// 0, skipped | |
// 1 through 5, oversampling *1, *2, *4, *8, *16 respectively | |
bme.settings.humidOverSample = 1; | |
Serial.begin(57600); | |
Serial.print("Program Started\n"); | |
Serial.print("Starting BME280... result of .begin(): 0x"); | |
//Calling .begin() causes the settings to be loaded | |
delay(10); //Make sure sensor had enough time to turn on. BME280 requires 2ms to start up. | |
Serial.println(bme.begin(), HEX); | |
Serial.println(); | |
} | |
void loop() { | |
if(seconds_asleep >= 60) | |
{ | |
seconds_asleep = 0; | |
measureData(); | |
delay(100); | |
} | |
enterSleep(); | |
} | |
void measureData(void) { | |
Serial.print("{ \"outside\" : {\"temperature\" :"); | |
Serial.print(bme.readTempC(), 6); | |
Serial.print(", \"humidity\" : "); | |
Serial.print(bme.readFloatHumidity(), 2); | |
Serial.print(", \"pressure\" : "); | |
Serial.print(bme.readFloatPressure() / 100, 2); | |
Serial.print("}, \"inside\" : {\"temperature\" : "); | |
Serial.print(hdc.readTemperature(), 6); | |
Serial.print(", \"humidity\" : "); | |
Serial.print(hdc.readHumidity(), 6); | |
Serial.print("} }"); | |
Serial.println(); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment