121-Mesure de Température DS18B20
Mesure de Température DS18B20
Pour le matériel et les bibliothèques voir chapitre ici
Le but de ce montage est de faire une sonde de température sans fil. L’envoi des données se fera sur le Raspberry voir chapitre ici
Montage:
Le module nRF24L01+ doit être alimenté avec une tension inférieure à 3,6V.
Programme:
Choisir comme type de carte ATtiny84@8Mhz.
Charger le programme suivant dans l'Attiny:
/*
This is an attiny84 example code for the nRF24L01 that can communicate with RF24 library
All the support files and libraries for the attiny for nRF24L01 is at repo listed below
* repo : https://github.com/stanleyseow/arduino-nrf24l01/
* Author : Stanley Seow
* e-mail : stanleyseow@gmail.com
* date : 8 Aug 2013
Some default values to take note when using this mirf/spi85 library
Uses Mirf forked library from https://github.com/xdarklight/arduino-nrf24l01
- node addressing is similar to RF24 libs but the bytes are flipped
byte TADDR[] = {0xe3, 0xf0, 0xf0, 0xf0, 0xf0}; will matches receiver node of RF24 below
const uint64_t pipes[2] = { 0x7365727631LL, 0xF0F0F0F0E3LL };
The repo for the RF24 lib is at https://github.com/stanleyseow/RF24/
Added TinyDebugSerial to the codes for TX only serial debugging
https://code.google.com/p/arduino-tiny/
*/
//Modifier par Christophe CARON
//www.caron.ws
#include <SPI85.h>
#include <Mirf85.h>
#include <nRF24L0185.h>
#include <MirfHardwareSpiDriver85.h>
#include <OneWire.h> // Inclusion de la librairie OneWire
#include <DallasTemperature.h> // http://download.milesburton.com/Arduino/MaximTemperature/DallasTemperature_LATEST.zip
#include <Narcoleptic.h> // https://code.google.com/p/narcoleptic/
#define BROCHE_ONEWIRE 10 // Broche utilisée pour le bus 1-Wire
#define ONE_WIRE_POWER 0 // DS18B20 Power pin is connected on D0
OneWire ds(BROCHE_ONEWIRE); // Création de l'objet OneWire ds
DallasTemperature sensors(&ds); // Passe la reference à la biblio Dallas Temperature
// This USI was defined in SPI85.cpp
// Not to be confused with SPI (MOSI/MISO) used by ICSP pins
// Refer to page 61 of attiny84 datahseet
//
//#define USI-DO 5
//#define USI-DI 4
//#define USCK 6
#define CE 8
#define CSN 7
// ATMEL ATTINY84 / ARDUINO
//
// +-\/-+
// VCC 1| |14 GND
// SerialTx (D 0) PB0 2| |13 AREF (D 10)
// (D 1) PB1 3| |12 PA1 (D 9)
// RESET PB3 4| |11 PA2 (D 8)
// PWM INT0 (D 2) PB2 5| |10 PA3 (D 7) CE
// SS/CSN (D 3) PA7 6| |9 PA4 (D 6) USCK
// USI-DI (D 4) PA6 7| |8 PA5 (D 5) USI-DO
// +----+
uint8_t nodeID = 0;
void setup(){
Mirf.cePin = CE;
Mirf.csnPin = CSN;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.payload = 32;
// This address is compatible with my example of rpi-hub or nRF24_Arduino_as_hub
// at repo https://github.com/stanleyseow/RF24/examples/
//byte RADDR[] = {0xe7, 0xde, 0xde, 0xde, 0xde};
byte TADDR[] = {0xe2, 0xf0, 0xf0, 0xf0, 0xf0}; // Ardresse du raspberry . Lecture en sens inverse
// Get nodeID from TXADDR
nodeID = *TADDR & 0xff;
Mirf.channel = 0x0; // Same as rpi-hub and sendto_hub ( channel 0 )
Mirf.rfsetup = 0x06; // 1Mbps, Max power
//Mirf.setRADDR(RADDR);
Mirf.setTADDR(TADDR);
Mirf.config();
// Enable dynamic payload on the other side
Mirf.configRegister( FEATURE, 1<<EN_DPL );
Mirf.configRegister( DYNPD, 1<<DPL_P0 | 1<<DPL_P1 | 1<<DPL_P2 | 1<<DPL_P3 | 1<<DPL_P4 | 1<<DPL_P5 );
pinMode(ONE_WIRE_POWER, OUTPUT); // set power pin for DS18B20 to output
delay(100);
}
void loop(){
char buffer[32] = ""; //texte à envoyer
float temp;
digitalWrite(ONE_WIRE_POWER, HIGH); // turn DS18B20 sensor on
delay(5); // Allow 5ms for the sensor to be ready
sensors.begin(); //demarrage du capteur de temperature
sensors.requestTemperatures(); // demmande mesure de temperature
temp=(sensors.getTempCByIndex(0)); // Lecture de la temperature
if (temp>-55 && temp!=85.00 && temp<125){ // affichage si temperature valide
// Affiche la température
dtostrf(temp, 5, 2, buffer);
envoi(buffer);
}
digitalWrite(ONE_WIRE_POWER, LOW); // turn DS18B20 sensor off
Narcoleptic.delay(32000); // enter low power mode for 60 seconds, valid range 16-32000 ms with standard Narcoleptic lib
// change variable for delay from int to unsigned int in Narcoleptic.cpp and Narcoleptic.h
// to extend range up to 65000 ms
} // End loop()
void envoi( char *text){
uint8_t sent = false;
Mirf.send((byte *) text);
while( Mirf.isSending() )
{
delay(1);
sent = true; // Sent success
}
}
Téléchargement du programme ici
Maintenant votre Attiny envoie la température toute les 32000ms. Pour les voir sur le Raspberry aller dans le chapitre ici
Version basse consommation et avec une adresse:
Il faut installer la bibliothèque : https://code.google.com/p/narcoleptic/ ou Télécharger ici
/*
This is an attiny84 example code for the nRF24L01 that can communicate with RF24 library
All the support files and libraries for the attiny for nRF24L01 is at repo listed below
* repo : https://github.com/stanleyseow/arduino-nrf24l01/
* Author : Stanley Seow
* e-mail : stanleyseow@gmail.com
* date : 8 Aug 2013
Some default values to take note when using this mirf/spi85 library
Uses Mirf forked library from https://github.com/xdarklight/arduino-nrf24l01
- node addressing is similar to RF24 libs but the bytes are flipped
byte TADDR[] = {0xe3, 0xf0, 0xf0, 0xf0, 0xf0}; will matches receiver node of RF24 below
const uint64_t pipes[2] = { 0x7365727631LL, 0xF0F0F0F0E3LL };
The repo for the RF24 lib is at https://github.com/stanleyseow/RF24/
Added TinyDebugSerial to the codes for TX only serial debugging
https://code.google.com/p/arduino-tiny/
*/
//Modifier par Christophe CARON
//www.caron.ws
#include <SPI85.h>
#include <Mirf85.h>
#include <nRF24L0185.h>
#include <MirfHardwareSpiDriver85.h>
#include <OneWire.h> // Inclusion de la librairie OneWire
#include <DallasTemperature.h> // http://download.milesburton.com/Arduino/MaximTemperature/DallasTemperature_LATEST.zip
#include <Narcoleptic.h> // https://code.google.com/p/narcoleptic/
#define BROCHE_ONEWIRE 10 // Broche utilisée pour le bus 1-Wire
#define ONE_WIRE_POWER 0 // DS18B20 alimentation D0
OneWire ds(BROCHE_ONEWIRE); // Création de l'objet OneWire ds
DallasTemperature sensors(&ds); // Passe la reference à la biblio Dallas Temperature
// This USI was defined in SPI85.cpp
// Not to be confused with SPI (MOSI/MISO) used by ICSP pins
// Refer to page 61 of attiny84 datahseet
//
//#define USI-DO 5
//#define USI-DI 4
//#define USCK 6
#define CE 8
#define CSN 7
// ATMEL ATTINY84 / ARDUINO
//
// +-\/-+
// VCC 1| |14 GND
// SerialTx (D 0) PB0 2| |13 AREF (D 10)
// (D 1) PB1 3| |12 PA1 (D 9)
// RESET PB3 4| |11 PA2 (D 8)
// PWM INT0 (D 2) PB2 5| |10 PA3 (D 7) CE
// SS/CSN (D 3) PA7 6| |9 PA4 (D 6) USCK
// USI-DI (D 4) PA6 7| |8 PA5 (D 5) USI-DO
// +----+
void setup(){
Mirf.cePin = CE;
Mirf.csnPin = CSN;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.payload = 32;
// This address is compatible with my example of rpi-hub or nRF24_Arduino_as_hub
// at repo https://github.com/stanleyseow/RF24/examples/
//byte RADDR[] = {0xe7, 0xde, 0xde, 0xde, 0xde};
byte TADDR[] = {0xe2, 0xf0, 0xf0, 0xf0, 0xf0}; // Ardresse du raspberry . Lecture en sens inverse
Mirf.channel = 0x0; // Same as rpi-hub and sendto_hub ( channel 0 )
Mirf.rfsetup = 0x06; // 1Mbps, Max power
//Mirf.setRADDR(RADDR); //Pas de reception
Mirf.setTADDR(TADDR);
Mirf.config();
// Enable dynamic payload on the other side
Mirf.configRegister( FEATURE, 1<<EN_DPL );
Mirf.configRegister( DYNPD, 1<<DPL_P0 | 1<<DPL_P1 | 1<<DPL_P2 | 1<<DPL_P3 | 1<<DPL_P4 | 1<<DPL_P5 );
pinMode(ONE_WIRE_POWER, OUTPUT); // set power pin for DS18B20 to output
delay(100);
}
void loop(){
char buffer[32] = ""; //texte à envoyer
char buffer1[32] = "#ID60#"; //ID Node choix
int a=0; // variable boucle
float temp; // valeur de la temperature
digitalWrite(ONE_WIRE_POWER, HIGH); // turn DS18B20 sensor on
delay(5); // Allow 5ms for the sensor to be ready
sensors.begin(); //demarrage du capteur de temperature
sensors.requestTemperatures(); // demmande mesure de temperature
temp=(sensors.getTempCByIndex(0)); // Lecture de la temperature
if (temp>-55 && temp!=85.00 && temp<125){ // affichage si temperature valide
// Affiche la température
dtostrf(temp, 5, 2, buffer); // Passage de la valeur de la temperature en char
strcat ( buffer1,buffer); // Ajout de ID node
envoi(buffer1); // envoie
}
digitalWrite(ONE_WIRE_POWER, LOW); // turn DS18B20 sensor off
Mirf.powerDown(); // passage en mode sleep du module RF
while(a < 31){ //60Sec
Narcoleptic.delay(2000); // enter low power mode for 60 seconds, valid range 16-32000 ms with standard Narcoleptic lib
// change variable for delay from int to unsigned int in Narcoleptic.cpp and Narcoleptic.h
// to extend range up to 65000 ms
a++;
}
} // End loop()
void envoi( char *text){
uint8_t sent = false;
Mirf.send((byte *) text);
while( Mirf.isSending() )
{
delay(1);
sent = true; // Sent success
}
}
Téléchargement du programme ici
Temps d'autonomie avec trois accus de 1,2V 1100mAh en cours .Démarrage le 06/12/2013 à 22:50 Arrêt le 02/04/2014.
Affichage d'une température de 96,19°C
Mon module a fonctionné 4 mois.
Mise à jour 02/04/2014
Créé avec HelpNDoc Personal Edition: Créer des fichiers d'aide Qt Help multi-plateformes