一,效果展示
二,Arduino源代码(两个代码都是可以运行的,只是一个简单一个麻烦而已并不影响)
(1)
#include
#include
#define LED_PIN 13
// 定义DS18B20数据口连接arduino的10号IO上
#define ONE_WIRE_BUS 10
// 初始连接在单总线上的单总线设备
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//pinMode (LED_PIN,OUTPUT);
//digitalWrite (LED_PIN,LOW);
void setup(void)
{
// 设置串口通信波特率
Serial.begin(9600);
Serial.println(“Dallas Temperature IC Control Library Demo”);
// 初始库
sensors.begin();
}
void loop(void)
{
sensors.requestTemperatures();
Serial.print(“T”);
Serial.println(sensors.getTempCByIndex(0));
delay (500);
}
if(Serial.available()>0)
{
String uartRead = Serial.readString();
if(uarRead[0]‘2’)
{digitalWrite(LED_PIN,HIGH);}
if(uarRead[0]‘1’)
{digitalWrite(LED_PIN,LOW);}
}
(2)
#include “DHT.h”
DHT dht;
#include “U8glib.h” // OLED库
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE); // I2C
#define LED_PIN3 13 //红色LED代表客厅灯
#define LED_PIN2 12 //绿色LED代表风扇
#define LED_PIN1 11 //黄色LED代表加湿器
void draw(void)
{
u8g.setFont(u8g_font_8x13); //使用8x13大小的字符
u8g.setPrintPos(0, 20); // 对应 x,y 轴值
u8g.print(“HUMI(%) :”);
u8g.setPrintPos(73, 20);
u8g.print(dht.getHumidity());
u8g.setPrintPos(0, 40);
u8g.print(“TEMP(oC):”);
u8g.setPrintPos(73, 40);
u8g.print(dht.getTemperature());
}
void setup()
{
Serial.begin(9600);
dht.setup(2); // data pin 2
pinMode(LED_PIN1, OUTPUT);
digitalWrite(LED_PIN1, LOW);
pinMode(LED_PIN2, OUTPUT);
digitalWrite(LED_PIN2, LOW);
pinMode(LED_PIN3, OUTPUT);
digitalWrite(LED_PIN3, LOW);
}
void loop()
{
delay(dht.getMinimumSamplingPeriod());
Serial.print(“H”);
Serial.print(dht.getHumidity());
delay(500);
Serial.print(“T”);
Serial.print(dht.getTemperature());
u8g.firstPage();
do {
draw();
}
while( u8g.nextPage() );
delay(200);
if (Serial.available()>0)
{
String uartRead = Serial.readString();
if(uartRead[0]‘2’)
{ digitalWrite(LED_PIN3, HIGH);}//红灯亮客厅灯开
if(uartRead[0]‘1’)
{ digitalWrite(LED_PIN3,LOW);}//红灯灭客厅灯关
if(uartRead[0]‘3’)
{ digitalWrite(LED_PIN2, HIGH);}//绿灯亮风扇开
if(uartRead[0]‘4’)
{ digitalWrite(LED_PIN2,LOW);}//绿灯灭风扇关
if(uartRead[0]‘5’)
{ digitalWrite(LED_PIN1, HIGH);}//黄灯亮加湿器开
if(uartRead[0]‘6’)
{ digitalWrite(LED_PIN1,LOW);} //黄灯灭加湿器关
}
}