Using DHT22 with dimmer and nrf24l01+

Good day,

I am using an Arduino Nano to read the temperature and humidity and set the value to a dimmer and send all of the temperature information to a raspberry pi via a nrf24l01+

I have used this library to control the dimmer:

I can control the dimmer from 1 to 99% and it works fine.
I used the Arduino library for the DHT22 and when i run the code i get the humidity and temperature fine as well, but when i combine both codes, the dimmer keeps working but for the humidity and temperature, i get NAN values.

Am i doing something wrong?

As soon as i comment this line i get the temperature and humidity again:

dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)

here is the code i have used:

/**************
#include <RBDdimmer.h>//
#include <DHT.h>;

//#define USE_SERIAL  SerialUSB //Serial for boards whith USB serial port
#define outputPin  8
#define DHTPIN 3     // what pin we're connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup() {
  Serial.begin(9600);
  dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
  Serial.println("--- Simple dimmer example ---");
  dht.begin();
}

void loop() {
  hum = dht.readHumidity();
  temp = dht.readTemperature();

  Serial.print("Humidity: ");
  Serial.print(hum);
  Serial.print(" %, Temp: ");
  Serial.print(temp);
  Serial.println(" Celsius");

  dimmer.setPower(90); // setPower(90%);
  delay(1000);
  dimmer.setPower(10); // setPower(10%);
  delay(2000);

}

Thanks a lot for your help!

Your DHT22 requires precise timing control to read the information. Because of the frequent interrupts probably on your dimmer you might mess up the timing and thus the reading

You might want to check if using an Si7021 (based on I2C protocol) would perform better

Okay, thanks for your reply.
Would it also be possible to use 2 Arduino Nano’s (since i have many of them), use 1 for temp measuring and the orther for controlling the dimmer, and connect them via wires to send over the temp data?

Thank you

Yes that could work too (a bit over-engineered)

You could connect 2 Arduinos using Serial (cf Serial Input Basics) or I2C or SPI and the "slave" arduino would maintain the %Humidity and T° and you need to decide what type of request handling you want to implement to read it from the main Arduino

Thanks, maybe a bit over engineered but i got it working like that using the TX to RX signal.

bluebirdsoftware:
I got it working like that using the TX to RX signal.

well done!

Well... it stopped there also haha,
Today i tried to send the temperature data from the Arduino to a Raspberry pi that acts as a "server".
I have bought the NRF24L01 with big antenna and adapter.

I tried a lot of tutorials and youtube video's which show how to make a connection between 2 arduino's or an arduino and Raspberry Pi.
The following link does exactly what i need (even with the temp data):
https://www.raspberrypi.org/forums/viewtopic.php?t=91043

But whatever i do, there is no communication between the Arduino and the Raspberry pi.
The distance between both devices now is about 10 meter (the devices will be about 100m apart from each other if it works)

I think i made the connections good because on both devices i get information but i think there is still something wrong:

Can someone help me?
Thanks again!

bluebirdsoftware:
Can someone help me?
Thanks again!

Whithout wiring diagram and code, it's a tough one! Try this.

I’m sorry, the code is copied from the website but i will put everything here again:

Arduino Code:

#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <DHT.h>
#include "nRF24L01.h"
#include "RF24.h"

#define LED 7
#define DHTPIN 2
#define DHTTYPE DHT11

//const int chipSelect = 10;
const int SER = 8;
const int LATCH = 9;
const int CLK = 10;

//nRF24 set the pin 9 to CE and 10 to CSN/SS
// Cables are:
//     SS       -> 10
//     MOSI     -> 11
//     MISO     -> 12
//     SCK      -> 13

//for nrf24 debug
int serial_putc( char c, FILE * ) 
{
  Serial.write( c );
  return c;
} 
void printf_begin(void)
{
  fdevopen( &serial_putc, 0 );
}

RF24 radio(9,10);
DHT dht(DHTPIN,DHTTYPE);

const uint64_t pipes[2] = { 0xF0F0F0F0E1LL,0xF0F0F0F0D2LL };

void setup(void) {
  Serial.begin(9600); //Debug 
  printf_begin();
  dht.begin();
  //nRF24 configurations
  Wire.begin();
  pinMode(SER, OUTPUT);
  pinMode(LATCH,OUTPUT);
  pinMode(CLK, OUTPUT);
  pinMode(LED,OUTPUT);
  //nRF24 configurations
  radio.begin();
  radio.setChannel(0x4c);
  radio.setAutoAck(1);
  radio.setRetries(15,15);
  radio.setDataRate(RF24_250KBPS);
  radio.setPayloadSize(32);
  radio.openReadingPipe(1,pipes[0]);
  radio.openWritingPipe(pipes[1]);
  radio.startListening();
  radio.printDetails(); //for Debugging
}

void loop() {
  int SendPayload[4] ;
  
  //Get data from sensors
  int t = dht.readTemperature(); // deg C
  int f = dht.readTemperature(true); // deg F
  int h = dht.readHumidity();
  
  if (isnan(t)){
    Serial.println("Sensor read failed");
    return;
  }
  int lux = 25;  
  // Assign data to payload array
    
  SendPayload[0] = t;
  Serial.print("degC: ");
  Serial.println(SendPayload[0]);
  SendPayload[1] = f;
  Serial.print("degF: ");
  Serial.println(SendPayload[1]);
  SendPayload[2] = h;
  Serial.print("Hum: ");
  Serial.println(SendPayload[2]);
  Serial.print("Lux : ");
  SendPayload[3] = lux;
  Serial.println(SendPayload[3]);
  radio.stopListening();
  bool ok = radio.write(&SendPayload,sizeof(SendPayload));
  radio.startListening();
  digitalWrite(LED,HIGH);
  delay(1000);  
  digitalWrite(LED,LOW);
}

Raspberry Pi code

#	1 GND		GND
#	2 3.3v	1 3.3v
#	3 CE		22 GPIO25
#	4 CSN		24 CE0_0
#	5 SCK		23 SCLK_0
#	6 MOSI	19 MOSI_0
#	7 MISO	21 MISO_0
#	8 N/C
#  -----------------------------------
#

from nrf24 import NRF24
import time
from time import gmtime, strftime
import RPi.GPIO as GPIO
try:
    pipes = [[0xf0, 0xf0, 0xf0, 0xf0, 0xe1], [0xf0, 0xf0, 0xf0, 0xf0, 0xd2]]

    radio = NRF24()
    radio.begin(0, 0,5,18)  # Edited from radio.begin(0, 0,25,18) -> Changed port to gpio 5 but also tried with gpio port 25
    radio.setRetries(15,15)
    radio.setPayloadSize(32)
    radio.setChannel(0x4c)
    radio.setDataRate(NRF24.BR_250KBPS)
    radio.setPALevel(NRF24.PA_MAX)
    radio.setAutoAck(1)
    radio.openWritingPipe(pipes[0])
    radio.openReadingPipe(1, pipes[1])
    radio.startListening()
    radio.stopListening()
    radio.printDetails()
    radio.startListening()

    while True:
        pipe = [0]
        while not radio.available(pipe, True):
            time.sleep(1000/1000000.0)
        recv_buffer =[]
        radio.read(recv_buffer)
        #print recv_buffer
        print "Deg C: ",recv_buffer[0]
        print "Deg F: ",recv_buffer[2]
        print "Humidity: ",recv_buffer[4]
        print "Light level (lux): ",recv_buffer[6]
except KeyboardInterrupt:
    print "\n Program stopped \n"
    
finally:
    GPIO.cleanup()

Wiring:

I also tried the link you suggested, but i get the same or even worse result (with worse, i mean that all the values are zero’s or something like that

I'm using the method from the link posted for a weather station (ATmega328P stand alone) which sends its readings to a Pi - works like a charm. Have you soldered 10uf caps onto the radios as is recommended?

no, i didn't. I thought that was only if i didn't use the adapter pcb.

Do i need it on both the raspberry and the arduino, and between the vcc and gnd?

Sorry, I didn't realize that you used the sockets until now :expressionless: Funny though, that another user had problems with the NRF modules in combination with the sockets as well. Therefore I suspect the problem being the sockets and the fact that they have an on board voltage regulator which may require a higher input voltage than is available on the 5V pins on the Nano and/or Pi. According to this link the sockets requires at least 4.8V and at most 12V, so I would try to apply 6V and see how it goes. If that does not change anything, I would try to remove the socket all along and test the radios without.

Thanks for your reply,
I did a lot since yesterday but so far no luck at all.

I did add a power supply to the socket which delivers 12V, but still not receiving data.
Also i bought an RF-NANO, installed the "GettingStarted" file to communicate with another arduino. Even than i cannot get any communication. The strange thing is that i need to set

RF24 radio(10,9);
instead of
RF24 radio(9,10);

How can i see if the wiring is correct, the radio.printDetails() show values but for example, the RF_CH is always different, even if i use radio.setChannel(0x26), the RF_CH always has a different value

Also for example SetPALevel. When i set it to NRF24.PA_MAX, the info on the screen shows PA_MIN as a value...
Can it be the wiring or am i just too stupid to get this working

I would try to get 2 radios communicating with 2 Arduinos and no sockets. When that is working, I would try to add the socket to one of the radios and see if that changes anything.

Well, i got a bit further now, i was trying this:

I tried on both arduino's but still no communication, then i swapped transmitter to the arduino and receiver to the RF-NANO and it worked! i received the messages on the RF-NANO which where sent from the normal arduino with socket and NFR24L01.

The only problem is that the pipe address in that program is set to "const byte thisSlaveAddress[5] = {'R','x','A','A','A'};"

I have no idea how i can recode it for python.

Another crazy problem is that if i unplug the arduino (transmitter) from my computer, and power it by a USB power adapter (12W from ipad), the transmitter tx led flashes but the receiver doesn't get any information anymore

If your arduino pipe is:

const uint64_t PIPE = 0x1122334455LL;

then the python equivalent is:

PIPE = [0x11, 0x22, 0x33, 0x44, 0x55]

R=0x52, x=0x78, A=0x41

Dunno what exactely is the cause of your problem, but I still think that it is related to the socket.

Thank you, i am going to try on another arduino and start from scratch without the socket.
Also thank you for explaining how the pipes work

Well, i was kind of happy (for a short time)...
I got everything working between 2 arduino's. I read the temperature, send it via the NRF24L01 to the other arduino and set the dimmer accordingly.

From the second arduino i want to send the data via tx to the raspberry pi so i walked to the raspberry pi, connected the arduino via usb and read the data from the arduino via serialpy.

I got a few reading but not all (and for now the 2 devices are only 10 meters apart). I connected both to my pc to try if i could extend the range with the radio.setPALevel(RF24_PA_MAX); and tested again, but even when the 2 devices where in range of 1 meter i couldn't get any communication. I switched back to the code without the levels and i got some connection again but only half of the readings came throug, then i tried to walk a little further away and after a few meters i got less and less communication. Then i came back to the other arduino but i still got fewer and fewer messages, now i don't get any messages at all anymore... i am getting crazy.

I think i will stop for now and check back tomorrow but i feel like "almost ready" to "start over again".
Or did i do something wrong and is this normal?

Thanks agian

It depends.. NRF24 radios are very low power and thus their signal is quite weak and because the signal is both high frequency and weak, it will not penetrate obstructions very well. My wheater station will only transmit "line of sight" up to 25 meters and as little as 2 meters (if at all) if a concrete wall is in between.

You should, however, be able to get a stable signal with two arduinos and two identical radios placed at a fixed distance. If you are moving around, you should try to notice what is in between of the radios when messages starts to drop. If you cannot get a stable signal, you should try to use a different library to handle the NRF's.

If you need long distance communication, the NRF24's may not be the best sollution for you. You may want to use something like SX1278 (LoRa RA-02) modules which will allow you to transmit messages over very long distances (up to 20km) - but at a higher power consumption. Since the LoRa modules use a lower frequency (433Mhz) they are less sensitive to obstructions.

Well, i have bought the "big" antenna's because they say they have a range till 1km. Ofcourse i don't need this but i guessed it would be enough for my garden to my home. When both arduino's worked, i walked to the end of my garden en i got a response every second (the transmitter was programmed to send a command every second). So the signal should be enough.

The strange thing is that when i was updating my program, both arduino's where almost touching each other. I still got a signal every second but after i walked to the place where the raspberry pi is (10 meters away) i immediately got no signal anymore. After reconnecting the arduino i got signal again but just a bit, then i walked back to the other arduino (where i was programming both) and i got no signal at all anymore.

So i really don't know how this is happening.

I unplugged both arduino's several times, reuploaded the code but nothing changed

I will keep the SX1278 in mind, it sounds like a device that could be really handy sometime