15-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇---远程升级STM32程序,基于GPRS(LUA开发),STM32主动升级版

https://www.cnblogs.com/yangfengwu/p/10891908.html

视频教程:

https://www.bilibili.com/video/av55663292

 

请先自行补充基础内容

 

https://www.cnblogs.com/yangfengwu/p/6921832.html

 

https://www.cnblogs.com/yangfengwu/category/1383497.html    1-3节

 

这一节的GPRS程序和  https://www.cnblogs.com/yangfengwu/p/10875886.html   这节的WIFI做相同的功能,整体功能也一样,单片机复位自动升级

 

 现在测试

 

1,下载GPRS程序 ,主要的GPRS程序就是收到连接TCP指令就去连接TCP,然后透传...

 
 

复制代码

复制代码

复制代码

require "socket"
module(..., package.seeall)

--串口配置
local globalSendData1 = nil
local globalSendData = nil
local UART_ID = 1 --uart1
local uartReadData = "";
local uartReadDataFlage = false;

local ip=nil
local port=nil

local asyncClient
sys.taskInit(function()
  while true do
      while not socket.isReady() do sys.wait(1000) end
      
      if  port~=nil and ip~=nil  then
          asyncClient = socket.tcp()
          if  asyncClient:connect(ip, port)  then
              uart.write(1,"{\"datemcu\":\"updata\",\"state\":\"ConnectOK\"}")--发送链接成功指令
              print("{\"datemcu\":\"updata\",\"state\":\"ConnectOK\"}");
          else
              uart.write(1,"{\"datemcu\":\"updata\",\"state\":\"ConnectNO\"}")
          end
          port = nil;
          ip=nil;

          while asyncClient:asyncSelect(60, "ping") do end--必须有这个,负责TCP的后台运行
          asyncClient:close()
      end

      sys.wait(1000)
  end
end)


-- TCP接受到数据,发送串口
sys.subscribe("SOCKET_RECV", function(id)
    if  asyncClient.id == id then
        local data = asyncClient:asyncRecv()
        uart.write(1,data)
    end
end)


local jsondata,result,errinfo;
--定时器空闲中断检测(串口空闲接收数据)
local TimerFunc4Cnt = 0;
local function TimerFunc4()
    if  uartReadDataFlage == true then
        TimerFunc4Cnt = TimerFunc4Cnt+1;
        if  TimerFunc4Cnt >= 20 then
            TimerFunc4Cnt=0;
            uartReadDataFlage = false;
            globalSendData = uartReadData;
            uartReadData = "";
        end
    end

    if  globalSendData ~= nil  then
        jsondata,result,errinfo = json.decode(globalSendData)--判断是不是json
        if  result and type(jsondata)=="table" then -- 是json数据
            if  jsondata["datemcu"] ~= nil and jsondata["datemcu"] =="updata"  then
                if  jsondata["cmd"] ~= nil and jsondata["cmd"] =="ConnectTCP"  then
                    if  jsondata["ip"] ~= nil and jsondata["port"] ~=nil  then
                        ip = jsondata["ip"]
                        port = jsondata["port"]
                    end    
                end    
            end    
        else
            if  asyncClient ~=nil  then
                asyncClient:asyncSend(globalSendData)--TCP发送数据
            end
        end
        globalSendData = nil;
    end
end
sys.timerLoopStart(TimerFunc4,10)



--读取串口接收到的数据
local uartdata = ""
local function read()
    uartdata = ""
    while true do
        uartdata = uart.read(UART_ID,"*l",0)
        if not uartdata or string.len(uartdata) == 0 then break end
        uartReadData = uartReadData..uartdata;
        uartReadDataFlage = true
        TimerFunc4Cnt = 0;
    end
end


uart.on(UART_ID,"receive",read)
--配置并且打开串口
uart.setup(UART_ID,115200,8,uart.PAR_NONE,uart.STOP_1)

复制代码

复制代码

复制代码

 
 
 
 

 
 
后期文章改动,根据文章标题查找
 
 

 

 
 
 
 

 下载单片机IAP程序

 
 

 
 
 
 

 云端的还是这个

 
 

 
 
 
 

 测试

 
 

 

 
 

 
 
 
 

 

 
 
 
 

 

 
 
 
 
 
 

修改云端版本,复位下单片机

 
 

 

https://www.cnblogs.com/yangfengwu/p/10891914.html

猜你喜欢

转载自blog.csdn.net/qq_14941407/article/details/93321545