rf433demo/src/main.ino

161 lines
3.4 KiB
C++

#include <RCSwitch.h>
//#include <Oregon_TM.h>
#include <NexusTX.h>
#include <SensorTransmitter.h>
#include <DHTStable.h>
#include <math.h>
#if defined(ARDUINO_AVR_NANO)
#include "pins-nano.h"
#elif defined(ARDUINO_AVR_ATTINYX5)
#include "pins-attiny85.h"
#else
#error "Pins undef for this core"
#endif
#include "config.h"
RCSwitch mySwitch = RCSwitch();
NexusTX transmitter(PIN_SEND);
//Oregon_TM transmitter(PIN_SEND);
//ThermoHygroTransmitter transmitter(PIN_SEND, SENSORID, 2);
DHTStable DHT;
bool sensor_enabled = false;
//bool output_enabled = false;
bool output_state = false;
unsigned long lastsend = 0;
void setup() {
pinMode(PIN_OUTPUT, OUTPUT);
digitalWrite(PIN_OUTPUT, LOW);
//delay(5000);
if(LOGGER) {
Serial.begin(9600);
#if defined(ARDUINO_AVR_ATTINYX5)
// Disable RX (free port PB1)
ACSR &=~(1<<ACIE);
ACSR |=~(1<<ACD);
#endif
}
delay(5000);
LogS("Hello");
Logln();
//sensor_enabled = true;
if(DHT.read22(PIN_DHT) == DHTLIB_OK) { sensor_enabled = true; LogS("DHT detected"); Logln(); } else { LogS("No DHT"); Logln(); }
delay(1000);
//THGN132
// Model for Oregon_TM only
//transmitter.setType(THGN132);
//transmitter.setType(THGR810); //preferred
//transmitter.setType(RTGN318);
//transmitter.setType(BTHGN129);
//transmitter.setType(BTHR968);
transmitter.setChannel(0);
transmitter.setId(SENSORID);
transmitter.setBatteryFlag(1);
//transmitter.setComfort(24.2, 30);
mySwitch.enableReceive(0); // Receiver on pin INT0
delay(1000);
}
void LogS(const char* logstr)
{
if(LOGGER) {
Serial.print(logstr);
}
}
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();
}
}
void loop() {
if (mySwitch.available()) {
unsigned long recvvalue = mySwitch.getReceivedValue();
/*
LogS("Received ");
LogI(recvvalue);
LogS(" / ");
LogI(mySwitch.getReceivedBitlength());
LogS("bit ");
LogS("Protocol: ");
LogI(mySwitch.getReceivedProtocol());
Logln();
*/
switch (recvvalue) {
case OUTPUTCODE_ON:
if(output_state == false)
{
output_state = true;
digitalWrite(PIN_OUTPUT, HIGH);
LogS("Output ON");
Logln();
}
break;
case OUTPUTCODE_OFF:
if(output_state == true)
{
output_state = false;
digitalWrite(PIN_OUTPUT, LOW);
LogS("Output OFF");
Logln();
}
break;
}
mySwitch.resetAvailable();
}
if(sensor_enabled == true) {
unsigned long curmillis = millis();
if((lastsend == 0) || (curmillis > (lastsend + (SEND_DELAY*1000))) || (curmillis < lastsend)) {
lastsend = curmillis;
mySwitch.disableReceive();
if(DHT.read22(PIN_DHT) == DHTLIB_OK)
{
LogS("Send ");
LogS("Temperature: ");
LogF(DHT.getTemperature());
LogS(" Humidity: ");
LogF(DHT.getHumidity());
Logln();
transmitter.setTemperature(DHT.getTemperature()); // -49.9C...+69.9C
transmitter.setHumidity(lround(DHT.getHumidity())); // 2...98%
transmitter.SendPacket();
//transmitter.sendTempHumi(lround(DHT.getTemperature()*10), lround(DHT.getHumidity()));
} else {
LogS("DHT Error");
Logln();
}
mySwitch.enableReceive(0);
}
}
}