From 874a5c5a78721fa9615af753099ae10ec015dc1a Mon Sep 17 00:00:00 2001 From: Thomas Schwery Date: Sat, 28 Apr 2018 12:08:25 +0200 Subject: [PATCH] Add draft for a NodeMCU lua sensor --- lua-sensor/init.lua | 11 +++++++ lua-sensor/user.lua | 77 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 lua-sensor/init.lua create mode 100644 lua-sensor/user.lua diff --git a/lua-sensor/init.lua b/lua-sensor/init.lua new file mode 100644 index 0000000..6dee38e --- /dev/null +++ b/lua-sensor/init.lua @@ -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 diff --git a/lua-sensor/user.lua b/lua-sensor/user.lua new file mode 100644 index 0000000..6c67fef --- /dev/null +++ b/lua-sensor/user.lua @@ -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)