📜 ⬆️ ⬇️

Wi-Fi CO2 meter on ESP8266 + K-30


In this article, we will continue experiments with the Wi-Fi module ESP8266 and try to implement a poll of the CO2 sensor K-30 via MODBUS.

In the first part, we requested NodeMCU in ESP8266, connected the DS18b20 and learned how to send temperature data to the cloud. Let's try to add a poll of the CO2 sensor.
The most interesting thing to implement all the functionality in ESP8266 without connecting an external controller

Hardware and connection
Add to the configuration of the previous article, the sensor K-30 .
We connect the UART RX, UART TX and GND on the K-30 and ESP8266 sensors. Meals will be different.
The difficulty lies in the fact that the UART at ESP8266 only one, so that either the console and debug, or K-30
Attention! Powered by K-30 5-14V, by ESP8266 - 3.3V.
I solved this problem by applying the converter 5-> 3.3V


Software part
')
1) init.lua - almost unchanged
--init.lua print("Setting up WIFI...") wifi.setmode(wifi.STATION) --modify according your wireless router settings wifi.sta.config("YOUR_SSID","YOUR_PASSWD") 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("all.lua") end end) 


2) all.lua - code for polling sensors and sending data to the server
 pin = 6 t = 0 function read(addr, unit) ow.setup(pin) result = nil flag = false if(addr == nil) then ow.reset_search(pin) count = 0 repeat count = count + 1 addr = ow.search(pin) tmr.wdclr() until((addr ~= nil) or (count > 100)) ow.reset_search(pin) end if(addr == nil) then return result end crc = ow.crc8(string.sub(addr,1,7)) if (crc == addr:byte(8)) then if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then ow.reset(pin) ow.select(pin, addr) ow.write(pin, 0x44, 1) present = ow.reset(pin) ow.select(pin, addr) ow.write(pin,0xBE,1) data = nil data = string.char(ow.read(pin)) for i = 1, 8 do data = data .. string.char(ow.read(pin)) end crc = ow.crc8(string.sub(data,1,8)) if (crc == data:byte(9)) then t = (data:byte(1) + data:byte(2) * 256) * 625 t = t / 10000 return t end tmr.wdclr() else end else end return result end function sendt(t,co,key) print("Temp:"..t.." C\n") conn=net.createConnection(net.TCP, 0) conn:on("receive", function(conn, payload) print(payload) end) conn:connect(80,'184.106.153.149') conn:send("GET /update?key="..key.."&field1="..t.."&field2="..co.." 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 uart.setup(0,9600,8,0,1,0) uart.on("data", 0, function(data) --print("receive from uart:", data) if string.len(data) == 7 then result = string.byte(data,4)*256 + string.byte(data,5) print (result) sendt(t,result,"YOUR_KEY") end end, 0) function sendData() t = read() uart.write(0,0xFE,0x04,0x00,0x03,0x00,0x01,0xD5,0xC5) end tmr.alarm(0, 60000, 1, function() sendData() end ) 


Work algorithm:

It did not work out on separate files, there was not enough memory, so I implemented it as I could. Lua for me is still a dark forest.

results


It turned out very interesting device. Power made from USB, respectively, you can take anywhere. PowerBank today is almost everyone. As for Wi-Fi, we are raising a point on the phone and our sensor is back online.

It feels like - really there is discomfort when the CO2 level is in excess of 800-1000 ppm.

Source: https://habr.com/ru/post/258137/


All Articles