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
Connection: keep-alive
Host: 47.92.31.46

")
            
        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

原文地址:https://www.cnblogs.com/yangfengwu/p/10360618.html