|
基本元器件:LCD1602顯示(IIC驅動)、DS18B20溫度傳感器、NRF24L01、Arduino-UNO板。
有LCD1602顯示的接收端主機代碼:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define CE_PIN 9
#define CSN_PIN 10
#define CHANNEL 108
// DS18B20溫度模塊連接的引腳
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RF24 radio(CE_PIN, CSN_PIN);
const byte address[6] = "00001";
// I2C顯示屏連接的地址
LiquidCrystal_I2C lcd(0x27,16,2);
void setup() {
Serial.begin(9600);
sensors.begin();
radio.begin();
radio.setChannel(CHANNEL);
radio.openReadingPipe(0, address);
radio.startListening();
lcd.init();
lcd.backlight();
}
void loop() {
if(radio.available()) {
float indoorTemp;
radio.read(&indoorTemp, sizeof(indoorTemp));
Serial.print("Indoor:");
Serial.print(indoorTemp);
Serial.print("℃");
sensors.requestTemperatures();
float outdoorTemp = sensors.getTempCByIndex(0);
lcd.setCursor(0, 0);
lcd.print("Indoor:");
lcd.print(outdoorTemp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Outdoor:");
lcd.print(indoorTemp);
lcd.print("C");
}
delay(100);
}
發射外界溫度信息的從機代碼:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <nRF24L01.h>
#include <RF24.h>
#define CE_PIN 9
#define CSN_PIN 10
#define CHANNEL 108// DS18B20溫度模塊連接的引腳
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
RF24 radio(CE_PIN, CSN_PIN);
const byte address[6] = "00001";
void setup() {
Serial.begin(9600);
sensors.begin();
radio.begin();
radio.setChannel(CHANNEL);
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN); }
void loop() {
sensors.requestTemperatures();
float outdoorTemp = sensors.getTempCByIndex(0);
Serial.print("Outdoor:");
Serial.print(outdoorTemp);
Serial.print("℃");
radio.write(&outdoorTemp, sizeof(outdoorTemp));
delay(1000); }
|
|