Cov txheej txheem:

Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom: 7 Kauj Ruam
Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom: 7 Kauj Ruam

Video: Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom: 7 Kauj Ruam

Video: Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom: 7 Kauj Ruam
Video: $5 WiFi Camera Setup | ESP32 Wifi Setup view on Mobile phone 2024, Hlis ntuj nqeg
Anonim
Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom
Teeb ESP8266 Tsis Siv Neeg Hloov Kho Neeg Siv Khoom

Coob leej neeg tam sim no siv ESP8266 hauv nws ntau lub tswv yim (ESP-01S, Wemos D1, NodeMCU, Sonoff thiab lwm yam) rau kev siv tshuab hauv tsev. Yog tias koj sau koj tus kheej cov cai (ib yam li kuv ua) hloov kho txhua qhov ntawm no cais txawm tias dhau ntawm OTA (dhau saum huab cua) dhau los ua qhov nyuaj me ntsis.

Kuv tus kheej, piv txwv li muaj 8x ESP-01S, 6x Wemos D1, 4x Sonoff Basic 12x Sonoff S20, 2x Sonoff SV thiab NodeMCU uas sib koom ua ke ib qho chaws hauv paus, yog li ntawd yog 33 yam khoom hauv txhua qhov hloov tshiab thaum kuv ua tus lej yooj yim hloov

Tab sis muaj txoj hauv kev yooj yim dua: Ib qho "hloov tshiab server". Qhov zoo tshaj plaws Arduino IDE + ESP8266 tub ntxhais muaj lub tsev qiv ntawv los ua haujlwm feem ntau (ESP8266httpUpdate), tab sis koj yuav tsum paub teeb tsa koj li server li cas thiaj ua haujlwm tau.

Cov Lus Qhia no qhia koj yuav siv NODE-RED server li cas, tab sis tib lub tswv yim siv rau ib qho server thev naus laus zis uas koj xaiv xws li Apache + PHP thiab lwm yam

Kauj Ruam 1: Yam Koj Xav Tau

  1. Arduino IDE Cov
  2. ESP8266 tub ntxhais
  3. Ib qho ESP8266 dev board nrog 1M lossis ntau dua flash RAM
  4. Lub Vev Xaib (txawm tias txo qis raspberry Pi yuav ua - Nws yog qhov kuv siv)
  5. (xaiv tau) mkspiffs cuab yeej yog tias koj xav pib kho tshiab SPIFFS cov ntaub ntawv kaw lus system

Kauj Ruam 2: Tsim Chaw Tso Khoom los tuav Binary Firmwares

Tsim qhov chaw cia khoom los tuav Binary Firmwares
Tsim qhov chaw cia khoom los tuav Binary Firmwares

Ntawm kuv tus neeg rau zaub mov, Kuv muaj daim nplaub tshev hu ua/home/pi/trucFirmware uas tuav ntau yam khoom siv ruaj khov thiab cov duab SPIFFS

Kuv khaws cais cais cais rau txhua hom kho vajtse (los ntawm cov ntaub ntawv ib qho nrog ob peb #defines) thiab thaum qhov kev tshaj tawm tshiab tau npaj tiav Kuv siv Arduino IDE "kos duab/Export suav ua Binary" ntawv qhia zaub mov rau txhua lub hom phiaj ntaus ntawv. txawm hais tias muaj 5 yam kho vajtse sib txawv, tsuas muaj ob lub SPIFFS binaries: 1M thiab 4M version - tsim nrog mkspiffs cuab yeej - txij li txhua lub cuab yeej muaj 1M lossis 4M flash.

Kauj Ruam 3: Tsim Binaries

Siv Arduino IDE cov ntawv qhia zaub mov xaiv sketch/Export Compiled Binary, tsim cov firmware uas yuav muab tso rau hauv lub cuab yeej thaum nws thov nws los ntawm hloov tshiab server.

Yog tias koj xav tau SPIFFS binary koj yuav xav tau nruab mkspiffs cuab yeej.

Thaum koj muaj nws, tsim SPIFFS binary yog qhov yooj yim. Kuv muaj cov kab ntawv ib kab rau 1M version uas siv tus lej lej raws li qhov ntsuas (%1)

mkspiffs -c cov ntaub ntawv/ spiffs_%1_1M.bin

thiab lwm qhov rau 4M version:

mkspiffs -p 256 -b 8192 -s 0x0FB000 -c cov ntaub ntawv/ spiffs_%1_4M.bin

Tom qab ntawd kuv theej tag nrho cov ntawv sau ua ke thiab SPIFFS.binary cov ntaub ntawv hla mus rau qhov chaw cia khoom

Kauj Ruam 4: Tsim Server Flow

Tsim cov Server Flow
Tsim cov Server Flow

Kuv tabtom siv NODE-RED, tab sis cov laj thawj yooj yim yuav zoo ib yam ntawm cov thev naus laus zis thev naus laus zis / lus.

a) Txheeb xyuas qhov url uas yuav mloog rau ESP8266httpUpdate thov. Kuv tus raspberryPi serevr nyob ntawm 192.168.1.4 thiab mloog ntawm chaw nres nkoj 1880 rau /hloov tshiab nrog hom kho vajtse txuas ntxiv. Yog li yog tias kuv yuav thov binary rau Wemos D1 Mini, qhov url xaus li:

192.168.1.4:1880/update/d1_mini

b) Tsim cov cai los daws cov laj thawj hauv qab no:

ESP8266: "Nyob zoo, Kuv tab tom khiav firmware version a.b.c, koj puas muaj tus tshiab dua?" Neeg rau zaub mov: "Cia kuv pom … ah yog kuv muaj a bd - ntawm no nws tuaj …"

Yog tias qhov hloov pauv tshiab muaj tshwm sim tus neeg rau zaub mov tsuas yog xa nws raws li lub nra ntawm cov ntaub ntawv binary hauv http teb. ESP8266httpUpdate chav kawm ua qhov nyuaj ntawm kev luam cov binary rau hauv lub cim xeeb, hloov kho qhov chaw nyob ntawm lub khoos phis tawj rau tus lej tshiab dua li (yog tias thov) rov pib dua lub cuab yeej los ua tus lej tshiab.

Yog tias ntawm qhov tod tes tsis muaj qhov hloov dua siab tshiab, nws teb nrog http 304 yuam kev uas hais tau zoo: "Kuv tsis muaj dab tsi rau koj" thiab koj cov cai tseem ua haujlwm zoo li qub.

Kauj Ruam 5: Ntxiv Server Logic

Thawj qhov ntawm qhov ntws "mloog" rau qhov thov http rau url https://192.168.1.4:1880/ hloov kho tshiab nrog cov cuab yeej ntaus ntawv txuas ntxiv. Nws hla qhov no mus rau "Tsim txoj hauv kev nrhiav" txoj haujlwm uas muaj cov lej javascript hauv qab no:

msg.type = msg.req.params.type; var h = msg.req.headers; msg.version = h ["x-esp8266-version"];

msg.mode = h ["x-esp8266-mode"];

yog (msg.mode == "sketch") {msg.payload = "/home/pi/trucFirmware/*. ino."+msg.type+". bin"; } lwm {var sz = h ['x-esp8266-chip-size']; msg.payload = "/home/pi/trucFirmware/spiffs _*_"+(sz/1048576)+"M.bin"; } rov qab msg;

Qhov no tsuas yog teeb tsa txoj hauv kev tsim nyog nrog wildcard rau sys muaj nuj nqi uas ua raws, uas yooj yim sau

ls -r ib

Cov txiaj ntsig tau muab pub rau "Sib piv cov qauv" ua haujlwm ntawm:

var f = msg.payload.split ("\ n") [0]; msg.filename = f;

yog (msg.mode == "sketch") {

f = f.replace ("/home/pi/trucFirmware/truc_", ""); f = f.replace (". ino."+msg.type+". bin", ""); } lwm {f = f.replace ("/home/pi/trucFirmware/spiffs_", ""); f = f.replace (/_ / dM \.bin/, ""); }

yog (msg.version <f) {

node.warn ("yuav tsum tau hloov kho tshiab");

node.warn ("yuav rov qab"+msg.filename); rov qab msg; } node.warn ("tsis muaj kev hloov kho tshiab"); msg.statusCode = 304; msg.payload = ;

rov qab msg;

Qhov hloov pauv tom qab ntawd ua kom ntseeg tau tias 304 "tsis xav tau hloov tshiab" cov lus tau xa lossis qhov tseeb tshiab binary raug xa rov qab thiab xa rov qab mus rau lub cuab yeej.

Kauj Ruam 6: Ntxiv Code rau Sketch kom thov hloov kho tshiab

Cov duab kos yuav tsum muaj cov lej hauv qab no suav nrog nws kom nws yuav hloov kho tshiab lub sijhawm tom ntej koj nce tus lej lej:

#suav nrog

#define TRUC_VERSION "0_4_99"

#define SPIFFS_VERSION "0_5_0"

// THIS_DEVICE tau teeb tsa ua ntej nyob ntawm ntau yam kev suav sau-lub sijhawm piav qhia // uas thaum kawg txheeb xyuas hom hw, piv txwv li #define THIS_DEVICE "d1_mini" const char * updateUrl = "https://192.168.1.4:1880/update/" THIS_DEVICE; // qhov no yog kuv tus neeg rau zaub mov Raspberry Pi, xyoo 1880 yog lub chaw nres nkoj NODE-RED default // /hloov tshiab yog qhov url kuv xaiv rau tus neeg rau zaub mov kom "mloog" rau, ua raws li hom ntaus ntawv … bool actualUpdate (bool sketch = cuav) {Txoj hlua msg; t_httpUpdate_return ret; ESPhttpUpdate.rebootOnUpdate (tsis tseeb); yog (kos duab) {ret = ESPhttpUpdate.update (updateUrl, TRUC_VERSION); // **************** Nov yog kab ntawv "ua lag luam"} lwm qhov {ret = ESPhttpUpdate.updateSpiffs (updateUrl, SPIFFS_VERSION); } yog (ret! = HTTP_UPDATE_NO_UPDATES) {yog (ret == HTTP_UPDATE_OK) {

Serial.printf ("YUAV TSUM XYUAS TUS KHEEJ");

rov muaj tseeb; } lwm {yog (ret == HTTP_UPDATE_FAILED) {

Serial.printf ("Ua tsis tau tejyam");

}}} rov tsis tseeb; }

Kauj Ruam 7: Thaum kawg, Pib Hloov Kho

Thaum lub sijhawm khau raj, lossis tej zaum yuav teb rau MQTT cov lus (raws li kuv ua) khiav cov cai hauv qab no:

yog (_actualUpdate (tseeb)) ESP.restart ();

// lossis rau SPIFFS …

yog (_actualUpdate (cuav)) ESP.restart ();

Cov cuab yeej yuav hloov kho nws tus kheej thiab rov pib ua haujlwm tshiab qhov chaws los ntawm tus neeg rau zaub mov. Nws yooj yim dua rau kuv ntau dua li hloov kho tus kheej li 33 yam khoom!

Ntau cov ntaub ntawv muaj txiaj ntsig ntau ntxiv txog Tsev Automation, IOT thiab cov phiaj xwm ESP8266 tuaj yeem pom ntawm Kuv Blog

Pom zoo: