Hi everybody,
I 'm trying to do a meteo station with a DHT22 sensor who give me temperature and humidity and a trans mitter and a receiver UHF433. I'm a beginner in programmation so I copy paste and modify codes I found on internet. I use the rc-switch bibliotheque and here is my transmitter code:
/* TEMPERATURE SENSOR DHT22 + TRANSMITTER 433 MHTZ */
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
#include <DHT.h> //include DHT library
#define brocheBranchementDHT 6 //define "la broche de branchement"
#define typeDeDHT DHT22 //define the type of DHT
DHT dht(brocheBranchementDHT, typeDeDHT);
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
dht.begin();
// transmitter connected on the pin n° 10
mySwitch.enableTransmit(10);
mySwitch.setProtocol(2); // best config after test
mySwitch.setPulseLength(550); // best config after test
mySwitch.setRepeatTransmit(6); // best config after test
}
void loop() {
// reading data
float tauxHumidite = dht.readHumidity(); // readiing of "taux d'humidité (en %)"
float temperatureEnCelsius = dht.readTemperature(); // reading of the temperature in celsius degrees
// verification if we received data
if (isnan(tauxHumidite) || isnan(temperatureEnCelsius)) {
Serial.println("Aucune valeur retournée par le DHT22. Est-il bien branché ?");
delay(1000);
return; // if no data is received by arduino, wait 1 sec then restart the void loop()
}
// Feeling temperature calculation
float temperatureRessentieEnCelsius = dht.computeHeatIndex(temperatureEnCelsius, tauxHumidite, false); // Le "false" est là pour dire qu'on travaille en °C, et non en °F
// Displaying values
Serial.print("Humidité: "); Serial.print(tauxHumidite); Serial.println(" %");
Serial.print("Température: "); Serial.print(temperatureEnCelsius); Serial.println(" °C");
Serial.print("Température ressentie: "); Serial.print(temperatureRessentieEnCelsius); Serial.println(" °C");
Serial.println();
delay(1000);
mySwitch.send((888000 + tauxHumidite), 24); // send data to the UHF433 transmitter
mySwitch.send((999000 + temperatureEnCelsius), 24);
mySwitch.send((777000 + temperatureRessentieEnCelsius), 24);
delay(1500);
}
and here is the receiver code:
#include <RCSwitch.h>
// call 433 library
RCSwitch mySwitch = RCSwitch();
String temporaryValue ;
float temperatureout = 0 ;
float humiditeout = 0 ;
float temperatureressentieout = 0 ;
long oldDataTime;
void setup() {
Serial.begin(115200);
mySwitch.enableReceive(0); // Receiver on interrupt 0 => that is pin #2
}
void loop() {
if (mySwitch.available()) {
long value = mySwitch.getReceivedValue();
if (value == 0) {
Serial.print("Unknown encoding");
} else /*{
Serial.print("Received ");
Serial.print( mySwitch.getReceivedValue() );
Serial.print(" / ");
Serial.print( mySwitch.getReceivedBitlength() );
Serial.print("bit ");
Serial.print("Protocol: ");
Serial.println( mySwitch.getReceivedProtocol() );
} */
// get data and split values
temporaryValue = String(mySwitch.getReceivedValue());
if(temporaryValue.substring(0,3) == "999"){
temperatureout = temporaryValue.substring(3).toFloat() ;
}
if(temporaryValue.substring(0,3) == "888"){
humiditeout = temporaryValue.substring(3).toFloat() ;
}
if(temporaryValue.substring(0,3) == "777"){
temperatureressentieout = temporaryValue.substring(3).toFloat() ;
}
// reset 433
mySwitch.resetAvailable();
oldDataTime = millis();
}
// control if we lost information , 10 second
if(millis() > (oldDataTime + 10000) ){
temperatureout = 999 ;
humiditeout = 888 ;
temperatureressentieout = 777 ;
}
// print value
// displaying values
Serial.print("Humidite: ");
Serial.print(humiditeout);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(temperatureout);
Serial.println(" °C");
Serial.print("température ressentie: ");
Serial.print(temperatureressentieout);
Serial.println(" °C");
Serial.println();
delay(2000);
}