nodemcu 智能插座

  • nodemcu 通过贝壳物联微信小程序与微信公众号实现远程控制排插
  • 材料清单:nodemcu开发板一个 手机充电头一个 排插一个 二路单偶继电器一个
  • 前往贝壳物联官网申请IP APKEY
  • 在这里插入图片描述
  • WiFi连接配置 文件命名为 init.lua (nodemcu上电自动执行该文件)
print("set up wifi mode")--shezhimoshi

wifi.setmode(wifi.STATION) 

station_cfg={}
--here SSID and PassWord should be modified according your wireless router
station_cfg.ssid="Zdd-4089139"  --我的wifi名称 
station_cfg.pwd="1234567890" --我的wifi密码
station_cfg.save=true
wifi.sta.config(station_cfg)

wifi.sta.connect()
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...") --dengdailianjie
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("kaiguan.lua")   --zuanhuanwenjianzhixing
end
end)
  • 开关控制 将下列文件命名为 kiaguan.lua 将init.lua 和kaiguan.lua 两个文件烧录至nodemcu
--use sjson
_G.cjson = sjson
--modify DEVICEID INPUTID APIKEY
DEVICEID = "10359"   --贝壳物联申请
APIKEY = "0d68cab66"--贝壳物联申请
INPUTID = "36"
host = host or "www.bigiot.net"
port = port or 8181
LED = 5
LED1 = 6
gpio.write(LED, gpio.HIGH)
gpio.write(LED1, gpio.HIGH)
isConnect = false
gpio.mode(LED,gpio.OUTPUT)
gpio.mode(LED1,gpio.OUTPUT)
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="0kai"})
        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="0guan"})
        cu:send( stoped.."\n" ) 
      end
    end
    if r.M == "say" then
      if r.C == "play1" then   
        gpio.write(LED1, gpio.LOW)  
        ok, played = pcall(cjson.encode, {M="say",ID=r.ID,C="1kai"})
        cu:send( played.."\n" )
      end
      if r.C == "stop1" then   
        gpio.write(LED1, gpio.HIGH)
        ok, stoped = pcall(cjson.encode, {M="say",ID=r.ID,C="1guan"})
        cu:send( stoped.."\n" ) 
      end
    end
  end)
  cu:on('disconnection',function(scu)
    cu = nil
    isConnect = false
    tmr.stop(1)
    tmr.alarm(6, 5000, 0, 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
  tmr.alarm(1, 6000, 1, function()
    if isConnect then
      cu:send(s.."\n")
    end
  end)
end
run()

  • 上电重置nodemcu
  • 登录微信进入微信小程序会显示
    在这里插入图片描述
    在这里插入图片描述
  • 登录官网显示nodemcu在线
  • 在这里插入图片描述
    -发送 play 打开第一个继电器 发送stop关闭 发送play1打开第二继电器 发送stop1关闭
  • 本文章只是做下笔记,供以后查阅 ,如有错误请指正。
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

怪兽小

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值