This commit is contained in:
Nigreon 2025-03-04 18:03:44 +01:00
parent 97e26f0105
commit 941a44cd82
4 changed files with 186 additions and 93 deletions

View File

@ -1,5 +1,5 @@
[platformio] [platformio]
default_envs = nanoatmega328 default_envs = tiny85
[env:nanoatmega328] [env:nanoatmega328]
platform = atmelavr platform = atmelavr
@ -7,7 +7,7 @@ board = nanoatmega328
framework = arduino framework = arduino
board_build.f_cpu = 16000000L board_build.f_cpu = 16000000L
lib_deps = lib_deps =
robtillaart/DHTStable milesburton/DallasTemperature
targets = upload, monitor targets = upload, monitor
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0
momitor_speed = 9600 momitor_speed = 9600
@ -18,15 +18,21 @@ platform = atmelavr
framework = arduino framework = arduino
#board = digispark-tiny #board = digispark-tiny
board = attiny85 board = attiny85
board_build.f_cpu = 8000000L board_build.f_cpu = 4000000L
build_flags =
-DRH_ASK_ATTINY_USE_TIMER1
-DRH_ASK_TX_ONLY
lib_deps = lib_deps =
robtillaart/DHTStable milesburton/DallasTemperature
https://github.com/vlastahajek/RH_ASKTiny
upload_protocol = stk500v1 upload_protocol = stk500v1
upload_flags = upload_flags =
-P$UPLOAD_PORT -P$UPLOAD_PORT
-b$UPLOAD_SPEED -b$UPLOAD_SPEED
-V
targets = upload, monitor targets = upload, monitor
#targets = upload
upload_speed = 19200 upload_speed = 19200
upload_port = /dev/ttyUSB0 upload_port = /dev/ttyUSB0
momitor_speed = 9600 momitor_speed = 9600
monitor_port = /dev/ttyUSB1 monitor_port = /dev/ttyACM0

View File

@ -1,2 +1,3 @@
#define LOGGER 0 #define LOGGER 1
#define SEND_DELAY 120 #define SEND_DELAY 120
#define SEND_DELAY_BATTERY 3600

View File

@ -1,54 +1,116 @@
#include <avr/sleep.h> #include <avr/sleep.h>
#include <avr/wdt.h> #include <avr/wdt.h>
#include <NexusTX.h> #include <RH_ASK.h>
#include <DHTStable.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <math.h>
#if defined(ARDUINO_AVR_NANO) #if defined(ARDUINO_AVR_NANO)
#include "pins-nano.h" #include "pins-nano.h"
#elif defined(ARDUINO_AVR_ATTINYX5) #elif defined(ARDUINO_AVR_ATTINYX5)
#include "pins-attiny85.h" #include "pins-attiny85.h"
#else #else
#error "Pins undef for this core" #error "Pins undef for this core"
#endif #endif
#include "config.h" #include "config.h"
NexusTX transmitter(PIN_SEND); #if LOGGER
DHTStable DHT; #warning "Logger"
#define Log(l) Serial.print(l)
#define LogLn(l) Serial.println(l)
#else
#warning "No Logger"
#define Log(l)
#define LogLn(l)
#endif
int sleeprepeat;
void LogS(const char* logstr) OneWire oneWire(PIN_DS18B20);
{ DallasTemperature ds18b20(&oneWire);
if(LOGGER) {
Serial.print(logstr);
}
}
void LogI(long unsigned int logint) #if !defined(ARDUINO_AVR_ATTINYX5) || (defined(ARDUINO_AVR_ATTINYX5) && !LOGGER)
{ //RH_ASK driver(2000, -1, PIN_SEND, -1, false);
if(LOGGER) { RH_ASK driver(2000, -1, PIN_SEND, -1, false, 8);
Serial.print(logint); #endif
}
} uint8_t rh_payload[6];
void LogF(float logfloat)
{ unsigned long myTime;
if(LOGGER) {
Serial.print(logfloat); uint8_t sleeprepeat;
} uint8_t sleeprepeatbat;
} uint8_t sleepcount;
void Logln()
{ bool temp_enabled = false;
if(LOGGER) {
Serial.println();
}
}
ISR(WDT_vect) { ISR(WDT_vect) {
//Don't do anything. This is just here so that we wake up. // Nothing needs to be done, however interrupt handler must be defined to
// prevent a reset.
} }
void setSensorType(uint8_t stype)
{
uint8_t payload0;
payload0 = stype << 4;
rh_payload[0] &= 0b00001111;
rh_payload[0] += payload0;
}
void setBatteryStatus(uint8_t bstatus)
{
if(bstatus == 1) {
rh_payload[0] |= 0b00000001;
} else {
rh_payload[0] &= 0b11111110;
}
}
void setBatteryVoltage(uint16_t bvoltage)
{
uint16_t payloadbat = rh_payload[2];
payloadbat += rh_payload[1] << 8;
payloadbat &= 0b0000000001111111;
payloadbat += bvoltage << 7;
//Serial.println(payloadbat);
rh_payload[1] = payloadbat >> 8;
rh_payload[2] = payloadbat;
}
void setBatteryPercent(uint8_t bpercent)
{
uint16_t payloadbat = rh_payload[2];
payloadbat += rh_payload[1] << 8;
payloadbat &= 0b1111111110000000;
payloadbat += bpercent;
//Serial.println(payloadbat);
rh_payload[1] = payloadbat >> 8;
rh_payload[2] = payloadbat;
}
void setTemperature()
{
ds18b20.requestTemperaturesByIndex(0);
delay(500);
int16_t tempC = (int16_t) roundf(ds18b20.getTempCByIndex(0)*100);
if(tempC != DEVICE_DISCONNECTED_C) {
//int16_t tempC = (int16_t) (ds18b20.getTempCByIndex(0)*100.0);
LogLn(tempC);
rh_payload[3] = tempC >> 8;
rh_payload[4] = tempC;
}
}
void setHumidity()
{
rh_payload[5] = 33;
}
void setupWatchDogTimer(int time) void setupWatchDogTimer(int time)
{ {
if (time > 9 ) if (time > 9 )
@ -64,67 +126,94 @@ void setupWatchDogTimer(int time)
void powerDown(void) void powerDown(void)
{ {
#if defined(BODS) && defined(BODSE) //if MCU has bulit-in BOD it will be disabled, ATmega328P, ATtiny85, AVR_ATtiny45, ATtiny25 setupWatchDogTimer(WDTO_8S);
sleep_bod_disable(); //disable Brown Out Detector (BOD) before going to sleep, saves more power #if defined(BODS) && defined(BODSE) //if MCU has bulit-in BOD it will be disabled, ATmega328P, ATtiny8>
sleep_bod_disable(); //disable Brown Out Detector (BOD) before going to sleep, saves mo>
#endif #endif
sleep_enable();
sleep_mode(); sleep_mode();
sleep_disable();
} }
void setup(void) void setup()
{ {
Serial.begin(9600); ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA
#if defined(ARDUINO_AVR_NANO) // setupWatchDogTimer(WDTO_8S);
pinMode(LED_BUILTIN, OUTPUT); set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
#endif
if(SEND_DELAY < 8) if(SEND_DELAY < 8)
{ {
sleeprepeat = 1; sleeprepeat = 1;
} else { } else {
sleeprepeat = lround(SEND_DELAY / 8); sleeprepeat = lround(SEND_DELAY / 8);
} }
delay(5000);
// setup power if(SEND_DELAY_BATTERY < 8)
ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA {
setupWatchDogTimer(WDTO_8S); sleeprepeatbat = 1;
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
sleep_enable();
transmitter.setChannel(0);
transmitter.setId(SENSORID);
transmitter.setBatteryFlag(1);
}
void loop(void)
{
if(DHT.read22(PIN_DHT) == DHTLIB_OK)
{
LogS("Send ");
LogS("Temperature: ");
LogF(DHT.getTemperature());
//LogF(DHT.temperature);
LogS(" Humidity: ");
LogF(DHT.getHumidity());
//LogF(DHT.humidity);
Logln();
transmitter.setTemperature(DHT.getTemperature()); // -49.9C...+69.9C
transmitter.setHumidity(lround(DHT.getHumidity())); // 2...98%
transmitter.SendPacket();
} else { } else {
LogS("DHT Error"); sleeprepeatbat = lround(SEND_DELAY_BATTERY / (sleeprepeat*8));
Logln();
} }
#if defined(ARDUINO_AVR_NANO)
digitalWrite(LED_BUILTIN, HIGH); #if LOGGER
delay(500); Serial.begin(9600); // Debugging only
digitalWrite(LED_BUILTIN, LOW); #if defined(ARDUINO_AVR_ATTINYX5)
// Disable RX (free port PB1)
ACSR &=~(1<<ACIE);
ACSR |=~(1<<ACD);
#endif
#endif #endif
LogS("Sleep"); #if !defined(ARDUINO_AVR_ATTINYX5) || (defined(ARDUINO_AVR_ATTINYX5) && !LOGGER)
Logln(); driver.init();
delay(500); driver.setHeaderId(SENSORID);
for (int i=0; i<sleeprepeat; i++) #endif
powerDown(); LogLn("Startup");
ds18b20.begin();
if(ds18b20.getDeviceCount() > 0) { temp_enabled = true; }
//rh_payload[0]=10;
// Temperature initial (-100)
rh_payload[3] = 0xff;
rh_payload[4] = 0x9c;
setSensorType(1);
setBatteryStatus(0);
setBatteryVoltage(123);
setBatteryPercent(50);
//if(temp_enabled == true) { setTemperature(); }
//setHumidity();
}
void loop()
{
//Serial.println("Send");
if(temp_enabled == true) { setTemperature(); }
setHumidity();
#if !defined(ARDUINO_AVR_ATTINYX5) || (defined(ARDUINO_AVR_ATTINYX5) && !LOGGER)
for(uint8_t i=0;i<3;i++)
{
driver.send(rh_payload, 6);
driver.waitPacketSent();
}
#endif
LogLn("Before delay");
delay(15000);
myTime = millis();
LogLn(sleepcount);
LogLn(myTime);
if(sleepcount > sleeprepeatbat)
{
LogLn("Probe Battery");
sleepcount = 0;
} else {
sleepcount = sleepcount + 1;
}
//setupWatchDogTimer(WDTO_8S);
for (uint8_t i=0; i<sleeprepeat; i++) { powerDown(); }
//sleep_disable();
} }

View File

@ -1,6 +1,3 @@
#define PIN_SEND 3 #define PIN_SEND 0
#define SENSORID 12 #define SENSORID 101
#define PIN_DHT 4 #define PIN_DS18B20 1
#define OUTPUTCODE_OFF 5312788
#define OUTPUTCODE_ON 5312789