Cov txheej txheem:

Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv: 5 Kauj Ruam
Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv: 5 Kauj Ruam

Video: Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv: 5 Kauj Ruam

Video: Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv: 5 Kauj Ruam
Video: PIR motion sensor | How do they Work? 2024, Hlis ntuj nqeg
Anonim
Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv
Tswj Arduino Siv Telegram Bot Tsis Muaj Kev Kho Vajtse Ntxiv

Muaj ntau yam uas koj tuaj yeem ua nrog Arduino, tab sis koj puas tau xav txog kev tswj hwm koj Arduino siv Telegram bot?

  • Arduino UAS
  • Node.js ntsia koj lub PC
  • Qee lub cuab yeej tswj tau (Peb siv Arduino's on-board LED ntawm tus pin 13 ntawm no, tab sis xav tias dawb nug txog txhua yam koj xav ua hauv cov lus)

Kauj ruam 1: Nruab Node.js ntawm koj lub PC

Nruab Node.js ntawm koj lub PC
Nruab Node.js ntawm koj lub PC

Node.js yog JavaScript runtime, tab sis tsis txhob poob siab yog tias koj tsis muaj lub programming tom qab. Kuv tau npaj qhov koj xav tau thiab koj tuaj yeem rub tawm lawv. Hauv qhov haujlwm no, peb yuav siv Node.js rau:

  1. Tsim Telegram bot
  2. Tswj Arduino

INSTALL NODE. JS:

Mus rau nom Node.js nplooj ntawv rub tawm thiab rub tawm pob installer kawg raws li koj li OS.

*Nco ntsoov tus lej tshiab tshaj plaws raws li tau hais nyob rau sab saum toj ntawm nplooj ntawv rub tawm, peb yuav siv nws hauv qib tom ntej.

Tom qab rub tawm pob thiab teeb tsa nws, koj yuav tsum txheeb xyuas seb Node.js tau teeb tsa tag lossis tsis yog.

NCO NTSOOV YUAV TSUM YOG PACKAGE INSTALLED KAWG:

Yog tias koj nyob ntawm windows, qhib CMD thiab yog tias koj nyob ntawm macOS qhib lub davhlau ya nyob twg thiab ntaus:

node -v

Tam sim no koj yuav tsum pom cov ntawv uas koj tau mus ntsib ntawm Node.js nplooj ntawv rub tawm, luam tawm ntawm no.

Txwv tsis pub kuv muab qee cov lus qhia hauv qab no los sim dua:

  • Qhov rai
  • macOS os

Kauj Ruam 2: Tshooj Arduino

Arduino Tshooj
Arduino Tshooj

Hauv cov kauj ruam no tsuas yog qhov koj yuav tsum tau ua yog rub tawm ib qho tsim hauv Arduino IDE firmware ntawm koj Arduino Uno.

Tseem ceeb: Koj yuav tsum hloov kho koj li Arduino IDE mus rau qhov hloov tshiab tshaj plaws los ntawm Arduino.

Ntshuam firmata tsev qiv ntawv:

Cov ntawv qhia saum toj kawg nkaus >> Cov ntaub ntawv >> Piv txwv >> Firmata >> StandardFirmata

Txheeb xyuas thiab xa cov cai ntawm Arduino:

1. Cov ntawv qhia zaub mov saum toj kawg nkaus >> Kos duab >> Txheeb Xyuas/Sau

2. Ntawv qhia zaub mov saum toj kawg nkaus >> Sketch >> Upload

Txhua yam ua tiav.

Kauj Ruam 3: Tau Telegram Bot Token

Tau Telegram Bot Token
Tau Telegram Bot Token

Qhib Telegram thiab tshawb rau @BotFather lossis nyem qhov no los qhib lub bot ntawm xov tooj no.

  1. Ntaus /newbot thiab ntaus nkag
  2. Xaiv lub npe rau koj tus bot
  3. Xaiv lub npe siv rau koj tus bot. Nws yuav tsum xaus rau hauv 'bot'
  4. Tam sim no bot xa koj qee cov ntaub ntawv suav nrog koj li API nkag mus token. Sau nws, peb, yuav siv tus cim no hauv cov kauj ruam tom ntej

Kauj Ruam 4: Tsim Node.js Project

CREATE A NEW NODE. JS PROJECT

Rau tsim Node.js txoj haujlwm thiab teeb tsa cov qauv peb yuav tsum siv davhlau ya nyob twg, yog li yog koj nyob ntawm Windows, siv CMD thiab thaum siv macOS siv Terminal rau txhua qib hauv qab no.

1. Tsim daim nplaub tshev qhov chaw rau txoj haujlwm

2. CD (Hloov Cov Ntawv Teev Npe) rau cov ntawv uas nyuam qhuav tsim. Piv txwv li yog tias koj lub npe ntawv ntawd 'TelegramBot' thiab muab tso rau ntawm lub desktop, sau qhov no hauv lub davhlau ya nyob twg thiab ntaus nkag mus:

cd Desktop/TelegramBot

3. Rau tsim Node.js project sau cov lus txib hauv qab no:

npm pib

4. Teb qee cov lus nug suav nrog lub npe, lus piav qhia, daim ntawv tso cai thiab lwm yam yog tias koj tsis paub yuav teb dab tsi, tsuas yog ntaus nkag. Hauv qhov no lo lus nruab nrab ntawm kab lus yuav raug siv raws li tus nqi qub.

5. Tam sim no koj yuav tsum pom 'pob.json' cov ntaub ntawv tsim hauv koj daim nplaub tshev thiab qhov no txhais tau tias koj tau tsim Node.js txoj haujlwm tiav.

Kauj ruam 5: Coding

Peb xav tau sau qee cov lej ntawm no. Cia peb piav qhia qee lub tsev qiv ntawv siv:

  1. Node Telegram Bot API los lis telegram bot API thov.
  2. Johnny-Tsib Platform txhawm rau txuas rau Arduino.

Txhua yam koj xav paub txog cov tsev qiv ntawv no tuaj yeem pom hauv lawv cov vev xaib txuas. peb tsuas yog siv lawv hauv peb cov cai tab sis tag nrho cov ntaub ntawv ntawm lawv yog ntawm lawv cov blog.

Kuv siv Microsoft Visual Studio Code rau kev kho qhov chaws. tab sis koj tuaj yeem siv lwm yam uas koj nyiam. 1. Qhib cov cai kho software thiab tsim cov ntaub ntawv hu ua 'index.js' (lossis txhua yam koj tau nkag mus rau ntu nkag ntawm Node.js txoj haujlwm tsim theem) hauv cov npe ntsig txog.

2. Sau cov cai:

var tsib = xav tau ("johnny-five"); cia TelegramBot = xav tau ('node-telegram-bot-api'); const token = '############################################'; const bot = TelegramBot tshiab (token, {pov npav: tseeb}); var board = tshiab tsib. Board (); const answerCallbacks = {};

bot.on ("message", muaj nuj nqi (msg) {

hu rov qab = answerCallbacks [msg.chat.id]; yog (hu rov qab) {rho tawm callCallbacks [msg.chat.id]; rov hu rov qab (msg); }});

board.on ("npaj tau", muaj nuj nqi () {

var coj = tshiab tsib. Led (13);

bot.on ('lus', (msg) => {

const chatId = msg.chat.id; cov ntawv = lus.text; yog (ntawv == '/pib') {pib (chatId, coj); }}); });

muaj nuj nqi pib (chatId, coj) {

bot.sendMessage (chatId, "Arduino Control Panel", getKeyboardOptions ());

bot.on ("callback_query", (callbackQuery) => {

const lus = callbackQuery.message; bot.answerCallbackQuery (callbackQuery.id).then (() => {const data = callbackQuery.data; if (data == 'turnon') {led.on ();} else if (data == 'turnoff') {coj.off ();}})}); }

muaj nuj nqi getKeyboardOptions () {

const options = {"reply_markup": {resize_keyboard: true, "inline_keyboard":

rov qab xaiv;

}

3. Hloov cov khoom ntiag tug tus nqi nrog ib qho uas koj tau sau rau hauv bot tsim theem

4. Txuag cov cai

5. Qhib davhlau ya nyob twg thiab sau cov lus txib hauv qab no:

npm kuv-txuag johnny-tsib node-telegram-bot-api

6. Tom qab txhim kho cov qauv, thaum koj Arduino txuas nrog USB chaw nres nkoj, hauv lub davhlau ya nyob twg nkag mus rau cov lus txib hauv qab no:

node index.js

7. Koj yuav tsum pom qee yam xws li:

1534514872949 Muaj /dev/cu.usbmodem1411

1534514872957 Txuas nrog /dev/cu.usbmodem1411 1534514876660 Repl Initialized >>

8. Qhib xov tooj thiab tshawb rau koj tus bot lub npe siv (lossis qhib nws los ntawm BotFather) thiab sau cov lus txib no:

/pib

9. Koj yuav tsum pom lub tswj vaj huam sib luag uas tuaj yeem tswj hwm Arduino LED nrog 'Qhib' thiab 'Tua tawm' cov lus txib, yog tias koj muaj hmoo txaus;)

Pom zoo: