diff --git a/platformio.ini b/platformio.ini index 9743af3..e158da9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -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 diff --git a/src/config.h b/src/config.h index 8bd7004..499e15f 100644 --- a/src/config.h +++ b/src/config.h @@ -1,2 +1,3 @@ -#define LOGGER 0 +#define LOGGER 1 #define SEND_DELAY 120 +#define SEND_DELAY_BATTERY 3600 diff --git a/src/main.ino b/src/main.ino index eba3863..1835f5c 100644 --- a/src/main.ino +++ b/src/main.ino @@ -1,54 +1,116 @@ #include #include -#include -#include +#include + +#include +#include + +//#include #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< 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