Hi,
this is my code working with 2.1 version. The code is not tidied up, please excuse.
/*
MySensors-Node for DS18B20 Temperature-Sensors.
Mysensors.Library-Version 1.6
*/
#define MY_RADIO_NRF24
#define MY_TRANSPORT_UPLINK_CHECK_DISABLED
//#define MY_DEBUG
#include <MySensors.h>
#include <SPI.h>
#include <Wire.h>
#include <SI7021.h>
#define BATT_SENSOR
//#define VCCGND_PINS
#ifdef VCCGND_PINS
const uint8_t GND = A2;
const uint8_t VCC = A3;
#endif
#ifdef BATT_SENSOR
#define REPORT_VOLTAGE
#endif
const unsigned long SLEEP_TIME = 300000; // Sleep time between reads (in milliseconds)
const uint8_t TEMP_TIME = 12; //at least every nth time battery is reported
const uint8_t HUM_TIME = 12;
const uint8_t BATT_TIME = 12; //when also BATT-LEVEL is reportet
const float BATT_100 = 3; //3.3V for CR2032, 3V for 2xAA
const float BATT_0 = 2.2;
SI7021 sensor;
float lastTemperature, lastHum;
uint8_t lastTempSent = 0;//, lastHumSent = 0;
uint8_t numSensors = 0;
boolean receivedConfig = false;
boolean metric = true;
// Initialize temperature message
MyMessage msgTemp(0, V_TEMP);
MyMessage msgHum(0, V_HUM);
#ifdef REPORT_VOLTAGE
MyMessage msgBatt(1, V_VOLTAGE);
#endif
#ifdef BATT_SENSOR
uint8_t battReport = BATT_TIME - 1; //First report at startup
long oldvalue = 0;
#endif
void setup()
{
#ifdef VCCGND_PINS
pinMode(VCC, OUTPUT);
digitalWrite(VCC, HIGH);
pinMode(GND, OUTPUT);
digitalWrite(GND, LOW);
#endif
if (!sensor.begin()) {
Serial.println("No Sensor found!");
while (true);
}
}
void presentation() {
// Send the sketch version information to the gateway and Controller
sendSketchInfo("TempHumSi7021", "0.1a");
// Present all sensors to controller
present(0, S_HUM);
//present(1, S_HUM);
#ifdef REPORT_VOLTAGE
present(1, S_MULTIMETER);
#endif
}
void loop()
{
//delay(2000);//for sensor to start up
boolean tempsent = false;
// Fetch temperatures from Dallas sensors
si7021_thc temphum = sensor.getTempAndRH();
// Read temperatures and send them to controller
// Fetch and round temperature to one decimal
float temperature = (float)(temphum.celsiusHundredths) / 100.0;
float humidity = (float)(temphum.humidityPercent);
// Only send data if temperature has changed and no error
if ((lastTemperature != temperature) || lastHum != humidity || (++lastTempSent >= TEMP_TIME)) {
// Send in the new temperature
send(msgTemp.set(temperature, 1));
send(msgHum.set(humidity, 1));
lastHum = humidity;
lastTemperature = temperature;
lastTempSent = 0;
tempsent = true;
}
#ifdef BATT_SENSOR
if (++battReport >= BATT_TIME && tempsent) {
//gw.sleep(10);
long value = readVcc();
if (value != oldvalue) {
int percent = (( (float)(value) / 1000 ) - BATT_0) / (BATT_100 - BATT_0) * 100;
percent = (percent > 100) ? 100 : percent;
percent = (percent < 0) ? 0 : percent;
sendBatteryLevel(percent);
#ifdef REPORT_VOLTAGE
send(msgBatt.set((float)(value) / 1000, 2));
#endif
}
oldvalue = value;
battReport = 0;
}
#endif
sleep(SLEEP_TIME);//wake on interrupt or after sleep-time
//delay(2000);//for sensor to start up
}
long readVcc() {
// Read 1.1V reference against AVcc
// set the reference to Vcc and the measurement to the internal 1.1V reference
#if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
ADMUX = _BV(MUX5) | _BV(MUX0);
#elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = _BV(MUX3) | _BV(MUX2);
#else
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
#endif
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA, ADSC)); // measuring
uint8_t low = ADCL; // must read ADCL first - it then locks ADCH
uint8_t high = ADCH; // unlocks both
long result = (high << 8) | low;
result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000
return result; // Vcc in millivolts
}```