First try with WiFi module ESP8266 (ESP-07) produced by Espressif. Actually first trying to use this module I have performed at last year. But then my experiments with module end with some firmware malfunction. At this time all goes more or less fine. The first thing which I made after purchasing the module was upload a NodeMCU firmware. This firmware allows me to use the lua-scripts for programming the ESP8266. Since previous year NodeMCU has a lot of changes. A lot of sensors have a support which is built-in NodeMCU. So data from these sensors could be read with use embedded functions without any third-party libraries.
Code for work with AM2302 (another name DHT22) becomes easier. Before it was necessary to use additional libraries in separate or embedded module. But now it is enough only define the GPIO pin of ESP8266 connected to the data output from the DHT22 and then make software data read with embed function dht.read(). The entire code looks like it presents below.
pin = 7 status, temp, humi, temp_dec, humi_dec = dht.read(pin) if status == dht.OK then -- Integer firmware using this example print(string.format("%d.%d;%d.%d\r\n", math.floor(temp), temp_dec/100, math.floor(humi), humi_dec/100 )) elseif status == dht.ERROR_CHECKSUM then print( "DHT Checksum error." ) elseif status == dht.ERROR_TIMEOUT then print( "DHT timed out." ) end
Interesting that function dht.read() not only read the temperature/humidity data from DHT22 but also can return the condition of data-flow from sensor. And depending on this condition can return the values dht.OK – in case if all OK, dht.ERROR_CHECKSUM and dht.ERROR_TIMEOUT in case if it something wrong.
The NodeMCU readme claims that the last version of NodeMCU contains the built-in function for work with bmp085/bmp180 sensor. But my trying to use functions bmp085.temperature() and bmp085.pressure() was not really successful. I’m actually not sure that it is a problem of firmware more likely, it’s my fault. Maybe I not so carefully read the instruction or simply forgot to enable this option prior the building firmware.
Therefore I made all further work with sensor with using additional code which make communication with BMP085 with using embedded i2c interface function. As a result I have made the next code.
bmp085 = require("BMP") sda_pin = 1 scl_pin = 2 pin = 7 -- initialize the BMP085 and reads data from it bmp085.init(sda_pin, scl_pin) p = bmp085.getUP(oss) -- read DHT22 status, temp, humi, temp_dec, humi_dec = dht.read(pin) if status == dht.OK then -- Integer firmware using this example print(string.format("%d.%d;%d.%d,%d", math.floor(temp), temp_dec/100, math.floor(humi), humi_dec/100, p )) elseif status == dht.ERROR_CHECKSUM then print( "DHT Checksum error." ) elseif status == dht.ERROR_TIMEOUT then print( "DHT timed out." ) end -- preparing string variables for server и thingspeak scripts h = (math.floor(humi)).."."..(humi_dec/100) t = (math.floor(temp)).."."..(temp_dec/100) press = (math.floor((p*75)/10000)).."."..(((p*75)%10000)/1000) print(string.format("%s;%s,%s\r\n",t,h,press)) -- clean garbage bmp085 = nil package.loaded["BMP"]=nil collectgarbage()
The line bmp085 = require(«BMP») is actually request the additional file BMP.lua which is make all necessary work with BMP085 via i2c interface.
At the end here is the table of correspondence between hardware GPIO and NodeMCU pin number (io index).
IO index | ESP8266 pin | IO index | ESP8266 pin |
---|---|---|---|
0 [*] | GPIO16 | 7 | GPIO13 |
1 | GPIO5 | 8 | GPIO15 |
2 | GPIO4 | 9 | GPIO3 |
3 | GPIO0 | 10 | GPIO1 |
4 | GPIO2 | 11 | GPIO9 |
5 | GPIO14 | 12 | GPIO10 |
6 | GPIO12 |