ESP8266-Arduino网络编程实例-Web页面显示DS18B20数据

Web页面显示DS18B20数据

1、应用实例介绍

本次实例实现将DS18B20温度传感器数据在Web页面显示,实例主要由三个部分组成:

  • WiFi连接
  • DS18B20驱动及数据采集
  • Web服务器及客户端请求处理

在前面的文章中,对ESP8266的连接做了介绍,请参考:

  • ESP8266-Arduino开发实例-接入WiFi网络

在前面的文章中,对DS18B20的应用及驱动做了介绍,请参考:

在前面的文章中,对ESP8266实现Web服务器做了介绍,请参考:

  • ESP8266-Arduino开发实例-简单服Web务器

2、硬件准备

  • ESP8266 NodeMCU开发板一块
  • DS18B20传感器模块一个
  • 面板板一个
  • 杜邦线若干
  • 数据线一条

硬件接线图如下:

在这里插入图片描述

3、软件准备

  • Arduino IDE或VSCode + PlatformIO

在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:

ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:

4、代码实现

本次实例使用Web器驱动库如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <ESP8266WiFi.h>
#include <OneWire.h>

const char\* ssid = "\*\*\*\*\*";
const char\* ssid_pwd = "\*\*\*\*\*";

OneWire ds(D2);
WiFiServer server(80);

void setup()
{
Serial.begin(115200);
delay(10);

// Connect to WiFi network
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.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");

}

void loop()
{
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float celsius, fahrenheit;
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}

// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}

// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();

//now the DS18b20
if ( !ds.search(addr))
{
ds.reset\_search();
delay(250);
return;
}

if (OneWire::crc8(addr, 7) != addr[7])
{
Serial.println("CRC is not valid!");
return;
}

// the first ROM byte indicates which chip
switch (addr[0])
{
case 0x10:
type_s = 1;
break;
case 0x28:
type_s = 0;
break;
case 0x22:
type_s = 0;
break;
default:
Serial.println("Device is not a DS18x20 family device.");
return;
}

ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000);
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad

for ( i = 0; i < 9; i++)
{
data[i] = ds.read();
}

// Convert the data to actual temperature
int16\_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10)
{
raw = (raw & 0xFFF0) + 12 - data[6];
}
}
else
{
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms

}

celsius = (float)raw / 16.0;
fahrenheit = celsius \* 1.8 + 32.0;

// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<meta http-equiv=\"refresh\" content=\"5\">"); //refresh every 5 seconds


client.println("<H3>DS18b20 temperature example</h3>");
client.print("Temperature in Celsius = ");
client.println(celsius);
client.println("<br><br>");
client.print("Temperature in Celsius = ");
client.println(fahrenheit);
client.println("</html>");

delay(1);
Serial.println("Client disconnected");
Serial.println("");

}

文章来源: https://iotsmart.blog.csdn.net/article/details/127222850