Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1a1b74609d | |||
| 65501447fd | |||
| 247f061c2f | |||
| 3661309081 | |||
| 0ce4f0cd4d | |||
| ca66b1c380 | |||
| 594e8ea06c |
Binary file not shown.
@@ -2,3 +2,4 @@
|
|||||||
|-------|------------------|-----------------|---------|---------|
|
|-------|------------------|-----------------|---------|---------|
|
||||||
| 1.0 | D2 | D3 | U7 | U8 |
|
| 1.0 | D2 | D3 | U7 | U8 |
|
||||||
| 2.0 | D2 | (D10)| U8 | U7 |
|
| 2.0 | D2 | (D10)| U8 | U7 |
|
||||||
|
| 2.1 | D3 | D2 | U8 | U7 |
|
||||||
@@ -1,95 +0,0 @@
|
|||||||
/* RS 485-Implementierung, die sowohl Schreiben als auch Lesen kann */
|
|
||||||
/* Basis für andere Projekte */
|
|
||||||
|
|
||||||
#define BUTTON 2
|
|
||||||
#define RECEIVE 3
|
|
||||||
#define RECEIVE_ENABLE 4
|
|
||||||
#define SEND 5
|
|
||||||
#define SEND_ENABLE 6
|
|
||||||
|
|
||||||
#define MAX_LEN 128
|
|
||||||
|
|
||||||
void setup() {
|
|
||||||
// Buttons connected to analog input lines
|
|
||||||
pinMode(A0,INPUT_PULLUP);
|
|
||||||
pinMode(A1,INPUT_PULLUP);
|
|
||||||
pinMode(A2,INPUT_PULLUP);
|
|
||||||
pinMode(A3,INPUT_PULLUP);
|
|
||||||
pinMode(A4,INPUT_PULLUP);
|
|
||||||
pinMode(A5,INPUT_PULLUP);
|
|
||||||
pinMode(A6,INPUT_PULLUP);
|
|
||||||
pinMode(A7,INPUT_PULLUP);
|
|
||||||
// Currently, all buttons are also connected via a diode to an interruptable pin, to notify on input changes.
|
|
||||||
// This interruptable pin should be assgined to BUTTON
|
|
||||||
pinMode(BUTTON,INPUT_PULLUP);
|
|
||||||
attachInterrupt(digitalPinToInterrupt(BUTTON),button,FALLING); // interrupt for button change
|
|
||||||
|
|
||||||
// Set up pins for RS485 connection
|
|
||||||
pinMode(RECEIVE, INPUT);
|
|
||||||
pinMode(RECEIVE_ENABLE, OUTPUT);
|
|
||||||
pinMode(SEND, OUTPUT);
|
|
||||||
pinMode(SEND_ENABLE, OUTPUT);
|
|
||||||
attachInterrupt(digitalPinToInterrupt(RECEIVE),receive,CHANGE); // interrupt for incoming RS485 traffic
|
|
||||||
|
|
||||||
reset485();
|
|
||||||
buttons = 0x0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void button() {
|
|
||||||
buttons = 0xFF ^ (digitalRead(A7)<<7 | digitalRead(A6)<<6 | digitalRead(A5)<<5 | digitalRead(A4)<<4 | digitalRead(A3)<<3 | (!digitalRead(A2))<<2 | digitalRead(A1)<<1 | digitalRead(A0));
|
|
||||||
}
|
|
||||||
|
|
||||||
void receive(){
|
|
||||||
bool received_bit = digitalRead(RECEIVE);
|
|
||||||
curr = micros();
|
|
||||||
duration = curr - last;
|
|
||||||
Serial.print("Input switched to ");
|
|
||||||
Serial.print(received_bit?"H":"L");
|
|
||||||
Serial.print(" after ");
|
|
||||||
Serial.print(duration);
|
|
||||||
Serial.println(" ticks");
|
|
||||||
last = curr;
|
|
||||||
if (duration > MAX_TICKS) reset485();
|
|
||||||
received_bit = !received_bit; // received_bit is the current value, the duration of the previous value (=inverted) has been measured
|
|
||||||
duration /= BASE;
|
|
||||||
while (duration > 0){
|
|
||||||
byte_idx--;
|
|
||||||
recv |= received_bit<<byte_idx;
|
|
||||||
if (byte_idx == 0) {
|
|
||||||
msg[msg_idx] = recv;
|
|
||||||
msg_idx++;
|
|
||||||
byte_idx = 8;
|
|
||||||
if (recv == 0x00) message_received();
|
|
||||||
}
|
|
||||||
duration--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset485(){
|
|
||||||
byte_idx = 8;
|
|
||||||
msg_idx = 0;
|
|
||||||
recv=0x0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void message_received(){
|
|
||||||
for (int i=0; i<MAX_LEN; i++) message[i]=msg[i];
|
|
||||||
reset485();
|
|
||||||
}
|
|
||||||
|
|
||||||
void button_pressed(int i){
|
|
||||||
Serial.println("not implemented");
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop(){
|
|
||||||
if (buttons>0){
|
|
||||||
for (int i=0; i<7; i++){
|
|
||||||
if (buttons & (1<<i)) button_pressed(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (message[0] != 0x0){
|
|
||||||
Serial.print("Received '");
|
|
||||||
Serial.print(message);
|
|
||||||
Serial.println("'");
|
|
||||||
message[0] = 0x0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,48 +9,47 @@
|
|||||||
#include <LiquidCrystal.h>
|
#include <LiquidCrystal.h>
|
||||||
#include <SoftRS485.h>
|
#include <SoftRS485.h>
|
||||||
|
|
||||||
#define PROGRAM "RS485-Nano Rev 2.0 / LCDReceive 1.2"
|
#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 Max485_RO 3 // read-output of Max485
|
#define LCD_RS 10
|
||||||
#define Max485_RE 5 // not-read-enable of Max485
|
#define LCD_EN 11
|
||||||
#define Max485_DE 9 // data enable of Max485
|
#define LCD_D4 4
|
||||||
#define Max485_DI 8 // data input of Max485
|
#define LCD_D5 5
|
||||||
|
#define LCD_D6 6
|
||||||
|
#define LCD_D7 7
|
||||||
|
|
||||||
|
#define PROGRAM "RS485-Nano 2.1 / LCDReceive 1.2"
|
||||||
|
|
||||||
#define LCD_RS 4
|
|
||||||
#define LCD_EN 6
|
|
||||||
#define LCD_D4 7
|
|
||||||
#define LCD_D5 11
|
|
||||||
#define LCD_D6 12
|
|
||||||
#define LCD_D7 13
|
|
||||||
|
|
||||||
// initialize the library by associating any needed LCD interface pin with the arduino pin number it is connected to
|
// 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);
|
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
|
||||||
|
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
||||||
|
invert485polarity(true);
|
||||||
// set up the LCD's number of columns and rows:
|
// set up the LCD's number of columns and rows:
|
||||||
lcd.begin(16, 2);
|
lcd.begin(16, 2);
|
||||||
// Print a message to the LCD.
|
// Print a message to the LCD.
|
||||||
printMessage(PROGRAM);
|
printMessage(PROGRAM);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void printMessage(String s){
|
void printMessage(String s){
|
||||||
Serial.println(s);
|
Serial.println(s);
|
||||||
lcd.clear();
|
lcd.clear();
|
||||||
int line = 0;
|
lcd.setCursor(0, 0);
|
||||||
int col = 0;
|
lcd.print(s);
|
||||||
auto cstr = s.c_str();
|
if (s.length()>16) {
|
||||||
for (int i=0;i<s.length();i++){
|
lcd.setCursor(0, 1);
|
||||||
if (col == 0 && cstr[i] == ' ') continue;
|
s=s.substring(16);
|
||||||
lcd.print(cstr[i]);
|
s.trim();
|
||||||
col++;
|
lcd.print(s);
|
||||||
if (cstr[i]==',' || col==16){
|
}
|
||||||
line++;
|
|
||||||
col=0;
|
|
||||||
lcd.setCursor(col, line);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|||||||
@@ -1,61 +0,0 @@
|
|||||||
// Für Controllino könnte man benutzen: sRx = 20, sTx = 21, enable = 53
|
|
||||||
|
|
||||||
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),change485,CHANGE);
|
|
||||||
Serial.begin(115200);
|
|
||||||
Serial.println("Receiver ready");
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset485(long dif){
|
|
||||||
rcv = 0;
|
|
||||||
idx = -1;
|
|
||||||
message.reserve(32);
|
|
||||||
}
|
|
||||||
void change485(){
|
|
||||||
bool bt = digitalRead(sRx);
|
|
||||||
now = micros();
|
|
||||||
dif = now - lt;
|
|
||||||
lt = now;
|
|
||||||
if (dif > rst){
|
|
||||||
reset485(dif);
|
|
||||||
} else {
|
|
||||||
handle485(!bt,dif);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle485(boolean bt, long dif){
|
|
||||||
long count = dif/base;
|
|
||||||
for (int i=0; i<count; i++) push485(bt);
|
|
||||||
}
|
|
||||||
|
|
||||||
void push485(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(){}
|
|
||||||
+41
-42
@@ -9,31 +9,24 @@
|
|||||||
// include the library code:
|
// include the library code:
|
||||||
#include <SoftRS485.h>
|
#include <SoftRS485.h>
|
||||||
|
|
||||||
// 0=Test
|
#define PROGRAM "RS485-Nano 2.1 / Sender 1.1"
|
||||||
// 1=Flur
|
|
||||||
// 2=Wohnzimmer
|
|
||||||
// 3=Bad EG
|
|
||||||
// 4=Treppenhaus
|
|
||||||
// 5=Arbeitszimmer
|
|
||||||
// 6=Bad OG
|
|
||||||
|
|
||||||
|
#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
|
#define ID 0
|
||||||
#define PROGRAM "Sender"
|
// 0=Test
|
||||||
#define SW_VERSION "1.3"
|
// 1=Arbeitszimmer
|
||||||
#define HW_VERSION "2.0"
|
// 2=Küche
|
||||||
|
// 3=Bad
|
||||||
|
|
||||||
#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 DEBUG
|
|
||||||
//#define LOG_TO_SERIAL
|
//#define LOG_TO_SERIAL
|
||||||
#define SEND_485
|
#define SEND_485
|
||||||
|
#define DEBUG
|
||||||
|
|
||||||
boolean raw_states[8];
|
boolean raw_states[8];
|
||||||
boolean states[8];
|
boolean states[8];
|
||||||
@@ -60,54 +53,56 @@ void setup(){
|
|||||||
pinMode(20,INPUT_PULLUP);
|
pinMode(20,INPUT_PULLUP);
|
||||||
pinMode(21,INPUT_PULLUP);
|
pinMode(21,INPUT_PULLUP);
|
||||||
|
|
||||||
attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE);
|
attachInterrupt(digitalPinToInterrupt(2),isr,CHANGE);
|
||||||
String s = "{nano:"+String(ID)+",hw:"+HW_VERSION+","+PROGRAM+":"+SW_VERSION+"}";
|
// Print a message to the LCD.
|
||||||
Serial.println(s);
|
Serial.println(PROGRAM);
|
||||||
send485(s.c_str());
|
|
||||||
#ifdef LOG_TO_SERIAL
|
#ifdef LOG_TO_SERIAL
|
||||||
Serial.println("14 15 16 17 18 19 20 21");
|
Serial.println("14 15 16 17 18 19 20 21");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void isr(){
|
void isr(){
|
||||||
raw_states[0] = analogRead(14)<TRIGGER_LEVEL;
|
raw_states[0] = !digitalRead(14);
|
||||||
raw_states[1] = analogRead(15)<TRIGGER_LEVEL;
|
raw_states[1] = !digitalRead(15);
|
||||||
raw_states[2] = analogRead(16)<TRIGGER_LEVEL;
|
raw_states[2] = !digitalRead(16);
|
||||||
raw_states[3] = analogRead(17)<TRIGGER_LEVEL;
|
raw_states[3] = !digitalRead(17);
|
||||||
|
|
||||||
raw_states[4] = analogRead(18)<TRIGGER_LEVEL;
|
raw_states[4] = !digitalRead(18);
|
||||||
raw_states[5] = analogRead(19)<TRIGGER_LEVEL;
|
raw_states[5] = !digitalRead(19);
|
||||||
raw_states[6] = analogRead(20)<TRIGGER_LEVEL;
|
raw_states[6] = !(analogRead(20)>400);
|
||||||
raw_states[7] = analogRead(21)<TRIGGER_LEVEL;
|
raw_states[7] = !(analogRead(21)>400);
|
||||||
#ifdef LOG_TO_SERIAL
|
#ifdef LOG_TO_SERIAL
|
||||||
for (int i=0;i<8;i++){
|
for (int i=0;i<8;i++){
|
||||||
Serial.print(" ");
|
Serial.print(" "); Serial.print(raw_states[i]); Serial.print(" ");
|
||||||
Serial.print(raw_states[i]);
|
|
||||||
Serial.print(" ");
|
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(int btn, long duration){
|
|
||||||
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",ms:"+String(duration)+"}";
|
|
||||||
#ifdef SEND_485
|
#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++){
|
for (int i = 0; i<10; i++){
|
||||||
if (send485(s.c_str())) break;
|
if (send485(s.c_str())) break;
|
||||||
Serial.println("collision detected, trying again:");
|
Serial.println("collision detected, trying again:");
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
Serial.println(s);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
unsigned long now = micros();
|
unsigned long now = micros();
|
||||||
|
boolean pause = true;
|
||||||
for (int i=0;i<8;i++){
|
for (int i=0;i<8;i++){
|
||||||
// we emulate a capacitor:
|
// we emulate a capacitor:
|
||||||
// when the button is pressed, it slowly loads, if the button is released, it unloads.
|
// 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;
|
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
|
// if the capacitor reaches a certain treshold, the state is changed
|
||||||
boolean new_state = capacitor[i]>TRESHOLD;
|
boolean new_state = capacitor[i]>TRESHOLD;
|
||||||
|
|
||||||
@@ -115,9 +110,13 @@ void loop(){
|
|||||||
|
|
||||||
// when the state changes, the duration of the last state is calculated
|
// when the state changes, the duration of the last state is calculated
|
||||||
if (states[i] != new_state){
|
if (states[i] != new_state){
|
||||||
long diff = (now - times[i])/1000;
|
long diff = now - times[i];
|
||||||
times[i] = now;
|
times[i] = now;
|
||||||
if (states[i]) send(i+1,diff); // old state was HIGH → we are going LOW
|
#ifdef SEND_485
|
||||||
|
if (states[i]){ // old state was HIGH → we are going LOW
|
||||||
|
send(i+1,diff);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
states[i] = new_state;
|
states[i] = new_state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 <SoftRS485.h>
|
|
||||||
|
|
||||||
#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 PROGRAM "{hw:RS485-Nano rev 2.0,firmware:Transceiver,version:1.0}"
|
|
||||||
|
|
||||||
String message = "";
|
|
||||||
char c;
|
|
||||||
void setup() {
|
|
||||||
Serial.begin(115200);
|
|
||||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
|
||||||
send485(PROGRAM);
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop() {
|
|
||||||
if (available485()) Serial.println(get485message());
|
|
||||||
while (Serial.available()) {
|
|
||||||
c = (char)Serial.read();
|
|
||||||
if (c == '\n') {
|
|
||||||
send485(message.c_str());
|
|
||||||
message = "";
|
|
||||||
} else {
|
|
||||||
message += c;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -4,61 +4,41 @@
|
|||||||
* If a button is pressed, a corresponding flag is set.
|
* If a button is pressed, a corresponding flag is set.
|
||||||
* The main loop checks for the state of those flags and
|
* The main loop checks for the state of those flags and
|
||||||
* sends a message on the RS485 bus whenever a flag is set.
|
* 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 the library code:
|
||||||
#include <SoftRS485.h>
|
#include <SoftRS485.h>
|
||||||
#include <SimpleDHT.h>
|
#include <AM2302-Sensor.h>
|
||||||
|
|
||||||
// 0=Test
|
#define PROGRAM "RS485-Nano 2.1 / TempSender 1.1"
|
||||||
// 1=Flur
|
|
||||||
// 2=Wohnzimmer
|
|
||||||
// 3=Bad EG
|
|
||||||
// 4=Treppenhaus
|
|
||||||
// 5=Arbeitszimmer
|
|
||||||
// 6=Bad OG
|
|
||||||
|
|
||||||
#define ID 5
|
|
||||||
#define PROGRAM "TempSender"
|
|
||||||
#define SW_VERSION "1.5"
|
|
||||||
#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 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 DHT_PIN 4
|
||||||
|
#define TRESHOLD 100000 // 100ms
|
||||||
#define PERIOD 100000000 // 100s
|
#define PERIOD 100000000 // 100s
|
||||||
|
#define ID 3
|
||||||
|
// 0=Test
|
||||||
|
// 1=Arbeitszimmer
|
||||||
|
// 2=Küche
|
||||||
|
// 3=Bad
|
||||||
|
|
||||||
//#define DEBUG
|
|
||||||
//#define LOG_TO_SERIAL
|
//#define LOG_TO_SERIAL
|
||||||
#define SEND_485
|
#define SEND_485
|
||||||
|
|
||||||
boolean raw_states[8];
|
AM2302::AM2302_Sensor am2302{DHT_PIN};
|
||||||
boolean states[8];
|
|
||||||
unsigned long capacitor[8];
|
boolean states[8];
|
||||||
unsigned long times[8];
|
unsigned long times[8];
|
||||||
unsigned long sensor_time = 0;
|
unsigned long sensor_time = PERIOD;
|
||||||
|
|
||||||
SimpleDHT22 sensor(DHT_PIN);
|
|
||||||
float temperature = 0;
|
|
||||||
float humidity = 0;
|
|
||||||
int err = SimpleDHTErrSuccess;
|
|
||||||
|
|
||||||
void setup(){
|
void setup(){
|
||||||
for (int i=0; i<8;i++) {
|
|
||||||
capacitor[i] = 0;
|
|
||||||
raw_states[i] = LOW;
|
|
||||||
states[i] = LOW;
|
|
||||||
}
|
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
am2302.begin();
|
||||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
||||||
pinMode(BTN_INT,INPUT_PULLUP);
|
pinMode(BTN_INT,INPUT_PULLUP);
|
||||||
pinMode(14,INPUT_PULLUP);
|
pinMode(14,INPUT_PULLUP);
|
||||||
@@ -70,74 +50,67 @@ void setup(){
|
|||||||
pinMode(20,INPUT_PULLUP);
|
pinMode(20,INPUT_PULLUP);
|
||||||
pinMode(21,INPUT_PULLUP);
|
pinMode(21,INPUT_PULLUP);
|
||||||
|
|
||||||
attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE);
|
attachInterrupt(digitalPinToInterrupt(2),isr,FALLING);
|
||||||
String s = "{nano:"+String(ID)+",hw:"+HW_VERSION+","+PROGRAM+":"+SW_VERSION+"}";
|
// Print a message to the LCD.
|
||||||
Serial.println(s);
|
Serial.println(PROGRAM);
|
||||||
send485(s.c_str());
|
|
||||||
#ifdef LOG_TO_SERIAL
|
#ifdef LOG_TO_SERIAL
|
||||||
Serial.println("14 15 16 17 18 19 20 21");
|
Serial.println("14 15 16 17 18 19 20 21");
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void isr(){
|
void isr(){
|
||||||
raw_states[0] = analogRead(14)<TRIGGER_LEVEL;
|
states[0] = !digitalRead(14);
|
||||||
raw_states[1] = analogRead(15)<TRIGGER_LEVEL;
|
states[1] = !digitalRead(15);
|
||||||
raw_states[2] = analogRead(16)<TRIGGER_LEVEL;
|
states[2] = !digitalRead(16);
|
||||||
raw_states[3] = analogRead(17)<TRIGGER_LEVEL;
|
states[3] = !digitalRead(17);
|
||||||
|
|
||||||
raw_states[4] = analogRead(18)<TRIGGER_LEVEL;
|
states[4] = !digitalRead(18);
|
||||||
raw_states[5] = analogRead(19)<TRIGGER_LEVEL;
|
states[5] = !digitalRead(19);
|
||||||
raw_states[6] = analogRead(20)<TRIGGER_LEVEL;
|
states[6] = !(analogRead(20)>200);
|
||||||
raw_states[7] = analogRead(21)<TRIGGER_LEVEL;
|
states[7] = !(analogRead(21)>200);
|
||||||
#ifdef LOG_TO_SERIAL
|
#ifdef LOG_TO_SERIAL
|
||||||
for (int i=0;i<8;i++){
|
for (int i=0;i<8;i++){
|
||||||
Serial.print(" ");
|
Serial.print(" "); Serial.print(states[i]); Serial.print(" ");
|
||||||
Serial.print(raw_states[i]);
|
|
||||||
Serial.print(" ");
|
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(int btn, long duration){
|
|
||||||
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",ms:"+String(duration)+"}";
|
|
||||||
#ifdef SEND_485
|
#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;
|
if (send485(s.c_str())) break;
|
||||||
Serial.println("collision detected, trying again:");
|
Serial.println("collision detected, trying again:");
|
||||||
}
|
}
|
||||||
#else
|
|
||||||
Serial.println(s);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
void loop(){
|
void loop(){
|
||||||
unsigned long now = micros();
|
unsigned long now = micros();
|
||||||
for (int i=0;i<8;i++){
|
for (int i=0;i<8;i++){
|
||||||
// we emulate a capacitor:
|
if (states[i]){
|
||||||
// when the button is pressed, it slowly loads, if the button is released, it unloads.
|
delay(1);
|
||||||
capacitor[i] = raw_states[i] ? capacitor[i]+1 : capacitor[i]>>1;
|
if (now - times[i] > TRESHOLD){
|
||||||
|
#ifdef SEND_485
|
||||||
// if the capacitor reaches a certain treshold, the state is changed
|
send(i+1);
|
||||||
boolean new_state = capacitor[i]>TRESHOLD;
|
#endif
|
||||||
|
times[i] = now;
|
||||||
// TODO: drop state after a maximum HIGH time
|
}
|
||||||
|
states[i] = 0;
|
||||||
// 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){
|
if (now - sensor_time > PERIOD){
|
||||||
sensor_time = now;
|
sensor_time = now;
|
||||||
if ((err = sensor.read2(&temperature, &humidity, NULL)) == SimpleDHTErrSuccess) {
|
auto status = am2302.read();
|
||||||
String s = "{nano:"+String(ID)+",temp:"+String(temperature,1)+",humi:"+String(humidity,1)+"}";
|
if (status == 0){
|
||||||
Serial.println(s);
|
String s = "{nano:"+String(ID)+",temp:"+String(am2302.get_Temperature())+",humi:"+String(am2302.get_Hunidity())+"}";
|
||||||
send485(s.c_str());
|
send485(s.c_str());
|
||||||
|
#ifdef LOG_TO_SERIAL
|
||||||
|
Serial.println(s);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,194 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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 <SoftRS485.h>
|
|
||||||
#include <SimpleDHT.h>
|
|
||||||
#include <TinyIRSender.hpp>
|
|
||||||
|
|
||||||
// 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)<TRIGGER_LEVEL;
|
|
||||||
raw_states[1] = analogRead(15)<TRIGGER_LEVEL;
|
|
||||||
raw_states[2] = analogRead(16)<TRIGGER_LEVEL;
|
|
||||||
raw_states[3] = analogRead(17)<TRIGGER_LEVEL;
|
|
||||||
|
|
||||||
raw_states[4] = analogRead(18)<TRIGGER_LEVEL;
|
|
||||||
raw_states[5] = analogRead(19)<TRIGGER_LEVEL;
|
|
||||||
raw_states[6] = analogRead(20)<TRIGGER_LEVEL;
|
|
||||||
raw_states[7] = analogRead(21)<TRIGGER_LEVEL;
|
|
||||||
#ifdef LOG_TO_SERIAL
|
|
||||||
for (int i=0;i<8;i++){
|
|
||||||
Serial.print(" ");
|
|
||||||
Serial.print(raw_states[i]);
|
|
||||||
Serial.print(" ");
|
|
||||||
}
|
|
||||||
Serial.println();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void send(int btn, long duration){
|
|
||||||
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",ms:"+String(duration)+"}";
|
|
||||||
#ifdef SEND_485
|
|
||||||
for (int i = 0; i<10; i++){
|
|
||||||
if (send485(s.c_str())) break;
|
|
||||||
Serial.println("collision detected, trying again:");
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
Serial.println(s);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void handle(String message){
|
|
||||||
#ifdef LOG_TO_SERIAL
|
|
||||||
Serial.println(message);
|
|
||||||
#endif
|
|
||||||
unsigned int ir_cmd = 0;
|
|
||||||
unsigned int ir_addr = 0;
|
|
||||||
unsigned int nano = 0;
|
|
||||||
|
|
||||||
const char* outer_state = NULL;
|
|
||||||
const char* inner_state = NULL;
|
|
||||||
char* token = strtok_r(message.c_str(), outer_delimiter, &outer_state);
|
|
||||||
|
|
||||||
while (token != NULL) {
|
|
||||||
char* key = strtok_r(token,inner_delimiter,&inner_state);
|
|
||||||
char* val = strtok_r(NULL,inner_delimiter,&inner_state);
|
|
||||||
if (strcmp(key,"nano")==0) nano = atoi(val);
|
|
||||||
if (strcmp(key,"addr")==0) ir_addr = atoi(val);
|
|
||||||
if (strcmp(key,"ir")==0) ir_cmd = atoi(val);
|
|
||||||
token=strtok_r(NULL, outer_delimiter, &outer_state);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nano == ID){ // the message is addressed to us!
|
|
||||||
|
|
||||||
if (ir_addr && ir_cmd){
|
|
||||||
#ifdef LOG_TO_SERIAL
|
|
||||||
Serial.print("Sending 0x");
|
|
||||||
Serial.print(ir_cmd,HEX);
|
|
||||||
Serial.print(" to 0x");
|
|
||||||
Serial.println(ir_addr,HEX);
|
|
||||||
#endif
|
|
||||||
sendNECMinimal(IR_PIN, ir_addr, ir_cmd, REPEATS);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void loop(){
|
|
||||||
unsigned long now = micros();
|
|
||||||
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;
|
|
||||||
|
|
||||||
// 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());
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user