Cov txheej txheem:

Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board: 9 Kauj Ruam
Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board: 9 Kauj Ruam

Video: Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board: 9 Kauj Ruam

Video: Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board: 9 Kauj Ruam
Video: What’s it like to be an international student at THUAS? – short version 2024, Kaum ib hlis
Anonim
Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board
Txuas DHT11/DHT22 Sensor mus rau Huab nrog ESP8266-based Board

Hauv tsab xov xwm dhau los, Kuv txuas kuv li ESP8266-based NodeMCU board rau Cloud4RPi kev pabcuam. Tam sim no, nws yog lub sijhawm rau qhov project tiag!

Khoom siv

Yuav tsum tau kho vajtse:

  • Txhua lub rooj tsav xwm raws li ESP8266 nti (piv txwv li, NodeMCU)
  • DHT11 lossis DHT22 sensor

Software thiab kev pabcuam:

  • DHT sensor tsev qiv ntawv los ntawm Adafruit - v1.3.7
  • Adafruit Unified Sensor - v1.0.3
  • cloud4rpi-esp-arduino -v0.1.0
  • Cloud4RPI - Huab tswj vaj huam sib luag rau IoT khoom siv
  • PlatformIO IDE rau VSCode

Kauj Ruam 1: Ntsuas Kub thiab Vaum

Ntsuas Kub thiab Vaum
Ntsuas Kub thiab Vaum

Kuv twb muaj lub DHT11 sensor, yog li kuv txiav txim siab siv nws rau qhov ntsuas kub thiab av noo. Cia peb xaiv lub tsev qiv ntawv Arduino los nyeem cov ntaub ntawv sensor.

Arduino sau npe muaj ntau lub tsev qiv ntawv, los ntawm qhov kuv xaiv qhov nrov tshaj plaws.

Raws li lawv qhov chaw cia khoom GitHub, peb kuj tseem xav tau ntxiv Adafruit Unified Sensor pob.

Kauj Ruam 2: Tsim thiab teeb tsa Project

Tsim thiab kho qhov Project
Tsim thiab kho qhov Project

Kuv twb tau piav qhia yuav tsim PlatformIO project li cas thiab teeb tsa cov tsev qiv ntawv hauv thawj ntu. Kuv qhov haujlwm hu ua "MyNodeMCU". Tus qauv yog qhia saum toj no.

Txoj haujlwm no yog qhov hloov pauv me ntsis Cloud4RPi piv txwv. Kuv txiav txim siab khaws cov cuab yeej token thiab Wi-nkaus daim ntawv pov thawj hauv cov ntaub ntawv teeb tsa hloov chaw.

Cov ntaub ntawv platform.io zoo li hauv qab no:

[platformio] default_envs = nodemcuv2 [env: nodemcuv2] platform = espressif8266 moj khaum = arduino board = nodemcuv2

Kauj ruam 3: Nruab Libraries

Nruab Libraries
Nruab Libraries

Kev teeb tsa lub tsev qiv ntawv yog qhov yooj yim heev. Koj tuaj yeem ua nws los ntawm IDE's graphical interface, lossis ntxiv cov npe tsev qiv ntawv uas xav tau mus rau ntu lib_deps ntawm platform.io cov ntaub ntawv:

; … Lib_deps = cloud4rpi -esp -arduino Adafruit Unified Sensor DHT sensor library build_flags = -D MQTT_MAX_PACKET_SIZE = 1024 -D MQTT_MAX_TRANSFER_SIZE = 128 -D CLOUD4RPI_DEBUG = 0 -D SSID_NAME = / "_" _ _ " D CLOUD4RPI_TOKEN = / "_ YOUR_DEVICE_TOKEN _ \"

Cov tsev qiv ntawv ntxiv yuav raug teeb tsa ncaj qha rau hauv qhov project's subfolder.

Main.cpp header zoo li hauv qab no:

#include #include #include #include "DHT.h"

Kauj Ruam 4: Txuas DHT11 Sensor

Adafruit muab DHTtester.ino piv txwv ntawm kev sib txuas sensor.

Txoj cai no pib ua lub ntsuas thiab piav qhia tus qauv txhawm rau khaws qhov ntsuas ntsuas (yog tias nws ua tiav):

#define DHTPIN 2 // Cov lej txuas nrog DHT sensor#txhais DHTTYPE DHT11 // DHT 11 //… DHT dht (DHTPIN, DHTTYPE); dht.begin (); //… struct DHT_Result {ntab h; ploj t; }; DHT_Result dhtResult;

Lub luag haujlwm tom ntej qhia yuav ua li cas nyeem cov ntaub ntawv sensor thiab khaws nws hauv cov ntaub ntawv cov qauv piav qhia saum toj no

void readSensors () {ntab h = dht.readHumidity (); // Nyeem kub li Celsius (lub neej ntawd) ntab t = dht.readTemperature ();

// Txheeb xyuas yog tias ib qho twg nyeem tsis tau zoo thiab tawm mus

yog (isnan (h) || isnan (t)) {Serial.println (F ("Ua tsis tau tejyam nyeem los ntawm DHT sensor!")); rov qab; } dhtResult.h = h; dhtResult.t = t; }

Kauj Ruam 5: Xa Cov Ntaub Ntawv mus rau Huab

Thaum peb muaj cov ntaub ntawv ntawd, cov kauj ruam tom ntej yog xa nws mus rau Cloud4RPi kev pabcuam.

Cloud4RPi rau nplooj ntawv Arduino piav qhia lub tsev qiv ntawv API, uas yog txheej txheem txheej txheem siv rau:

  • tsim, nyeem thiab hloov pauv,
  • xa qhov txawv txav mus rau huab siv MQTT raws tu qauv.

Lub tsev qiv ntawv txhawb nqa peb yam sib txawv: Bool, Numeric, thiab String.

Lub tsev qiv ntawv ua haujlwm pib nrog tsim ib qho piv txwv API siv Device Token los ntawm cloud4rpi.io lub vev xaib (xa mus rau kab lus ntu 1 kom paub meej).

#if txhais (CLOUD4RPI_TOKEN) Cloud4RPi c4r (CLOUD4RPI_TOKEN); #else Cloud4RPi c4r ("!!! _ NO_DEVICE_TOKEN _ !!!"); #ib

Tom qab ntawd, tshaj tawm cov kev hloov pauv rau DHT11 nyeem:

c4r.declareNumericVariable ("DHT11_Temp"); c4r.declareNumericVariable ("DHT11_Hum");

Tom qab ntawd, tau txais cov ntaub ntawv los ntawm lub ntsuas cua, txuag lawv rau hauv qhov hloov pauv thiab tshaj tawm cov ntaub ntawv rau Cloud4RPi:

c4r.setVariable ("DHT11_Temp", dhtResult.t); c4r.setVariable ("DHT11_Hum", dhtResult.h); c4r.publishData ();

Kub thiab av noo tsis hloov pauv sai, yog li xa ntau dua ib tus nqi rau 5 feeb tsis tas yuav tsum tau ua.

Kauj Ruam 6: Kev kuaj mob

Cloud4RPi txhawb cov ntaub ntawv kuaj mob nrog rau qhov sib txawv qhov tseem ceeb. Kuv siv sijhawm ua haujlwm, Wi-Fi teeb liab lub zog, thiab IP chaw nyob raws li cov ntaub ntawv kuaj mob:

c4r.declareDiagVariable ("IP_Address"); c4r.declareDiagVariable ("RSSI"); // WiFi teeb liab lub zog c4r.declareDiagVariable ("Uptime");

Nco tseg: Lub millis muaj nuj nqi kuv siv kom tau txais cov sijhawm rov pib dua rau xoom txhua ~ 50 hnub. Uas yog ntau tshaj li txaus rau kuv qhov project.

Cov cai hauv qab no teeb tsa cov kev ntsuas ntsuas sib txawv:

c4r.setDiagVariable ("RSSI", (String) WiFi. RSSI () + "dBm"); c4r.setDiagVariable ("IP_Address", WiFi.localIP (). toString ()); c4r.setDiagVariable ("Uptime", uptimeHumanReadable (currentMillis)); c4r.publishDiag ();

Lub sijhawm ua haujlwm HumanReadable hloov pauv milliseconds rau daim ntawv yooj yim:

Txoj hlua uptimeHumanReadable (tsis tau kos npe ntev milliseconds) {zoo li qub uptimeStr [32]; unsigned ntev secs = milliseconds / 1000; unsigned ntev feeb = secs / 60; unsigned int teev = feeb / 60; unsigned int hnub = teev / 24; secs -= feeb * 60; feeb -= teev * 60; teev -= hnub * 24; sprintf (uptimeStr, "%d hnub%2.2d:%2.2d:%2.2d", (byte) hnub, (byte) teev, (byte) feeb, (byte) secs); rov hlua (uptimeStr); }

Txoj haujlwm tso tawm ib txoj hlua zoo li no 5 hnub 10:23:14 tsis yog tus lej loj coj txawv txawv.

Kauj ruam 7: Pib thiab Debug qhov Project

Pib thiab Debug qhov Project
Pib thiab Debug qhov Project

Tom qab suav sau cov cai tsim thiab ua rau nws ntsaws rau hauv NodeMCU, lub cuab yeej txuas nrog rau kev pabcuam huab thiab pib xa cov ntaub ntawv.

Koj tuaj yeem nce kev hais lus tsis zoo los ntawm kev teeb tsa CLOUD4RPI_DEBUG cov txheej txheem ua ntej hloov pauv mus rau 1 (ntxiv -D CLOUD4RPI_DEBUG = 1 rau build_flags ntu hauv platform.io ntawv).

Tom ntej no, qhib lub vev xaib cloud4rpi.io thiab pom lub cuab yeej tshiab online. Qhib nws kom pom txhua qhov txawv txav tau txais los ntawm lub cuab yeej: sensor thiab kuaj mob.

Kauj ruam 8: Dashboard Configuration

Dashboard Configuration
Dashboard Configuration

Hauv theem no, cov ntaub ntawv txuas rau huab tau ua haujlwm. Tam sim no, cia teeb tsa qhov pom kev sawv cev ntawm cov ntaub ntawv.

Kuv siv Dashboard teeb tsa UI los tsim cov dashboard hauv qab no.

Lub dashboard yog qhov sib faib, yog li kuv tam sim qhia nws nrog kuv tus phooj ywg.

Kauj Ruam 9: Xaus

Xaus
Xaus

Tag nrho txoj haujlwm txoj cai muaj nyob hauv qhov tseem ceeb.

Nov yog txhua yam tam sim no!

Cov lus nug thiab lus pom zoo txais tos hauv cov lus.

Pom zoo: