Cov txheej txheem:

Ntiv Lub Vev Xaib!: 4 Kauj Ruam
Ntiv Lub Vev Xaib!: 4 Kauj Ruam

Video: Ntiv Lub Vev Xaib!: 4 Kauj Ruam

Video: Ntiv Lub Vev Xaib!: 4 Kauj Ruam
Video: Saib Mis paub Poj niam (hluas nkauj) tus yam ntxwv lub siab 2024, Kaum ib hlis
Anonim
Image
Image
Pushbutton rau Arduino
Pushbutton rau Arduino

Ntiv Lub Vev Xaib yog cuam tshuam rau cov vev xaib los ntawm qhov chaw/qhov chaw tiag.

Xav paub ntau ntxiv txog nws:

makker.hu/RingTheWeb/

Koj yuav tsum tau:

  • 1 lub khawm
  • 10 k resistor
  • Arduino (txhua yam)
  • cov kab
  • lub computer me me, lub zog qis - qhov no yog RPi
  • nkag mus rau server lossis khoos phis tawj nrog IP pej xeem nrog node.js
  • lub vev xaib

Cov kauj ruam:

  1. Pushbutton rau arduino
  2. Arduino rau Raspberry
  3. Raspberry rau server
  4. Lub vev xaib rau server

Kauj ruam 1: Pushbutton rau Arduino

Ua ntej koj xav tau Arduino thiab lub laub khawm!

Txhua yam ntawm lawv tuaj yeem ua tau, nws nyob ntawm koj xaiv.

Txhawm rau txuas lawv, thov ua raws txoj cai khawm qhia ntawm Arduino.

Nov yog Arduino code:

// Arduino code rau nyeem tus lej digital thiab xa tus nqi mus rau qhov chaw nres nkoj

// Balázs Kovács, 2018. void setup () {Serial.begin (9600); // qhib qhov chaw nres nkoj serial pinMode (8, INPUT); // txuas khawm rau Pin 8} int counter = 0; // qee yam siv tom qab tsis muaj dab tsi voj () {yog (digitalRead (8) == 1) {// kos tus pin 8 xwm txheej Serial.write ("8"); } ncua (100); txee ++; yog (counter = 20) {// txhua 20x100 = 2000ms -> counter = 0; Serial.write ("0"); // xa ib qho "kuv twb muaj" cov lus rau server}} // Qhov ntawd yog txhua yam!

Kauj Ruam 2: Arduino rau Raspberry

Arduino rau Raspberry
Arduino rau Raspberry

Tam sim no peb tuaj yeem txuas Arduino rau lub khoos phis tawj. Hauv qhov no peb siv Raspberry, vim tias nws lub zog qis.

Txuas nws ntawm USB lossis ncaj qha nrog RX-TX pins, piav qhia ntawm no.

Tom qab ntawd teeb tsa node.js thiab npm raws li tau piav qhia ntawm no. Cov ntsiab lus tseem ceeb yog:

curl -sL https://deb.nodesource.com/setup_8.x | sudo -E tsoo -

thiab ces

sudo apt -tau nruab -y nodejs

Npm (Node.js tus thawj tswj hwm pob) xav tau socket.io-tus thov kev pabcuam thiab cov qauv txheej txheem, yog li nruab lawv:

npm nruab socket.io-client

npm nruab serialport

Qhib thiab khaws qee yam.js cov ntaub ntawv nrog cov cai hauv qab no:

// pib qhov socket.io kev txuas:

qhov socket; var io = xav tau ('socket.io-client'); qhov = io ("https://yourserver.com:port"); // yog tias kev txuas mus rau server tau ua tiav: socket.on ('txuas', ua haujlwm () {socket.send ("Kuv nyob ntawm no!"); console.log ("txuas nrog server");}; // pib qhov kev sib txuas lus chaw nres nkoj, NB /dev = ttyACM0 tuaj yeem hloov pauv: var SerialPort = xav tau ('serialport'); var serialPort = tshiab SerialPort ('/dev/ttyACM0', {baudRate: 9600}); // Yog tias qee yam los ntawm Arduino, xa cov lus sib txawv // mus rau tus neeg rau zaub mov raws li nws serialPort.on ('cov ntaub ntawv', ua haujlwm (cov ntaub ntawv) {console.log ('Cov ntaub ntawv:', data.toString ('ascii')); yog (data.indexOf ('8')! ==-1) {socket.send ('/RingTheBell 1');} yog (data.indexOf ('0')! ==-1) {socket. xa ('/client1 1');}}); // Nyeem cov ntaub ntawv uas muaj - kuv xav tias nws tsis tsim nyog serialPort.on ('nyeem tau', muaj nuj nqi () {console.log ('Cov ntaub ntawv:', port.read ());});

Tam sim no Koj yuav tsum teeb tsa lub server sab node.js code ib yam nkaus, kom txog thaum ntawd koj tuaj yeem pib thiab sim cov ntawv los ntawm

node./something.js

Yog tias muaj qee yam tsis raug, thov qhia rau kuv paub!

Kauj ruam 3: Server-side Code

Server-sab Code
Server-sab Code

Ntawm sab server, peb xav tau node.js nrog socket.io server.

Yog li ntxiv nws nrog:

npm nruab socket-io

Tom qab ntawd Koj yuav xav tau cov ntawv zoo sib xws rau cov cai ntawm qib 2, nrog qhov sib txawv, tias nws tos rau kev sib txuas, thiab yog tias lawv nyob tam sim no, nws yuav tshaj tawm cov lus xa los ntawm tus neeg siv khoom rau txhua tus neeg siv khoom, hauv qhov no rooj plaub, rau cov neeg siv lub vev xaib…

Yog li, qhib servercript.js nrog cov hauv qab no:

var http = xav tau ('http'), io = xav tau ('socket.io'); // qhib qhov tsawg kawg http server. socket.io xav tau nws. var server = http.createServer (muaj nuj nqi (req, res) {res.writeHead (200, {'Cov ntsiab lus-Hom': 'ntawv/html'}); res.end ('nyob zoo');}); // tig tcp lub qhov (socket) rau - teeb koj qhov chaw nres nkoj! server.listen (7004, ua haujlwm () {console.log ("TCP server khiav ntawm chaw nres nkoj 7004");}); // txheeb xyuas tcp lus var socket = io.listen (server); socket.on ('kev sib txuas', ua haujlwm (tus thov kev pabcuam, rinfo) {client.broadcast.emit ('system', 'qee leej txuas nrog …'); client.on ('message', function (event) {console.log (event); // tshaj tawm cov lus rau txhua tus siv txuas nrog! socket.emit ('lus', xwm txheej);}); client.on ('txhua tus', ua haujlwm (xwm txheej) {}); client.on ('disconnect', muaj nuj nqi () {socket.emit ('message', 'somebody disconneted …');});});

Sim sim los sim nrog

node./serverscript.js

Yog tias tus neeg siv khoom tau dhau mus, Koj yuav tsum pom lawv cov kev sib txuas lus ntawm ob lub consoles. Yam tsawg kawg no:

Cov ntaub ntawv: 0

-qhia cov txheej txheem ib ntus tias Arduino-> Raspberry-> server kev sib txuas lus ua haujlwm.

thiab

Cov ntaub ntawv: 8

- qhia tias khawm nyob.

Kauj ruam 4: Kho lub Vev Xaib

Kho lub Vev Xaib
Kho lub Vev Xaib

Tam sim no peb tau npaj nrog 75%!

Ua kom tiav txoj haujlwm nyuaj nrog suav nrog cov cai rau lub vev xaib.

Nws yooj yim.

ua ntej, suav nrog socket.io tus thov kev pab:

tom qab ntawd tsim cov lus-tshuaj ntsuam xyuas qhov system:

qhov socket;

qhov = io ("yourserver.com:port"); socket.on ('txuas', ua haujlwm () {socket.send ('tus neeg siv tsis qhia npe - tus neeg siv lub vev xaib - txuas nrog!'); socket.on ('lus', ua haujlwm (msg) {// yog tias koj xav pom txhua cov lus, tsuas yog tsis pom zoo nws: // console.log (msg); yog (msg == "/RingTheBell 1") // ntawm no los tus lej los siv rau kev hais tawm qhov kev tshwm sim khawm: {document.body.style.background = "#ccc"; setTimeout (muaj nuj nqi () {document.body.style.background = "#000";}, 1000);}; yog (msg == ""/client1 1 ") {// ntawm no koj tuaj yeem tso qee yam uas ua rau cov xwm txheej txuas nrog cov neeg siv khoom};});

Voilá!

npaj txhij

Pom zoo: