創作者:臨風 | 更新日期:2017-09-01 | 在線時長:399天
臨風的第一個設備,來自貝殼物聯
ESP8266+DHT11+DS18B20
實時采集環境溫度、濕度,每隔45秒上傳一次;
offOn按鈕可以控制下圖中的Led亮滅;
其他按鈕led亮0.5秒。
ESP8266+DHT11+LCD1602
由于原開發板的廠商不再提供支持,現改用Arduino來開發。參考代碼如下。
Arduino的相應插件請自行下載。
暫時還沒有加控制的功能上來,有興趣的朋友可以增加。
[code]
//Compatible with the Arduino IDE 1.0
//Library version:1.1
#include
#include
#include "DHT.h"
#include
const char* ssid = "";
const char* password = "";
const char* host = "121.42.180.30";
const int port = 8181;
const char* key = ""; //APIKEY
const int id = ***; //設備ID
const int id1=***; //數據接口1 ID
const int id2=***; //數據接口2 ID
WiFiClient client;
#define DHTPIN 12 // what digital pin we're connected to
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define SDA 4
#define SCL 5
LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display
DHT dht(DHTPIN, DHTTYPE);
int sensorPin = A0;
int sensorValue = 0;
int count=0;
/*
0 未連接
1 checkinok
*/
int state=0;
float t;
float h;
void setup()
{
Serial.begin(115200);
Serial.println("Esp8266 with DHT11 Test");
dht.begin();
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.println("starting...");
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (!client.connect(host, port)) {
Serial.println("connection failed");
return;
}
}
void loop()
{
//計數器,每秒加1
++count;
//接收數據
if(client.available()>0){
String line = client.readStringUntil('\n');
Serial.print(line);
state=1;
}
//checkin
if(state==0){
Serial.println("checkin");
client.print(String("{\"M\":\"checkin\",\"ID\":\"") + id + "\",\"K\":\""+key+"\"}\n");
}
//每30秒發心跳包
if(count%30==0){
client.print("{\"M\":\"status\"}\n");
}
//每45秒發實時數據
if(count%45==0){
Serial.println("update");
//{"M":"update","ID":"xx1","V":{"id1":"value1","id2":"value2"}}\n
client.print(String("{\"M\":\"update\",\"ID\":\"") + id + "\",\"V\":{\""+id1+"\":\""+t+"\",\""+id2+"\":\""+h+"\"}}\n");
}
//讀光敏電阻,控制LCD背光
sensorValue = analogRead(sensorPin);
if(sensorValue>150){
lcd.backlight();
}else{
lcd.noBacklight();
}
//讀傳感器數據
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
h = dht.readHumidity();
// Read temperature as Celsius (the default)
t = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Fahrenheit (the default)
float hif = dht.computeHeatIndex(f, h);
// Compute heat index in Celsius (isFahreheit = false)
float hic = dht.computeHeatIndex(t, h, false);
Serial.print("==========> ");
Serial.println( count);
Serial.print("Humidity: ");
Serial.print(h);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" *C ");
Serial.print(f);
Serial.print(" *F\t");
Serial.print("Heat index: ");
Serial.print(hic);
Serial.print(" *C ");
Serial.print(hif);
Serial.print(" *F");
Serial.print(" light: ");
Serial.println(sensorValue);
lcd.clear();
//lcd.home();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(t);
lcd.setCursor(0,1);
lcd.print("Hum: ");
lcd.print(h);
delay(1000);
}
[/code]