极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 38434|回复: 18

利用arduino W5100 DHT11实现web浏览温湿度(初级简单)

[复制链接]
发表于 2014-9-4 22:21:59 | 显示全部楼层 |阅读模式
本帖最后由 zhaopengxslc 于 2014-9-4 22:23 编辑
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. #include <EthernetClient.h>
  4. #include <EthernetServer.h>
  5. #include <DHT.h>
  6. #define DHTPIN 2
  7. #define DHTTYPE DHT11  
  8. DHT dht(DHTPIN, DHTTYPE);

  9. byte mac[] = {
  10.   0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  11. IPAddress ip(192,168,2,177);
  12. EthernetServer server(80);

  13. void setup() {
  14.   Serial.begin(9600);
  15.   Ethernet.begin(mac, ip);
  16.   server.begin();
  17.   dht.begin();
  18. }

  19. void loop() {
  20.   delay(2000);
  21.   // listen for incoming clients
  22.   EthernetClient client = server.available();
  23.   if (client) {
  24.     // an http request ends with a blank line
  25.     boolean currentLineIsBlank = true;
  26.     while (client.connected()) {
  27.       if (client.available()) {
  28.         char c = client.read();
  29.         // if you've gotten to the end of the line (received a newline
  30.         // character) and the line is blank, the http request has ended,
  31.         // so you can send a reply
  32.         if (c == '\n' && currentLineIsBlank) {
  33.           // send a standard http response header
  34.           client.println("HTTP/1.1 200 OK");
  35.           client.println("Content-Type: text/html");
  36.           client.println("Connection: close");  // the connection will be closed after completion of the response
  37.           client.println("Refresh: 5");  // refresh the page automatically every 5 sec
  38.           client.println();
  39.           client.println("<!DOCTYPE HTML>");
  40.           client.println("<html>");
  41.           // output the value of each analog input pin
  42.           client.println("<head>");
  43.           client.println("</head>");
  44.           client.println("<body>");
  45.          
  46.           float h=dht.readHumidity();
  47.           float t=dht.readTemperature();  
  48.           if(isnan(h) || isnan(t))
  49.           {
  50.              client.println("Error!");
  51.              client.println("<br />");
  52.               return;
  53.           }  
  54.           client.print("Humidity:");
  55.           client.println(h);
  56.           client.print("Temperture:");
  57.           client.print(t);
  58.           client.println("<br />");
  59.           client.println("<br />");
  60.           client.println("</body>");
  61.           client.println("</html>");
  62.           break;
  63.         }
  64.         if (c == '\n') {
  65.           // you're starting a new line
  66.           currentLineIsBlank = true;
  67.         }
  68.         else if (c != '\r') {
  69.           // you've gotten a character on the current line
  70.           currentLineIsBlank = false;
  71.         }
  72.       }
  73.     }
  74.     // give the web browser time to receive the data
  75.     delay(1);
  76.     // close the connection:
  77.     client.stop();
  78.   }
  79. }

复制代码


连接方式
DHT11
pin1 5V
pin2 D2
pin3 悬空
pin4 GND

W5100作为web服务器

本帖子中包含更多资源

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

x
回复

使用道具 举报

发表于 2014-9-5 09:14:02 | 显示全部楼层
是否将Arduino作为数据采集,W5100作为Server ,PC 浏览器作为Clint,在浏览器上打上IP地址,直接读数?
回复 支持 反对

使用道具 举报

发表于 2014-9-5 12:13:34 | 显示全部楼层
实验成功,但感觉刷新有点慢
回复 支持 反对

使用道具 举报

发表于 2014-9-5 20:50:44 | 显示全部楼层
温度,湿度变化本来就是慢变化特性,刷新慢些也无仿
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-6 22:45:55 | 显示全部楼层
林定祥 发表于 2014-9-5 09:14
是否将Arduino作为数据采集,W5100作为Server ,PC 浏览器作为Clint,在浏览器上打上IP地址,直接读数?

可以呀 输入http://192.168.2.177就可以看到
回复 支持 反对

使用道具 举报

 楼主| 发表于 2014-9-6 22:47:14 | 显示全部楼层
504835618 发表于 2014-9-5 12:13
实验成功,但感觉刷新有点慢

修改delay(2000)的数值
回复 支持 反对

使用道具 举报

发表于 2015-3-4 21:47:15 | 显示全部楼层
你好,请问下你的这个温湿度采集实验除了 arduino开发板,DHT11传感器,还需要哪些模块,可以看下你的硬件原理图不?
回复 支持 反对

使用道具 举报

发表于 2015-4-26 14:14:30 | 显示全部楼层
硬件连接图  可以找来看看吗
回复 支持 反对

使用道具 举报

发表于 2015-4-26 19:17:54 | 显示全部楼层
zhaopengxslc 发表于 2014-9-6 22:45
可以呀 输入http://192.168.2.177就可以看到

一定是192.168.2.177吗?
回复 支持 反对

使用道具 举报

发表于 2015-8-5 21:44:11 | 显示全部楼层
suoma 发表于 2015-4-26 19:17
一定是192.168.2.177吗?

只要和这个对应就行了“IPAddress ip(192,168,2,177);”
回复 支持 反对

使用道具 举报

发表于 2015-8-5 21:46:09 | 显示全部楼层
乘风破浪 发表于 2015-3-4 21:47
你好,请问下你的这个温湿度采集实验除了 arduino开发板,DHT11传感器,还需要哪些模块,可以看下你的硬件原 ...

除了 arduino开发板,DHT11传感器,还需要一个w5100网络模块
回复 支持 反对

使用道具 举报

发表于 2015-8-6 20:24:58 | 显示全部楼层
13189359450 发表于 2015-8-5 21:44
只要和这个对应就行了“IPAddress ip(192,168,2,177);”

O(∩_∩)O谢谢
回复 支持 反对

使用道具 举报

发表于 2015-12-27 21:41:09 | 显示全部楼层
504835618 发表于 2014-9-5 12:13
实验成功,但感觉刷新有点慢

你好 问下W5100怎么连接网络的 必须通过路由器吗
回复 支持 反对

使用道具 举报

发表于 2015-12-28 17:45:40 | 显示全部楼层
爱上即可 发表于 2015-12-27 21:41
你好 问下W5100怎么连接网络的 必须通过路由器吗

当然要通过路由器了
回复 支持 反对

使用道具 举报

发表于 2016-4-2 19:14:29 | 显示全部楼层
请问,这个温度和湿度显示实验, 是在哪个网站上显示的?????
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-19 00:49 , Processed in 0.052167 second(s), 27 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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