|
本帖最后由 zhaopengxslc 于 2014-9-4 22:23 编辑
- #include <SPI.h>
- #include <Ethernet.h>
- #include <EthernetClient.h>
- #include <EthernetServer.h>
- #include <DHT.h>
- #define DHTPIN 2
- #define DHTTYPE DHT11
- DHT dht(DHTPIN, DHTTYPE);
- byte mac[] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
- IPAddress ip(192,168,2,177);
- EthernetServer server(80);
- void setup() {
- Serial.begin(9600);
- Ethernet.begin(mac, ip);
- server.begin();
- dht.begin();
- }
- void loop() {
- delay(2000);
- // listen for incoming clients
- EthernetClient client = server.available();
- if (client) {
- // an http request ends with a blank line
- boolean currentLineIsBlank = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- // if you've gotten to the end of the line (received a newline
- // character) and the line is blank, the http request has ended,
- // so you can send a reply
- if (c == '\n' && currentLineIsBlank) {
- // send a standard http response header
- client.println("HTTP/1.1 200 OK");
- client.println("Content-Type: text/html");
- client.println("Connection: close"); // the connection will be closed after completion of the response
- client.println("Refresh: 5"); // refresh the page automatically every 5 sec
- client.println();
- client.println("<!DOCTYPE HTML>");
- client.println("<html>");
- // output the value of each analog input pin
- client.println("<head>");
- client.println("</head>");
- client.println("<body>");
-
- float h=dht.readHumidity();
- float t=dht.readTemperature();
- if(isnan(h) || isnan(t))
- {
- client.println("Error!");
- client.println("<br />");
- return;
- }
- client.print("Humidity:");
- client.println(h);
- client.print("Temperture:");
- client.print(t);
- client.println("<br />");
- client.println("<br />");
- client.println("</body>");
- client.println("</html>");
- break;
- }
- if (c == '\n') {
- // you're starting a new line
- currentLineIsBlank = true;
- }
- else if (c != '\r') {
- // you've gotten a character on the current line
- currentLineIsBlank = false;
- }
- }
- }
- // give the web browser time to receive the data
- delay(1);
- // close the connection:
- client.stop();
- }
- }
复制代码
连接方式
DHT11
pin1 5V
pin2 D2
pin3 悬空
pin4 GND
W5100作为web服务器
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|