updated temp sender
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -82,7 +82,7 @@ void isr(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void send(int btn, long duration){
|
void send(int btn, long duration){
|
||||||
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",dura:"+String(duration)+"}";
|
String s = "{nano:"+String(ID)+",btn:"+String(btn)+",ms:"+String(duration)+"}";
|
||||||
#ifdef SEND_485
|
#ifdef SEND_485
|
||||||
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;
|
||||||
@@ -95,7 +95,6 @@ void send(int btn, long duration){
|
|||||||
|
|
||||||
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.
|
||||||
|
|||||||
@@ -4,23 +4,28 @@
|
|||||||
* 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 <AM2302-Sensor.h>
|
#include <AM2302-Sensor.h>
|
||||||
|
|
||||||
#define PROGRAM "RS485-Nano 2.0 / TempSender 1.1"
|
#define PROGRAM "RS485-Nano 2.0 / TempSender 1.2"
|
||||||
|
|
||||||
#define BTN_INT 2 // button interrupt pin
|
#define BTN_INT 2 // button interrupt pin
|
||||||
#define Max485_RO 3 // read-output of Max485
|
#define Max485_RO 3 // read-output of Max485
|
||||||
#define Max485_RE 5 // not-read-enable of Max485
|
#define Max485_RE 5 // not-read-enable of Max485
|
||||||
#define Max485_DE 9 // data enable of Max485
|
#define Max485_DE 9 // data enable of Max485
|
||||||
#define Max485_DI 8 // data input of Max485
|
#define Max485_DI 8 // data input of Max485
|
||||||
|
|
||||||
|
#define TRESHOLD 1024
|
||||||
|
|
||||||
#define DHT_PIN 4
|
#define DHT_PIN 4
|
||||||
#define TRESHOLD 100000 // 100ms
|
|
||||||
#define PERIOD 100000000 // 100s
|
#define PERIOD 100000000 // 100s
|
||||||
#define ID 3
|
|
||||||
|
#define ID 0
|
||||||
// 0=Test
|
// 0=Test
|
||||||
// 1=Arbeitszimmer
|
// 1=Arbeitszimmer
|
||||||
// 2=Küche
|
// 2=Küche
|
||||||
@@ -31,12 +36,19 @@
|
|||||||
|
|
||||||
AM2302::AM2302_Sensor am2302{DHT_PIN};
|
AM2302::AM2302_Sensor am2302{DHT_PIN};
|
||||||
|
|
||||||
|
boolean raw_states[8];
|
||||||
boolean states[8];
|
boolean states[8];
|
||||||
|
unsigned long capacitor[8];
|
||||||
unsigned long times[8];
|
unsigned long times[8];
|
||||||
unsigned long sensor_time = PERIOD;
|
unsigned long sensor_time = PERIOD;
|
||||||
|
|
||||||
|
|
||||||
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();
|
am2302.begin();
|
||||||
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
init485(Max485_RO,Max485_RE,Max485_DE,Max485_DI); // library initialization:
|
||||||
@@ -50,7 +62,7 @@ void setup(){
|
|||||||
pinMode(20,INPUT_PULLUP);
|
pinMode(20,INPUT_PULLUP);
|
||||||
pinMode(21,INPUT_PULLUP);
|
pinMode(21,INPUT_PULLUP);
|
||||||
|
|
||||||
attachInterrupt(digitalPinToInterrupt(2),isr,FALLING);
|
attachInterrupt(digitalPinToInterrupt(BTN_INT),isr,CHANGE);
|
||||||
// Print a message to the LCD.
|
// Print a message to the LCD.
|
||||||
Serial.println(PROGRAM);
|
Serial.println(PROGRAM);
|
||||||
#ifdef LOG_TO_SERIAL
|
#ifdef LOG_TO_SERIAL
|
||||||
@@ -59,45 +71,55 @@ void setup(){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void isr(){
|
void isr(){
|
||||||
states[0] = !digitalRead(14);
|
raw_states[0] = analogRead(14)<400;
|
||||||
states[1] = !digitalRead(15);
|
raw_states[1] = analogRead(15)<400;
|
||||||
states[2] = !digitalRead(16);
|
raw_states[2] = analogRead(16)<400;
|
||||||
states[3] = !digitalRead(17);
|
raw_states[3] = analogRead(17)<400;
|
||||||
|
|
||||||
states[4] = !digitalRead(18);
|
raw_states[4] = analogRead(18)<400;
|
||||||
states[5] = !digitalRead(19);
|
raw_states[5] = analogRead(19)<400;
|
||||||
states[6] = !(analogRead(20)>200);
|
raw_states[6] = analogRead(20)<400;
|
||||||
states[7] = !(analogRead(21)>200);
|
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(states[i]); Serial.print(" ");
|
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++){
|
||||||
if (states[i]){
|
// we emulate a capacitor:
|
||||||
delay(1);
|
// when the button is pressed, it slowly loads, if the button is released, it unloads.
|
||||||
if (now - times[i] > TRESHOLD){
|
capacitor[i] = raw_states[i] ? capacitor[i]+1 : capacitor[i]>>1;
|
||||||
#ifdef SEND_485
|
|
||||||
send(i+1);
|
// if the capacitor reaches a certain treshold, the state is changed
|
||||||
#endif
|
boolean new_state = capacitor[i]>TRESHOLD;
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,10 +129,6 @@ void loop(){
|
|||||||
if (status == 0){
|
if (status == 0){
|
||||||
String s = "{nano:"+String(ID)+",temp:"+String(am2302.get_Temperature())+",humi:"+String(am2302.get_Hunidity())+"}";
|
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user