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);
}
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
Thanks for all