Continué à optimiser la RAM en ne chargeant les functions pour la mesure et l'envoi au Cloud que quand on en a besoin

This commit is contained in:
Christian Zufferey
2020-01-19 14:41:12 +01:00
parent 11630c1395
commit f14a9bae68
6 changed files with 78 additions and 57 deletions

View File

@@ -1,14 +1,20 @@
-- Petit script pour faire office de crontab pour les mesures
print("\n 0_cron.lua zf200119.1009 \n")
print("\n 0_cron.lua zf200119.1436 \n")
cron1=tmr.create()
cron1:alarm(20*1000, tmr.ALARM_AUTO, function()
if verbose then print("cron1........................") end
if verbose then gpio.write(zLED, gpio.LOW) tmr.delay(10000) gpio.write(zLED, gpio.HIGH) end
ztemp1=readTemp() zhum1=readHumi()
if verbose then print("Temperature: "..ztemp1.." °C") end
if verbose then print("Humidity: "..zhum1.." %") end
zurl=thingspeak_url.."field1="..tostring(ztemp1).."&field2="..tostring(zhum1)
send_data() ztemp1=nil zhum1=nil r=nil
if verbose then print(node.heap()) collectgarbage() print(node.heap()) end
f = "0_htu21d.lua" if file.exists(f) then dofile(f) end
zurl = thingspeak_url.."field1="..tostring(ztemp1).."&field2="..tostring(zhum1)
f = "0_send_data.lua" if file.exists(f) then dofile(f) end
ztemp1=nil zhum1=nil
if verbose then print(node.heap()) end
collectgarbage()
if verbose then print(node.heap()) end
end)

View File

@@ -1,42 +1,43 @@
-- Lit le capteur I2C HTU21D de mesure d'humidité et de température
print("\n 0_htu21d.lua zf200119.0957 \n")
print("\n 0_htu21d.lua zf200119.1426 \n")
-- module https://learn.sparkfun.com/tutorials/htu21d-humidity-sensor-hookup-guide/all
-- acahat https://www.aliexpress.com/item/32480177429.html
-- schéma https://github.com/sparkfun/HTU21D_Breakout/blob/master/hardware/SparkFun_HTU21D_Breakout.pdf
-- data sheet https://cdn.sparkfun.com/assets/6/a/8/e/f/525778d4757b7f50398b4567.pdf
-- Comparaison DHT22, AM2302, AM2320, AM2321, SHT71, HTU21D, Si7021, BME280
-- http://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html
-- théorie http://electromag1.wifeo.com/capteurs-de-temperature-et-humidite-gy-21-et-si7021.php
-- source lua: https://github.com/tebben/NodeMCU
-- https://github.com/zuzu59/NodeMCU_Lua/tree/master/Mesures/humidity/bolo-thingspeak/docu/HTU21D.txt
-- ATTENTION: il n'y a seulement que certaines combinaisons de pins qui fonctionnent avec le capteur HTU21D avec le NodeMCU !
function readHTU21D()
id = 0 sda = 5 scl = 6 addr = 0x40
HUMIDITY = 0xE5 TEMPERATURE = 0xE3
i2c.setup(id, sda, scl, i2c.SLOW) sda = nil scl = nil
id = 0 sda = 5 scl = 6 addr = 0x40
HUMIDITY = 0xE5 TEMPERATURE = 0xE3
i2c.setup(id, sda, scl, i2c.SLOW) sda = nil scl = nil
function read_HTU21D(zreg, zdelay)
i2c.start(id) i2c.address(id, addr, i2c.TRANSMITTER)
i2c.write(id, zreg) i2c.stop(id)
i2c.start(id) i2c.address(id, addr, i2c.RECEIVER)
tmr.delay(zdelay)
r = i2c.read(id,3) i2c.stop(id)
return r
end
function read_HTU21D(zreg, zdelay)
i2c.start(id) i2c.address(id, addr, i2c.TRANSMITTER)
i2c.write(id, zreg) i2c.stop(id)
i2c.start(id) i2c.address(id, addr, i2c.RECEIVER)
tmr.delay(zdelay)
r = i2c.read(id,3) i2c.stop(id)
return r
function readTemp()
r = read_HTU21D(TEMPERATURE, 50000)
r = (bit.band((bit.lshift(string.byte(r,1),8)+string.byte(r,2)),0xfffc)*17572)/65536-4685
return tonumber(string.format("%.1f", tostring(r/100)))
end
function readHumi()
r = read_HTU21D(HUMIDITY, 16000)
r = (bit.band((bit.lshift(string.byte(r,1),8)+string.byte(r,2)),0xfffc)*12500)/65536-600
return tonumber(string.format("%.1f", tostring(r/100)))
end
ztemp1=readTemp() zhum1=readHumi()
if verbose then print("Temperature: "..ztemp1.." °C") end
if verbose then print("Humidity: "..zhum1.." %") end
id=nil sda=nil scl=nil addr=nil HUMIDITY=nil TEMPERATURE=nil r=nil
read_HTU21D=nil readTemp=nil readHumi=nil readHTU21D=nil
if verbose then print(node.heap()) end
collectgarbage()
if verbose then print(node.heap()) end
end
function readTemp()
r = read_HTU21D(TEMPERATURE, 50000)
r = (bit.band((bit.lshift(string.byte(r,1),8)+string.byte(r,2)),0xfffc)*17572)/65536-4685
return tonumber(string.format("%.1f", tostring(r/100)))
end
function readHumi()
r = read_HTU21D(HUMIDITY, 16000)
r = (bit.band((bit.lshift(string.byte(r,1),8)+string.byte(r,2)),0xfffc)*12500)/65536-600
return tonumber(string.format("%.1f", tostring(r/100)))
end
print("Temperature: "..readTemp().." °C")
print("Humidity: "..readHumi().." %")
r = nil
readHTU21D()

View File

@@ -1,5 +1,5 @@
-- Petit script pour envoyer les valeurs de température sur un serveur WEB via un HTTP GET
print("\n 0_send_data.lua zf200119.1009 \n")
print("\n 0_send_data.lua zf200119.1433 \n")
function send_data()
if verbose then print("send_data_web: ") end
@@ -14,8 +14,10 @@ function send_data()
end
zurl=nil
end)
zurl=nil send_data=nil
if verbose then print(node.heap()) end
collectgarbage()
if verbose then print(node.heap()) end
end
--[[
send_data()
]]

View File

@@ -1,9 +1,10 @@
# Quelques commandes remote (luatool) à envoyer avec le plugin Atom-IDE-terminal de l'éditeur Atom
# zf200119.1034
# zf200119.1043
Todo à faire pour ce projet !
- faire que send_data et read_temp soient lancés à la volée afin d'économiser de la RAM quand ce n'est pas utilisé
- arrêter de demander de connecter le WIFI automatiquement en mode station juste après le boot dans la config wifiinit (automatic connect)
- lancement ou arrêt du WEBIDE via la home page
- le wifi setup ne fonctionne toujours pas quand il y a déjà un ap de connecté (problème du reboot quand adrs ip ok)!
@@ -81,11 +82,13 @@ telnet -rN $zIP $zport
verbose=false
~.
=node.heap()
collectgarbage()
=node.heap()
dofile("dir.lua")
dir()
collectgarbage()
=node.heap()
for k,v in pairs(_G) do print(k,v) end
dofile("wifi_info.lua")

View File

@@ -1,19 +1,18 @@
-- Scripts à charger après le boot pour démarrer son projet
print("\n boot.lua zf20015.1700 \n")
print("\n boot.lua zf20019.1438 \n")
function boot()
verbose = true
print("booooooooooot...")
print(node.heap()) collectgarbage() print(node.heap())
f= "0_htu21d.lua" if file.exists(f) then dofile(f) end
f= "0_send_data.lua" if file.exists(f) then dofile(f) end
f= "0_cron.lua" if file.exists(f) then dofile(f) end
--f= "web_ide2.lua" if file.exists(f) then dofile(f) end
f=nil boot=nil
verbose = true
f="0_htu21d.lua" if file.exists(f) then dofile(f) end
zurl=thingspeak_url.."field1="..tostring(ztemp1).."&field2="..tostring(zhum1)
f="0_send_data.lua" if file.exists(f) then dofile(f) end
f="0_cron.lua" if file.exists(f) then dofile(f) end
--f = "web_ide2.lua" if file.exists(f) then dofile(f) end
f=nil boot=nil
end
boot()

View File

@@ -0,0 +1,10 @@
-- module https://learn.sparkfun.com/tutorials/htu21d-humidity-sensor-hookup-guide/all
-- achat https://www.aliexpress.com/item/32480177429.html
-- schéma https://github.com/sparkfun/HTU21D_Breakout/blob/master/hardware/SparkFun_HTU21D_Breakout.pdf
-- data sheet https://cdn.sparkfun.com/assets/6/a/8/e/f/525778d4757b7f50398b4567.pdf
-- Comparaison DHT22, AM2302, AM2320, AM2321, SHT71, HTU21D, Si7021, BME280
-- http://www.kandrsmith.org/RJS/Misc/Hygrometers/calib_many.html
-- théorie http://electromag1.wifeo.com/capteurs-de-temperature-et-humidite-gy-21-et-si7021.php
-- source lua: https://github.com/tebben/NodeMCU
-- ATTENTION: il n'y a seulement que certaines combinaisons de pins qui fonctionnent avec le capteur HTU21D avec le NodeMCU !