From 7111fd706e9ace34eab9aa5413923125e43e639f Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Mon, 9 Sep 2024 23:41:16 +0200 Subject: [PATCH] added new project for nano with temp sensor and ir sender Signed-off-by: Stephan Richter --- .../TempSenderPlusIR/TempSenderPlusIR.ino | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 Software/TempSenderPlusIR/TempSenderPlusIR.ino diff --git a/Software/TempSenderPlusIR/TempSenderPlusIR.ino b/Software/TempSenderPlusIR/TempSenderPlusIR.ino new file mode 100644 index 0000000..afd8877 --- /dev/null +++ b/Software/TempSenderPlusIR/TempSenderPlusIR.ino @@ -0,0 +1,194 @@ +/* + * Demonstrates how to use Read Buttons in conjunction with SoftRS485: + * + * If a button is pressed, a corresponding flag is set. + * The main loop checks for the state of those flags and + * sends a message on the RS485 bus whenever a flag is set. + * + * Additionally, the temperature is read from a DHT22 sensor once per minute + */ + +// include the library code: +#include +#include +#include + +// 0=Test +// 1=Flur +// 2=Wohnzimmer +// 3=Bad EG +// 4=Treppenhaus +// 5=Arbeitszimmer +// 6=Bad OG + +#define ID 0 +#define PROGRAM "Temp+IR" +#define SW_VERSION "0.1" +#define HW_VERSION "2.0" + +#define BTN_INT 2 // button interrupt pin +#define Max485_RO 3 // read-output of Max485 +#define Max485_RE 5 // not-read-enable of Max485 +#define Max485_DE 9 // data enable of Max485 +#define Max485_DI 8 // data input of Max485 + +#define TRESHOLD 1024 +#define TRIGGER_LEVEL 200 + +#define DHT_PIN 4 +#define PERIOD 100000000 // 100s + +#define IR_PIN 5 +#define REPEATS 2 + +//#define DEBUG +//#define LOG_TO_SERIAL +#define SEND_485 + +boolean raw_states[8]; +boolean states[8]; +unsigned long capacitor[8]; +unsigned long times[8]; +unsigned long sensor_time = 0; + +// these values are used for temperature processing +SimpleDHT11 sensor(DHT_PIN); +byte temperature = 0; +byte humidity = 0; +int err = SimpleDHTErrSuccess; + +// these constants are used for message parsing in handle(message) +const char* outer_delimiter = "{,}"; +const char* inner_delimiter = ":"; + +void setup(){ + for (int i=0; i<8;i++) { + capacitor[i] = 0; + raw_states[i] = LOW; + states[i] = LOW; + } + Serial.begin(115200); + init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization: + pinMode(BTN_INT,INPUT_PULLUP); + pinMode(14,INPUT_PULLUP); + pinMode(15,INPUT_PULLUP); + pinMode(16,INPUT_PULLUP); + pinMode(17,INPUT_PULLUP); + pinMode(18,INPUT_PULLUP); + pinMode(19,INPUT_PULLUP); + pinMode(20,INPUT_PULLUP); + pinMode(21,INPUT_PULLUP); + + attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE); + String s = "{nano:"+String(ID)+",hw:"+HW_VERSION+","+PROGRAM+":"+SW_VERSION+"}"; + Serial.println(s); + send485(s.c_str()); +#ifdef LOG_TO_SERIAL + Serial.println("14 15 16 17 18 19 20 21"); +#endif +} + +void isr(){ + raw_states[0] = analogRead(14)>1; + + // if the capacitor reaches a certain treshold, the state is changed + boolean new_state = capacitor[i]>TRESHOLD; + + // TODO: drop state after a maximum HIGH time + + // when the state changes, the duration of the last state is calculated + if (states[i] != new_state){ + long diff = (now - times[i])/1000; + times[i] = now; + if (states[i]) send(i+1,diff); // old state was HIGH → we are going LOW + states[i] = new_state; + } + } + + if (now - sensor_time > PERIOD){ + sensor_time = now; + if ((err = sensor.read(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) { + String s = "{nano:"+String(ID)+",temp:"+String((int)temperature)+",humi:"+String((int)humidity)+"}"; +#ifdef LOG_TO_SERIAL + Serial.println(s); +#endif + send485(s.c_str()); + } + } + + if (available485()) handle(get485message()); +}