added Documentation

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2023-11-20 00:35:03 +01:00
parent 6cd774b00a
commit 3bb87bad44
6 changed files with 107 additions and 104 deletions

View File

@@ -2,10 +2,31 @@
#include <SoftRS485.h>
#include <SimpleDHT.h>
// TempSend
//
// This is an example for sending DHT11 measures over an RS485 multimaster network.
// It makes use of an internal timer to periodically query values from an DHT11 sensor, connected at pin 6:
//
// This timer is set up in the method "setupTimer".
// The _interrupt service routine_ (ISR) just sets a flag _readSample_, which is checked in the main loop.
// If the _readSample_ flag is set, a measure is fetched from the sensor and transmitted via _send485_ call.
//
// The main loop also listens for incoming messages and echos them on the Serial console.
int pinDHT11 = 6;
int count = 0;
String s;
boolean readSample = false;
SimpleDHT11 dht11(pinDHT11);
SimpleDHT11 dht11(pinDHT11); // Sensor connected on pin 6
void setupTimer(){
TCCR1A = 0; // Init Timer1A
TCCR1B = 0; // Init Timer1B
TCCR1B |= B00000101; // Prescaler = 1024
OCR1A = 31250; // Timer Compare1A Register
TIMSK1 |= B00000010; // Enable Timer COMPA Interrupt
}
void setup() {
Serial.begin(115200);
@@ -15,21 +36,32 @@ void setup() {
// connect pin 3 to ^RE of Max485
// connect pin 4 to DE of Max485
// connect pin 5 to DI of Max485
while (!send485("Hello world!")) {}
while (!send485("Hello world!")) {} // send hello on network
setupTimer(); // start timer for DHT 11 measurements
}
/* Interrupt service routine sets _readSample_ flag */
ISR(TIMER1_COMPA_vect){
OCR1A += 31250; // Advance The COMPA Register
readSample = true;
}
void loop() {
delay(2000);
// read without samples.
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Read DHT11 failed, err="); Serial.print(SimpleDHTErrCode(err));
Serial.print(","); Serial.println(SimpleDHTErrDuration(err)); delay(1000);
return;
if (available485()) Serial.println("Received: "+get485message());
if (readSample){ // it is time to take a reading!
readSample = false;
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
count++;
s = "Sample "+String(count);
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
s += " failed, err="+String(SimpleDHTErrCode(err))+","+String(SimpleDHTErrDuration(err));
} else {
s += " ok: "+ String(temperature)+" *C, "+String(humidity)+"% rel";
}
while (!send485(s.c_str())) { /* repeat until successfull */ }
}
count++;
String s = "Sample "+String(count)+" ok: "+ String(temperature)+" *C, "+String(humidity)+"% rel";
send485(s.c_str());
}