Post your best Lua script examples here

User avatar
By stgoddv
#69959 Hi!

I think it would be extremely valuable if someone could post and example code connecting esp8266 nodemcu with IoT AWS Mqtt services. I've done research over internet but all the solutions that i've came across uses the C SDK presented by AWS or Mongoose or similar. But none of them uses properly MQTT NodeMCU Lua libraries to accomplish that. It's not straighforward in this case, because i think AWS needs some extra certificate logic before establishing the connection, as oppose to Azure (in that case the connection is straighforward and simple).

I'm sure that this example would be very useful for many people, especially the following years with the new powerful esp32 around. Has someone worked on this? Please post your code if so.. We'll appreciate that.

Any comments would help also, even if you don't have the code but have experience with this platforms.
User avatar
By Dsbaha
#70940 Hi, the below is what I'm using. Seems stable. Have 23kb of heap left after the connection. I also have some wifi event manager things and other misc tasks running in the background.

Code: Select allm_dis={}

function reportshadow(m)
  print("Reporting Shadow State")
  ss = {}
  ss.state = {}
  ss.state.reported = {}
  ss.state.reported.gpio = { gpio.read(3), gpio.read(4) }
  m:publish("$aws/things/"..node.chipid().."/shadow/update", sjson.encode(ss), 0, 0)
  ss = nil
end

function dispatch(m,t,pl)
  if pl ~= nil and m_dis[t] then
    m_dis[t](m,pl)
  end
end

function topic1func(m,pl)
  print("get1: "..pl)
end

function topic2func(m,pl)
  node.input(pl)
end

function topic3func(m,pl)
  print("get3: "..pl)
  delta = sjson.decode(pl)
  if delta.state.gpio[1] ~= nil then
    print("GPIO0: " .. delta.state.gpio[1])
    gpio.write(3, delta.state.gpio[1])
  end

  if delta.state.gpio[2] ~= nil then
    print("GPIO2: " .. delta.state.gpio[2])
    gpio.write(4, delta.state.gpio[2])
  end
  reportshadow(m)
end

m_dis["all/status"]=topic1func
m_dis[node.chipid() .. "/status"]=topic2func
m_dis["$aws/things/".. node.chipid() .."/shadow/update/delta"]=topic3func

m=mqtt.Client(node.chipid(), 1500, node.chipid(), node.chipid())
m:on("connect", function(m)
  print("connected "..node.heap())
  for k,v in pairs(m_dis) do
    m:subscribe(k, 0)
  end
  reportshadow(m)
end)

m:on("offline", function(conn)
  print("disconnected "..node.heap())
end)

--m:lwt("$aws/things/".. node.chipid() .."/shadow/update", sjson.encode(lwt))

m:on("message", dispatch)
tls.cert.auth(true)
tls.cert.verify(true)
gpio.mode(3, gpio.OUTPUT)
gpio.mode(4, gpio.OUTPUT)
m:connect("xxxxxx.iot.xxxxxx.amazonaws.com", 8883, 1, 0)


I should probably mention I use the following to get the device/ca certs in the module;
Code: Select alltls.cert.auth(
[[
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
]]
,
[[
-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
]]
)

tls.cert.verify(
[[
-----BEGIN CERTIFICATE-----
-----END CERTIFICATE-----
]]
)
User avatar
By stgoddv
#72650 Awesome!!!
Congrats! You're probably one of the few people that got this working.
I'll try your example to see how it goes. I'm sure that your code will benefit a lot of fellows.
Thanks!
User avatar
By stgoddv
#72658 Hi Dsbaha!

Thank you for the reply, very useful.
I'm getting the -5 error on connecting to AWS via your code.
Can you check if with the current master build, your code works well?

Please! Thanks!
One last question:
In some portion of your code you wrote:
m=mqtt.Client(node.chipid(), 1500, node.chipid(), node.chipid())

How did you configure in AWS IoT Hub that you id node.chipid() would have a password and a user
named node.chipid()?
Should this work too?:
m=mqtt.Client(node.chipid(), 1500)

Thank you very much!