带DHT11传感器的ESP8266温湿度Web服务器

所述ESP8266 ESP-01被用作从所述Web服务器以显示温度和湿度信息DHT11传感器

步骤1:零件清单

  • ESP8266 ESP-01
  • DHT11温湿度传感器
  • 4.7k电阻
 
 

步骤2:ESP8266 + DHT11  传感器接线

 

步骤3:将代码上传到ESP8266

 
**将“ ssid ”和“ password ” 更改为您的无线网络**
编译并上传代码至ESP8266。

#include"ESP8266WiFi.h" #include"DHT11.h" const char* ssid = "ssid"; // Your ssid const char* password = "password"; // Your Password int pin = 2; WiFiServer server(80); DHT11 dht11(pin); double Fahrenheit(double celsius) { return ((double)(9 / 5) * celsius) + 32; } double Kelvin(double celsius) { return celsius + 273.15; } void setup() { Serial.begin(115200); delay(10); Serial.println(); // Connect to WiFi network WiFi.mode(WIFI_STA); Serial.println(); 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"); // Start the server server.begin(); Serial.println("Server started"); // Print the IP address Serial.println(WiFi.localIP()); } void loop() { int err; float temp, humi; if ((err = dht11.read(humi, temp)) == 0) { Serial.print("temperature:"); Serial.print(temp); Serial.print(" humidity:"); Serial.print(humi); Serial.println(); } else { Serial.println(); Serial.print("Error No :"); Serial.print(err); Serial.println(); } WiFiClient client = server.available(); 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(""); client.println(""); client.println("n"); client.println("ESP8266 Temperature & Humidity DHT11 Sensor"); client.println("n"); client.println("

ESP8266 & DHT11 Sensor

");
client.println("Humidity / Temperature");
client.println("
");
client.print("Humidity (%)         : ");
client.println((float)humi, 2);
client.print("Temperature (°C)  : ");
client.println((float)temp, 2);
client.print("Temperature (°F)  : ");
client.println(Fahrenheit(temp), 2);
client.print("Temperature (°K)  : ");
client.println(Kelvin(temp), 2);
client.println("

“);
client.println(“www.elec-cafe.com”);
client.print(“n

 

步骤4:串行监视器

检查ESP8266 Module的IP地址。

步骤5:Web监视器

通过ip地址打开Internet浏览器以显示温度和湿度信息
(至“自动刷新5秒”的页面)

人已赞赏
免费项目

ESP32:使用ESP32 DIY电机驱动器

2019-11-17 23:04:41

免费项目Arduino

基于ESP32的MQTT远程喂狗器

2020-6-6 12:09:11

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索