This commit is contained in:
Татьяна Фарбер 2026-03-09 14:13:34 +04:00
parent a7a2dcbc7a
commit f9d39e533e
2 changed files with 43 additions and 52 deletions

View File

@ -1,5 +1,9 @@
# Бродильная камера на ESP 8266
# Формат передачи данных
## Необходимые детали
1. Wemos D1 Mini

View File

@ -1,70 +1,69 @@
timing = 5000 -- Sending timer
local t = require("DS1820")
timing = 60000 -- Sending timer
srv = nil -- TCP client
uid = '' -- MAC UID
dhtpin = 1 -- DHT11 pin
sda, scl = 3, 4 -- BME280 pins
alt = 320 -- altitude of the measurement place
dhtpin_room = 1 -- DHT11 room pin
dhtpin_cellar = 2 -- DHT11 cellar pin
dhtpin_weather = 5 -- DHT11 weather pin
ds_pin = 6
local results = {} -- DS1822 results
-- Weather vars
T = - 300 -- BME temp
P = - 300 -- BME pressure
curAlt = - 300 -- BME altitude
TB = - 300 -- DHT temp
TH = - 300 -- DHT humidity
UF = 0 -- UF index
TB_room = - 300 -- DHT temp
TH_room = - 300 -- DHT humidity
TB_cellar = - 300 -- DHT temp
TH_cellar = - 300 -- DHT humidity
TB_weather = - 300 -- DHT temp
TH_weather = - 300 -- DHT humidity
-- Read the solar sensor
-- Read DS1822
function adc_read()
UF = adc.read(0)
print('UF index: '..UF)
local function readout(temp)
for addr, temp in pairs(temp) do
local uid = string.format('%s',('%02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X'):format(addr:byte(1,8)))
results[uid] = temp
print(uid,temp)
end
end
-- Read the BME280 sensor
function bme_read()
T, P, H, QNH = bme280.read(alt)
local Tsgn = (T < 0 and -1 or 1); T = Tsgn*T
print(string.format("T=%s%d.%02d", Tsgn<0 and "-" or "", T/100, T%100))
print(string.format("QFE=%d.%03d", P/1000, P%1000))
print(string.format("QNH=%d.%03d", QNH/1000, QNH%1000))
P = bme280.baro()
curAlt = bme280.altitude(P, QNH)
local curAltsgn = (curAlt < 0 and -1 or 1); curAlt = curAltsgn*curAlt
print(string.format("altitude=%s%d.%02d", curAltsgn<0 and "-" or "", curAlt/100, curAlt%100))
local function read_sensors()
t:read_temp(readout, ds_pin, t.C)
end
-- Read the DHT11 sensor
function dht_read()
status, temp, humi, temp_dec, humi_dec = dht.read(dhtpin)
local function dht_read(dhtpin)
local status, temp, humi, temp_dec, humi_dec = dht.read(dhtpin)
if status == dht.OK then
TB = temp
TH = humi
print("DHT Temperature:"..temp..";".."Humidity:"..humi)
return {['temp'] = temp, ['humi'] = humi}
elseif status == dht.ERROR_CHECKSUM then
print( "DHT Checksum error." )
elseif status == dht.ERROR_TIMEOUT then
print( "DHT timed out." )
end
return {}
end
-- Prepare data for TCP server
-- Data for TCP server
function data_out()
read_sensors()
local pack = {}
pack['id'] = uid
pack['bme'] = {['temp'] = T,['press'] = P,['alt'] = curAlt}
pack['dht'] = {['temp'] = TB,['humi'] = TH}
pack['uf'] = UF
pack['room'] = dht_read(dhtpin_room)
pack['cellar'] = dht_read(dhtpin_cellar)
pack['weather'] = dht_read(dhtpin_weather)
pack['sensors'] = results
return encoder.toBase64(sjson.encode(pack))
end
-- Exchange data with server
function tcp()
srv:connect(4999,"braga.ulgrad.ru")
local function send()
srv:connect(4999,"chan.ulgrad.ru")
srv:on("connection", function(sck, c)
print('connection')
sck:send(data_out().."\r\n")
@ -100,30 +99,18 @@ function on_connect()
end
end
-- Setup ADC
if adc.force_init_mode(adc.INIT_ADC)
then
node.restart()
return -- don't bother continuing, the restart is scheduled
end
-- Setup sensors
i2c.setup(0, sda, scl, i2c.SLOW) -- call i2c.setup() only once
bme280.setup()
-- Setup timers
tcptimer = tmr.create()
tcptimer:register(12000, tmr.ALARM_AUTO,tcp)
tcptimer:register(timing, tmr.ALARM_AUTO,send)
inittimer = tmr.create()
inittimer:register(5000, tmr.ALARM_SINGLE, function()
adc_read()
bme_read()
dht_read()
enduser_setup.start('meteo',on_connect)
enduser_setup.start('chan',on_connect)
end)
inittimer:start()