本帖最后由 gaoyang9992006 于 2020-3-2 21:54 编辑
更新版本,之前是通过文件写入的WIFI账号和密码,实际上可以通过手机配置。
方法是通过启用enduser setup Module aka Captive Portal aka WiFi Manager的方式,先让模块设置为AP模式并启用一个配置设置,生成一个AP,手机连接该AP,然后访问IP:192.168.4.1即可进入界面,然后输入你的WIFI账号和密码,或者搜索WIFI列表。
然后就可以自动完成WIFI接入了,之后等待约一分钟,即可与第三方云服务器实现沟通,完整在线动作。即可与天猫精灵对话。
该接入方式的代码如下
- --init.lua
- enduser_setup.start(
- function()
- print("Connected to WiFi as:" .. wifi.sta.getip())
- dofile("kaiguan.lua")
- end,
- function(err, str)
- print("enduser_setup: Err #" .. err .. ": " .. str)
- end,
- print -- Lua print function can serve as the debug callback
- )
- --kaiguan.lua
- print("Start BeiKeKaiGuan File. ")
- --use sjson
- _G.cjson = sjson
- --modify DEVICEID INPUTID APIKEY
- DEVICEID = "*****"
- APIKEY = "*********"
- host = host or "www.bigiot.net"
- port = port or 8181
- LED = 3
- isConnect = false
- gpio.mode(LED,gpio.OUTPUT)
- gpio.write(LED, gpio.HIGH)
- local function run()
- local cu = net.createConnection(net.TCP)
- cu:on("receive", function(cu, c)
- print(c)
- isConnect = true
- r = cjson.decode(c)
- if r.M == "say" then
- if r.C == "play" then
- gpio.write(LED, gpio.LOW)
- ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})
- cu:send( played.."\n" )
- end
- if r.C == "stop" then
- gpio.write(LED, gpio.HIGH)
- ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn off!"})
- cu:send( stoped.."\n" )
- end
- end
- end)
-
- cu:on('disconnection',function(scu)
- cu = nil
- isConnect = false
- --停止心跳包发送定时器,5秒后重试
- mytmr=tmr.create()
- mytmr:start()
- -- mytmr:stop()
- mytmr:alarm(5000, tmr.ALARM_SEMI, run)
- end)
-
- cu:connect(port, host)
- ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})
- if ok then
- print(s)
- else
- print("failed to encode!")
- end
- if isConnect then
- cu:send(s.."\n")
- end
- mytmr=tmr.create()
- mytmr:start()
- mytmr:alarm(60000, tmr.ALARM_AUTO, function()
- if isConnect then
- cu:send(s.."\n")
- end
- end)
- end
- run()
手机登录8266生成的AP后,进入192.168.4.1配置完WIFI账号密码后,即可完成网络接入。如上图所示,提示分配的IP地址。
|