最近用arduino做了一个小实验,通过温度和湿度传感器采集当前室内温度和湿度,并在温度和湿度超标的时候发出报警,同时将温湿度数据实时oled显示,也可以通过串口观测数据,话不多说,代码如下:
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <dht.h>
dht DHT;
#include <MsTimer2.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define uint8 unsigned char
#define uint16 unsigned int
#define OLED_RESET 8
Adafruit_SSD1306 display(OLED_RESET);
#define uint8 unsigned char
#define uint16 unsigned int
#include <MsTimer2.h>
#define OLED_RESET 8
const int DHT11_PIN= 8;//Humiture sensor attach to pin7
int buzzerPin = 6;
int incomingByte = 0; // 接收到的 data byte
String inputString = ""; // 用来储存接收到的内容
boolean newLineReceived = false; // 前一次数据结束标志
boolean startBit = false; //协议开始标志
String returntemp = ""; //存储返回值
char temp[100] = {0};
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
sensors.begin(); // initialize the bus
display.begin(SSD1306_SWITCHCAPVCC, 0x3c); // initialize with the I2C addr 0x3D (for the 128x64)
display.clearDisplay();
pinMode(buzzerPin,OUTPUT);//set the buzzer as as OUTPUT
// digitalWrite(buzzerPin,LOW);//initialize the buzzerPin as LOW level
digitalWrite(buzzerPin,HIGH);
}
void loop() {
while(1){
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("deg:");
display.setCursor(110,0);
display.println("C");
display.setCursor(0,30);
display.println("wet:");
display.display();
gettempture();
dht();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("deg:");
display.setCursor(110,0);
display.println("C");
display.setCursor(0,30);
display.println("wet:");
display.display();
//int chk = DHT11.read(8); //读取温湿度传感器管脚的数值
//int fTemp = (float)DHT11.temperature; //fTemp赋值为浮点型读取到的温度值
//int iHumidity = DHT11.humidity; //iHumidity赋值为读取到的湿度值
memset(temp, 0x00, sizeof(temp)); //清空temp数组
dtostrf(gettempture(), 3, 1, temp); // 相當於 %3.2f
String sTemp = temp; //数组temp中的字符串赋给sTemp
String sHum = "";
sHum += dht();
returntemp = "$TH-T" + sTemp + "-H" + sHum + "#";
Serial.print(returntemp); //返回协议数据包
inputString = ""; // clear the string
// delay(3000);
}
}
float gettempture(void) {
float temp1=0;
float realtemp=0;
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(50,0);
for(int a=0;a<2;a++){
sensors.requestTemperatures();
temp1=sensors.getTempCByIndex(0);
realtemp+=temp1;
}
realtemp=realtemp/2;
display.println(realtemp);
display.display();
if(realtemp>30)
{
digitalWrite(buzzerPin,LOW);
}
else
{
digitalWrite(buzzerPin,HIGH);
}
return realtemp;
}
float dht(void)
{
D: int chk = DHT.read11(DHT11_PIN);//read the value returned from sensor
switch (chk)
{
case DHTLIB_OK:
//Serial.println("OK!");
break;
case DHTLIB_ERROR_CHECKSUM:
//goto D;
//Serial.print("Checksum error,\t");
break;
case DHTLIB_ERROR_TIMEOUT:
goto D;
//Serial.print("Time out error,\t");
break;
default:
// goto D;
//Serial.print("Unknown error,\t");
break;
}
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(50,30);
display.println(DHT.humidity,1);
display.display();
if(DHT.humidity>50)
{
digitalWrite(buzzerPin,LOW);
}
else
{
digitalWrite(buzzerPin,HIGH);
}
return DHT.humidity;
}
|