Files
SoftRS485/examples/TempSend/TempSend.ino
2023-11-20 00:35:03 +01:00

68 lines
2.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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); // 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);
Serial.println("This is TempSend 0.1");
init485(2,3,4,5); // library initialization:
// connect pin 2 to RO of Max485
// 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!")) {} // 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() {
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 */ }
}
}