15-STM32物聯網開發WIFI(ESP8266)+GPRS(Air202)系統方案升級篇---遠程升級STM32程序,基於GPRS(LUA開發),STM32主動升級版

http://www.javashuo.com/article/p-nfuqvsnf-ey.htmlhtml

 

視頻教程:json

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

 

請先自行補充基礎內容async

 

http://www.javashuo.com/article/p-dlcvocfk-gc.htmltcp

 

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

 

這一節的GPRS程序和  http://www.javashuo.com/article/p-ctqkimcd-gg.html   這節的WIFI作相同的功能,總體功能也同樣,單片機復位自動升級oop

 

 

 如今測試測試

 

1,下載GPRS程序 ,主要的GPRS程序就是收到鏈接TCP指令就去鏈接TCP,而後透傳...ui

 

 

 
複製代碼
複製代碼
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程序spa

 

 

 

 

 

 

 

 

 

 

 雲端的仍是這個

 

 

 

 

 

 

 

 

 

 

 測試

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

修改雲端版本,復位下單片機

 

 

 

 

 

 

http://www.javashuo.com/article/p-qrkdgsiu-cv.html

相關文章
相關標籤/搜索