django 搭建 webserver 让 ESP8266 通过 http 完成 OTA 升级

ESP8266 代码

#include 
#include 
#include 
#include 
#include 
#define USE_SERIAL Serial

const char* ssid = "hhahhah";//填上wifi账号
const char* password = "hahahh";//填上wifi密码

ESP8266WiFiMulti WiFiMulti;

void setup() {

    USE_SERIAL.begin(115200);
    // USE_SERIAL.setDebugOutput(true);

    USE_SERIAL.println();
    USE_SERIAL.println();
    USE_SERIAL.println();

    for(uint8_t t = 4; t > 0; t--) {
        USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
        USE_SERIAL.flush();
        delay(1000);
    }

    WiFi.mode(WIFI_STA);
    WiFiMulti.addAP(ssid, password);
}

void loop() {
    // wait for WiFi connection
    if((WiFiMulti.run() == WL_CONNECTED)) {

        t_httpUpdate_return ret = ESPhttpUpdate.update("http://192.168.1.5/api/health/check_update"); // 编译好的固件文件
        USE_SERIAL.println("************* start *************");
        USE_SERIAL.println(ret);
        USE_SERIAL.println("************* end *************");
        switch(ret) {
            case HTTP_UPDATE_FAILED:
                USE_SERIAL.printf("HTTP_UPDATE_FAILD Error (%d): %s", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str());
                break;

            case HTTP_UPDATE_NO_UPDATES:
                USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES");
                break;

            case HTTP_UPDATE_OK:
                USE_SERIAL.println("HTTP_UPDATE_OK");
                break;
        }
    }

    
}

 

 

django  端 地址

#-*-coding:utf-8-*-
import hashlib
from django.http import  JsonResponse
from rest_framework.decorators import api_view
from django.views.decorators.csrf import csrf_exempt
from django.http import StreamingHttpResponse,HttpResponse,FileResponse

# 检查服务是否健康
@api_view(['GET'])
@csrf_exempt
def check_health(request):
    body = {
        'code':0,
        'msg':'OK',
    }
    return  JsonResponse(body)

# 检查服务是否升级
@api_view(['GET'])
@csrf_exempt
def check_update(request):
    # 检查版本号
    print('HHHHHHHHHHHHHHHHHHHHHH')
    print(request)
    print(request.META)
    print('HHHHHHHHHHHHHHHHHHHHHH')
    current_version = '0.9'
    if request.method == 'GET':
        # version = request.GET.get('version', default='0.9')
        version = '1.0'
        if  version <= current_version:
            # 不需要升级
            body = {
                'code': 0,
                'msg': 'OK',
            }
            response = HttpResponse(status=304)
            SERVER_PROTOCOL = request.META['SERVER_PROTOCOL']
            response[SERVER_PROTOCOL + ' 304 Not Modified'] = 304
            return response
        else:
            file = open('staticfile/sketch_testOTA.ino.bin', 'rb')
            fcont = file.read()
            file.close()
            file = open('staticfile/sketch_testOTA.ino.bin', 'rb')
            response = FileResponse(file)
            response['Content-Type'] = 'application/octet-stream'
            response['Content-Disposition'] = 'attachment;filename="sketch_testOTA.ino.bin"'
            # SERVER_PROTOCOL = request.META['SERVER_PROTOCOL']
            # response[SERVER_PROTOCOL + ' 200 OK'] = 200

            response['Content-Length'] = len(fcont)
            # response['Content-Length'] = len(fcont)
            #334902 334688
            print(response['Content-Length'])
            # response['x-MD5'] = hashlib.md5(fcont)

            # header($_SERVER["SERVER_PROTOCOL"].
            # ' 200 OK', true, 200);

            # header('Content-Length: '.filesize($path), true);
            # header('x-MD5: '.md5_file($path), true);
            # readfile($path);

            return response

 

参考的资料

主要:

http://www.yfrobot.com/wiki/index.php?title=OTA_Updates

https://arduino-esp8266.readthedocs.io/en/latest/ota_updates/readme.html#id5

https://www.bakke.online/index.php/2017/06/02/self-updating-ota-firmware-for-esp8266/

https://zhuanlan.zhihu.com/p/47557855

https://blog.csdn.net/dpjcn1990/article/details/92830412

 

次要:

https://blog.csdn.net/liwei16611/article/details/81051909

你可能感兴趣的:(ESP8266,家庭自动化项目,ESP8266家庭自动化项目,ESP,8266,英文)