Cov txheej txheem:

Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU: 14 Cov Kauj Ruam
Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU: 14 Cov Kauj Ruam

Video: Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU: 14 Cov Kauj Ruam

Video: Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU: 14 Cov Kauj Ruam
Video: Kev Hlub Tsis Khaws Zoo Cia Official MV by Dang Thao 2024, Hlis ntuj nqeg
Anonim
Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU
Kev Tswj Chaw Taws Teeb Timer Nrog NodeMCU

Nov peb yuav tsim lub sijhawm siv NodeMCU thiab Adafruit. Peb tab tom yuav nthuav tawm peb lub sijhawm ua haujlwm nrog cov hlua LED thiab peb yuav tuaj yeem tswj nws siv peb lub xov tooj lossis khoos phis tawj!

Peb lub hom phiaj:

Tsim lub sijhawm siv cov hlua LED uas peb tuaj yeem: pib, ncua thiab rov pib siv peb lub xov tooj lossis khoos phis tawj.

Khoom siv

Kho vajtse:

  • NodeMCU ESP 8266
  • Adafruit NeoPixel LED sawb

Tsev qiv ntawv:

  • Adafruit_NeoPixel.h
  • AdafruitIO_WiFi.h

Lwm yam:

Kev sib txuas WiFi zoo

Kauj Ruam 1: Cia Tsim Kev Pub Dawb hauv Adafruit

Cia Peb Tsim Khoom Noj Hauv Adafruit!
Cia Peb Tsim Khoom Noj Hauv Adafruit!

Tam sim no peb tau txais txhua yam peb xav tau, peb tau npaj pib tsim! Ua ntej tshaj plaws peb yuav tsum tsim cov khoom noj hauv Adafruit. Adafruit yog qhov kev pabcuam uas ua rau nws yooj yim heev rau peb txuas peb Arduino rau hauv internet thiab ua kom muaj txiaj ntsig zoo tswj cov chaw. Yog tias koj tsis tau ua, sau npe rau Adafruit.

Tam sim no cia peb tsim cov khoom noj tshiab

Peb tuaj yeem tso npe rau peb cov khoom noj txhua yam peb xav tau, Kuv hu nws 'timer'.

Dab tsi pub noj yog sau txhua yam peb ua hauv peb lub dashboard (uas peb yuav tsim hauv ib pliag) thiab xa cov ntaub ntawv ntawd mus rau peb pawg thawj coj, tab sis qhov ntawd yog qee yam peb yuav saib ib zaug peb muaj cov ntaub ntawv raug xa mus.

Kauj Ruam 2: Tam sim no Cia Ua Tus Dashboard

Tam sim no Cia Ua Tus Dashboard
Tam sim no Cia Ua Tus Dashboard
Tam sim no Cia Ua Tus Dashboard
Tam sim no Cia Ua Tus Dashboard
Tam sim no Cia Ua Tus Dashboard
Tam sim no Cia Ua Tus Dashboard

Tam sim no hauv tib txoj kev uas peb tau ua pub, peb yuav tsim Dashboard tshiab. Kuv yuav hu kuv lub dashboard: 'timer interface' vim tias qhov ntawd yog dab tsi peb lub dashboard yuav yog: kev cuam tshuam nrog cov nyees khawm los tswj peb lub sijhawm.

Kauj Ruam 3: Tau Txais Peb Qhov Tseem Ceeb

Tau Txais Peb Qhov Tseem Ceeb
Tau Txais Peb Qhov Tseem Ceeb

Yog tias peb xav txuas peb Arduino rau peb cov khoom noj peb yuav tsum tau txais peb tus yuam sij Adafruit, qhov no yog koj tus lej zais uas yuav ua kom ntseeg tau tias tsuas yog koj tuaj yeem txuas rau koj cov txau.

Koj tuaj yeem tau txais koj tus yuam sij los ntawm kev nyem lub pob AIO daj daj nyob rau sab xis saum toj ntawm koj lub vijtsam hauv Adafruit.

Txuag tus yuam sij no rau qee qhov, peb yuav xav tau nws tom qab.

Tsis txhob qhia koj tus yuam sij! Txwv tsis pub cov neeg uas muaj lub siab phem tuaj yeem txuas rau koj cov khoom noj thiab khoom siv.

Kauj Ruam 4: Qhib Pub Nyeem Piv Txwv

Qhib Qhov Pub Nyeem Piv Txwv
Qhib Qhov Pub Nyeem Piv Txwv

Tam sim no cia qhib peb Arduino IDE thiab pib ua cov txheej txheem. Ua ntej tshaj plaws peb yuav tsum muaj Adafruit IO Arduino lub tsev qiv ntawv teeb tsa.

Tsis paub yuav nruab cov tsev qiv ntawv li cas? qhov no yog phau ntawv qhia zoo los ntawm Adafruit: Arduino Libraries

Adafruit muaj ntau qhov ua piv txwv ua ntej rau peb siv thiab kawm los ntawm. Ib qho piv txwv no yog: adafruitio_21_feed_read. Koj tuaj yeem pom qhov piv txwv no ntawm no: Cov Ntaub Ntawv - Piv txwv Adafruit IO Arduino adafruitio_21_feed_read

(thov zam txim rau kuv cov lus Dutch hauv daim duab)

Kauj Ruam 5: Cia Teem Peb Qhov Kev Sib Txuas

Yog tias koj tau ua tiav qhib adafruitio_21_feed_read kos duab koj yuav tsum pom 2 tabs hauv koj daim duab: adafruitio_21_feed_read thiab teeb tsa.h. Cov duab kos no tau ua kom peb tuaj yeem txuas rau cov khoom noj uas peb tau ua hauv qib 1.

Txhawm rau txuas rau cov khoom noj no peb yuav tsum tau sau ob peb cov ntsiab lus hais txog peb li WiFi thiab peb Adafruit tus account,

Cia peb taug kev mus rau config.h, ntawm no peb yuav sau cov ntsiab lus hauv qab no:

Koj lub npe Adafruit:

#define IO_USERNAME "joopert"

Koj tus yuam sij Adafruit:

#define IO_KEY "1234567890abcdefghijklmnop"

Koj lub npe WiFi:

#Txhais WIFI_SSID "MyWifi"

Thiab koj tus password WiFi:

#define WIFI_PASS "aVerySecretPassword"

Tam sim no wb rov qab mus rau adafruitio_21_feed_read tab thiab sau rau hauv:

Koj Adafruit username… ntxiv:

#txhais FEED_OWNER "joopert"

Thiab thaum kawg koj lub npe pub khoom noj (los ntawm cov khoom noj peb tau ua hauv qib 1):

AdafruitIO_Feed *sharedFeed = io.feed ("timer", FEED_OWNER);

Kauj Ruam 6: Txheeb xyuas Kev Sib Txuas rau Adafruit IO

Txheeb xyuas Kev Sib Txuas rau Adafruit IO
Txheeb xyuas Kev Sib Txuas rau Adafruit IO

Txhawm rau txheeb xyuas yog tias cov kauj ruam peb tau ua ua ntej tau ua tiav kom raug, peb yuav xa peb cov duab kos rau NodeMCU. Yog tias koj qhib koj lub xov tooj ntawm tes, nws yuav tsum zoo ib yam li daim duab saum toj no. Ua ntej tshaj tawm tus lej saib xyuas tau qhia koj tias nws tau sim txuas rau koj li WiFi thiab rau Adafruit. yog tias ua tiav nws yuav tsum hais:

Adafruit IO txuas nrog

Nov yog xov xwm zoo! Tam sim no peb tuaj yeem pib ntxiv kev ua haujlwm rau peb lub sijhawm …

Kauj Ruam 7: Tso LED

Cia LED
Cia LED
Cia LED
Cia LED

Tam sim no nws yog lub sijhawm los qhib peb cov hlua LED!

Txuas koj cov hlua LED rau D5 PIN (yog tias koj tsis tuaj yeem pom D5 txuas nws mus rau lwm tus PIN thiab hloov LED_PIN hauv txoj cai).

Txhais LED sawb

Cov cai hauv qab no yuav ntxiv NeoPixel lub tsev qiv ntawv (kawm paub ntau ntxiv txog lub tsev qiv ntawv no ntawm no: https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-use) thiab paub tseeb tias peb Arduino paub qhov twg peb cov kab LED yog thiab muaj pes tsawg lub teeb nws muaj. Yog tias koj cov hlua LED muaj qhov sib txawv tshwj xeeb, hloov nws hauv txoj cai.

Ntxiv cov cai hauv qab no NTAU TSHAJ qhov khoob khoob ():

// teeb tsa NeoPixel kev sib txuas #suav nrog #ifdef _AVR_ #suav nrog #endif

#define LED_PIN D5 // Cov hlua LED txuas rau qhov twg?

#define LED_COUNT 30 // Nws muaj pes tsawg pixels?

#define BRIGHTNESS 50 // NeoPixel ci ci, 0 (min) txog 255 (max)

Adafruit_NeoPixel sawb (LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);

Pib teeb LED sawb

Tam sim no peb yuav pib peb cov hlua LED thiab teeb tsa qhov ci ci (qhov ci mus los ntawm 0 min txog 255 max, tab sis kuv pom zoo 50 … 255 yog qhov kaj tiag tiag).

Ntxiv cov cai hauv qab no hauv qhov tsis muaj teeb tsa teeb tsa ():

strip.begin (); // INITIALIZE NeoPixel strip object (REQUIRED) strip.show (); // Kaw tag nrho cov pixels ASAP strip.setBrightness (50); // Teeb ci rau li 1/5 (max = 255)

ledsOn (); // Hu rau lub luag haujlwm kom tig rau txhua LED ib ntawm ib qho

Qhib lub teeb

Tej zaum koj twb tau pom lawm tias peb tab tom hu lub luag haujlwm uas tseem tsis tau muaj, uas yuav yog peb ntu tom ntej ntawm kev sau npe. Yog tias peb xav tig peb lub teeb peb yuav tsum tsim 'rau lub voj' uas tig rau txhua tus LED ib tus zuj zus (tsis paub tias lub voj yog dab tsi? Txheeb nws tawm ntawm no: www.arduino.cc/reference/ en/lus/qauv/tswj-qauv/rau/).

Tam sim no peb tab tom yuav tsim txoj haujlwm: ledsOn ();

Ntxiv cov cai hauv qab no hauv qab no tsis muaj dab tsi handleMessage ():

void ledsOn () {rau (int i = 0; i <strip.numPixels (); i ++) {// Rau txhua lub pixel hauv strip … strip.setPixelColor (i, 0, 0, 255); // Teeb pixel cov xim rau xiav strip.show (); // Hloov kho kab txaij kom phim}}

Upload

Tam sim no yog qhov lom zem, cia peb upload peb cov cai rau NodeMCU…

Puas tsis muaj cov duab pom?: Koj puas tau txuas lub sawb rau tus lej PIN raug? Lossis tej zaum koj li internet txuas tau ploj mus.

Puas yog qee qhov pixels tsis tshwm?: Txheeb xyuas yog tias koj teeb tsa tus lej pixels raug cai!

Kauj Ruam 8: Ntxiv Sijhawm rau Timer

Tam sim no peb yuav ua kom ntseeg tau tias peb lub sijhawm ua tau zoo … zoo … sijhawm, qee yam.

Muaj ntau txoj hauv kev los ua sijhawm nrog Arduino, ib qho yog ncua (), qhov no nres qhov kev ua tiav ntawm peb pawg thawj coj rau lub sijhawm uas tau teev tseg. Peb yuav tsis siv qhov no txij li peb Arduino pib khov thaum lub sijhawm ncua, thiab peb xav kom peb pawg thawj coj hloov kho tas li. Yog li peb tab tom yuav siv Millis (), qhov no yog txoj hauv kev zoo tshaj plaws ntawm kev ua sijhawm hauv Arduino, nrog Millis () peb tseem tuaj yeem ua txoj cai nyob hauv nruab nrab.

Yog tias koj txaus siab rau qhov no ua haujlwm li cas koj tuaj yeem tshawb xyuas kab lus no:

Cia peb ntxiv cov cai rau Millis ():

Muab cov cai no SAWV DAWS teeb tsa teeb tsa ()

unsigned ntev yav dhau losMillis = 0; // yuav khaws lub sijhawm kawg cov hlua LED tau hloov kho tshiab nyob rau lub sijhawm = 1000; // ncua sijhawm ntawm peb lub sijhawm (milliseconds)

Thiab cia peb muab cov cai no tso rau hauv lub voj void ():

unsigned ntev currentMillis = millis ();// pib TIMER RYTHM ********************************************* ********* yog (currentMillis - previousMillis> = luv) {yav dhau losMillis = currentMillis;

// xaus TIMER RYTHM ********************************************* ***********

Qhov peb nyuam qhuav tau ua yog tsim rythm rau peb cov hlua LED kom tua LEDs, hauv cov kauj ruam tom ntej peb yuav ntxiv cov cai los ua qhov ntawd xwb.

Kauj ruam 9: Suav rov qab

Hauv cov kauj ruam no: peb yuav ua lub luag haujlwm uas yuav ua kom ntseeg tau tias txhua thib ob (lossis txhua lub sijhawm uas koj tau teeb tsa) peb kaw ib qho LED ntawm qhov kawg ntawm kab txaij kom txog thaum tag nrho peb cov LEDs tawm.

Peb yuav tsim 'int' nyob rau sab saum toj hu ua 'leds'. Qhov no yuav piav qhia tom qab peb lub sijhawm muaj pes tsawg leds nws tseem yuav tsum tua.

Ntxiv cov cai hauv qab no NTAU TSHAJ qhov khoob khoob ():

int leds = LED_COUNT-1; // tels pes tsawg pixels yuav tsum tau taws

Tam sim no cia ntxiv qhov ua haujlwm 'timer ();', qhov no yuav tig lub teeb txhua thib ob

Ntxiv cov cai hauv qab no hauv qab no qhov tsis muaj ledsOn ():

void timer () {strip.setPixelColor (leds, 0, 0, 0);

yog (leds> -1) {leds--; } lwm {blinkie (); }}

Peb xav muab qee cov lus tawm tswv yim rau tus neeg siv tias lub sijhawm ua tiav tom qab txhua lub teeb tau hlawv lawm. Nrog 'blinkie ();' LEDs yuav ntsais liab tom qab lub sijhawm ua tiav!

Ntxiv cov cai hauv qab no NTAU TSHAJ qhov khoob khoob ():

int blinkStatus = 1; // qhia rau blinkie (); ua haujlwm qis dua lub teeb yuav tsum nyob lossis tawm

Ntxiv cov cai hauv qab no hauv qab lub void timer ():

void blinkie () {yog (blinkStatus == 1) {

blinkStatus = 0; strip.clear (); } lwm {blinkStatus = 1; rau (int i = 0; kuv <strip.numPixels (); i ++) {// Rau txhua lub pixel hauv strip … strip.setPixelColor (i, 255, 0, 0); // Teeb pixel cov xim (hauv RAM) strip.show (); // Hloov kho kab txaij kom phim}}}}

Thaum kawg peb yuav tsum hu lub luag haujlwm hauv peb lub void loop ();, Nco qab TIMER RYTHM peb tau ntxiv hauv kauj ruam 8? Hauv qhov no yog cov lus peb yuav hu ua lub sijhawm ua haujlwm ();

TIMER RYTHM yuav tsum zoo li no:

// pib TIMER RYTHM ********************************************* *********

yog (currentMillis - previousMillis> = luv) {yav dhau losMillis = currentMillis; timer (); strip.show ();} // kawg TIMER RYTHM ************************************** **************************

Tam sim no upload cov cai no!

Tam sim no koj cov hlua LED yuav tsum tig tawm 1 LED txhua ob thiab ntsais liab thaum nws ua tiav …

Tam sim no cia peb ua qhov chaw tswj hwm niam txiv no!

Kauj Ruam 10: Ua Nws Kev Tswj Chaw Taws Teeb: Tshooj I

Ua Nws Kev Tswj Chaw Taws Teeb: Ntu I
Ua Nws Kev Tswj Chaw Taws Teeb: Ntu I
Ua Nws Kev Tswj Chaw Taws Teeb: Ntu I
Ua Nws Kev Tswj Chaw Taws Teeb: Ntu I

Peb tau txais peb lub sijhawm, txhua yam zoo thiab dandy tab sis kuv tau cog lus rau koj tias koj yuav tuaj yeem tswj nws nrog koj lub xov tooj txoj cai? Cia peb nkag mus rau theem kawg ntawm peb txoj haujlwm: ua kom peb lub sijhawm timer tswj tau.

Tseem tau txais koj li Adafruit tab qhib? Cia peb rov qab mus rau io.adafruit.com thiab cia peb mus rau peb lub timer interface dashboard thiab tsim qhov thaiv tshiab: TOGGLE

  1. Txuas lub toggle rau TIMER pub
  2. Teeb tus nqi ON rau: 1
  3. Teem tus nqi OFF rau: 0

Qhov laj thawj rau qhov no yog Adafruit tsis xa cov ntawv xa mus rau peb NodeMCU, tab sis tsuas yog tus lej.

Koj tseem tuaj yeem muab Toggle lub npe yog tias koj nyiam, tej zaum: 'On/Off switch'

Tam sim no cia peb rov qab mus rau Arduino IDE

Tsuas yog kom paub tseeb tias peb cov cai tau hloov kho tshiab, cia UPLOAD peb cov cai ib zaug ntxiv.

Qhib Tus Kws Saib Xyuas Ib Leeg thaum nws ua tiav kev rub tawm thiab Nias lub TOGGLE ntawm peb Adafruit dashboard ob peb zaug. Tam sim no peb yuav tsum pom qee yam zoo li qhov no tshwm sim hauv peb cov saib xyuas:

tau txais <- 0received <- 1 tau txais <- 0 tau txais <- 1 tau txais <- 0

Qhov no txhais tau tias peb tuaj yeem xa cov kev tswj hwm rau peb NodeMCU!

Yog tias koj tsis tau txais cov lus no, txheeb xyuas yog tias koj li Adafruit Cov Ntaub Ntawv hauv koj qhov config.h tseem raug.

Kauj Ruam 11: Ua Kom Nws Tswj Chaw Taws Teeb: Ntu II

Tam sim no txhua yam tseem tshuav yog ua qee yam nrog peb tau txais kev tswj hwm.

Txhawm rau ua qhov ntawd peb yuav tsum hloov peb TIMER RYTHM yog cov lus hais hauv qhov khoob khoob (); me ntsis.

Peb yuav qhia peb NodeMCU tias nws yuav tsum tsuas yog pib lub sijhawm thaum peb tau txais '1' (uas txhais tau tias: ON) los ntawm Adafruit.

Ua ntej cia peb sau cov ntaub ntawv Adafruit xa tuaj rau peb

Ntxiv cov cai hauv qab no NTAU TSHAJ qhov khoob khoob ():

koj adaData; // sau cov ntaub ntawv AdaFruit xa tuaj rau peb

Tam sim no cia peb sau cov ntaub ntawv ntawd

Ntxiv cov cai hauv qab no INSIDE qhov tsis muaj dab tsi handleMessage ():

adaData = data-> toInt ();

Cia peb hloov peb lub voj void ();

TIMER RYTHM yuav tsum zoo li no:

// pib TIMER RYTHM ********************************************* ********* yog (currentMillis - previousMillis> = luv) {yav dhau losMillis = currentMillis; yog (adaData == 0) {Serial.println ("kuv tsis khiav"); } lwm yog (adaData == 1) {timer (); } strip.show (); } // xaus TIMER RYTHM ******************************************** ************

UPLOAD koj tus lej…

Tam sim no koj yuav tsum muaj peev xwm tig lub timer los ntawm kev siv qhov hloov pauv hauv Adafruit!

Kauj Ruam 12: Ua Si Ib Leeg! Peb Ua Tiav … lossis Puas Yog Peb?

Tam sim no koj yuav tsum muaj lub sijhawm ua haujlwm zoo nyob deb! Yog tias nws yog rau koj ces ua tsaug rau koj los nrog txoj haujlwm no!

Yog tias koj xav txuas ntxiv me ntsis ntxiv, hauv ob peb kauj ruam tom ntej no peb yuav ntxiv RESET khawm rau peb lub sijhawm!

Kauj Ruam 13: EXTRA: Tsim Momentary Button

EXTRA: Tsim Khawm Momentary
EXTRA: Tsim Khawm Momentary

Cia peb rov qab mus rau peb lub dashboard. Tam sim no peb xav tsim lub BLOCK tshiab dua.

  1. Tsim lub Momentary khawm
  2. Txuas nws mus rau 'timer' pub
  3. Teeb tus nqi nias rau: 2
  4. Tshem tawm tus nqi tso tawm
  5. Muab nws cov xim zoo!

Yooj yim rau lub sijhawm tam sim no twb hu ua 'Reset'!

Thaum koj tau tsim qhov ntsuas no sim nws, lub pob ua haujlwm yog tias koj tau txais:

tau txais <- 2

Hauv koj tus lej saib.

Yog tias koj tsis pom cov lus no, sim rov xa daim duab dua

Kauj Ruam 14: EXTRA: Ntxiv qhov Pib Ua Haujlwm

Ua kom khawm rov pib ua txhua yam peb xav tau ntxiv qhov ua haujlwm uas rov pib dua txhua lub LEDs rov qab mus rau lub xeev qub.

Ntxiv cov cai hauv qab no hauv qab qhov tsis muaj qhov muag blinkie ():

void rerun () {leds = LED_COUNT; ledsOn (); }

Tam sim no txhua yam tseem tshuav yog hu rau peb txoj haujlwm, rau qhov peb yuav tsum hloov TIMER RYTHM ib zaug zaum kawg

TIMER RYTHM yuav tsum zoo li no:

// pib TIMER RYTHM ********************************************* ********* yog (currentMillis - previousMillis> = luv) {yav dhau losMillis = currentMillis yog (adaData == 0) {Serial.println ("im tsis khiav"); } lwm yog (adaData == 1) {timer (); } lwm yog (adaData == 2) {rerun (); } strip.show (); } // xaus TIMER RYTHM ******************************************** ************

UPLOAD koj tus lej…

Tam sim no koj yuav tsum tuaj yeem rov pib lub sijhawm los ntawm nias lub pob!

Pom zoo: