<commit_before>if cfg.mqhost==nil then return end

mqsub={}
local mq_tmr=4

mqid = cfg.mqid or "esp8266_"..wifi.sta.getmac()
mqc = mqtt.Client(mqid, 60, cfg.mquser or "", cfg.mqpass or "")
status.mq_on = false

mqsub[mqid.."/rgb"]=function(m,t,d)
 modeset(nil)
 rgbset(nil,cjson.decode(d))
end
mqsub[mqid.."/play"]=function(m,t,d)
 modeset(nil)
 local p=cjson.decode(d)
 play(tonumber(p.s),tonumber(p.p),p.m,p.b)
end

mqc:lwt(mqid.."/status", "offline", 0, 1)

mqc:on("offline", function(conn)
 status.mq_on = false
 print("mqtt offline")
end)

mqc:on("message", function(m,t,d)
 print("mqtt msg:",t,d or "nil")
 if d~=nil and mqsub[t] then
  mqsub[t](m,t,d)
 end
end)

mqc:connect(cfg.mqhost, cfg.mqport and tonumber(cfg.mqport) or 1883, cfg.mqtls=="1" and 1 or 0, 1,
 function(m)
  print("mqtt connected")
  local s={}
  for t,f in pairs(mqsub) do
   s[t]=0
  end
  mqc:subscribe(s, function(m)
   status.mq_on = true
   mqc:publish(mqid.."/status", "online", 0, 1)
   print("mqtt online")
  end)
 end,
 function(m,e)
  status.mq_on = false
  print("mqtt error")
 end
)

--tmr.alarm(mq_tmr, 10000, 1, function()
-- if status.mq_on then
--  mqc:publish(mqid.."/uptime",tmr.time(),0,0)
-- end
--end)<commit_msg>Added automatic reconnection and periodic status publication to mqtt client<commit_after>if cfg.mqhost==nil then return end

mqsub={}
local mq_tmr=4

mqid = cfg.mqid or "esp8266_"..wifi.sta.getmac()
mqc = mqtt.Client(mqid, 60, cfg.mquser or "", cfg.mqpass or "")
status.mq_on = false

mqsub[mqid.."/rgb"]=function(m,t,d)
 modeset(nil)
 rgbset(nil,cjson.decode(d))
end
mqsub[mqid.."/play"]=function(m,t,d)
 modeset(nil)
 local p=cjson.decode(d)
 play(tonumber(p.s),tonumber(p.p),p.m,p.b)
end

local mqpubstatus=function()
 if not status.mq_on then return end
 status.heap=node.heap()
 status.uptime_s=tmr.time()
 status.counter_us=tmr.now()
 local ssid, password, bssid_set, bssid = wifi.sta.getconfig()
 status.wifi={ssid,bssid}
 local ip,mask,gw = wifi.sta.getip()
 status.ip={ip,mask,gw}
 mqc:publish(mqid.."/status", cjson.encode(status), 0, 1)
end

mqc:lwt(mqid.."/conn", "offline", 0, 1)

mqc:on("offline", function(conn)
 status.mq_on = false
 print("mqtt offline")
 tmr.alarm(mq_tmr, 5000, 0, mqconnect)
end)

mqc:on("message", function(m,t,d)
 print("mqtt msg:",t,d or "nil")
 if d~=nil and mqsub[t] then
  mqsub[t](m,t,d)
 end
end)

mqconnect=function()
 print("mqtt connecting to "..cfg.mqhost..":"..cfg.mqport.." ssl:"..cfg.mqtls)  
 collectgarbage()
 mqc:connect(cfg.mqhost, cfg.mqport and tonumber(cfg.mqport) or 1883, cfg.mqtls=="1" and 1 or 0, 0,
  function(m)
   print("mqtt connected")
   local s={}
   for t,f in pairs(mqsub) do
    s[t]=0
   end
   mqc:subscribe(s, function(m)
    print("mqtt online")
    status.mq_on = true
    mqc:publish(mqid.."/conn", "online", 0, 1)
    mqpubstatus()
    tmr.alarm(mq_tmr, 60000, 1, mqpubstatus)
   end)
  end,
  function(m,e)
   status.mq_on = false
   print("mqtt error "..e)
  end
 )
end

tmr.alarm(mq_tmr, 5000, 0, mqconnect)
{'fraction_non_alphanumeric': 0.18666666666666668, 'fraction_numerical': 0.022, 'mean_word_length': 4.426763110307414, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3237430', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1306, 'n_words': 263}