Hello friends, I hope you all are doing great. Today, we will create a wifi temperature monitoring system. For reading, we will use the DS18B20 sensor. For data processing and webpage creation, we will use our already known ESP8266.
The project will be built as follows:
But first, let's know a little about the sensor and the communication model it uses.
Where To Buy? | ||||
---|---|---|---|---|
No. | Components | Distributor | Link To Buy | |
1 | ESP8266 | Amazon | Buy Now |
For this project, we will need the following items: For this project, we will need the following items:
VDD operates with values from 3V to 5.5V and can even be omitted. The sensor has a Parasite Mode, using only the DQ and GND pins, and its power comes from the communication pin. This mode works well but is more susceptible to noise.
Data communication takes place over the 1-Wire (OneWire) protocol, using the DQ pin. We'll discuss the protocol later, but now it's important to know that, despite having only one wire, it allows two-way communication.
The reading is performed actively, the microcontroller sends a command and receives back a packet with the information.
In addition to the reading request, the sensor can also receive alarm configuration and data format commands. The DallasTemperature library already handles most of this for us. Including offering us some additional features, such as receiving reading in Faraday.
The most common models on the market are found in the TO-92 package (looks like a transistor) and the waterproof package. This second is more common due to its practical application, 1m long cable with stainless steel tip. It can be used to control water temperature, for example. The reading is performed actively, the microcontroller sends a command and receives back a packet with the information.
In addition to the reading request, the sensor can also receive alarm configuration and data format commands. The DallasTemperature library already handles most of this for us. Including offering us some additional features, such as receiving reading in Faraday.
The most common models on the market are the TO-92 package (looks like a transistor) and the waterproof package. This second is more common due to its practical application, 1m long cable with stainless steel tip. It can be used to control water temperature, for example.
OneWire (or 1-Wire) is a communication method designed by Dallas Semiconductor that transmits data using just one line, with a system of signaling who sends and when.
The method is very similar to i2C, but it has a much more limited data transfer speed. Another difference is that in the 1-wire case, it is possible to omit the power pin, using the data pin in parasite mode (by now, you may have noticed that despite the name, the method needs at least two wires: Data and GND).
Communication is done in master-slave mode, in which the microcontroller sends all requests, and the other devices only send data when nominally requested.
Each device has a unique address/name. This allows us to connect multiple devices on the same data line. The requests are sent in broadcast, the device that recognizes itself in it responds.
The circuit for our application is simple. We will connect the VDD pin of the sensor to the 3V3 of the NodeMCU, GND with GND, and we will use the D4 pin for the sensor data. It could be any other digital pin.
Additionally, a 4k7 ohm resistor must be placed between the data pin and the 3V3 to increase stability.
Next, we start a OneWire object on pin D4 and create a sensor using that object. From that moment on, the “sensors” object has all the properties and functions offered by the DallasTemperature library.
And we will make use of two functions Search(), which performs a search for devices in OneWire, and reset_search() which restarts this search.
What our code does is start a search, save the result in the addr variable and, if the variable is not empty, write it in the serial.
We found the result on the Serial Monitor. If there are other devices, they will appear here too. Keep the address, we'll need it.
Now that we know the sensor's address. Let's start our main code for reading the temperature. The objective here is to start the sensor and take a reading every 10s.
We started in the same way, but this time we created the sensor1 variable with the collected address.
In the readDs18b20() function we will use two functions:
Inside the Setup() function we started the sensor with the begin() function, it performs a reading automatically, if you didn't make new requests, the sensor would still respond to the getTemp() function, but with an outdated value.
In the loop, we have a timer with the millis() function so that the reading takes place every 10s.
Note that on line 15, we added one more parameter to the Serial.println() function. With that, we define the number of decimal places.
With our reading ready, let's create a web page to view this information in the browser. Remember that later we will put these files in FLASH ESP8266 with SPIFFS.
We will build the following screen:
The page structure is not the focus of this article, but basically, we have the index.html file creating the page itself and triggering a javascript function to update the reading.
The style.css file improves the appearance of the page but does not interfere with its functioning.
Both files must be in the data folder within the project folder and be transferred using the ESP8266 Sketch Data Upload.
With the page saved to FLASH, we need to create the structure to serve the page.
This step is nothing new for us, but it is worth noting a few points.
Now the readDs18b20() function also updates a variable of type String. We do this because server call-back functions do not accept integer or float variables.
For the server, we have three routes:
The DS18B20 is an extremely efficient and easy-to-use sensor. The application we developed today could be used to monitor the ambient temperature, or perhaps the temperature of a water reservoir. And the ESP8266 extends the range of that monitoring as far as we want.