YFROBOT创客社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10807|回复: 3
打印 上一主题 下一主题

ESP8266 core for Arduino应用:远程监控温湿度,基于yeelink物联网开发平台

[复制链接]

签到天数: 866 天

[LV.10]以坛为家III

跳转到指定楼层
楼主
发表于 2016-4-29 13:18:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
YEELINK 云平台已失联,帖子仅供参考学习,请谨慎阅读!!

DHT11 大家都是用过,本文将使用DHT11及ESP8266(虽然模块使用的nodemcu,但使用arduino语言开发,未使用LUA语言及nodemcu固件)监控当前环境温湿度并上传云端,实现远程监控温湿度功能!
云端我们基于yeelink免费平台,我们需要再yeelink平台注册账号(不介绍,看此帖)!
我已注册账号,直接添加DHT11传感器就可以,下面是具体步骤:
#添加传感器值,温度值及湿度值:

#添加完传感器后,返回设备管理界面可以得到传感器ID:


#电路图,DHT11数据引脚连接到D4(GPIO2):


程序:
[C] 纯文本查看 复制代码
/*
   ESP8266 TCPcleint 连接到NET WORK ,
   读取DHT11数据并上传云端,用手机或电脑可监控设备温湿度,实现远程监控实时温湿度!
   基于yeelink 免费物联平台 [url=http://www.yeelink.net]www.yeelink.net[/url]
   by yfrobot
   [url=http://www.yfrobot.com]http://www.yfrobot.com[/url]
*/

#include <ESP8266WiFi.h>
#include "DHT.h"

#define DHTPIN 4                          // what digital pin we're connected to
#define DHTTYPE DHT11                     // DHT 11 -- Uncomment whatever type you're using!

const char* ssid     = "YFROBOT";         // XXXXXX -- 使用时请修改为当前你的 wifi ssid
const char* password = "xxxxx";     // XXXXXX -- 使用时请修改为当前你的 wifi 密码
const char* host = "www.yeelink.net";
const char* APIKEY = "xxxxxx";    //API KEY

int deviceId = 345667;
int sensorId_T = 387465;
int sensorId_H = 387466;

WiFiClient client;
const int tcpPort = 80;
char data[512] ;
int x = 0;
int dat = 0;

DHT dht(DHTPIN, DHTTYPE);
int postCount = 0;

void setup() {
  WiFi.mode(WIFI_AP_STA);                 //set work mode:  WIFI_AP /WIFI_STA /WIFI_AP_STA
  Serial.begin(115200);
  delay(10);

  // We start by connecting to a WiFi network
  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.print("IP address: ");
  Serial.println(WiFi.localIP());

  dht.begin();
}

void loop() {
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  Serial.print(h);
  Serial.println(t);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  if (!client.connect(host, tcpPort)) {
    Serial.println("connection failed");
    return;
  }
  //post value
  if (postCount == 0) {
    postData(deviceId, sensorId_T, t);
    postCount++;
  } else {
    postData(deviceId, sensorId_H, h);
    postCount = 0;
  }

  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 2000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      return;
    }
  }
  Serial.println("closing connection");
}

void postData(int dId, int sId, float val) {
  // We now create a URI for the request
  String url = "/v1.0/device/";
  url += String(dId);
  url += "/sensor/";
  url += String(sId);
  url += "/datapoints";
  String str = String(val);
  String data = "{\"value\":" + str + "}";

  // This will send the request to the server
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "Accept: */*\r\n" +
               "U-ApiKey:" + APIKEY + "\r\n" +
               "Content-Length: " + String(str.length()+10) + "\r\n" +                       //发送数据长度
               "Content-Type: application/x-www-form-urlencoded\r\n" +
               "Connection: close\r\n\r\n" + 
               data
              );
  Serial.println(str);
  Serial.println(String(str.length()));
}



修改程序中的相关参数,即可使用!
程序下载:

#刷新网页,查看数据(两个数据发送需要相隔一段时间,否则数据上传错误):


你也可以登录手机端yeelink app,查看数据变化:




你还可以通过添加动作,实现“报警功能”,例如,当温度过高过低,湿度过高过低等情况时通过移动客户端、电子邮件等方式推送消息给你:

暂时还不支持微信推送功能!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 支持支持 反对反对

签到天数: 22 天

[LV.4]偶尔看看III

板凳
发表于 2019-5-6 17:20:38 | 只看该作者
不错,很值得学习一下
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2  

GMT+8, 2024-4-26 14:01 , Processed in 0.047361 second(s), 25 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表