设为首页收藏本站

arduino 单片机机器人爱好者之家

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3762|回复: 2

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

[复制链接]

签到天数: 511 天

[LV.9]以坛为家II

发表于 2016-4-29 13:18:38 | 显示全部楼层 |阅读模式
本帖最后由 AllBlue 于 2016-8-19 15:40 编辑

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

添加湿度值.png
#添加完传感器后,返回设备管理界面可以得到传感器ID:
获取ID.jpg

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


程序:
[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()));
}



修改程序中的相关参数,即可使用!
程序下载: RemoteMonitor.zip (1.86 KB, 下载次数: 138)

签到天数: 15 天

[LV.4]偶尔看看III

发表于 2016-11-13 08:06:40 | 显示全部楼层
nice good data!!!!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|Archiver|手机版|小黑屋|联系我们|YFRobot ( 苏ICP备13017135  

GMT+8, 2017-12-12 02:52 , Processed in 0.122201 second(s), 28 queries .

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

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