Cov txheej txheem:

NodeMCU MQTT Iot Project - Hloov Khawm: 4 Kauj Ruam
NodeMCU MQTT Iot Project - Hloov Khawm: 4 Kauj Ruam

Video: NodeMCU MQTT Iot Project - Hloov Khawm: 4 Kauj Ruam

Video: NodeMCU MQTT Iot Project - Hloov Khawm: 4 Kauj Ruam
Video: NodeMCU 4 Channel Relay Module Design using EasyEDA 2024, Kaum ib hlis
Anonim
Image
Image

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

Kev Sib Txuas Duab
Kev Sib Txuas Duab

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

Code
Code
Code
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

MQTT Client Chaw
MQTT Client Chaw
MQTT Client Chaw
MQTT Client Chaw

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: