battery sensor
This commit is contained in:
parent
34d33d05f0
commit
c685867d51
|
|
@ -14,12 +14,12 @@ RH_ASK driver(RH_SPEED, RH_RX_PIN);
|
|||
|
||||
float t = 0;
|
||||
float h = 0;
|
||||
uint8_t bat_percent = 0;
|
||||
uint8_t bat_raw = 0;
|
||||
long bat = 0;
|
||||
|
||||
#define MSG_START 0x00
|
||||
#define MSG_TEMP 0x01
|
||||
#define MSG_HUMID 0x02
|
||||
#define MSG_BAT 0x03
|
||||
#define MSG_ERR 0xee
|
||||
|
||||
void setup() {
|
||||
|
|
@ -55,6 +55,13 @@ void loop() {
|
|||
Serial.print(t);
|
||||
Serial.print("%");
|
||||
break;
|
||||
case MSG_BAT:
|
||||
// battery data
|
||||
Serial.println("0x03 Battery");
|
||||
memcpy(&bat, &rh_buf[1], 4);
|
||||
Serial.print(bat);
|
||||
Serial.print("mV");
|
||||
break;
|
||||
case MSG_ERR:
|
||||
// error
|
||||
Serial.println("0xEE error");
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
RadioHead attiny85 DHT22 Weather Station
|
||||
|
||||
2021 Stefan Ostermann based on several source from the internetz.
|
||||
|
||||
|
||||
Sources:
|
||||
https://git.cryhost.de/crycode/attiny85-radiohead-dht22-weather-sensor/-/tree/master
|
||||
*/
|
||||
|
|
@ -63,6 +63,7 @@ uint8_t rh_buf[RH_BUF_LEN] = { 0x00, 0x00, 0x00, 0x00, 0x00 };
|
|||
#define MSG_START 0x00
|
||||
#define MSG_TEMP 0x01
|
||||
#define MSG_HUMID 0x02
|
||||
#define MSG_BAT 0x03
|
||||
#define MSG_ERR 0xee
|
||||
|
||||
|
||||
|
|
@ -72,11 +73,13 @@ RH_ASK rh_driver(RH_SPEED, RH_RX_PIN, RH_TX_PIN, RH_PTT_PIN);
|
|||
volatile boolean f_wdt = 1;
|
||||
|
||||
// temp / humid toggle
|
||||
volatile boolean toggle = 0;
|
||||
volatile uint8_t counter = 0;
|
||||
|
||||
|
||||
void gotoSleep(void);
|
||||
|
||||
int getVcc();
|
||||
|
||||
void setup() {
|
||||
// setup the LED pin
|
||||
pinMode(LED_PIN, OUTPUT);
|
||||
|
|
@ -118,28 +121,29 @@ void loop() {
|
|||
// temperature and humidity
|
||||
float t = 0;//float is 32bit = 1byte
|
||||
float h = 0;
|
||||
long battery = 0;
|
||||
|
||||
// read from the sensor and check if it was successfull
|
||||
if (dht_read_data(&dht, &t, &h) == 1) {
|
||||
// normal message
|
||||
|
||||
if (toggle) {
|
||||
if (counter % 4 == 0) {
|
||||
rh_buf[0] = MSG_BAT;
|
||||
battery = getVcc();
|
||||
memcpy(&rh_buf[1], &battery, 4);
|
||||
rh_send(RH_BUF_LEN);
|
||||
}else if (dht_read_data(&dht, &t, &h) == 1) {
|
||||
if (counter % 2 == 0) {
|
||||
rh_buf[0] = MSG_TEMP;
|
||||
memcpy(&rh_buf[1], &t, 4);
|
||||
} else {
|
||||
} else if (counter % 2 == 1) {
|
||||
rh_buf[0] = MSG_HUMID;
|
||||
memcpy(&rh_buf[1],&h,4);
|
||||
memcpy(&rh_buf[1], &h, 4);
|
||||
}
|
||||
toggle = !toggle;
|
||||
|
||||
rh_send(RH_BUF_LEN);
|
||||
|
||||
rh_send(RH_BUF_LEN);
|
||||
} else {
|
||||
// error message
|
||||
rh_buf[0] = MSG_ERR;
|
||||
rh_send(1);
|
||||
|
||||
}
|
||||
counter++;
|
||||
|
||||
// turn off the LED
|
||||
digitalWrite(LED_PIN, LOW);
|
||||
|
|
@ -167,7 +171,7 @@ ISR(WDT_vect)
|
|||
system wakes up when watchdog is timed out
|
||||
*/
|
||||
void system_sleep() {
|
||||
cbi(ADCSRA, ADEN); // switch Analog to Digitalconverter OFF
|
||||
//cbi(ADCSRA, ADEN); // switch Analog to Digitalconverter OFF
|
||||
|
||||
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
|
||||
sleep_enable();
|
||||
|
|
@ -175,7 +179,7 @@ void system_sleep() {
|
|||
sleep_mode(); // System sleeps here
|
||||
|
||||
sleep_disable(); // System continues execution here when watchdog timed out
|
||||
sbi(ADCSRA, ADEN); // switch Analog to Digitalconverter ON
|
||||
//sbi(ADCSRA, ADEN); // switch Analog to Digitalconverter ON
|
||||
}
|
||||
|
||||
/* 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
|
||||
|
|
@ -198,3 +202,25 @@ void setup_watchdog(int ii) {
|
|||
WDTCR = bb;
|
||||
WDTCR |= _BV(WDIE);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
From https://github.com/cano64/ArduinoSystemStatus/blob/master/SystemStatus.cpp
|
||||
*/
|
||||
int getVcc() {
|
||||
//reads internal 1V1 reference against VCC
|
||||
ADMUX = _BV(MUX3) | _BV(MUX2); // For ATtiny85/45
|
||||
|
||||
_delay_ms(5);; // Wait for Vref to settle
|
||||
ADCSRA |= _BV(ADSC); // Convert
|
||||
while (bit_is_set(ADCSRA, ADSC));
|
||||
uint8_t low = ADCL;
|
||||
unsigned int val = (ADCH << 8) | low;
|
||||
//discard previous result
|
||||
ADCSRA |= _BV(ADSC); // Convert
|
||||
while (bit_is_set(ADCSRA, ADSC));
|
||||
low = ADCL;
|
||||
val = (ADCH << 8) | low;
|
||||
|
||||
return ((long)1024 * 1100) / val;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue