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]
default_envs = nanoatmega328
default_envs = tiny85
[env:nanoatmega328]
platform = atmelavr
@ -7,7 +7,7 @@ board = nanoatmega328
framework = arduino
board_build.f_cpu = 16000000L
lib_deps =
robtillaart/DHTStable
milesburton/DallasTemperature
targets = upload, monitor
upload_port = /dev/ttyUSB0
momitor_speed = 9600
@ -18,15 +18,21 @@ platform = atmelavr
framework = arduino
#board = digispark-tiny
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 =
robtillaart/DHTStable
milesburton/DallasTemperature
https://github.com/vlastahajek/RH_ASKTiny
upload_protocol = stk500v1
upload_flags =
-P$UPLOAD_PORT
-b$UPLOAD_SPEED
-V
targets = upload, monitor
#targets = upload
upload_speed = 19200
upload_port = /dev/ttyUSB0
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_BATTERY 3600

View File

@ -1,54 +1,116 @@
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <NexusTX.h>
#include <DHTStable.h>
#include <RH_ASK.h>
#include <OneWire.h>
#include <DallasTemperature.h>
//#include <math.h>
#if defined(ARDUINO_AVR_NANO)
#include "pins-nano.h"
#include "pins-nano.h"
#elif defined(ARDUINO_AVR_ATTINYX5)
#include "pins-attiny85.h"
#include "pins-attiny85.h"
#else
#error "Pins undef for this core"
#error "Pins undef for this core"
#endif
#include "config.h"
NexusTX transmitter(PIN_SEND);
DHTStable DHT;
#if LOGGER
#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)
{
if(LOGGER) {
Serial.print(logstr);
}
}
OneWire oneWire(PIN_DS18B20);
DallasTemperature ds18b20(&oneWire);
void LogI(long unsigned int logint)
{
if(LOGGER) {
Serial.print(logint);
}
}
void LogF(float logfloat)
{
if(LOGGER) {
Serial.print(logfloat);
}
}
void Logln()
{
if(LOGGER) {
Serial.println();
}
}
#if !defined(ARDUINO_AVR_ATTINYX5) || (defined(ARDUINO_AVR_ATTINYX5) && !LOGGER)
//RH_ASK driver(2000, -1, PIN_SEND, -1, false);
RH_ASK driver(2000, -1, PIN_SEND, -1, false, 8);
#endif
uint8_t rh_payload[6];
unsigned long myTime;
uint8_t sleeprepeat;
uint8_t sleeprepeatbat;
uint8_t sleepcount;
bool temp_enabled = false;
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)
{
if (time > 9 )
@ -64,67 +126,94 @@ void setupWatchDogTimer(int time)
void powerDown(void)
{
#if defined(BODS) && defined(BODSE) //if MCU has bulit-in BOD it will be disabled, ATmega328P, ATtiny85, AVR_ATtiny45, ATtiny25
sleep_bod_disable(); //disable Brown Out Detector (BOD) before going to sleep, saves more power
setupWatchDogTimer(WDTO_8S);
#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
sleep_enable();
sleep_mode();
sleep_disable();
}
void setup(void)
void setup()
{
Serial.begin(9600);
#if defined(ARDUINO_AVR_NANO)
pinMode(LED_BUILTIN, OUTPUT);
#endif
ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA
// setupWatchDogTimer(WDTO_8S);
set_sleep_mode(SLEEP_MODE_PWR_DOWN); //Power down everything, wake up from WDT
if(SEND_DELAY < 8)
{
{
sleeprepeat = 1;
} else {
sleeprepeat = lround(SEND_DELAY / 8);
}
delay(5000);
// setup power
ADCSRA &= ~(1<<ADEN); //Disable ADC, saves ~230uA
setupWatchDogTimer(WDTO_8S);
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();
if(SEND_DELAY_BATTERY < 8)
{
sleeprepeatbat = 1;
} else {
LogS("DHT Error");
Logln();
sleeprepeatbat = lround(SEND_DELAY_BATTERY / (sleeprepeat*8));
}
#if defined(ARDUINO_AVR_NANO)
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
#if LOGGER
Serial.begin(9600); // Debugging only
#if defined(ARDUINO_AVR_ATTINYX5)
// Disable RX (free port PB1)
ACSR &=~(1<<ACIE);
ACSR |=~(1<<ACD);
#endif
#endif
LogS("Sleep");
Logln();
delay(500);
for (int i=0; i<sleeprepeat; i++)
powerDown();
#if !defined(ARDUINO_AVR_ATTINYX5) || (defined(ARDUINO_AVR_ATTINYX5) && !LOGGER)
driver.init();
driver.setHeaderId(SENSORID);
#endif
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 SENSORID 12
#define PIN_DHT 4
#define OUTPUTCODE_OFF 5312788
#define OUTPUTCODE_ON 5312789
#define PIN_SEND 0
#define SENSORID 101
#define PIN_DS18B20 1