0
votes

I have a working init.lua on my nodemcu esp8266:

-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
dofile("credentials.lua")

function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
    else
        print("Running")
        file.close("init.lua")
        -- the actual application is stored in 'application.lua'
        -- dofile("application.lua")
    end
end

print("Connecting to WiFi access point...")
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID, PASSWORD)
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.create():alarm(1000, tmr.ALARM_AUTO, function(cb_timer)
    if wifi.sta.getip() == nil then
        print("Waiting for IP address...")
    else
        cb_timer:unregister()
        print("WiFi connection established, IP address: " .. wifi.sta.getip())
        print("You have 3 seconds to abort")
        print("Waiting...")
        tmr.create():alarm(3000, tmr.ALARM_SINGLE, startup)
    end
end)

It runs without errors and the wireless connection is established.

Now I have written a second bme280_mqtt.lua that I want to run automatically:

alt=45 -- altitude of the measurement place

bme280.init(3, 4)

P, T = bme280.baro()
-- convert measure air pressure to sea level pressure
QNH = bme280.qfe2qnh(P, alt)
ldk=string.format("Ld=%d.%03d ", QNH/1000, QNH%1000)

H, T = bme280.humi()
if T<0 then    
  temp=string.format("T=-%d.%02d°C ", -T/100, -T%100)
else
  temp=string.format("T=%d.%02d°C ", T/100, T%100)
end
luftf=string.format("Lf=%d%% ", H/1000, H%1000)
D = bme280.dewpoint(H, T)
if D<0 then
  taupu=string.format("Tp=-%d.%02d°C ", -D/100, -D%100)
else
  taupu=string.format("Tp=%d.%02d°C ", D/100, D%100)
end
m = mqtt.Client("wetterstation", 120)
m:connect("192.168.1.116", 1883)
m:publish("wetterstation",temp .. taupu .. luftf .. ldk,0,0)
node.dsleep(10*1000000)

Called by hand in ESPlorer via Send to ESP Button everything works fine.

But with

dofile(bme280_mqtt.lua)

I get:

dofile('bme280_mqtt.lua')
bme280_mqtt.lua:25: not connected
stack traceback:
    [C]: in function 'publish'
    bme280_mqtt.lua:25: in main chunk
    [C]: in function 'dofile'
    stdin:1: in main chunk

What is the mistake here? And how do I call the bme280_mqtt.lua from init.lua correctly?

Kind regards

1
Is this solved or do you need any more feedback? - Marcel Stör

1 Answers

1
votes

how do I call the bme280_mqtt.lua from init.lua correctly?

You do invoke it correctly.

bme280_mqtt.lua:25: not connected

Means that there's an error on/from line 25 from bme280_mqtt.lua.

I didn't count the lines but the problem is right here

m:connect("192.168.1.116", 1883)
m:publish("wetterstation",temp .. taupu .. luftf .. ldk,0,0)

You can only publish once the connection to the broker was established. Look at the example at http://nodemcu.readthedocs.io/en/latest/en/modules/mqtt/#example. You can either use the callback function in the connect function to publish or register an on-connect event handler before you call connect like so:

m:on("connect", function(client) 
  -- publish here
 end)