贝壳物联智能开关,更新修正tmr.alarm问题

  • 代码部分

8266纯粹是个坑,搞了3周,陷了无数次。写在2020疫情之后,武汉加油,中国加油!!!下面随便聊个坑:
nodemcu-build官网 的固件代码部分已经更新,特别是tmr.alarm计时(钟表)函数,原来的结构多简单,现在生生的代码编译不过去,只能用18年12月以前的固件。可是那些封装好的有些不是你需要的,好尴尬。现在使用了动态tmr函数重新修改封装。----------2020.4.5

固件下载nodemcu站点

共init.lua config.lua两个文件,烧录固件看附件。
1老版固件
包含以下模块:
file , GPIO , HTTP , MQTT , net , node , PWM , SJSON , timer , UART , WIFI

2新版固件
包含以下模块:
file,gpio,http,mqtt,net,node,pcm,pwm,rtcfifo,rtcmem,rtctime,sjson,tmr,uart,wifi,tls

源码固件下载

代码部分


--------------------------------------file (init.lua)print("set up wifi mode------------masunbo001 wulianwang")wifi.setmode(wifi.STATIONAP)station_cfg={}--here SSID and PassWord should be modified according your wireless routerstation_cfg.ssid="X-HU"                ------------yourselfstation_cfg.pwd="chenpeng"         ------------yourselfstation_cfg.save=truewifi.sta.config(station_cfg)wifi.sta.autoconnect(1)APcfg={}APcfg.ssid="ESP8266001"APcfg.pwd="masunbo080412"wifi.ap.config(APcfg)str=nilssidTemp=nilstr=wifi.ap.getmac()ssidTemp=string.format("%s%s%s",string.sub(str,10,11),string.sub(str,13,14),string.sub(str,16,17))collectgarbage()print("Soft AP started")print("Heep:(bytes)"..node.heap())print("MAC:"..wifi.ap.getmac())print("\r\nIP:"..wifi.ap.getip())print("------------Ready to start soft ap-------------")-----------------------------------------------------mytimer = tmr.create()mytimer:alarm(3000, tmr.ALARM_AUTO, function ()if wifi.sta.getip()== nil thenprint("IP unavaiable, Waiting...")elsemytimer:stop()print("Config done, IP is "..wifi.sta.getip())dofile("config.lua")endend)TCPSever=net.createServer(net.TCP,28800) --creat TCP systemTcpClientCnt = 0 TcpSocketList={} TCPSever:listen(8080,function(socket)if  TcpClientCnt == 5 then if  TcpSocketList[0] ~= nil thenTcpSocketList[0]:close()   TcpSocketList[0] = nil end   endTcpSocketList[TcpClientCnt] = socketprint(TcpClientCnt.."-Connect")TcpClientCnt = TcpClientCnt + 1if  TcpClientCnt == 5 thenTcpClientCnt = 0endsocket:on("receive",function(socket,data)uart.write(0,data)end)socket:on("disconnection",function(sck,c)for i=0,5 do               if  TcpSocketList == sck thenTcpSocketList = nilprint(i.."-Disconnect")endendend)end)uart.on("data",0,function(data)for i=0,5 doif  TcpSocketList ~= nil then TcpSocketList:send(data)endendend, 0)-------------------------------------------file (config.lua)--use sjson
_G.cjson = sjson
--modify DEVICEID INPUTID APIKEY
DEVICEID = "xxxxxxxxxxxxx" --yourself
APIKEY = "xxxxxxxxxxxxx"  --yourself
INPUTID = "xxxxxxxxxxxxx" --yourself
host = host or "www.bigiot.net"
port = port or 8181
LED = 3
--LED1 = 4
isConnect = falsegpio.mode(LED,gpio.OUTPUT)
--gpio.mode(LED1,gpio.OUTPUT)----------------------------------------------local _Me = {}autoTime = 5000hadCloseRoter = falsecount = 1function watch()
mytimer2 = tmr.create()
mytimer2:alarm(autoTime, tmr.ALARM_AUTO, function ()print("lianjie "..count,"ci")count = count+1if wifi.sta.getip() == nil thenautoTime = 20000
mytimer1 = tmr.create()
mytimer1:stop()if hadCloseRoter == false then
mytimer2:stop()hadCloseRoter = trueprint("dog watch Time set 4s once")watch()endelseif hadCloseRoter thenprint("check had close wifi link, now need auto link service,and dog watch time set 1s once")isConnect = falsehadCloseRoter = falseautoTime = 5000mytimer1:stop()mytimer2:stop()cu = nilwatch()run()endendend)endwatch()function run()local cu = net.createConnection(net.TCP)cu:on("receive", function(cu, c)print("masunbo,received\n")print(c)
isConnect = true
-------------------------------------------------------
r = cjson.decode(c)if r.M == "say" thenif r.C == "offOn" then   gpio.write(LED, gpio.LOW)tmr.delay(1850000) gpio.write(LED, gpio.HIGH)  ok, offOned = pcall(cjson.encode, {M="say",ID=r.ID,C="ON OFF"})cu:send( offOned.."\n" )endif 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" )endif 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    --if r.C == "pause" then   --   gpio.write(LED1, gpio.LOW)--   ok, pauseed = pcall(cjson.encode, {M="say",ID=r.ID,C="LED turn on!"})--   cu:send( pauseed.."\n" )      -- end        endend)---------------------------------------------------------------------------
cu:on('disconnection',function(scu)cu = nilisConnect = falseprint("beike,disconnection")mytimer1:stop()mytimer6 = tmr.create()mytimer6:alarm(10000, tmr.ALARM_SINGLE, run)end)cu:connect(port, host)ok, s = pcall(cjson.encode, {M="checkin",ID=DEVICEID,K=APIKEY})if ok thenprint(s)elseprint("failed to encode!")endif isConnect thencu:send(s.."\n")endmytimer1 = tmr.create()mytimer1:alarm(12000, tmr.ALARM_AUTO,function()if isConnect thenprint("auto send headPackge")cu:send(s.."\n")elseprint("not connected ...")endend)
end
run()

8266 lua贝壳物联智能开关,更新修正tmr.alarm问题相关推荐

  1. 通过贝壳物联,让小度或天猫精灵语音控制你的8266

    我用的是网上继电器模块上可以插8266-01S那种5V取电,自带5转3.3电源,模块是安装在顶灯里面的.原始开关还在,考虑到单火取电难度太大,主要是8266功率太大了.所以既能在开关处控制灯,也能智能 ...

  2. MicroPython_ESP8266_IoT——第四回 初入联网(接入了贝壳物联)

    第四回 初入联网(接入了贝壳物联) 本来计划先把所有的硬件介绍完,再介绍如何介入贝壳物联的.但是那样就比较枯燥,还是先尝试接入贝壳物联,来增加ESP8266模块的可玩性. 需要了解[贝壳物联平台通信协 ...

  3. ESP8266 贝壳物联

    小白10元玩转智能家居使用天猫精灵+ESP8266WiFi模块对接贝壳物联,可以OLED 屏幕显示温湿度并上传服务器,WiFi控制小车等一系列智能产品 2019-03-24 10:38:17 昂好多个 ...

  4. ESP8266(ESP-12F)+DS18B20+贝壳物联

    ESP8266(ESP-12F)+DS18B20+贝壳物联 新手上路,大家多包涵 写这篇博文的背景 初次刷固件: 初次遭遇lua 初识贝壳物联 贝壳校时钟 ESP8266+STC15F104另辟蹊径 ...

  5. ESP8266 AT指令模式接入贝壳物联,实现远程控制

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 ESP8266 AT指令模式接入贝壳物联,实现远程控制 前言 一.接线方法 二.AT指令配置ESP8266 三.ESP8266与贝壳物 ...

  6. 贝壳物联平台通讯协议

    贝壳物联平台通讯协议 贝壳物联平台通讯协议TCP.UDP.HTTP.Websocket说明,设备登录服务器,收发命令,发送实时数据,上传图片等协议. 首页 帮助文档 API文档 目录 一.概述 二.通 ...

  7. Arduino--ESP8266物联网WIFI模块(贝壳物联)--数据上传服务器(单数据接口)

    一.简介 随着移动物联网的发展,各场景下对于物联控制.数据上传.远程控制的诉求也越来越多,基于此乐鑫科技推出了便宜好用性价比极高的wifi物联模块--ESP8266,话不多少我们先来看看这个神奇的模块 ...

  8. Linux下c语言模拟贝壳物联设备在线

    开发环境:Virtualbox ubuntu 14.04 使用说明: 1.下载使用安装cJSON 2.将贝壳物联对应设备的ID和APIKEY及数据接口DID替换后,直接make运行#define PA ...

  9. 【esp8266】④esp8266对接贝壳物联平台

    源码github地址:https://github.com/linzhongpaihuai/smartplug ①烧录方法:https://blog.csdn.net/u010177891/artic ...

最新文章

  1. windows10中git 远程仓库使用
  2. 华为等向联合国提议重构互联网:提议采用一种新的核心网络技术新标准,名为“New IP”...
  3. live555从RTSP服务器读取数据到使用接收到的数据流程分析
  4. python 漂亮的excel_python 自定义漂亮的 excel 结果测试报告
  5. zookeeper源码分析之一服务端启动过程
  6. 小心DataAdapter陷阱
  7. javascript中this
  8. 最全Java面试180题:阿里11面试+网易+百度+美团!含答案大赠送!
  9. java 正则表达式 table_Java 使用正则表达式
  10. 轻松搞定vmware + win2003Cluste
  11. 终于开通了,呵呵,以后跟大家一起讨论
  12. JAVA连接数据库 遍历集合数组!!!
  13. C语言基础第三次作业
  14. 腾讯x5内核(TBS)简单集成封装
  15. 维生素D与肠道菌群的互作
  16. 三三速记英语 需要者看
  17. 打印Service运行时间与Aspect相关注解使用
  18. JVM上篇:内存与垃圾回收篇十四--垃圾回收器
  19. python基础编程简单案例:购买商品
  20. Deep Learning Based Registration文章阅读(五)《Anatomy-guided Multimodal Registration by Learning Segment 》

热门文章

  1. 买面茶--郭德纲相声
  2. strtoupper() 把字符串转换为大写字母
  3. [Plant Simulation]使用TableFile和Method进行仓库仿真(Byref函数)
  4. c语言中位数怎么求,C计算中位数参考
  5. 冷门游戏脚本开发软件-TC
  6. SSH协议原理和实践
  7. 基础计算机教学论文,基础计算机论文,关于民办院校计算机基础课程实践教学体会相关参考文献资料-免费论文范文...
  8. SVM适合小数据量原因
  9. linux修改禅道端口,CentOS7上安装了gitlab和禅道,改了禅道端口访问不了
  10. 在控制台,打印出某个具体的变量,并监听其变化