Add draft for a NodeMCU lua sensor
This commit is contained in:
parent
7ec30730e1
commit
874a5c5a78
2 changed files with 88 additions and 0 deletions
11
lua-sensor/init.lua
Normal file
11
lua-sensor/init.lua
Normal file
|
@ -0,0 +1,11 @@
|
|||
FileToExecute="user.lua"
|
||||
l = file.list()
|
||||
for k,v in pairs(l) do
|
||||
if k == FileToExecute then
|
||||
print("*** You've got 5 sec to stop timer ***")
|
||||
tmr.alarm(0, 5000, 0, function()
|
||||
print("Executing ".. FileToExecute)
|
||||
dofile(FileToExecute)
|
||||
end)
|
||||
end
|
||||
end
|
77
lua-sensor/user.lua
Normal file
77
lua-sensor/user.lua
Normal file
|
@ -0,0 +1,77 @@
|
|||
-- MQTT connect script with deep sleep
|
||||
-- Remember to connect GPIO16 and RST to enable deep sleep
|
||||
|
||||
--- MQTT ---
|
||||
mqtt_broker_ip = "10.42.1.101"
|
||||
mqtt_broker_port = 1883
|
||||
mqtt_username = ""
|
||||
mqtt_password = ""
|
||||
mqtt_client_id = "therm1"
|
||||
mqtt_prefix = "maison/bureau"
|
||||
|
||||
--- WIFI ---
|
||||
wifi_cfg = {}
|
||||
wifi_cfg.ssid = "FLRLAN"
|
||||
wifi_cfg.pwd = "XXXXX"
|
||||
|
||||
client_ip=""
|
||||
client_netmask=""
|
||||
client_gateway=""
|
||||
|
||||
dht22_pin = 4
|
||||
|
||||
--- INTERVAL ---
|
||||
time_between_sensor_readings = 120000
|
||||
|
||||
-- Setup MQTT client and events
|
||||
m = mqtt.Client(mqtt_client_id, 120, mqtt_username, mqtt_password)
|
||||
|
||||
-- Connect to the wifi network
|
||||
wifi.setmode(wifi.STATION)
|
||||
wifi.sta.config(wifi_cfg)
|
||||
|
||||
if client_ip ~= "" then
|
||||
wifi.sta.setip({ip=client_ip,netmask=client_netmask,gateway=client_gateway})
|
||||
end
|
||||
|
||||
-- DHT22 sensor logic
|
||||
function get_sensor_data()
|
||||
local status, temp, humi, temp_dec, humi_dec = dht.read(dht22_pin)
|
||||
if status == dht.OK then
|
||||
local temp_str = string.format("%d.%03d", math.floor(temp), temp_dec);
|
||||
local humi_str = string.format("%d.%03d", math.floor(humi), humi_dec);
|
||||
print(string.format("DHT Temperature:%s ;Humidity:%s\r\n", temp_str, humi_str))
|
||||
return temp_str, humi_str
|
||||
elseif status == dht.ERROR_CHECKSUM then
|
||||
print( "DHT Checksum error." )
|
||||
return nil, nil
|
||||
elseif status == dht.ERROR_TIMEOUT then
|
||||
print( "DHT timed out." )
|
||||
return nil, nil
|
||||
end
|
||||
end
|
||||
|
||||
function loop()
|
||||
if wifi.sta.status() == 5 then
|
||||
print("Connected with " .. wifi.sta.getip())
|
||||
-- Stop the loop
|
||||
tmr.stop(0)
|
||||
m:connect( mqtt_broker_ip , mqtt_broker_port, 0, function(conn)
|
||||
print("Connected to MQTT")
|
||||
print(" IP: ".. mqtt_broker_ip)
|
||||
print(" Port: ".. mqtt_broker_port)
|
||||
print(" Client ID: ".. mqtt_client_id)
|
||||
print(" Username: ".. mqtt_username)
|
||||
-- Get sensor data
|
||||
local temperature, humidity = get_sensor_data()
|
||||
m:publish(mqtt_prefix .. "/temperature", temperature, 0, 0, function(conn)
|
||||
m:publish(mqtt_prefix .. "/humidity", humidity, 0, 0, function(conn)
|
||||
print("Going to deep sleep for "..(time_between_sensor_readings/1000).." seconds")
|
||||
node.dsleep(time_between_sensor_readings*1000)
|
||||
end)
|
||||
end)
|
||||
end )
|
||||
end
|
||||
end
|
||||
|
||||
tmr.alarm(0, 100, 1, function() loop() end)
|
Reference in a new issue