3-STM32物聯網開發WIFI(ESP8266)+GPRS(Air202)系統方案升級篇(HTTP介紹,TCP實現HTTP下載文件)


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

 

看了好多文章.....唉,還是自己親自動手用網絡監控軟件測試吧

 

先看這個節安裝WEB服務器.....安裝好以后就可以用HTTP訪問電腦文件了

 

事先不知道HTTP,最后先看這個  https://www.cnblogs.com/yangfengwu/p/10357564.html

 

其實HTTP就是建立在TCP通信上,然后自己又封裝了一套協議罷了,不過協議也不算多,協議內容都是用字符串發送的,也好理解

 

感覺要比我以前自己用TCP實現MQTT協議簡單多了,MQTT規定的協議就是復雜點,全部用16進制組合......麻煩死了...

 

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

 

大家學了這個文章,只要自己的模塊支持TCP,那么就可以實現用HTTP訪問下載文件,

 

廢話少說,我就下載我自己雲端的這個文件

 

 

 

 

 

 

 

 

 

 

 

 

 

 

https://blog.csdn.net/runner_diego/article/details/51379116    (這個是我在網上找的介紹http協議的)

 

 

 

 啟動個TCP客戶端

 

連接的ip地址選擇自己的哈  我測試用的是  47.92.31.46    端口號80     

 

 

 

GET /hardware/wifi1/updata1.lua HTTP/1.1
Host: 47.92.31.46


 

 先看get的用法

 

 

 

GET,一個空格,訪問文件的路徑,一個空格,用哪個版本的HTTP協議

 

Host,冒號,一個空格,訪問的地址

 

 

 

 

 

 

 

 

 

 

 

然后咱看看發送和具體接收的數據

 

復制代碼
3:26:18 發送數據:GET /hardware/wifi1/updata1.lua HTTP/1.1
Host: 47.92.31.46

[1次]
3:26:18 收到數據:HTTP/1.1 200 OK
Date: Mon, 29 Apr 2019 19:26:19 GMT
Server: Apache/2.4.39 (Win64)
Last-Modified: Sat, 20 Apr 2019 15:48:39 GMT
ETag: "7ac-586f82b4b7b40"
Accept-Ranges: bytes
Content-Length: 1964

local model = "wifi1"  --product model

--[[Do not update the following program !!!!]]
local version1 = "0.0.0";
local version2 = "1.0.0";

if  file.open("version2.lua", "r") then--local
    version2 = file.read()
    file.close();
end
print("local version:"..version2)

local JsonTable = {};

function UpdataFun(client, topic, data,jsondata)
    if  jsondata["version"] ~= nil and jsondata["url"] ~= nil  then
        if  jsondata["version"] ~= version2  then
            version1 = jsondata["version"]
    
            JsonTable["data"] = "updata";
            JsonTable["status"] = "unlike";
            JsonTable["version"] = version2;
             
            if  file.open("url.lua", "w+") then
                file.write((jsondata["url"]))
                file.close() 
            end
            print(jsondata["version"],jsondata["url"])
        else
            JsonTable["data"] = "updata";
            JsonTable["status"] = "alike";
            JsonTable["version"] = version2;    
        end
        client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) end)   
        JsonTable = {}  
    elseif  jsondata["cmd"] ~= nil and jsondata["cmd"] == "start" then
            if  file.open("version1.lua", "w+") then
                file.write(version1)
                file.close() 
            end
            JsonTable["data"] = "updata";
            JsonTable["status"] = "start";
            print(data)
            client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) node.restart(); end)   
            JsonTable = {} 
    elseif  jsondata["cmd"] ~= nil and jsondata["cmd"] == "model" then                   
            JsonTable["data"] = "updata";
            JsonTable["status"] = "model";
            JsonTable["model"] = model;
            print(data)
            client:publish(PublishTopic,sjson.encode(JsonTable), 0, 0, function(client) end)   
            JsonTable = {}         
    end 
end
復制代碼

 

 

 

 

 

 

 

 

 

 

 

 

其實就這么簡單就可以用HTTP訪問下載文件了

 

 

 

其實我學習用TCP實現HTTP功能是為了想用HTTP下載大文件,最終是為了實現遠程更新單片機程序,所以我為了讓程序穩定可靠,我必須深入了解HTTP

 

 

 

 

先看用WIFI模塊自帶的HTTP API下載大文件

 

 

 

復制代碼
http.get("http://47.92.31.46/hardware/wifi1/Progect.hex", nil, function(code, data)
    if (code < 0) then
      print("HTTP request failed")
    else
      print(code, data)
    end
 end)
復制代碼

 

 

 

 

 

 

 

 

 

 

 

 直接報錯說數據量太大

 

然而我用TCP調試助手發指令下載的時候發現了個問題

 

 

 

 

 第一   下載下來了

 

第二    我監聽了一下網絡數據,發現

 

 

 

 

 其實Apache服務器默認就會把大文件分段傳輸過來

 

然后我就做了個WIFI用TCP實現HTTP,然后下載

 

復制代碼
wifi.setmode(wifi.STATIONAP)
apcfg={}
apcfg.ssid="qqqqq"
apcfg.pwd="11223344"
wifi.sta.config(apcfg)
wifi.sta.autoconnect(1)

ClientConnectedFlage = 0
TcpConnect = nil
tmr.alarm(1, 10000, 1, function()

    if  ClientConnectedFlage == 0 then
        Client = net.createConnection(net.TCP, 0) 
        Client:connect(80,"47.92.31.46")

        Client:on("receive", function(Client, data) 
            uart.write(0,data)
        end)
        
        Client:on("connection", function(sck, c) 
            ClientConnectedFlage = 1
            TcpConnect = Client
            print("Link OK")
            tmr.stop(1)

            Client:on("disconnection", function(sck, c) 
                ClientConnectedFlage = 0
                TcpConnect = nil
                tmr.start(1)
            end)

            TcpConnect:send("GET /hardware/wifi1/Progect.hex HTTP/1.1\r\nConnection: keep-alive\r\nHost: 47.92.31.46\r\n\r\n")
            
        end)

        if  ClientConnectedFlage == 0 then
            print("Link Error")
        end
    end
end)


uart.on("data",0,function(data) 
    
end, 1)


printip = 0
wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T)
    printip = 0
end)


wifi.eventmon.register(wifi.eventmon.STA_GOT_IP, function(T)
   if printip == 0 then
      print("+IP"..T.IP)
   end
   printip = 1
end)
復制代碼

 

 

 

 

 

 

 毫無壓力的全部下載下來了.

 

所以我才知道,WIFI模塊里面寫的HTTP是把所有分段過來的數據全部接收到一個數組里面再調用回調....然而就會造成內存不足

 

用TCP實現HTTP的時候是接收一段打印出來一段,並不是把所有的數據全部放到一個數組里面,然后打印.....

 

經過這一次,我感覺我以后用HTTP的時候還是直接用TCP來實現,主要還是很簡單,而且還能預防再次出現內存問題....

https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.3e3b1deb80xzYz&id=569295486025

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM