前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >9-开发板接入小五物联实现远程控制(Wi-Fi模块)

9-开发板接入小五物联实现远程控制(Wi-Fi模块)

作者头像
杨奉武
发布2018-08-15 11:08:00
6970
发布2018-08-15 11:08:00
举报
文章被收录于专栏:知识分享知识分享

这一节为教程最终版功能演示,现在不必深究,早晚自己全部都会实现的(静静的跟着我学哈)

这一篇主要是让大家尝尝鲜

资料链接

链接:https://pan.baidu.com/s/1-SRfsKGQ7rZVvFmp1ObHWw 密码:p9qs

一,刷固件

因为在网页上构建固件不支持smart,所以自己编译了固件,为了更加方便,自己还把lua的执行程序打包进了bin里面.

给大家各种模块的Flash大小的表

http://wiki.ai-thinker.com/_media/esp8266/esp8266_module_list.png

 二,打开刷固件的软件

如果是用的8M的

如果是用的32M的

现在刷固件

其实刷固件都是GPIO0接低电平,然后复位一下,然后就可以刷固件了

先看ESP01刷固件(8M)

拨码开关

然后GPIO0接低电平的时候复位一下模块(ESP8266所有型号都是这样)

 或者

然后

复位一下Wi-Fi

 开始刷固件

好了

 打开使用lua开发的编程软件

复位一下模块

其实就是默认内部有这两个文件

上面是自己写的利用MQTT实现远程升级的程序,好像还没有人这样做过,自己这样做呢,是为了实现每个人都可以随意给自己的设备升级自己的程序,下下节给大家远程升级软件教给大家使用

 现在用手机绑定

松开按钮

添加设备

 添加设备完成会自动在主界面添加一个设备

因为小五物联面向的是开发者所以默认的程序只是负责远程更新程序的.并没有执行相应动作的程序

 大约30S(实际上大约15S)以后程序更新完成,然后设备状态会显示在线

由于小五物联是面向开发者的,所以支持开发者升级自己的程序(下下节教给用户升级自己的程序),

自己云端默认程序控制的端口号是GPIO4 !!!!

默认下载进去的执行程序如下(如果现在看不懂没关系,会用就行,主要是告诉大家跟着自己学,能做到什么程度)

代码语言:javascript
复制
local RelayNowState=0
local RelayNowStateCopy=0
globalSendData ="nil";

local UsartReceiveData="";
local UsartReceiveDataCnt=0;
local UsartReceiveDataCntCopy=0;

RelayPin = 2;

if  file.open("switch.lua", "r") then
    if  file.read() == "relay=1" then
        gpio.write(RelayPin,1)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=1")
    else
        gpio.write(RelayPin,0)
        gpio.mode(RelayPin,gpio.OUTPUT)
        print("relay=0")
    end
    file.close();
end

local DataDisposeTimerCnt = 0;
local DataDisposeTimer = tmr.create()
DataDisposeTimer:register(10, 1, function() 
    RelayNowState = gpio.read(RelayPin)
    if  RelayNowState ~= RelayNowStateCopy then
        RelayNowStateCopy = RelayNowState
        globalSendData = "switch;relay="..RelayNowState
    end

    if  UsartReceiveDataCnt ~= 0 then
        DataDisposeTimerCnt=DataDisposeTimerCnt + 1;
    if  DataDisposeTimerCnt>=10 then
            UsartReceiveDataCnt = 0;
        DataDisposeTimerCnt=0;
            globalSendData = UsartReceiveData;
            UsartReceiveData="";
    end
    end
    if  Mymqtt ~= nil and MqttConnectedFlage == true then
        if  globalSendData ~= "nil" then
            Mymqtt:publish(PublishTopic,globalSendData, 0, 0, function(client)
            end)
            globalSendData = "nil"
        end
    end
end)
DataDisposeTimer:start()

function MqttDisposeMethod(client, topic, data)
    uart.write(0,data)
    list = split(data, ';')
    if  string.find(list[1],"switch") ~= nil then
        if  list[2] ~= nil then
            if  file.open(list[1]..".lua", "w+") then
                file.write(list[2])
                file.close()
            end
            if  list[2] == "relay=0"  then
                 gpio.write(RelayPin,0)
                 gpio.mode(RelayPin,gpio.OUTPUT)
            elseif  list[2] == "relay=1"  then
                 gpio.write(RelayPin,1)
                 gpio.mode(RelayPin,gpio.OUTPUT)
            end
            globalSendData = data;
        end
    elseif  string.find(list[1],"query")  then
        if  list[2] ~= nil then
            if  list[2] == "switch" then
                RelayNowState = gpio.read(RelayPin)
                globalSendData = "switch;relay="..RelayNowState
            end
        end
    end
end

--[[
uart.setup(0, 115200, 8, uart.PARITY_NONE, uart.STOPBITS_1)
uart.on("data",0,function(data)
    UsartReceiveData = UsartReceiveData..data;
    UsartReceiveDataCnt = UsartReceiveDataCnt + 1;
    DataDisposeTimerCnt = 0;
end, 0)]]

因为ESP01并没有GPIO4,可以打开串口调试助手看一下通信的数据

现在说一下GPIO0这个按钮的其它功能

 一,上面说了持续按下3,4秒左右是用来绑定设备(指示灯快闪),然后松开

 二,持续按下6秒左右,是用来切换执行文件(指示灯慢闪几下后熄灭),然后松开

其实内部程序更新的时候是两套程序在互相切换,就是无论何时都会保留一套,如果更新执行失败

可以切换到以前的程序接着执行.

三,持续按下9秒左右,恢复到最一开始的状态(只保留底层的更新程序,清除其余程序文件)

主要是处理极端,两套程序全有问题了..

大家可以看我写的程序细细品味哈(匠芯还是希望和大家分享知识)

代码语言:javascript
复制
--[[
GPIO0--3
GPIO1--10
GPIO2--4
GPIO3--9
GPIO4--2
GPIO5--1
GPIO9--11
GPIO10--12
GPIO12--6
GPIO13--7
GPIO14--5
GPIO15--8
GPIO16--0
0--GPIO16   1--GPIO5   2--GPIO4   3--GPIO0   4--GPIO2
5--GPIO14   6--GPIO12  7--GPIO13  8--GPIO15  9--GPIO3
10--GPIO1   11--GPIO9  12--GPIO10
]]

---------------------------------------split-----------------------------------
splitStart = 1;
splitIdex=0;
splitTable={};
function split(s, delim)
    if type(delim) ~= "string" or string.len(delim) <= 0 then
        return
    end
    splitStart = 1
    splitTable = {}
    while true do
        splitIdex = string.find (s, delim, splitStart, true) -- plain find
        if not splitIdex then
          break
        end
        table.insert (splitTable, string.sub (s, splitStart, splitIdex - 1))
        splitStart = splitIdex + string.len (delim)
    end
    table.insert (splitTable, string.sub (s, splitStart))
    return splitTable
end

--------------------Config Pin---------------------------
local InputKeyPin = 3;
IndicateLed = 4;

gpio.mode(IndicateLed, gpio.OUTPUT)
gpio.write(IndicateLed, 0) 
node.stripdebug(3) 

--------------------get A or B orUpdata---------------------------
ABUpdataFile = "A"
ABUpdataFileCopy = "B"
UpdataFlage = false;

if file.open("UpdataFlage.lua", "r") then if file.read() == "1" then UpdataFlage = true; end file.close() end
file.remove("UpdataFlage.lua")

if  file.open("ABUpdataFile.lua", "r") then ABUpdataFile = file.read();file.close();end
if  ABUpdataFile =="A" then ABUpdataFileCopy = "B" else ABUpdataFileCopy = "A" end

print("run:"..ABUpdataFile.."file");
--------------------public.lua---------------------------
local i=0;
local PubilcTimerOut=0;
local PubilcTimerCnt = 0;
PubilcTimerState = 0;
local PubilcTimer = tmr.create()
PubilcTimer:register(100, 1, function() 
    if  gpio.read(InputKeyPin) == 0 then
        PubilcTimerCnt = PubilcTimerCnt + 1;
        if  PubilcTimerCnt == 30 then
            PubilcTimerState = 1;
            PubilcTimerOut=0;
            print("startsmart")
            wifi.setmode(wifi.STATION)
            wifi.stopsmart()
            wifi.startsmart(0,function(ssid, password)
                   print(string.format("Success. SSID:%s ; PASSWORD:%s", ssid, password))
                   PubilcTimerOut=0;
                   PubilcTimerState=2;
            end) 
        elseif  PubilcTimerCnt == 60 then 
            PubilcTimerOut=0;   
            PubilcTimerState=3;
            if file.open("ABUpdataFile.lua", "w+") then
               file.write(ABUpdataFileCopy)
               file.close() 
               print("change run:"..ABUpdataFileCopy);
            end
            for i = 0,6 do
                gpio.write(IndicateLed, 1-gpio.read(IndicateLed)); 
                tmr.delay(300000)
            end
            gpio.write(IndicateLed, 0);    
        elseif  PubilcTimerCnt == 90 then 
            PubilcTimerOut=0;   
            PubilcTimerState=4;
            file.remove("ABUpdataFile.lua")
            file.remove("version.lua")
            file.remove("Aupdata1.lua")
            file.remove("Aupdata2.lua")
            file.remove("Aupdata3.lua")
            file.remove("Aupdata4.lua")
            file.remove("Aupdata5.lua")
            file.remove("Aupdata6.lua")
            file.remove("Aupdata7.lua")
            file.remove("Bupdata1.lua")
            file.remove("Bupdata2.lua")
            file.remove("Bupdata3.lua")
            file.remove("Bupdata4.lua")
            file.remove("Bupdata5.lua")
            file.remove("Bupdata6.lua")
            file.remove("Bupdata7.lua")
            print("Remove all file");        
            node.restart();   
        end
        print("PubilcTimerState="..PubilcTimerState)
    else
        PubilcTimerCnt=0;   
        if  PubilcTimerState == 3 then 
            PubilcTimerState=0;
            node.restart(); 
        end         
    end
    
    if  PubilcTimerState==1 then
        PubilcTimerOut=PubilcTimerOut+1;
        if PubilcTimerOut == 600 then node.restart();end
        gpio.write(IndicateLed, 1-gpio.read(IndicateLed));
    elseif PubilcTimerState ==2 then
        PubilcTimerOut=PubilcTimerOut+1;
        if  PubilcTimerOut==50 then node.restart();end
    end
end)
PubilcTimer:start()


--------------------get version---------------------------
version = 0;
if  file.open("version.lua", "r") then
    version = file.read()
    file.close();
end
print("Program version:"..version)
--------------------backup function---------------------------
StandbyFun = {}
if  gpio.read(InputKeyPin) == 1 and UpdataFlage == false then 
    if  file.open(ABUpdataFile.."updata1.lua", "r") then
        file.close();
        dofile(ABUpdataFile.."updata1.lua")
    else
        print("run:no update program");    
    end
end
if StandbyFun.first ~= nil then--first function
   StandbyFun.first();
end

--------------------Variable---------------------------
MqttConnectedFlage = false;
clientid = wifi.sta.getmac()

print("MAC: "..clientid)
--[[
--UpdataSoleId = "90M9482C8D0EM5C361AZ0A0Q"
UpdataSoleId = "B029287C4DEEP5E301DZLA2Q"
UpdataScope  = "1"

SubscribeGroup = "wifi/user".."/group/switch"
SubscribeTopic = "wifi/user".."/"..clientid
PublishTopic = "wifi/device".."/"..clientid

SubscribeUP1 = UpdataSoleId.."/"..UpdataScope.."/"..clientid
SubscribeUP2 = UpdataSoleId.."/"..UpdataScope
SubscribeUP3 = UpdataSoleId
SubscribeUP = UpdataSoleId.."/"..UpdataScope.."/".."UpdataR".."/"..clientid
PublishUP = UpdataSoleId.."/"..UpdataScope.."/".."UpdataS".."/"..clientid

if StandbyFun.variable ~= nil then--variable function
   StandbyFun.variable();
end]]

--------------------Rst MCU---------------------------
local InputKeyCnt = 0
local InputKeyDownFlage = false
local function InputKeyTrig()
    if  InputKeyDownFlage==false then
        InputKeyDownFlage=true
        InputKeyCnt = tmr.now()
        gpio.trig(InputKeyPin,"high",InputKeyTrig)
    else
        InputKeyDownFlage = false;
        if   tmr.now() - InputKeyCnt <1000000 then
             node.restart();
        end     
        gpio.trig(InputKeyPin,"low",InputKeyTrig)
    end
end
gpio.trig(InputKeyPin,"low",InputKeyTrig)


if  file.open("mqttconf.lua", "r") then 
file.close(); 
node.stripdebug(3) 
node.compile("mqttconf.lua")
require("mqttconf")
--dofile("mqttconf.lua") 
end


--if  file.open("updata.lua", "r") then file.close(); dofile("updata.lua") end

if  gpio.read(InputKeyPin) == 1 and UpdataFlage == false  then
    if  file.open(ABUpdataFile.."updata2.lua", "r") then file.close(); dofile(ABUpdataFile.."updata2.lua") end 
    if  file.open(ABUpdataFile.."updata3.lua", "r") then file.close(); dofile(ABUpdataFile.."updata3.lua") end
    if  file.open(ABUpdataFile.."updata4.lua", "r") then file.close(); dofile(ABUpdataFile.."updata4.lua") end 
    if  file.open(ABUpdataFile.."updata5.lua", "r") then file.close(); dofile(ABUpdataFile.."updata5.lua") end  
    if  file.open(ABUpdataFile.."updata6.lua", "r") then file.close(); dofile(ABUpdataFile.."updata6.lua") end  
    if  file.open(ABUpdataFile.."updata7.lua", "r") then file.close(); dofile(ABUpdataFile.."updata7.lua") end  
end

if StandbyFun.finally ~= nil then--finally function
   StandbyFun.finally();
end


wifi.setmode(wifi.STATIONAP)

ConnectApFalge = false;
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    if  ConnectApFalge == true  then
        if  StandbyFun.STA_DISCONNECTED ~= nil then
            StandbyFun.STA_DISCONNECTED();
        end
    end
    ConnectApFalge = false;
    MqttConnectedFlage = false;
end)

wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
    if  ConnectApFalge == false  then
        print(wifi.sta.getip())
        if  StandbyFun.STA_GOT_IP ~= nil then
            StandbyFun.STA_GOT_IP();
        end
    end
    ConnectApFalge = true;
end)
代码语言:javascript
复制
local UpdataSoleId = "90M9482C8D0EM5C361AZ0A0Q"
--UpdataSoleId = "B029287C4DEEP5E301DZLA2Q"
local UpdataScope  = "1"

local SubscribeTopic = "wifi/user".."/"..clientid
      PublishTopic = "wifi/device".."/"..clientid
local SubscribeUP1 = UpdataSoleId.."/"..UpdataScope.."/"..clientid
local SubscribeUP2 = UpdataSoleId.."/"..UpdataScope
local SubscribeUP3 = UpdataSoleId
local SubscribeUP = UpdataSoleId.."/"..UpdataScope.."/".."UpdataR".."/"..clientid
local PublishUP = UpdataSoleId.."/"..UpdataScope.."/".."UpdataS".."/"..clientid

if StandbyFun.variable ~= nil then--variable function
   StandbyFun.variable();
end


local MqttConnectTimer = tmr.create()

Mymqtt = mqtt.Client(clientid,1,"yang", "11223344");
Mymqtt:lwt(PublishTopic, "offline", 0, 0)
Mymqtt:on("offline", function(client) MqttConnectTimer:start(); end)


local function subscribeSuccess(client)
    print("subscribe success") 
end
local function ConnectSuccess(client)
     if UpdataFlage == false  then
        client:subscribe(SubscribeTopic, 0, subscribeSuccess)
     end
     
     client:subscribe(SubscribeUP1, 0, subscribeSuccess)
     client:subscribe(SubscribeUP2, 0, subscribeSuccess)
     client:subscribe(SubscribeUP3, 0, subscribeSuccess)
     client:subscribe(SubscribeUP, 0, subscribeSuccess)
     MqttConnectTimer:stop();
     print("connected")
     gpio.write(IndicateLed, 1) 
     MqttConnectedFlage = true;
     
     if  UpdataFlage == true  then
         OpenUpdataCnt = 1;
         local UpdataDelayTimer = tmr.create()
         UpdataDelayTimer:register(6000, 0, function() 
         Mymqtt:publish(PublishUP,"v=?", 0, 0, function(client)end) 
        end)
        UpdataDelayTimer:start();
     end  
end
local function ConnectFailed(client,reason)
   MqttConnectedFlage = false;
   MqttConnectTimer:start();
   gpio.write(IndicateLed, 0) 
   print("failed reason: " .. reason)
end

MqttConnectTimer:register(1000, 1, function() 
    if Mymqtt ~= nil  then
       Mymqtt:close()
    end
    Mymqtt:connect("mnifdv.cn", 1883, 0,ConnectSuccess,ConnectFailed)
end)
MqttConnectTimer:start();


local HaveMqttMethod=false;

local UpdataTime = tmr.create()
UpdataTime:register(10000, 1, function() node.restart();end)

local versionCopy = 0;
local OpenUpdataCnt = 0;
local OpenUpdataValue = 0;

if  file.open(ABUpdataFile.."MqttMethod.lua", "r") then 
    if  file.read() == "1" then 
        print("HaveMqttMethod = true");
        HaveMqttMethod = true; 
    end 
    file.close() 
end

Mymqtt:on("message", function(client, topic, data) 
    if topic == SubscribeUP1 or topic == SubscribeUP2 or topic == SubscribeUP3 then
       local  list = split(data, ';')
       if list[1] == "updata" then
          if file.open("UpdataFlage.lua", "w+") then
             file.write("1")
             file.close() 
          end
          node.restart();
       elseif list[1] == "version" then
          --print("Request version:"..tonumber(list[2]));
          if  tonumber(version) <= tonumber(list[2]) and UpdataFlage == true then
              UpdataFlage = false;
              versionCopy = list[2]
              OpenUpdataValue =1;
              OpenUpdataCnt = 1;
              UpdataTime:start()
              HaveMqttMethod=false;
              print("open updata1.lua")
              file.open(ABUpdataFileCopy.."updata1.lua", "w+")
              client:publish(PublishUP,"request1_"..OpenUpdataCnt, 0, 0, function(client)end) 
          end
       elseif list[1] == "UpdataSuccess" then
          UpdataTime:stop()
          if file.open("ABUpdataFile.lua", "w+") then
             file.write(ABUpdataFileCopy)
             file.close() 
          end
          
          if  file.open(ABUpdataFileCopy.."MqttMethod.lua", "w+") then
              if  HaveMqttMethod ==true then
                  file.write("1")
              else
                  file.write("0")    
              end
              file.close()
          end
          if  file.open("version.lua", "w+") then
              file.write(versionCopy)
              file.close() 
          end
          node.restart();
       end      
    elseif topic == SubscribeUP then
        if HaveMqttMethod ==false and string.find(data,"MqttDisposeMethod")~=nil then
           HaveMqttMethod = true;
        end
        file.write(data)
        if OpenUpdataCnt == 8 then
           OpenUpdataCnt = 0
           OpenUpdataValue = OpenUpdataValue + 1;
           if OpenUpdataValue == 8 then
              file.close()
              client:publish(PublishUP,"UpdataOK", 0, 0, function(client)end) 
           else
              file.close()
              file.open(ABUpdataFileCopy.."updata"..OpenUpdataValue..".lua", "w+")   
           end
        end
        if OpenUpdataValue ~= 8 then
           OpenUpdataCnt = OpenUpdataCnt + 1;
           client:publish(PublishUP,"request"..OpenUpdataValue.."_"..OpenUpdataCnt, 0, 0, function(client)end)  
        end
        print("updata...."..OpenUpdataCnt)
    else
        if  data == "v=?" then
            client:publish(PublishTopic,"version;"..version, 0, 0, function(client)end)
        end
        --print(data)
        if  OpenUpdataValue == 0 and HaveMqttMethod==true then
            MqttDisposeMethod(client, topic, data)
        end 
    end
end)
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-08-03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档