Cov txheej txheem:

NodeMCU - MQTT Piv Txwv Piv Txwv: 4 Cov Kauj Ruam
NodeMCU - MQTT Piv Txwv Piv Txwv: 4 Cov Kauj Ruam

Video: NodeMCU - MQTT Piv Txwv Piv Txwv: 4 Cov Kauj Ruam

Video: NodeMCU - MQTT Piv Txwv Piv Txwv: 4 Cov Kauj Ruam
Video: Arduino esp8266 NodeMCU MQTT client 2024, Kaum ib hlis
Anonim
Image
Image

Zaj lus qhia no yuav qhia qhov pib siv MQTT raws tu qauv ntawm NodeMCU board. Peb siv MQTTBox ua MQTT tus neeg siv khoom ntawm no, thiab peb yuav siv NodeMCU kom ua tiav cov haujlwm hauv qab no:

Tshaj tawm "nyob zoo ntiaj teb" rau cov ncauj lus "tawmTopic" txhua ob vib nas this. Sau npe rau lub ncauj lus "inTopic", luam tawm cov lus tau txais. onboards LED. Tsov tawm onboard LED yog cov lus sau npe yuav yog "0".

Kev npaj: Osoyoo NodeMCU x1

USB Cable x1

Pc1 xwm

Arduino IDE (Versin 1.6.4+)

Txuas NodeMCU rau PC ntawm USB cable.

Kauj Ruam 1: Kev Txhim Kho Tsev Qiv Ntawv

Kev Txhim Kho Tsev Qiv Ntawv
Kev Txhim Kho Tsev Qiv Ntawv
Kev Txhim Kho Tsev Qiv Ntawv
Kev Txhim Kho Tsev Qiv Ntawv

Nruab PubSubClientlibrary

Peb xav tau nruab MQTT lub tsev qiv ntawv kawg (PubSubClient) sib txuas lus nrog MQTT tus tswv lag luam, thov rub lub tsev qiv ntawv los ntawm qhov txuas hauv qab no:

Unzip cov ntaub ntawv saum toj no, txav daim nplaub tshev unzipped rau Arduino IDE lub tsev qiv ntawv tais ceev tseg.

Qhib Arduino IED, koj tuaj yeem pom "pubsubclient" ntawm kab ntawv "Piv txwv".

Nruab MQTT Client:

peb yuav siv MQTTBox ua MQTT cov neeg siv khoom. thov rub tawm los ntawm:

Kauj ruam 2: Code

Code
Code
Code
Code

qhib Arduino IDE–> File–> Piv txwv li> pubsubclient–> mqtt esp8266, koj yuav tau txais cov qauv piv txwv.

Kho qhov chaws kom haum rau koj tus kheej WiFi thiab MQTT teeb tsa raws li kev ua haujlwm hauv qab no: 1) Hotspot Configration: Nrhiav hauv qab kab kab, tso koj tus kheej ssid thiab password rau ntawd.

const char* ssid = "your_hotspot_ssid"; const char* password = "your_hotspot_password";

2) MQTT Server Chaw Nyob, ntawm no peb siv dawb MQTT broker "broker.mqtt-dashboard.com". Koj tuaj yeem siv koj tus kheej MQTT tus broker URL lossis IP chaw nyob los teeb tsa saum mqtt_server tus nqi. Koj kuj tseem tuaj yeem siv qee tus pub dawb MQTT server los sim qhov haujlwm xws li "broker.mqtt-dashboard.com", "iot.eclipse.org" thiab lwm yam.

const char* mqtt_server = “broker.mqtt-dashboard.com”;

3) MQTT Client SettingsIf koj li MQTT broker xav tau clientID, lub npe siv thiab tus password lees paub tseeb, koj xav tau

hloov

yog (client.connect (clientId.c_str ()))

Rau

yog (client.connect (clientId, userName, passWord)) // tso koj tus clientId/userName/passWord ntawm no

Yog tias tsis yog, cia li khaws lawv ua lub hauv paus Tom qab ua li ntawd, xaiv hom pawg thawj coj saib xyuas thiab hom chaw nres nkoj raws li hauv qab no, tom qab ntawd rub cov duab kos rau NodeMCU.

  • Pawg thawj coj: "NodeMCU 0.9 (ESP-12 Module)"
  • CPU Zaus: "80MHz" Flash Loj: "4M (3M SPIFFS)"
  • Upload Ceev: "115200 ″
  • Chaw nres nkoj: Xaiv koj tus kheej Serial Port rau koj NodeMCU

Kauj Ruam 3: Kho MQTT Client (MQTTBOX)

Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)
Config MQTT Client (MQTTBOX)

Hauv theem no, peb yuav qhia yuav ua li cas thiaj tsim tau MQTT tus neeg siv khoom ntawm MQTTBox.

Qhib koj lub MQTTBox thiab nyem lub pob xiav kom ntxiv MQTT tus neeg siv khoom tshiab.

Kho qhov MQTT CLIENT SETTINGS raws li hauv qab no:

  • MQTT Client Name - Xaiv ib lub npe uas koj nyiam
  • Raws tu qauv - Xaiv “mqtt/tcp”
  • Tus tswv tsev - Ntaus koj "mqtt_server" hauv kab ntawv no, xyuas kom nws zoo ib yam li koj daim duab. (Peb siv "broker.mqtt-dashboard.com" ntawm no)
  • Khaws lwm qhov chaw raws li lub neej ntawd
  • Nyem rau txuag koj qhov kev hloov pauv.

Tom ntej no, koj yuav nkag mus rau nplooj ntawv tshiab yog tias txhua qhov teeb tsa saum toj no raug, "Tsis Txuas" yuav hloov mus rau "Txuas", koj MQTT tus neeg siv lub npe thiab Tus tswv lub npe yuav tshwm nyob rau saum toj ntawm nplooj ntawv no.

Cov ncauj lus teeb tsa: Nco ntsoov tias koj MQTT cov neeg siv tshaj tawm cov ncauj lus zoo ib yam li koj li Arduino kos duab kos npe sau npe cov ncauj lus (inTopic ntawm no). Ua kom ntseeg tau tias koj MQTT cov neeg siv khoom sau npe lub ncauj lus zoo ib yam li koj daim duab Arduino tshaj tawm cov ncauj lus (tawmTopic ntawm no).

Kauj Ruam 4: Txoj Haujlwm Khiav Haujlwm

Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam
Qhov Kev Pab Cuam Tshuam

Thaum qhov upload tiav, yog tias lub npe wifi hotspot thiab tus password nkag mus tau zoo, thiab MQTT broker txuas nrog, qhib Serial Monitor, koj yuav pom cov lus tshaj tawm "nyob zoo ntiaj teb" ntawm tus saib xyuas.

Tom qab ntawd qhib MQTT tus neeg siv khoom thiab tshaj tawm cov nyiaj them "1" rau lub ncauj lus, NodeMCU no yuav tau txais cov lus no los ntawm kev sau npe rau "inTopic", thiab LED yuav taws.

Tshaj tawm cov nqi "0" rau lub ncauj lus no, NodeMCU LED yuav raug muab tua.

Pom zoo: