只需一步,快速开始
签到天数: 868 天
[LV.10]以坛为家III
添加温度值.png (21.39 KB, 下载次数: 47)
下载附件
2016-4-29 13:50 上传
添加湿度值.png (21.42 KB, 下载次数: 66)
获取ID.jpg (17.95 KB, 下载次数: 54)
2016-4-29 13:58 上传
ReadDHT11.JPG (125.73 KB, 下载次数: 43)
2016-8-19 15:39 上传
/* 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())); }
数据上传.png (64.18 KB, 下载次数: 45)
2016-4-29 13:49 上传
65E7ADD91C62A79B300D20A791F21C34.jpg (28.58 KB, 下载次数: 83)
2016-4-29 14:20 上传
添加动作.png (10.63 KB, 下载次数: 55)
2016-4-29 14:21 上传
使用道具 举报
签到天数: 63 天
[LV.6]常住居民II
签到天数: 22 天
[LV.4]偶尔看看III
本版积分规则 发表回复 回帖后跳转到最后一页
|小黑屋|联系我们|YFROBOT ( 苏ICP备20009901号-2 )
GMT+8, 2024-12-27 05:15 , Processed in 0.052388 second(s), 27 queries .
Powered by Discuz! X3.1
© 2001-2013 Comsenz Inc.