django 搭建 webserver 讓 ESP8266 通過 http 完成 OTA 升級

ESP8266 代碼

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266httpUpdate.h>
#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

發佈了251 篇原創文章 · 獲贊 73 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章