lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

DS18b20 rx on pi

Sun Mar 22, 2015 10:54 am

Dear all,

I'm creating a homestation for my house and i would like to create my own sensor.

I use a DS18b20 on a arduino nano to transmit the temperature to the pi.

Unless the code works for my arduino, i have more difficulties to receive the temperature from the arduino.

My wires are correct because i try the code on my pi ./gettingstarted and he works perfectly ( the connection between the arduino and pi work in the two way).

For the moment, i just try to receive the temperature to my pi with this code :

Arduino code

Code: Select all

 
//DS18b20 libraries
#include <OneWire.h>
#include <DallasTemperature.h>
//NRF24L01 libraries
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

#define CE_PIN 9
#define CSN_PIN 10

const uint64_t canal =  0xF0F0F0F0D2LL;

RF24 radio(CE_PIN, CSN_PIN);

const byte delay_seconds = 1; //Segundos entre ejecuciones del loop
const byte TEMPERATURE_SENSOR_BUS = 2; //Pin al que esta conectado el sensor de temperatura

OneWire oneWire(TEMPERATURE_SENSOR_BUS);
DallasTemperature sensors(&oneWire);

void setup()
{
 Serial.begin(9600); //Start serial port

 sensors.begin();

radio.begin();

 radio.openWritingPipe(canal);

}

void loop()
{

 String a = String(request_temperature());

 char s[a.length()];
 a.toCharArray(s, a.length());

 bool ok = radio.write( s, sizeof(s));


 delay(delay_seconds * 1000);
}

float request_temperature() {
 sensors.requestTemperatures();
return sensors.getTempCByIndex(0);
}

And for the pi code

Code: Select all


#!/usr/bin/python
# raspberry pi nrf24l01 hub

from nrf24 import NRF24
import time
from time import gmtime, strftime

pipes = [0xf0, 0xf0, 0xf0, 0xf0, 0xd2]

radio = NRF24()
radio.begin(0, 0,25,18) #set gpio 25 as CE pin
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)
    out = ''.join(chr(i) for i in recv_buffer)
    print out

In the future, my idea is to receive the temperature and the pi open an sql base to save the temperature every 15min.

Thanks for all :)

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: DS18b20 rx on pi

Sun Mar 22, 2015 3:08 pm

As much as I like Arduino (especially the Nano) I'd be tempted to leave it out of this project and just have the Pi reading the temperature. This may help https://www.cl.cam.ac.uk/projects/raspb ... mperature/

Dave.
Apple say... Monkey do !!

User avatar
paddyg
Posts: 2460
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: DS18b20 rx on pi

Sun Mar 22, 2015 7:54 pm

or use USB serial which is trivially easy.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Sun Mar 22, 2015 8:46 pm

Dear all,

Thanks for your answer.

Why do you want to use the USB ? To connect arduino in RF24 and read the recever through USB ?

Thanks davef21370 but i need RF24 to connect more than one sensor and moreover sensors will be in different room !

Thanks for all for any help :)

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Mon Mar 23, 2015 2:15 pm

Hi, I'm planing to put the Ds18B20 on an arduino mini using the nRF24L01 transmitter.

I do have put some working code for the DHT22 sensor at this url
http://www.raspberrypi.org/forums/viewt ... 22#p725222

The python code could be implement to read another packet type.

Presently I'm still on the drawing board since I want to reduce the power consumption of the arduino to the minimum.

I should finalize the standby mode for the Arduino mini and the nRF24L01 this week.

Once it is done I will implement the ds18b20 sensors into one module.

Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Mon Mar 23, 2015 7:54 pm

Hello danjperron,

Thanks for your example.

I will try asap to transpose it to my raspberry with my DS sensor.

For a lower consuption, i saw many sensors which use the narcotic library.

Moreover, it depend the number of time you want to awake your arduino to take your temperature or something else.

The problem of using sensor with battery, is the lifetime and the frequency :\

How many time you will hope to keep alive your sensor ?

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Mon Mar 23, 2015 8:26 pm

I don't know yet.

But I should be able to tell you at the end of the week.

On sleep mode It should be less than 1ma and maybe a maximum of 50ma for 5..7 seconds every 5 minutes. Than this should be good enough for 2 months with 2 AA cells.

I also report the power voltage so I could put a warning when it is time to change the batteries.

Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Wed Mar 25, 2015 10:14 am

Thanks in advance for your return.

Isn't too short two month ?

I still have trouble with my code.

I try to reinstall the RF24 library for the pi and now i have a strange error...

When i try to launch my code ''python test.py'' for example, the error is

" no module named RF24''

I don't understand why because i try to change my code with ''import NRF24 and putt my program in the same folder of the library but he return the same error....

Does someone have an explanation ?

Thanks

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Mar 25, 2015 11:17 am

Did you check my latest post
http://www.raspberrypi.org/forums/viewt ... d3#p726639

I got around 3 month now with a 5V arduino mini without too much optimization.
Just wonder how much current I lost because of the 3.3V regulator (LE33cz) needed for th nRF24L01?

From the specification, the LE33cz quiescent current is between 0.5 to 1 ma. So I really need to get it out!
I will order an arduino 3.3 V mini.

Are you using a MAC,a PC or the Pi to program your Arduino?

Did you import all the Library? Do you have another library which has the same name.

http://maniacbug.wordpress.com/2011/11/ ... rted-rf24/
https://github.com/RobTillaart/Arduino.git
https://github.com/n0m1/Sleep_n0m1

I will implement the DS18B20 tonight.

Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Wed Mar 25, 2015 6:56 pm

Dear Daniel,

Of course i read your topic !

I think i will try to read again and reinstall one more time because i think it's just a little error.

I'm using a PC to code the Arduino with the original software. I don't think the problem comes from the arduino because when i add some print function to check in the monitor it works.

I import the library of maniacbug and specificaly for the pi, i use this one http://tmrh20.github.io/RF24/ with this page explanation http://tmrh20.github.io/RF24/RPi.html.

I'm boring to be stoped just for this simple error...

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Mar 25, 2015 7:15 pm

I will try to implement the stuff with my acer laptop instead of my mac.

I need to change the brow out fuse in my Arduino mini and I will need my laptop to connect my atmel ISP programmer. This way I will be able to use only 3V and get ridd of the LE33cz regulator which draw too much current.

My acer laptop as already the Arduino software but I will need to update it to the newest version.

I did order some arduino pro mini at 3.3V.

Daniel

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Mar 25, 2015 7:52 pm

I just re-read your post. You need to have the python library in the same folder than your code.

For me the library is in /home/pi/lib_nrf24/

I just create a link in /home/pi , where my python script is , to load it.

Code: Select all

sudo ln -s /home/pi/lib_nrf24/lib_nrf24.py  lib_nrf24.py
The other method will be to change the current path inside your python script before the import.

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Thu Mar 26, 2015 1:32 am

OK I found the real bug.

I'm using https://github.com/BLavery/lib_nrf24.git to run the nRF24L01 on the Pi.


I also include in my github a arduino version with the DS18B20.

I used the library http://www.pjrc.com/teensy/arduino_libr ... neWire.zip
This is from http://www.pjrc.com/teensy/td_libs_OneWire.html


From the example DS18B20_Temperature I extract the needed information and I modified the readSensor() function to read the DS18B20.

I also used the "SKIP ROM" command. This way I don't need to know the sensor ID.
This is good only if you want to read only one sensor. But you could use other pin to add more sensor.

P.S. I disabled the sleep mode.I didn't have time to test it. Right now it is on my Arduino Uno with plenty of power.

I also modified the python version to accept the new DS18B20 data format.

Code: Select all

Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:43:39 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:43:49 2015 VCC:4.979V - DS18B20 T:20.75 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:43:49 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:43:59 2015 VCC:4.979V - DS18B20 T:20.81 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:43:59 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:44:09 2015 VCC:4.979V - DS18B20 T:20.75 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:44:09 2015 VCC:4.262V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:44:19 2015 VCC:4.979V - DS18B20 T:20.75 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:44:19 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:44:29 2015 VCC:4.979V - DS18B20 T:20.75 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:44:29 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:44:39 2015 VCC:4.979V - DS18B20 T:20.81 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:44:39 2015 VCC:4.278V - DHT22   T:20.70 C H:26 %
Sensor C2:C2:C2:C2:C3 - Wed Mar 25 20:44:49 2015 VCC:4.979V - DS18B20 T:20.75 C
Sensor C2:C2:C2:C2:C4 - Wed Mar 25 20:44:49 2015 VCC:4.262V - DHT22   T:20.70 C H:26 %
Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Mon Mar 30, 2015 11:23 am

Dear Daniel,

Thanks a lot for your multiple answer !

I couldn't answer before because i had to travel for work...

Could you give me the code of your python program for the pi to read it with attention ?

Tonight, i will try to use your RF24 lib for the pi.

Thanks a lot for your answers and your patience.

Regards,

Lawfix


lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Mon Mar 30, 2015 8:49 pm

Thanks a lot, i'm looking on it right now

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Mon Mar 30, 2015 10:23 pm

The schematic on the github is for the DHT22.

I didn't put the DS18B20 code with the power ON/OFF nRF24L01.

Still waiting for my Arduino pro mini .

There is a good chance that I will change the DS18B20 code to be like the DHT22. It will be 3 X AA or AAA batteries to run the system at 4.5V and one regulator LE33cz to get 3.3V on the nRF24L01 and one Pin will be used to power the transmitter up. This way I get 28 µA in sleep mode.


I will put code for the MAX6675 thermocouple sensor also. It uses SPI but I will code it in bit banging mode because the SPI is connected to the nRF24L01 and I want to power it ON/OFF. This mean I can't use arduino SPI if the nRF24L01 is off.

Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Tue Mar 31, 2015 9:18 pm

Hello,

I try to use your own library, but when i import them i have this error :

[email protected]:~/rf24libs# python test.py
Traceback (most recent call last):
File "test.py", line 12, in <module>
radio = NRF24()
TypeError: __init__() takes exactly 3 arguments (1 given)

I try to open the library.py and i don't know what is wrong with it ?

It's wird... i try to found the solution

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Tue Mar 31, 2015 10:42 pm

I check my github and it is okay

First I clone my github and lib_nrf24.git

Code: Select all

cd
git clone https://github.com/danjperron/nRF24L01_With_Sensor.git
git clone  https://github.com/BLavery/lib_nrf24.git
Then I move nRF24Reader.py to /home/pi. Use cp if you want to keep the original.

Code: Select all

 mv nRF24L01_With_Sensor/nRF24Reader.py . 
Don't forget the dot!

I make sure that lib_nrf24.py is available from /home/pi

Code: Select all

ln -s lib_nrf24/lib_nrf24.py lib_nrf24.py
And I execute the code

Code: Select all

sudo python nRF24Reader.py
And got

Code: Select all

  self.GPIO.setup(self.ce_pin, self.GPIO.OUT)
STATUS	 = 0x0e RX_DR=0 TX_DS=0 MAX_RT=0 RX_P_NO=7 TX_FULL=0
RX_ADDR_P0-1	 = 0xc2c2c2c2c3 0xe7e7e7e7e7 
RX_ADDR_P2-5	 = 0xc3 0xc4 0xc5 0xc6 
TX_ADDR		 = 0xc2c2c2c2c3 
RX_PW_P0-6	 = 0x20 0x20 0x00 0x00 0x00 0x00 
EN_AA		 = 0x3f 
EN_RXADDR	 = 0x03 
RF_CH		 = 0x4e 
RF_SETUP	 = 0x07 
CONFIG		 = 0x0c 
DYNPD/FEATURE	 = 0x3f 0x06 
Data Rate	 = 1MBPS
Model		 = nRF24l01+
CRC Length	 = 16 bits
PA Power	 = PA_HIGH
Sensor C2:C2:C2:C2:C3 - Tue Mar 31 18:34:09 2015 VCC:4.935V - DS18B20 T:18.37 C
Sensor C2:C2:C2:C2:C4 - Tue Mar 31 18:34:09 2015 VCC:4.262V - DHT22   T:18.90 C H:20 %
P.S. In nRF24Reader.py at the line 164 the address of the Pi transmitter is there.
The line 166 specify a table of the sensors addresses with the time interval for each.

Daniel

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Apr 01, 2015 11:15 am

My code is work in progress.


I temporary put a console re-router to put stdout into a file.

Just take it out if you don't want it

Code: Select all

class Logger(object):
  def __init__(self, filename="RFLog.txt"):
    self.terminal = sys.stdout
    self.log = open(filename, "a")

  def write(self, message):
    self.terminal.write(message)
    self.log.write(message)
    self.log.flush()

sys.stdout = Logger()
Good chance that nRF24Reader.py will be modified a lot!

Also the delay is every 10 seconds. It is easier to figure out if something is wrong but I change it to 60 sec to minimize the power. Maybe 5 mins will be better. The device peak around 18ma when it transmit and go down to 28 µA when it is waiting between the next reading.(Transmitter and Sensor power down in sleep mode).

Now I set it to 60 secs for two devices.

Code: Select all

device = [RF_Device([0xc2,0xc2,0xc2,0xc2,0xc3],60),
          RF_Device([0xc2,0xc2,0xc2,0xc2,0xc4],60)]
Maybe an digital input has to be read every second on one device.
I did some test and it is around 200 transactions per second.A delay of 0.1 sec is possible.

Daniel

lawfix
Posts: 19
Joined: Sun Jan 11, 2015 10:58 pm

Re: DS18b20 rx on pi

Wed Apr 01, 2015 9:12 pm

Hello !

I try to follow your instruction, step by step and when i execute nRFReader that's the return :

Traceback (most recent call last):
File "nRF24Reader.py", line 172, in <module>
radio.begin(0,17)
File "/root/lib_nrf24.py", line 373, in begin
self.spidev.open(0, csn_pin)
IOError: [Errno 2] No such file or directory

:\

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Apr 01, 2015 10:35 pm

Did you enable the SPI?

The RF24 run on the SPI.

sudo raspi-config , advanced options, SPI

reboot and check if you have the /dev/spidev0.0 and /dev/spidev0.1

Code: Select all

[email protected] ~ $ ls -l /dev/spi*
crw-rw---T 1 root spi 153, 0 déc 31  1969 /dev/spidev0.0
crw-rw---T 1 root spi 153, 1 déc 31  1969 /dev/spidev0.1
[email protected] ~ $ 

User avatar
DougieLawson
Posts: 36818
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: DS18b20 rx on pi

Wed Apr 01, 2015 10:37 pm

Do you have the latest version of python-spidev?
https://github.com/doceme/py-spidev
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Apr 01, 2015 10:37 pm

Also did you install spidev for python

Code: Select all

sudo pip install spidev
I think.

danjperron
Posts: 3431
Joined: Thu Dec 27, 2012 4:05 am
Location: Québec, Canada

Re: DS18b20 rx on pi

Wed Apr 01, 2015 11:19 pm

Wow! Dougie practically on sync!

This is how you could install spidev for python2.7 and python3.2

Code: Select all

sudo bash
apt-get install python-dev
apt-get install python3-dev
apt-get install python-pip
apt-get install python3-pip
pip install spidev
pip-3.2 install spidev
exit 
*** I was tire to type sudo all the time. This way I just do "exit" at the end to return to user mode.

Daniel

Return to “Python”