As the title says... Chat on...

User avatar
By sash99
#54777 hi there i wrote a simple program to simply past data from pre-existing arduino projects to IoT. but it seams to have a strange bug in lua for MQTT.

in your prexisting ardino project add this to it

Serial.print("cmd_1("); Serial.print(somevalue); Serial.println(")"); --to be captured by esp8266

and you connect the esp8266 to the tx of the the ardino ( or any serial device) and esp8266 captures and then rebroadcast to your IoT or webpage or data base
------
uart.setup(0,9600,8,0,1)
function cmd_1(value)
i= value
end
----------
works fine if producing a webpage or json.. but i was having some compatibility issues with json so I wanted to send as mqtt. and this is where it gets weird

if i run my scripts by "send to ESP" for MQTT it works fine if they are installed to flash they cause panics due to nill data--
so here my test out put running from "Send to ESP"( as flashed data would cause a panic and loop esp8266 if it loops just unplug your wifi router so it can not obtain an ip ( would not uses a static or as AP for testing - because even if you loop it and you reflash it it still looped afterward with MQTT module installed another strange bug) then restart esp8266 and format it and try again :) )
-------
uart.setup(0,9600,8,0,1)

function cmd_1(value)
print(value)
i= value
print(i)
payload_1=value
print(payload_1)
if pcall(payload_1) then

m:publish("/invert",payload_1,0,0, function(conn) print("sent") end) )

else
print(" error - nill value )
end
m:publish("/invert",payload_1,0,0 , function(conn) print("sent") end)) --- works with "send to ESP" but when not when loaded at boot - creates a panic )

end
-----------
here the output on the com window :
> cmd_1(146.00)
146 ( prints value)
146 ( prints i )
146 ( prints payload )
error - nil value ( pcall sees it as nil value does not matter if I use "value "," i " or "payload " as the input for pcall )
sent ( sent to mqtt broker - but only when ran from "send to ESP" - if run by an application through init if creates panic code due to nil value - but yet if a delete the mqtt section and display only a webpage or manipulate in in anyway. it does not create a panic condition- but yet any manipulation of data I ei multiply payload by ten then divide by ten it will print the result but will still be recognized as nil data by pcall and will cause panic in MQTT no where else just MQTT that i found )

it a very weird problem...

would anyone have a suggestion of what might be going wrong --
User avatar
By sash99
#54787 well figure out how to get it to work - it really is a bug in the firmware.( took a couple days to figure away around the bug and get things to work), I noticed after it did the pcall the value was no longer nil and it works, kind of strange but works fine. being running now for an hour maybe two sending data to a broker, where node-red sends it off to database, social media and a Iot more, even includes a simple quick access page for debugging on your private network but it seams to be sending data nicely to freeboard IoT via mqtt, and to some android Iot all work fine with it as I am using Mqtt with websockets

serial123.lau
Code: Select all


value_2=0
value=0
value_3=0
i=0
g=0
w=0

uart.setup(0,9600,8,0,1)

print ("uart.setup(0,9600,8,0,1) finished")
print ("waiting for serial data....")




function cmd_1(value)
-- print(value)
  i= value
 -- print(i)
  payload_1=(value)
 -- print(payload_1)
if pcall(payload_1) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
       m:publish("/invert",payload_1,0,0 )
       m:close()
 else
      print(" error -inverter" )
 end
--print(payload_1)
--m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
     --     m:subscribe("/invert",0)
            m:publish("/invert",payload_1,0,0 , function(conn) print("sent") end)
           -- m:close()
-- init mqtt client with keepalive timer 120sec



   
 
end



function cmd_2(value_2)

 -- print("cmd_2("..value_2..") received")
  g=value_2

payload_2=value_2
if pcall(payload_2) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
       m:publish("/grid",payload_2,0,0 )
       m:close()
 else
      print(" error - grid" )
 end
   
  --        m:subscribe("/invert",0)
         --   m:publish("/grid",payload_2,0,0 )
   m:publish("/grid",payload_2,0,0, function(conn) print("sent") end)

--m:close();   
   
 
end

function cmd_3(value_3)
 -- print("cmd_3("..value_3..") received")
  w=value_3
  payload_3=value_3
 if pcall(payload_3) then
m:connect("192.168.168.41", 1883, 0, function(conn) print("connected") end)
       m:publish("/wind",payload_1,0,0 )
       m:close()
 else
      print(" error - wind" )
 end
   
   --       m:subscribe("/invert",0)
          m:publish("/wind",payload_3,0,0, function(conn) print("sent") end)
         
   
end




srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
  conn:on("receive",function(conn, payload)
    print(payload)
    conn:send("HTTP/1.1 200 OK\nContent-Type: text/html\nRefresh: 10\n\n" ..
              "<!DOCTYPE HTML>" ..
              "<html><body>" ..
              "<h1>GTI LIMITER</h1>" ..
              "<b>Solar Output : " ..i.. " watts </b><br>" ..
              "<b>Grid Level : " ..g.. " watts </b><br>" ..
              "<b>Wind Output : " ..w.. " watts </b><br>" ..
              "</html></body>")
  end)
  conn:on("sent",function(conn)
    conn:close()
  end)
end)



here the other parts if you want to get it to work for yourself mqttW.lua sets up broker connection it kept alive as I am doing everything locally. to keep live database record . I made the the wait times abit long incase I needed to get access incase of panic. but it has not reboot inover an hour or mre and working flawlessly.. so it fine as it is
Code: Select allm = mqtt.Client(wifi.sta.getmac(), 120, "user", "password")
m:lwt("/lwt", wifi.sta.getmac(), 0, 0)


-- on publish message receive event
m:on("message", function(conn,topic, data)

end)
 m:connect("192.168.168.41", 1883, 0, function(conn)
          print("connected") end)

--m:close()
tmr.alarm(1, 20000, 1, function()

---dofile("serial123.lua")
end)


init.lua
Code: Select all-- load credentials, 'SSID' and 'PASSWORD' declared and initialize in there
r=0
g=0
b=0
pin_red=2
pin_green=4
pin_but=3
pin_blue=1
gpio.mode(pin_but, gpio.INPUT, gpio.PULLUP)
pwm.setup(pin_red, 100, 512)
pwm.setup(pin_green,100,512)
pwm.setup(pin_blue,100, 0)
pwm.start(pin_red)
pwm.start(pin_green)
pwm.start(pin_blue)
tmr.delay(200000)
pwm.setduty(pin_red,0)
pwm.setduty(pin_green,0)
pwm.setduty(pin_blue,0)


function startup()
    if file.open("init.lua") == nil then
        print("init.lua deleted or renamed")
        pwm.setduty(pin_green, 10)
    else
        print("Running")
        pwm.setduty(pin_blue, 0)
        pwm.setduty(pin_green, 100)
        file.close("init.lua")
        dofile("mqttW.lua")
        dofile("serial123.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("wifi","1234567890")
-- wifi.sta.connect() not necessary because config() uses auto-connect=true by default
tmr.alarm(1, 2000, 1, function()
    if wifi.sta.getip() == nil then
        print("Waiting for IP address...")
        pwm.setduty(pin_red, 100)
    else
        tmr.stop(1)
        print("WiFi connection established, IP address: " .. wifi.sta.getip())
        pwm.setduty(pin_blue,100)
        pwm.setduty(pin_red, 0)
        print("You have 3 seconds to abort")
        print("Waiting...")
        tmr.alarm(0, 10000, 0, startup)
       
    end
end)

User avatar
By marcelstoer
#54852
sash99 wrote:it really is a bug in the firmware


Then I suggest you report it at https://github.com/nodemcu/nodemcu-firmware/issues and provide a MVCE.