-
-
Save solars/688a635f791c2e558a1ec57b67c9beb4 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
The MySensors Arduino library handles the wireless radio link and protocol | |
between your home built sensors/actuators and HA controller of choice. | |
The sensors forms a self healing radio network with optional repeaters. Each | |
repeater and gateway builds a routing tables in EEPROM which keeps track of the | |
network topology allowing messages to be routed to nodes. | |
Created by Henrik Ekblad <henrik.ekblad@mysensors.org> | |
Copyright (C) 2013-2015 Sensnology AB | |
Full contributor list: https://github.com/mysensors/Arduino/graphs/contributors | |
Documentation: http://www.mysensors.org | |
Support Forum: http://forum.mysensors.org | |
This program is free software; you can redistribute it and/or | |
modify it under the terms of the GNU General Public License | |
version 2 as published by the Free Software Foundation. | |
**/ | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
Adafruit_BME280 bme; // I2C | |
// Enable debug prints to serial monitor | |
#define MY_DEBUG | |
// Adapt this constant: set it to the altitude above sealevel at your home location. | |
const float ALTITUDE = 435; // meters above sea level. Use your smartphone GPS to get an accurate value! | |
const float SEALEVEL_PRESSURE = 1013.25; | |
#include <MemoryFree.h> | |
#include <avr/wdt.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> | |
#endif | |
// Enable and select radio type attached | |
#define MY_RADIO_RFM69 | |
#define MY_RFM69_FREQUENCY RFM69_433MHZ | |
// Comment it out for CW version radio.msg_tmp | |
#define MY_IS_RFM69HW | |
// Comment it out for Auto Node ID # | |
#define MY_NODE_ID 7 | |
// Avoid battery drain if Gateway disconnected and the node sends more than MY_TRANSPORT_STATE_RETRIES times message. | |
#define MY_TRANSPORT_UPLINK_CHECK_DISABLED | |
#define MY_PARENT_NODE_IS_STATIC | |
#define MY_PARENT_NODE_ID 0 | |
//Enable OTA feature | |
#define MY_OTA_FIRMWARE_FEATURE | |
#define MY_OTA_FLASH_JDECID 0 //0x2020 | |
//Enable Crypto Authentication to secure the node | |
//#define MY_SIGNING_ATSHA204 | |
//#define MY_SIGNING_REQUEST_SIGNATURES | |
#include <Wire.h> | |
#include <MySensors.h> | |
// Redefining write codes for JDEC FLASH used in the node | |
// These two defines should always be after #include <MySensors.h> declaration | |
#define SPIFLASH_BLOCKERASE_32K 0xD8 | |
#define SPIFLASH_CHIPERASE 0x60 | |
#include <stdlib.h> | |
#define RELAY_pin 7 // Digital pin connected to relay | |
// Assign numbers for all sensors we will report to gateway\controller (they will be created as child devices) | |
#define BME_TEMP 1 | |
#define BME_HUM 2 | |
#define BME_PRES 3 | |
// Create MyMessage Instance for sending readins from sensors to gateway\controller (they will be created as child devices) | |
MyMessage msg_be_temp(BME_TEMP, V_TEMP); | |
MyMessage msg_be_hum(BME_HUM, V_HUM); | |
MyMessage msg_be_pres(BME_PRES, V_PRESSURE); | |
unsigned long wdiDelay2 = 0; | |
int BATTERY_SENSE_PIN = A6; // select the input pin for the battery sense point | |
static float oldBmeTemp = 999, bmeTemp; | |
static uint8_t oldBmeHum = 999, bmeHum; // no float needed | |
static float oldBmePres = 1, bmePres; | |
void swarm_report() | |
{ | |
static int oldBatteryPcnt = 0; | |
// Get the battery Voltage | |
int sensorValue = analogRead(BATTERY_SENSE_PIN); | |
// 1M, 470K divider across battery and using internal ADC ref of 1.1V1 | |
// ((1e6+470e3)/470e3)*1.1 = Vmax = 3.44 Volts | |
/* The MySensors Lib uses internal ADC ref of 1.1V which means analogRead of the pin connected to 470kOhms Battery Devider reaches | |
1023 when voltage on the divider is around 3.44 Volts. 2.5 volts is equal to 750. 2 volts is equal to 600. | |
RFM 69 CW works stable up to 2 volts. Assume 2.5 V is 0% and 1023 is 100% battery charge | |
RFM 69 HCW works stable up to 2.5 volts (sometimes it can work up to 2.0V). Assume 2.5 V is 0% and 1023 is 100% battery charge | |
3.3V ~ 1023 | |
3.0V ~ 900 | |
2.5V ~ 750 | |
2.0V ~ 600 | |
*/ | |
//Serial.print("sensorValue: "); Serial.println(sensorValue); | |
#ifdef MY_IS_RFM69HW | |
int batteryPcnt = (sensorValue - 750) / 1.5; | |
#else | |
int batteryPcnt = (sensorValue - 600) / 3; | |
#endif | |
batteryPcnt = batteryPcnt > 0 ? batteryPcnt : 0; // Cut down negative values. Just in case the battery goes below 2V (2.5V) and the node still working. | |
batteryPcnt = batteryPcnt < 100 ? batteryPcnt : 100; // Cut down more than "100%" values. In case of ADC fluctuations. | |
if (oldBatteryPcnt != batteryPcnt ) { | |
// Power up radio after sleep | |
sendBatteryLevel(batteryPcnt); | |
oldBatteryPcnt = batteryPcnt; | |
} | |
wait(100); | |
bmeTemp = bme.readTemperature(); | |
// round to next 0.x | |
bmeTemp = round(bmeTemp * 10) / 10.0; | |
if (bmeTemp != oldBmeTemp) { | |
send(msg_be_temp.set(bmeTemp, 1), true); | |
oldBmeTemp = bmeTemp; | |
} | |
wait(100); | |
// round to the next int | |
bmeHum = round(bme.readHumidity()); | |
if (bmeHum != oldBmeHum) { | |
send(msg_be_hum.set(bmeHum, 1), true); | |
send(msg_be_hum.set(bmeHum, 1), true); | |
oldBmeHum = bmeHum; | |
} | |
wait(100); | |
// Read atmospheric pressure at local altitude | |
bmePres = bme.readPressure() / 100.0F; | |
// Local pressure adjusted to sea level pressure using user altitude | |
bmePres = bmePres / pow((1.0 - ( ALTITUDE / 44330.0 )), 5.255); | |
bmePres = round(bmePres * 10) / 10.0; | |
if (bmePres != oldBmePres) { | |
send(msg_be_pres.set(bmePres, 1), true); | |
oldBmePres = bmePres; | |
} | |
wait(100); | |
} | |
void before() { | |
analogReference(INTERNAL); // using internal ADC ref of 1.1V | |
//No need watch dog enabled in case of battery power. | |
//wdt_enable(WDTO_4S); | |
wdt_disable(); | |
//lightMeter.begin(); | |
} | |
void setup() { | |
if (!bme.begin(0x76)) { | |
Serial.println("Could not find a valid BME280 sensor, check wiring!"); | |
while (1); | |
} | |
bme.setSampling(Adafruit_BME280::MODE_FORCED, | |
Adafruit_BME280::SAMPLING_X1, // temperature | |
Adafruit_BME280::SAMPLING_X1, // pressure | |
Adafruit_BME280::SAMPLING_X1, // humidity | |
Adafruit_BME280::FILTER_OFF ); | |
} | |
void presentation() | |
{ | |
// Send the sketch version information to the gateway and Controller | |
sendSketchInfo("BME280 node", "1.0"); | |
// Register all sensors to gw (they will be created as child devices) | |
present(BME_TEMP, S_TEMP); | |
present(BME_HUM, S_HUM); | |
present(BME_PRES, S_BARO); | |
} | |
unsigned long wdiDelay = 0; | |
void loop() { | |
//No need watch dog in case of battery power. | |
//wdt_reset(); | |
_flash.wakeup(); | |
bme.takeForcedMeasurement(); // has no effect in normal mode | |
swarm_report(); | |
/* Please comment out private declaration in BH1750.h | |
Otherwise you can't call lightMeter.write8(BH1750_POWER_DOWN); and BH1750 will not sleep! | |
//private: | |
void write8(uint8_t data); | |
*/ | |
// Go sleep for some milliseconds | |
_flash.sleep(); | |
sleep(300000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment