- Tue Feb 02, 2016 9:40 am
#40268
Hello,
first of all thanks to all the contributors to this (and the others) topic, it 's of great help for newcomers to this exciting IoT world.
As said, I'm new and before asking for help usually browse a lot searching for answers, so here I am with my problems:
- I have a MQTT / Node Red server working in an Ubuntu VM
- I have flashed my ESP8266 (wemos mini D1 and NodeMCU) with different versions of firmware to find an exit to the bottleneck (using the Windows ESP8266 flasher). Currently NodeMCU 0.9.6 build 20150704 floating on it.
- I have adapted to my setup the init.lua and transferred it, together with dht22.lua through Esplorer (also compiled dht22.lua) as found in post #1
- I have a DHT22 connected to the ESP8266 in a breadboard
My ESP connects to the MQTT server and nothing more, errors shown as follows:
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4
> Connected to MQTT:192.168.1.140:1883 as ESP1
PANIC: unprotected error in call to Lua API (init.lua:44: attempt to perform arithmetic on global 'humid' (a nil value))
PANIC: unprotected error in call to Lua API (attempt to perform arithmetic on a nil value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to concatenate a nil value)
PANIC: unprotected error in call to Lua API (attempt to call a string value)
��!�)�f�&!�F�ʰ���
My codes:
init.lua
Code: Select all-- Engineered by MikeV, modded by rutierut
t=require("dht22")
broker = "192.168.1.140" -- IP or hostname of MQTT broker
mqttport = 1883 -- MQTT port (default 1883)
userID = "" -- username for authentication if required
userPWD = "" -- user password if needed for security
clientID = "ESP1" -- Device ID
GPIO2 = 4 -- IO Index of GPIO2 which is connected to an LED
count = 0 -- Test number of mqtt_do cycles
mqtt_state = 0 -- State control
wifi.setmode(wifi.STATION)
wifi.sta.config("myssi","mypw")
wifi.sta.connect()
function dhtsensor()
dht22 = require("dht22")
dht22.read(GPIO2)
temp = dht22.getTemperature()
humid = dht22.getHumidity()
end
function mqtt_do()
count = count + 1 -- For testing number of interations before failure
if mqtt_state < 5 then
mqtt_state = wifi.sta.status() --State: Waiting for wifi
elseif mqtt_state == 5 then
m = mqtt.Client(clientID, 120, userID, userPWD)
m:connect( broker , mqttport, 0,
function(conn)
print("Connected to MQTT:" .. broker .. ":" .. mqttport .." as " .. clientID )
mqtt_state = 20 -- Go to publish state
end)
elseif mqtt_state == 20 then
mqtt_state = 25 -- Publishing...
dhtsensor() -- Getting values
m:publish("Testtopic","Temperature: "..((temp-(temp % 10)) / 10).."."..(temp % 10).." C, Humidity: "..(humid / 10).."."..(humid % 10).."%", 0, 0,
function(conn)
-- Print confirmation of data published
print(" Sent messeage #"..count.."\nTemp:"..((temp-(temp % 10)) / 10).."."..(temp % 10).."\nHumidity: "..(humid/10).."."..(humid % 10).."\npublished!")
mqtt_state = 20 -- Finished publishing - go back to publish state.
end)
else print("Publishing..."..mqtt_state)
mqtt_state = mqtt_state - 1 -- takes us gradually back to publish state to retry
end
end
-- release module
dht22 = nil
package.loaded["dht22"] = nil
tmr.alarm(0, 10000, 1, function() mqtt_do() end) -- convert 10000 to dynamic variable
the dht22.lua is the untouched ver. by Javier Yanez, as found in post #1.
I'm stuck with the errors loop, cannot find a solution for that.
Any hints?
thanks, regards
gm