From e04a2bc2020fb0ab5583429fcf494c4a8d90c535 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Fri, 8 Dec 2023 13:24:46 +0100 Subject: [PATCH 1/3] fixed comment in Sender.ino Signed-off-by: Stephan Richter --- Sender/Sender.ino | 129 ------------------------------------- Software/Sender/Sender.ino | 11 ++-- 2 files changed, 6 insertions(+), 134 deletions(-) delete mode 100644 Sender/Sender.ino diff --git a/Sender/Sender.ino b/Sender/Sender.ino deleted file mode 100644 index 3c0e651..0000000 --- a/Sender/Sender.ino +++ /dev/null @@ -1,129 +0,0 @@ -// Breadboard -//int enable = 2; -//int sTx = 4; -//int sRx = 5; - -// RS485-Nano Revision 1.0 -int enable = 9; -int sTx = 8; -int sRx = 3; - -// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53 - -int in1 = 4; // o -int in2 = 5; // |o -int in3 = 6; // ||o -int in4 = 7; // |||o -// ↓↓↓↓ -int irq = 2;// ←++++ - -long base = 1000; - -int id = 0; -int baud = 115200; -char c; -int counter = 0; -bool reg1=false,reg2=false,reg3=false,reg4=false; - -void setup(){ - Serial.begin(baud); - pinMode(A0,INPUT); - id = analogRead(A0); - - pinMode(enable,OUTPUT); - digitalWrite(enable,LOW); - - pinMode(sTx,OUTPUT); - digitalWrite(sTx,LOW); - - pinMode(in1,INPUT); - pinMode(in2,INPUT); - pinMode(in3,INPUT); - pinMode(in4,INPUT); - - pinMode(irq,INPUT); - attachInterrupt(digitalPinToInterrupt(irq),interrupt,RISING); - - pinMode(sRx,INPUT); - Serial.print("Baud rate set to "); - Serial.println(baud); - Serial.print("Initialized Arduino #"); - Serial.println(id); -} - -void interrupt(){ - Serial.println("Interrupt"); - reg1 = digitalRead(in1); - reg2 = digitalRead(in2); - reg3 = digitalRead(in3); - reg4 = digitalRead(in4); - -} -bool sendBit(bool bt){ - digitalWrite(sTx,bt); - delayMicroseconds(80); - if (digitalRead(sTx) != bt) return false; - delayMicroseconds(base-80); - return true; -} - -bool sendChar(char c){ - return sendBit(c & 0x01) - && sendBit(c & 0x02) - && sendBit(c & 0x04) - && sendBit(c & 0x08) - && sendBit(c & 0x10) - && sendBit(c & 0x20) - && sendBit(c & 0x40) - && sendBit(c & 0x80); -} - -bool sendString(String s){ - Serial.print("Sending '"+s+"'…"); - boolean busy = false; - for (int i = 0; i<11; i++){ - if (!digitalRead(sRx)) { - if (!busy){ - Serial.print("busy…"); - busy = true; - } - i = 0; // wait for free line - } - delayMicroseconds(base); - } - digitalWrite(enable,HIGH); - bool success = sendBit(0); // erste Flanke erzeugen - if (success) { - for (char c : s){ - if (!sendChar(c)){ - success = false; - break; - } - } - } - success = success && sendChar(13); - success = success && sendBit(1); // letzte Flanke erzeugen - digitalWrite(enable,LOW); - Serial.println(success ? "success" : "failed"); - return success; -} - -void loop(){ - if (reg1){ - reg1 = false; - sendString("t:02:01"); - } - if (reg2){ - reg2 = false; - sendString("t:02:02"); - } - if (reg3){ - reg3 = false; - sendString("t:02:03"); - } - if (reg4){ - reg4 = false; - sendString("t:02:04"); - } - -} diff --git a/Software/Sender/Sender.ino b/Software/Sender/Sender.ino index 253f6fa..97af85b 100644 --- a/Software/Sender/Sender.ino +++ b/Software/Sender/Sender.ino @@ -1,8 +1,9 @@ /* - * Demonstrates how to use LCDisplay in conjunction with SoftRS485: + * Demonstrates how to use Read Buttons in conjunction with SoftRS485: * - * The main _loop_ of this program listens on the RS485 bus. - * Whenever a message is received from the bus, it is displayed on the LCD. + * 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. */ // include the library code: @@ -31,7 +32,7 @@ unsigned long times[8]; -void setup() { +void setup(){ Serial.begin(115200); init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization: pinMode(BTN_INT,INPUT_PULLUP); @@ -73,7 +74,7 @@ void isr(){ #ifdef SEND_485 void send(int btn){ String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}"; - for (int i = 0; i<10; i++){ + for (int i = 0; i<10; i++){ if (send485(s.c_str())) break; Serial.println("collision detected, trying again:"); } From fe0f97cd34b106bcf8c63a551a1d557e5801615e Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 9 Dec 2023 18:12:33 +0100 Subject: [PATCH 2/3] first version of TempSender, that seems to work in the field Signed-off-by: Stephan Richter --- Software/TempSender/TempSender.ino | 116 +++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 Software/TempSender/TempSender.ino diff --git a/Software/TempSender/TempSender.ino b/Software/TempSender/TempSender.ino new file mode 100644 index 0000000..e60a414 --- /dev/null +++ b/Software/TempSender/TempSender.ino @@ -0,0 +1,116 @@ +/* + * 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. + */ + +// include the library code: +#include +#include + +#define PROGRAM "RS485-Nano 2.0 / TempSender 1.1" + +#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 DHT_PIN 4 +#define TRESHOLD 100000 // 100ms +#define PERIOD 100000000 // 100s +#define ID 3 +// 0=Test +// 1=Arbeitszimmer +// 2=Küche +// 3=Bad + +//#define LOG_TO_SERIAL +#define SEND_485 + +AM2302::AM2302_Sensor am2302{DHT_PIN}; + +boolean states[8]; +unsigned long times[8]; +unsigned long sensor_time = PERIOD; + + +void setup(){ + Serial.begin(115200); + am2302.begin(); + 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(2),isr,FALLING); + // Print a message to the LCD. + Serial.println(PROGRAM); +#ifdef LOG_TO_SERIAL + Serial.println("14 15 16 17 18 19 20 21"); +#endif +} + +void isr(){ + states[0] = !digitalRead(14); + states[1] = !digitalRead(15); + states[2] = !digitalRead(16); + states[3] = !digitalRead(17); + + states[4] = !digitalRead(18); + states[5] = !digitalRead(19); + states[6] = !(analogRead(20)>200); + states[7] = !(analogRead(21)>200); +#ifdef LOG_TO_SERIAL + for (int i=0;i<8;i++){ + Serial.print(" "); Serial.print(states[i]); Serial.print(" "); + } + Serial.println(); +#endif +} + +#ifdef SEND_485 +void send(int btn){ + String s = "{nano:"+String(ID)+",btn:"+String(btn)+"}"; + for (int i = 0; i<10; i++){ + if (send485(s.c_str())) break; + Serial.println("collision detected, trying again:"); + } +} +#endif + +void loop(){ + unsigned long now = micros(); + for (int i=0;i<8;i++){ + if (states[i]){ + delay(1); + if (now - times[i] > TRESHOLD){ +#ifdef SEND_485 + send(i+1); +#endif + times[i] = now; + } + states[i] = 0; + } + } + + if (now - sensor_time > PERIOD){ + sensor_time = now; + auto status = am2302.read(); + if (status == 0){ + String s = "{nano:"+String(ID)+",temp:"+String(am2302.get_Temperature())+",humi:"+String(am2302.get_Hunidity())+"}"; + send485(s.c_str()); +#ifdef LOG_TO_SERIAL + Serial.println(s); +#endif + } + } + +} From 09a622ca505ea16dd6d6089c46bb166d1b6c15e6 Mon Sep 17 00:00:00 2001 From: Stephan Richter Date: Sat, 9 Dec 2023 22:32:00 +0100 Subject: [PATCH 3/3] working on minor timing problems Signed-off-by: Stephan Richter --- Software/Sender/Sender.ino | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Software/Sender/Sender.ino b/Software/Sender/Sender.ino index 97af85b..95c3bcd 100644 --- a/Software/Sender/Sender.ino +++ b/Software/Sender/Sender.ino @@ -9,7 +9,7 @@ // include the library code: #include -#define PROGRAM "RS485-Nano 2.0 / Sender 1.0" +#define PROGRAM "RS485-Nano 2.0 / Sender 1.1" #define BTN_INT 2 // button interrupt pin #define Max485_RO 3 // read-output of Max485 @@ -17,7 +17,7 @@ #define Max485_DE 9 // data enable of Max485 #define Max485_DI 8 // data input of Max485 -#define TRESHOLD 100000 // 100ms +#define TRESHOLD 200000 // 200ms #define ID 0 // 0=Test // 1=Arbeitszimmer @@ -83,8 +83,13 @@ void send(int btn){ void loop(){ unsigned long now = micros(); + boolean pause = true; for (int i=0;i<8;i++){ if (states[i]){ + if (pause){ + delay(5); + pause=false; + } if (now - times[i] > TRESHOLD){ #ifdef SEND_485 send(i+1);