17 Commits

Author SHA1 Message Date
1a1b74609d Entrpellung implementiert – noch nicht getestet
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-18 01:12:56 +01:00
65501447fd pin change
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-16 17:58:27 +01:00
247f061c2f Merge branch 'nano-revision-2.0' 2023-12-16 13:53:58 +01:00
09a622ca50 working on minor timing problems
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-09 22:32:00 +01:00
fe0f97cd34 first version of TempSender, that seems to work in the field
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-09 18:12:33 +01:00
e04a2bc202 fixed comment in Sender.ino
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-08 13:24:46 +01:00
3661309081 Merge branch 'nano-revision-2.0' 2023-12-03 12:47:17 +01:00
9f0513426d Merge branch 'nano-revision-1.0' into nano-revision-2.0 2023-12-03 12:43:22 +01:00
1a18c96fa4 renamed fritzing file
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-03 12:40:46 +01:00
1bdfaa1523 adopted software to Nano 2.0
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-03 12:23:19 +01:00
0ce4f0cd4d Projekt aufgeräumt
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-12-03 12:11:16 +01:00
ca66b1c380 Board nochmals überarbeitet
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-11-30 03:12:37 +01:00
594e8ea06c Bugfix: RO des Max485 nun mit einem Interrupt-Pin verbunden
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-11-28 02:32:38 +01:00
9c8355482b started BiDi (for bi-directional communication)
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-10-31 10:12:17 +01:00
51995e37d7 weitere Verbesserung: RC/TX für den Max 485 belegen nun nicht mehr das SPI
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-10-25 01:20:40 +02:00
c9e3fbef48 kleinigkeiten auf der Platine verbessert
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-10-25 00:08:43 +02:00
3d37fb01ee neue Version der Leiterplatte fertiggestellt:
- jetzt mit 8 Optokoppler-Eingängen
- zusätzlich mit 8-Pin-Header für weitere Peripherie

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
2023-10-24 23:48:38 +02:00
11 changed files with 390 additions and 152 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
Gerber

BIN
Hardware/Nano845.fzz Normal file

Binary file not shown.

Binary file not shown.

View File

@@ -1,3 +1,5 @@
| Board | Button Interrupt | RS485-Interrupt | RS485/A | RS485/B |
|-------|------------------|-----------------|---------|---------|
| 1.0 | D2 | D3 | U7 | U8 |
| 2.0 | D2 | (D10)| U8 | U7 |
| 2.1 | D3 | D2 | U8 | U7 |

View File

@@ -1,59 +0,0 @@
int sTx = 2;
int sRx = 3;
long base = 100;
long rst = base*10; // reset
boolean bt = false;
long dif = 0;
long lt = 0;
long now = 0;
char rcv = 0;
int idx = 0;
String message;
void setup(){
pinMode(sRx,INPUT);
pinMode(sTx,OUTPUT);
attachInterrupt(digitalPinToInterrupt(sRx),change,CHANGE);
Serial.begin(115200);
Serial.println("Receiver ready");
}
void reset(long dif){
rcv = 0;
idx = -1;
message.reserve(32);
}
void change(){
bool bt = digitalRead(sRx);
now = micros();
dif = now - lt;
lt = now;
if (dif > rst){
reset(dif);
} else {
handle(!bt,dif);
}
}
void handle(boolean bt, long dif){
long count = dif/base;
for (int i=0; i<count; i++) push(bt);
}
void push(boolean bt){
if (idx>=0) rcv |= bt<<idx;
idx++;
if (idx == 8){
if (rcv == 13) {
Serial.println(message);
message = "";
} else {
message+=rcv;
}
idx =0;
rcv =0;
}
}
void loop(){}

View File

@@ -1,93 +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;
long base = 100;
int id = 0;
int baud = 19200;
char c;
int counter = 0;
void setup(){
pinMode(A0,INPUT);
id = analogRead(A0);
pinMode(enable,OUTPUT);
digitalWrite(enable,LOW);
pinMode(sTx,OUTPUT);
digitalWrite(sTx,LOW);
pinMode(sRx,INPUT);
Serial.begin(115200);
Serial.print("Baud rate set to ");
Serial.println(baud);
Serial.print("Initialized Arduino #");
Serial.println(id);
}
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(){
counter++;
String s = "Hier ist ";
s.concat(id);
s+=": counter = ";
s.concat(counter);
while (!sendString(s)) delay(10*base);
delay(id);
}

View File

@@ -0,0 +1,59 @@
/*
* Demonstrates how to use LCDisplay 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.
*/
// include the library code:
#include <LiquidCrystal.h>
#include <SoftRS485.h>
#define Max485_RO 2 // read-output of Max485
#define Max485_RE 8 // not-read-enable of Max485
#define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // data input of Max485
#define LCD_RS 10
#define LCD_EN 11
#define LCD_D4 4
#define LCD_D5 5
#define LCD_D6 6
#define LCD_D7 7
#define PROGRAM "RS485-Nano 2.1 / LCDReceive 1.2"
// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
void setup() {
Serial.begin(115200);
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
invert485polarity(true);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
printMessage(PROGRAM);
}
void printMessage(String s){
Serial.println(s);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(s);
if (s.length()>16) {
lcd.setCursor(0, 1);
s=s.substring(16);
s.trim();
lcd.print(s);
}
}
void loop() {
if (available485()) {
printMessage(get485message());
}
}

View File

@@ -0,0 +1,85 @@
/*// This sketch allows manual setting of the RS485-connected pins via serial terminal commands
Commands are:
r+ enable read mode (^RE ← LOW)
r- disable read mode (^RE ← HIGH)
w+ enable write mode (DE ← HIGH)
w- disable write mode (DE ← LOW)
h send HIGH level on DI
l send LOW level on DI
//*/
#if defined(ARDUINO_AVR_UNO)
#define RS485_RO 5
#define RS485_RE 4
#define RS485_DE 3
#define RS485_DI 2
#define BOARD "Arduino Uno"
#endif
#if defined(ARDUINO_AVR_NANO)
#define RS485_RO 3
#define RS485_RE 2
#define RS485_DE 4
#define RS485_DI 5
#define BOARD "Arduino Nano"
#endif
String command = "";
bool doPrint = false;
// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(RS485_RO, INPUT);
pinMode(RS485_RE, OUTPUT);
pinMode(RS485_DE, OUTPUT);
pinMode(RS485_DI, OUTPUT);
digitalWrite(RS485_RE,LOW);
digitalWrite(RS485_DE,LOW);
digitalWrite(RS485_DI,LOW);
Serial.begin(115200);
Serial.println(F("this is Projekte/Arduino/MAX485/ManualSend"));
Serial.println(F("send any of the followning commands:"));
Serial.println(F("- r+ (enable read)\n- r- (disable read)\n- w+ (enable write)\n- w- (disable write)\n- h (for high)\n- l (for low)"));
}
// the loop function runs over and over again forever
void loop() {
while (Serial.available()){
char inChar = (char)Serial.read();
if (inChar == '\n') {
process(command);
command = "";
doPrint = true;
} else {
command += inChar;
}
}
if (doPrint){
printState();
delay(1000);
}
}
void printState(){
Serial.print("RE = ");
Serial.print(1-digitalRead(RS485_RE));
Serial.print(", DE = ");
Serial.print(digitalRead(RS485_DE));
Serial.print(", DI = ");
Serial.print(digitalRead(RS485_DI));
Serial.print(", RO = ");
Serial.println(digitalRead(RS485_RO));
}
void process(String command){
Serial.print(F("received:"));
Serial.println(command);
if (command == "r+") digitalWrite(RS485_RE,LOW);
if (command == "r-") digitalWrite(RS485_RE,HIGH);
if (command == "w+") digitalWrite(RS485_DE,HIGH);
if (command == "w-") digitalWrite(RS485_DE,LOW);
if (command == "h") digitalWrite(RS485_DI,HIGH);
if (command == "l") digitalWrite(RS485_DI,LOW);
printState();
}

View File

@@ -0,0 +1,4 @@
Erkenntnisse:
1. Wenn alle Treiber inaktiv sind, also bei allen Teilnehmer DI = Low ist, kommt vom RO ein High-Signal
2. das High-Signal ist dominant, d.h. wenn mehrere Teilnehmer senden, und mindestens ein Teilnehmer eine 1 auf den Bus legt, erhalten alle am RO einen HIGH-Pegel

123
Software/Sender/Sender.ino Normal file
View File

@@ -0,0 +1,123 @@
/*
* 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 <SoftRS485.h>
#define PROGRAM "RS485-Nano 2.1 / Sender 1.1"
#define BTN_INT 3 // button interrupt pin
#define Max485_RO 2 // read-output of Max485
#define Max485_RE 3 // not-read-enable of Max485
#define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // data input of Max485
#define TRESHOLD 100
#define ID 0
// 0=Test
// 1=Arbeitszimmer
// 2=Küche
// 3=Bad
//#define LOG_TO_SERIAL
#define SEND_485
#define DEBUG
boolean raw_states[8];
boolean states[8];
unsigned long capacitor[8];
unsigned long times[8];
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(2),isr,CHANGE);
// 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(){
raw_states[0] = !digitalRead(14);
raw_states[1] = !digitalRead(15);
raw_states[2] = !digitalRead(16);
raw_states[3] = !digitalRead(17);
raw_states[4] = !digitalRead(18);
raw_states[5] = !digitalRead(19);
raw_states[6] = !(analogRead(20)>400);
raw_states[7] = !(analogRead(21)>400);
#ifdef LOG_TO_SERIAL
for (int i=0;i<8;i++){
Serial.print(" "); Serial.print(raw_states[i]); Serial.print(" ");
}
Serial.println();
#endif
}
#ifdef SEND_485
void send(int btn, long duration){
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",dura:"+String(duration)+"}";
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();
boolean pause = true;
for (int i=0;i<8;i++){
// we emulate a capacitor:
// when the button is pressed, it slowly loads, if the button is released, it unloads.
capacitor[i] = raw_states[i] ? capacitor[i]+1 : capacitor[i]>>1;
#ifdef DEBUG
if (capacitor[i]) {
Serial.print("cap "); Serial.print(i); Serial.print(": "); Serial.println(capacitor[i]);
}
#endif
// 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];
times[i] = now;
#ifdef SEND_485
if (states[i]){ // old state was HIGH → we are going LOW
send(i+1,diff);
}
#endif
states[i] = new_state;
}
}
}

View File

@@ -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 <SoftRS485.h>
#include <AM2302-Sensor.h>
#define PROGRAM "RS485-Nano 2.1 / TempSender 1.1"
#define BTN_INT 3 // button interrupt pin
#define Max485_RO 2 // read-output of Max485
#define Max485_RE 3 // not-read-enable of Max485
#define Max485_DE 8 // data enable of Max485
#define Max485_DI 9 // 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
}
}
}