Cov txheej txheem:
- Kauj ruam 1: Kev npaj
- Kauj Ruam 2: Kev Sib Txuas Duab
- Kauj ruam 3: Code
- Kauj ruam 4: MQTT Client Settings
Video: NodeMCU MQTT Iot Project - Hloov Khawm: 4 Kauj Ruam
2024 Tus sau: John Day | [email protected]. Kawg hloov kho: 2024-01-30 09:27
OSOYOO NodeMCU IOT Starter kit
Thov ua raws peb hauv facebook, nrhiav peb cov khoom tso tawm tshiab thiab qhia koj lub tswv yim thiab Video ntawm yuav siv cov khoom lag luam zoo li cas. Koj tuaj yeem tau txais nyiaj rov qab lossis muab khoom plig los ntawm peb! Facebook:
Youtube:
Hauv zaj lus qhia no, peb yuav txuas lub khawm hloov mus rau NodeMCU, thiab xa qhov hloov pauv mus rau MQTT broker. Thaum lub pob nyem, NodeMCU yuav tshaj tawm cov xwm txheej khawm "nias" rau MQTT tus neeg nqa khoom thiab MQTT tus neeg siv yuav sau npe rau cov lus no. Thaum lub laub khawm raug tso tawm, "tsis nias" yuav xa.
Kauj ruam 1: Kev npaj
Kho vajtse:
NodeMCU board x 1
Hloov khawm x 1
1 K resistor x 1
Breadboard x 1
Jumper xov hlau
Software:
Arduino IDE (version 1.6.4+)
ESP8266 Pawg Thawj Tswj Hwm Pob thiab Tsav Tsheb Chaw Nkoj
MQTT Client (MQTTBox ntawm no)
Tsev qiv ntawv Arduino: PubSubClient
Kauj Ruam 2: Kev Sib Txuas Duab
Hauv zaj lus qhia no, peb siv D2 (GPIO4) los tswj qhov hloov pauv, thov teeb tsa kho vajtse raws li kab duab txuas.
Nco tseg: 1k resistor siv los rub tawm tus neeg tawm tsam, Hauv qhov Circuit Court, thaum qhov hloov pauv raug kaw, NodeMCU cov tswv yim nyob ntawm tus lej muaj txiaj ntsig siab, tab sis thaum qhib qhib, rub tawm qis resistor rub tawm qhov hluav taws xob tawm. nqes mus rau hauv av (tus lej xoom tus nqi), tiv thaiv lub xeev tsis tau txiav txim siab ntawm lub tswv yim.
Kauj ruam 3: Code
Luam cov cai hauv qab no rau Arduino IDE:
/ * _ _ _ _ _ _ _ _ * / _ / / _) / _ / | | | | / _ / / _ / / _) _ / | / *| | _ | | _ | | _ | | | _ | | | _ | | | _ | ((_ | | _ | | | | | | * * / _/ (_/ / _/ / _ | / _/ / _ (_) _) _/ | _ | _ | _ | * (_/ * Use the NodeMCU xa hloov khawm xwm txheej rau MQTT tus neeg siv ntawm WiFi * Qhia URL: * CopyRight www.osoyoo.com */ #include #include
int BUTTON_PIN = D2; // khawm txuas nrog GPIO tus pin D1
// Hloov kho cov no nrog qhov tsim nyog haum rau koj lub network. const char*ssid = "********"; // muab koj lub wifi ssid ntawm no const char*password = "********"; // tso koj tus password wifi ntawm no. const char* mqtt_server = "broker.mqttdashboard.com"; // const char* mqtt_server = "iot.eclipse.org";
WiFiClient espClient;
PubSubClient tus thov kev pab (espClient); ntev lastMsg = 0; char lus [50];
void setup_wifi () {
ncua (100); // Peb pib los ntawm kev txuas mus rau WiFi network Serial.print ("Txuas rau"); Serial.println (ssid); WiFi.begin (ssid, password); thaum (WiFi.status ()! = WL_CONNECTED) {ncua (500); Serial.print ("."); } randomSeed (micros ()); Serial.println (""); Serial.println ("Txuas nrog WiFi"); Serial.println ("IP chaw nyob:"); Serial.println (WiFi.localIP ()); }
tsis muaj dab tsi hu rov qab (char* ntsiab lus, byte* them nyiaj, tsis tau kos npe ntev)
{} // kawg hu rov qab
void reconnect () {
// Loop kom txog thaum peb rov txuas nrog thaum (! Client.connected ()) {Serial.print ("Sim MQTT kev txuas …"); // Tsim cov neeg siv khoom tsis raug ID Txoj hlua clientId = "ESP8266Client-"; clientId += Txoj hlua (random (0xffff), HEX); // Sim txuas mus // yog tias koj MQTT tus broker muaj tus lej ID, tus neeg siv thiab tus lej nkag mus // thov hloov kab hauv qab no yog (client.connect (clientId, userName, passWord)) yog (client.connect (clientId.c_str ())) {Serial.println ("txuas nrog"); // ib zaug txuas nrog MQTT broker, sau npe hais kom ua yog tias muaj ib tus neeg siv khoom.subscribe ("OsoyooCommand"); } lwm {Serial.print ("ua tsis tiav, rc ="); Serial.print (client.state ()); Serial.println ("sim dua hauv 5 vib nas this"); // Tos 5 vib nas this ua ntej ncua sijhawm ncua (5000); }}} // kawg rov txuas ()
void teeb tsa () {
Serial.begin (115200); teeb_wifi (); client.setServer (mqtt_server, 1883); client.setCallback (hu rov qab); pinMode (BUTTON_PIN, INPUT); }
void voj () {
yog (! client.connected ()) {reconnect (); } client.loop (); ntev tam sim no = millis (); int xwm txheej; // xa xov txhua 2 thib ob yog (tam sim no - lastMsg> 2000) {lastMsg = tam sim no; xwm txheej = digitalRead (BUTTON_PIN); String msg = "Khawm xwm txheej:"; yog (xwm txheej == HIGH) {msg = msg+ "Nias"); ntawv xov xwm [58]; msg.toCharArray (lus, 58); Serial.println (lus); // tshaj tawm cov ntaub ntawv sensor rau MQTT broker tus neeg siv khoom.publish ("OsoyooData", lus); } lwm {msg = msg+ "Tsis Nias"; ntawv xov xwm [58]; msg.toCharArray (lus, 58); Serial.println (lus); // tshaj tawm cov ntaub ntawv sensor rau MQTT broker tus neeg siv khoom.publish ("OsoyooData", lus); }}}
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: Koj tuaj yeem siv koj tus kheej MQTT 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 yuav tsum hloov pauv
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 coresponding pawg thawj coj saib 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 4: MQTT Client Settings
Yog tias koj tsis paub yuav teeb tsa tus neeg siv MQTT li cas, thov mus saib peb kab lus kawg:
Cov ncauj lus teeb tsa: Cov ncauj lus tshaj tawm: OsoyooCommand
Cov ncauj lus los sau npe: OsoyooData
Khiav Tawm
Thaum qhov upload tiav, yog wifi hotspot lub npe thiab tus password teeb tsa tau zoo thiab MQTT broker txuas nrog, qhib Serial Monitor, koj yuav pom cov txiaj ntsig hauv qab no: Khaws nias lub pob no, Serial Monitor yuav tso tawm "Khawm xwm txheej: Nias" txhua 2 thib ob; ib zaug tso lub pob no, Serial Monitor yuav tso tawm "Button status: Not Pressed" every 2 second.
Pom zoo:
IoT IoT Kov Khawm: 10 Kauj Ruam
Khob IoT Kov Khawm: Kuv muaj ib daim ITO iav tso ncig lub khw ib hnub thiab xav txog muab nws tso rau qee qhov siv tau zoo. ITO, Indium Tin Oxide, iav feem ntau pom hauv cov kua siv lead ua kua, lub hnub ci, lub qhov rooj dav hlau dav hlau, thiab lwm yam
Rooj Bluetooth Hais Lus Nrog Pom Pom Lub Suab, Khawm Khawm thiab NFC.: 24 Kauj Ruam (nrog Duab)
Rooj Bluetooth Hais Lus Nrog Pom Pom Suab, Khawm Khawm thiab NFC.: Nyob zoo! Hauv Cov Lus Qhia no Kuv yuav qhia yuav ua li cas kuv thiaj ua Lub Rooj Sib Tham Bluetooth hais lus no uas muaj Lub Suab Pom Zoo Zoo nrog kov cov nyees khawm thiab NFC. Ua tau yooj yim ua ke nrog NFC Enabled cov cuab yeej nrog tsuas yog kov. Tsis muaj lub cev khawm
Khawm Thaum Muaj Xwm Ceev Nrog NodeMCU: 7 Kauj Ruam
Button Thaum Muaj Xwm Ceev Nrog NodeMCU: Khawm Xwm Ceev pab koj kom tau txais kev pab yog tias muaj xwm txheej xwm txheej ceev, tsuas yog Nias lub pob thiab nws yuav tshaj tawm hauv facebook lossis twitter txiav cov lus uas koj tso rau hauv txoj cai, koj tuaj yeem ntxiv lwm lub pob yog tias koj muaj txoj kev kho mob
Khawm IoT Uas Tswj Koj Lub App: 6 Kauj Ruam
IoT Khawm Uas Tswj Koj Lub App: Ib yam uas kuv nyiam tshaj plaws txog microcontrollers yog lub peev xwm los siv lawv ua cov khoom siv nkag, piv txwv li, Arduino Micro tuaj yeem siv ua HID (tib neeg siv khoom siv sib txuas) uas txhais nrog qee qhov coding thiab paub li cas koj tuaj yeem ua koj tus kheej keyboa
Micro: ntsis Cov Hauv Paus Kawm: Khawm Khawm: 11 Kauj Ruam
Micro: ntsis Cov Hauv Paus Kawm: Kov Khawm: Ntawm BBC Micro: ntsis, muaj 3 kov footers: pin0, pin1, pin2. Yog tias koj tuav tus GND tus pin nrog ib txhais tes thiab kov tus lej 0, 1, lossis 2 nrog rau lwm qhov, qhov hluav taws xob me me (nyab xeeb) yuav ntws los ntawm koj lub cev thiab rov qab mus rau hauv micro: me ntsis. Qhov no yog