smart-green-house/wifi_code/wg.lua

68 lines
1.6 KiB
Lua

wifi.setmode(wifi.STATION)
wifi_conf = { ssid = "coco", pwd = "lzf1997811", auto = true }
wifi.sta.config(wifi_conf)
wifi.sta.connect()
t = tmr.create()
t:register(500, tmr.ALARM_AUTO, function()
if wifi.sta.getip() ~= nil then
ip = wifi.sta.getip()
print("ip is -->", ip)
t:unregister()
Init_mqtt()
else
print("-----getting ip----")
end
end)
t:start()
s = softuart.setup(9600, 5, 6)
n = softuart.setup(9600, 8, 7)
s:on("data", '\r', function(data)
print("receive data:", data)
a, b = string.match(data, "(%d+),(%d+)")
a = tonumber(a)
b = tonumber(b)
local str_beep_on = "beep:on"
local str_beep_off = "beep:off"
if b > 80 then
n:write(str_beep_on)
print("beep:on")
else
n:write(str_beep_off)
print("beep:off")
end
mqtt_client:publish("ctos", data, 0, 120, function(client)
print("dht_data:success!");
end)
end)
function Init_mqtt()
mqtt_client = mqtt.Client("stm32_fan_client", 60);
mqtt_client:on("message", function(client, topic, message)
if topic == 'stm32' then
print("subed msg:" .. message)
s:write(message)
end
end)
mqtt_client:connect("flykhan.com", function(client)
-- mqtt_client:connect("39.106.7.53", function(client)
print("mqtt broker success")
mqtt_client:subscribe("stm32", 0, function(client)
print("subscribe ok")
end)
end, function(client, reason)
print("mqtt broker fail:" .. reason)
end)
mqtt_client:close()
end