Tisan开发板DS18B20

Tisan开发板DS18B20的详细介绍

创作者:临风 | 更新日期:2017-09-01 | 在线时长:399天
临风的第一个设备,来自贝壳物联 ESP8266+DHT11+DS18B20

功能介绍

实时采集环境温度、湿度,每隔45秒上传一次;

offOn按钮可以控制下图中的Led亮灭;

其他按钮led亮0.5秒。

硬件组成

ESP8266+DHT11+LCD1602

QQ图片20160611220647new.jpg

 

代码说明

由于原开发板的厂商不再提供支持,现改用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]