IoT: ESP8266 and DS18B20 temperature sensor

Based on a budget WIFI module ESP8266 you can easily and quickly develop IoT devices. Examples of such devices can be wireless sensors of temperature, humidity, pressure, illumination. To do this, just update the firmware of the module and connect the corresponding sensors. Additional microcontrollers are not needed.
The firmware currently supports reading sensors DHT11/22 BMP085/180, BH1750, DS18B20, AM2321 and others. There is a function of transmitting testimonies to the site of popular monitoring, to the metering service ThingSpeak.com, MQTT server, to the MajorDoMo smart home system, to Benux computer control system. It is possible to control the state of free GPIOs to control loads (relays), read the status of GPIO inputs, display data on an LCD or OLED screen, and many other functions.
An example of a sensor connection scheme for evaluating the ease of assembly of a device is shown in Figure 1.

esp8266_ds18b20_dht11_bmp180
Figure 1 – An example of connecting sensors to ESP8266

The actual and correct connection diagram may differ (for example, it is advisable to supply 5V for the DHT22 sensor). It is also not recommended to use ESP-01. Recommended modules ESP-07 and ESP-12 and others, with the maximum derived GPIO. For a more complete use of the firmware, modules with a flash memory of 1 megabyte or more are recommended, a re-soldering of the memory chip is possible.
Let’s take a closer look at an example of connecting a company temperature sensor DALLAS DS18B20 (рис.2) to ESP8266 to create an IoT device Wi-Fi thermometer.

ds18b20
Figure 2 – Appearance of the sensor DS18B20

We connect power to the ESP8266. We also give a plus to the contact CH_PD. Attention! If you are not using ESP-01, you must file GPIO15 – GND; GPIO2 -3.3V. ESP-01 has already done this. We connect RX and TX to the USB-TTL converter for flashing and debugging the code (Fig. 3).

usb_uart_esp8266
Figure 3 – ESP8266 firmware using a converter

We connect the DS18B20 to the ESP8266. If you look at the markings: left – ground, middle – signal (GPIO12 on ESP-03), if you have ESP-01, connect to GPIO0, right – power (3.3V). Between the power and the signal line, it is desirable to connect a 4.7 kOhm resistor (Fig. 4).

esp8266_ds18b20
Figure 4 – Connection DS18B20 to ESP8266

thingspeak.com
Register at https://thingspeak.com/, create a channel, copy the 16-character key from there. We need it to send temperature data to the cloud.
firmware of NodeMCU
NodeMCU (https://github.com/nodemcu/nodemcu-firmware) – is a firmware that allows you to run Lua scripts on ESP8266. Download the latest NodeMCU Flasher (https://github.com/nodemcu/nodemcu-flasher) and run it.
Connect GPIO0 to ground. We select the desired COM port and the speed is 74880 or 115200. Press Flash and distort the power to the ESP8266. If the firmware does not start, we check the correctness of the RX, TX connection by starting PuTTY at a speed of 74880. At the moment of power supply to the console, a line like “ets Jan 8 2013, rst cause: 1, boot mode: (1,0)“. After the correct firmware, disconnect GPIO0 from the ground.


Figure 5 – Firmware of ESP8266

Pouring Lua scripts and launch
Download and run LuaLoader (http://benlo.com/esp8266/index.html#LuaLoader). We select the COM port and speed 9600, if there is no connection, we try to distort the power supply of the ESP8266 module. Check the connection by clicking on the ChipID button. The chip identifier should be displayed in the console

= node.chipid()
10371968

We fill in the necessary files (button UploadFile):
1. ds18b20.lua – library for interacting with DS18B20;
2. httpsender.lua – our script that reads temperature data and sends it to thingspeak.com. Do not forget to replace YOURAPIKEY on your own key.
gpio = 6 – when connecting the sensor to GPIO12, in case GPIO0 – gpio = 3 (table – https://github.com/nodemcu/nodemcu-firmware/blob/master/README.md):

gpio = 6
require('ds18b20')
ds18b20.setup(gpio)

function sendData()
t=ds18b20.read()
print("Temp:"..t.." C\n")
-- conection to thingspeak.com
print("Sending data to thingspeak.com")
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
print(payload) end)
-- api.thingspeak.com 184.106.153.149
conn:connect(80,'184.106.153.149')
conn:send("GET /update?key=YOURAPIKEY&field1="..t.." HTTP/1.1\r\n") conn:send("Host: api.thingspeak.com\r\n")
conn:send("Accept: */*\r\n")
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
conn:send("\r\n")
conn:on("sent",function(conn)
print("Closing connection")
conn:close()
end)
conn:on("disconnection", function(conn)
print("Got disconnection...")
end)
end

-- send data every 60000 ms to thing speak
tmr.alarm(0, 60000, 1, function() sendData() end )

3. init.lua – script, initializes Wi-Fi and launches httpsender.lua. Do not forget to set your SSID and password for Wi-Fi:

print("Setting up WIFI...")
wifi.setmode(wifi.STATION)
--modify according your wireless router settings wifi.sta.config("SSID","SSIDPASSWD")
wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("httpsender.lua")
end

As result we have the following:

graph

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.