7-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇(TCP实现HTTP访问下载文件,明白底层如何实现的,地基稳才踏实)

看了好多文章.....唉,还是自己亲自动手用网络监控软件测试吧

先看这个节安装WEB服务器.....安装好以后就可以用HTTP访问电脑文件了

6-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案升级篇-优化升级(安装Apache (Web服务器)软件,测试HTTP)     事先不知道HTTP,最后先看这个

其实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访问下载文件了 .....您自己百度看看别人写的...唉,都抓不住咱真正要的需求,估计那些人只是搞上位机出身,只知道是那么用

,不知道考虑咱单片机硬件到底如何访问......

猜你喜欢

转载自www.cnblogs.com/yangfengwu/p/10793883.html
今日推荐