Cov txheej txheem:

Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP: 7 Kauj Ruam
Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP: 7 Kauj Ruam

Video: Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP: 7 Kauj Ruam

Video: Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP: 7 Kauj Ruam
Video: Sib Hawm Dhau (Time Passed) Music Video by: Deeda/Dib Xwb 2024, Kaum ib hlis
Anonim
Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP
Kub thiab Vaum Siv ESP32-DHT22-MQTT-MySQL-PHP

Kuv tus hluas nkauj xav tau lub tsev iav, yog li kuv tau ua nws. Tab sis kuv xav tau qhov ntsuas kub thiab av noo sab hauv lub tsev iav. Yog li, kuv googled rau cov piv txwv thiab pib sim.

Kuv qhov xaus yog tias txhua qhov piv txwv uas kuv pom tsis yog qhov kuv xav tsim. Kuv tau txais ntau qhov me me ntawm cov cai thiab ua ke lawv. Nws coj kuv ib pliag kom ua tiav kuv thawj qhov kev tsim ua vim tias cov ntaub ntawv ntawm cov piv txwv nyuaj dhau rau kuv kom nkag siab lossis lawv xav tias yog ib feem uas kuv yuav tsum paub ?? Tab sis kuv tsis paub dab tsi (tseem) ☹

Tias yog vim li cas kuv tsim cov lus qhia no. Ib qho "pib-txog thaum kawg" qhia rau txhua tus kom nkag siab. (Tsawg kawg kuv vam?)

Nws ua haujlwm li cas…

Qhov khoom kawg yog ESP32-CAM nrog DHT22 lub sensor txuas rau nws uas tau txais nws lub zog los ntawm 18650 roj teeb. Txhua peb feeb nws nyeem qhov kub thiab txias thiab xa qhov no hla WiFi mus rau MQTT sab nraud server thiab tom qab ntawd mus pw (rau peb feeb) kom siv roj teeb tsawg dua li xav tau

Ntawm Debian server, (uas tseem tuaj yeem yog raspberry pi Kuv twv) Kuv muaj sej 3, MQTT server, MySQL server thiab webserver

Python3 tsab ntawv sau ua qhov kev pabcuam thiab thaum twg nws tau txais MQTT cov lus, nws suav tus lej yav dhau los ntawm kev nkag (tus lej lej) thiab nce qhov no los ntawm ib qho. Tom qab ntawd nws nyeem qhov ntsuas kub thiab qhov av noo los ntawm MQTT cov lus. Nws kuaj xyuas qhov tsis raug thiab thaum twg qhov txiaj ntsig raug, nws xa qhov txiaj ntsig ua ke nrog tus lej ntsuas tshiab thiab hnub tim thiab sijhawm tam sim no rau MySQL server

Lub vev xaib muaj PHP tsab ntawv uas nyeem qhov txiaj ntsig los ntawm MySQL server thiab ua cov duab zoo los ntawm nws siv Google Charts. (piv txwv)

Khoom siv

Cov khoom kuv siv yog cov hauv qab no:

  • ESP32-CAM (Qhov laj thawj kuv siv lub koob yees duab yog vim nws muaj lub kav hlau txais xov sab nraud txuas rau nws. Tej zaum kuj tseem muaj lwm yam ESP32 koj tuaj yeem siv)
  • Kav hlau txais xov sab nraud
  • AM2302 DHT22 sensor (Qhov no muaj qhov ua haujlwm tiv thaiv, yog li koj tsuas xav tau peb lub xov hlau)

    https://www.amazon.de/gp/product/B07CM2VLBK/ref=p…

  • 18650 roj teeb thaiv v3
  • 18650 roj teeb (NCR18650B)
  • Qub micro USB cable (rau txuas ESP32 rau lub roj teeb thaiv)
  • Qee cov xov hlau jumper luv

Xav tau ntxiv:

  • USB rau TTL txuas (daim duab)

    https://www.amazon.de/FT232RL-Seriell-Unterst%C3%…

  • Soldering hlau
  • 3D tshuab luam ntawv (tsuas yog xav tau rau rooj plaub hauv tsev)

Kauj Ruam 1: Tshaj Tawm Arduino Code rau ESP32-CAM

Tshaj tawm Arduino Code rau ESP32-CAM
Tshaj tawm Arduino Code rau ESP32-CAM

Yog li cia pib!

Txhawm rau xa cov lej Arduino mus rau ESP32-CAM, koj yuav tsum txuas USBtoTTL txuas rau ESP32 siv cov lus qhia saum toj no.

Arduino code yog:

/*Tsuas yog qhov haujlwm me me los nyeem qhov ntsuas kub thiab av noo los ntawm DHT22 sensor thiab

hla nws mus rau MQTT B. Duijnhouwer Lub Rau Hli, 8th, 2020*/#include #include #include #define wifi_ssid "*** WIFI_SSID ***" // wifi ssid #define wifi_password "*** WIFI_PASSWORD ***" // wifi password #define mqtt_server "*** SERVER_NAME ***" // server npe lossis IP #define mqtt_user "*** MQTT_USER ***" // username #define mqtt_password "*** MQTT_PASSWORD ***" // password #define topic "glasshouse /dhtreadings "#define debug_topic" glasshouse /debug "// Cov ntsiab lus rau debugging /* cov ntsiab lus rau deeps pw tsaug zog* /#define uS_TO_S_FACTOR 1000000 /* Hloov pauv hloov pauv rau micro vib nas this rau vib nas this* /#define TIME_TO_SLEEP 180 /* Sijhawm ESP32 yuav mus pw rau 5 feeb (hauv vib nas this) */ bool debug = tseeb; // Zaub cov lus hais yog muaj tseeb #define DHT22_PIN 14 dht DHT; WiFiClient espClient; PubSubClient tus thov kev pab (espClient); cov ntaub ntawv [80]; teeb tsa tsis muaj dab tsi () {Serial.begin (115200); teeb_wifi (); // Txuas rau Wifi network tus thov kev pabcuam.setServer (mqtt_server, 1883); // Kho qhov txuas MQTT, hloov chaw nres nkoj yog xav tau. yog (! client.connected ()) {reconnect (); } // Nyeem cov ntaub ntawv int chk = DHT.read22 (DHT22_PIN); ntab t = DHT.temperature; ntab h = DHT.humidity; String dhtReadings = "{" temperature / ": \" " + String (t) +" / ", \" humidity / ": \" " + String (h) +" / "}"; dhtReadings.toCharArray (cov ntaub ntawv, (dhtReadings.length () + 1)); yog (debug) {Serial.print ("Kub:"); Serial.print (t); Serial.print ("| Vaum:"); Serial.println (h); } // Tshaj tawm qhov tseem ceeb rau MQTT cov ncauj lus client.publish (cov ncauj lus, cov ntaub ntawv); // Tshaj tawm kev nyeem ntawm lub ncauj lus (iav tsev/dhtreadings) yog (debug) {Serial.println ("Nyeem ntawv xa mus rau MQTT."); } esp_sleep_enable_timer_wakeup (TIME_TO_SLEEP * uS_TO_S_FACTOR); // mus pw Serial.println ("Teeb ESP32 kom tsaug zog rau txhua" + Txoj hlua (TIME_TO_SLEEP) + "Thib Ob"); Serial.println ("Mus pw li qub tam sim no."); esp_deep_sleep_start (); } // Teeb tsa kev sib txuas rau wifi tsis muaj teeb meem setup_wifi () {ncua (20); Serial.println (); Serial.print ("Txuas rau"); Serial.println (wifi_ssid); WiFi.begin (wifi_ssid, wifi_password); thaum (WiFi.status ()! = WL_CONNECTED) {ncua (100); Serial.print ("."); } Serial.println (""); Serial.println ("WiFi OK"); Serial.print ("=> ESP32 tus IP chaw nyob tshiab yog:"); Serial.print (WiFi.localIP ()); Serial.println (""); } // Rov txuas dua rau wifi yog tias kev txuas txuas tsis muaj dab tsi rov txuas dua () {thaum (! Client.connected ()) {Serial.print ("Txuas rau MQTT broker …"); yog (client.connect ("ESP32Client", mqtt_user, mqtt_password)) {Serial.println ("OK"); } lwm {Serial.print ("[Yuam Kev] Tsis txuas nrog:"); Serial.print (client.state ()); Serial.println ("Tos 5 vib nas this ua ntej rov sim dua."); ncua (5000); }}} tsis muaj dab tsi voj () {}

Thiab ntxiv, tsis txhob hnov qab hloov daim ntawv pov thawj nrog koj tus kheej daim ntawv pov thawj

Kauj Ruam 2: Tshaj Tawm

Hlau Hlau!
Hlau Hlau!

Txog rau lub zog, Kuv tau siv lub qub USB cable uas kuv txiav tawm USB-A txuas. Muaj plaub lub xov hlau hauv USB cable, peb tsuas xav tau cov dub thiab cov liab.

Yog li, txuas txhua yam raws li lub sijhawm teev tseg saum toj no.

Kauj ruam 3: Python3 Script

Python3 tsab ntawv mus rau hauv qhov chaw uas nws nkag tau mus rau tus neeg siv hauv paus.

Kuv siv /root/scripts/glasshouse/glasshouse.py rau tsab ntawv no. Cov ntsiab lus ntawm tsab ntawv nab nab yog:

# Python3 tsab ntawv txuas rau MQTT, nyeem qhov tseem ceeb thiab sau lawv rau hauv MySQL

# # B. Duijnhouwer # Lub Rau Hli, 8th 2020 # # version: 1.0 # # import paho.mqtt.client as mqtt import json import pymysql pymysql.install_as_MySQLdb () import MySQLdb from datetime import datetime db = MySQLdb.connect ("localhost", "glasshouse", "*** MYSQL_USERNAME ***", "*** MYSQL_PASSWORD ***") cursor = db.cursor () broker_address = "localhost" #Broker chaw nres nkoj chaw nyob = 1883 #Broker chaw nres nkoj neeg siv = "** *MQTT_USERNAME *** " #Kev sib txuas username password =" *** MQTT_PASSWORD *** " #Kev sib txuas tus lej password def on_connect (tus thov kev pabcuam, tus neeg siv, cov ntaub ntawv, tus chij, rc): nrog cov txiaj ntsig txiaj ntsig {0} ". hom ntawv (str (rc))) # Luam tawm qhov tshwm sim ntawm kev txuas txuas txuas rau cov neeg siv khoom siv.subscribe (" glasshouse/dhtreadings/ # ") def on_message (tus thov kev siv, cov ntaub ntawv, ntawv): # Kev hu rov qab rau thaum PUBLISH cov lus tau txais los ntawm server. cursor.execute ("xaiv * los ntawm sensordata") numrows = int (cursor.rowcount) newrow = numrows + 1 tam sim no = datetime.now () formatted_date = tam sim no.strftime ('%Y-%m-%d%H:% M:%S ') payload = json.loads (msg.payload.decode (' utf-8 ')) print ("New row:"+str (newrow)) temperature = float (payload ["temperature"]) av noo = float (payload ["humidity"]) print ("Kub:"+str (kub)) print ("Humidity:"+str (humidity)) print ("DateTime:"+str (formatted_date)) if ((kub > -20) thiab (kub = 0) thiab (av noo <= 100)): cur = db.cursor () cur.execute ("INSERT INTO glasshouse.sensordata (idx, kub, av noo, timestamp) VALUES ("+str (newrow)+","+str (kub)+","+str (humidity)+", %s)", (formatted_date)) db.commit () luam tawm ("cov ntaub ntawv tau txais thiab ntshuam hauv MySQL") ntxiv: luam tawm ("cov ntaub ntawv ntau tshaj qhov txwv thiab tsis tau xa tawm hauv MySQL") tus neeg siv = mqtt. Client ("duijnhouwer-com-glasshouse-script") client.username_pw_set (tus neeg siv, tus lej nkag mus = tus lej password) rau ua tiav kev sib txuas tus thov kev pabcuam.on_message = on_message # Txhais kev hu rov qab ua haujlwm kom tau txais cov lus xa xov client.connect (broker_address, port = port) #connect to broker client.loop_forever () # Pib sib tham daemon

Tsis txhob hnov qab hloov MySQL username thiab password thiab MQTT username thiab password rau koj tus kheej daim ntawv pov thawj

Koj tuaj yeem ua tsab ntawv khiav raws li kev pabcuam los ntawm kev tsim ob daim ntawv.

Thawj qhov yog "/etc/init/glasshouse.conf" nrog cov hauv qab no:

pib ntawm runlevel [2345]

nres ntawm runlevel [! 2345] exec /root/scripts/glasshouse/glasshouse.py

Qhov thib ob yog "/etc/systemd/system/multi-user.target.wants/glasshouse.service" nrog cov ntsiab lus hauv qab no:

[Chav]

Nqe lus piav qhia = Qhov Kev Pab Cuam Tsom iav tom qab = ntau tus neeg siv.target [Pabcuam] Hom = yooj yim Restart = ib txwm RestartSec = 1 ExecStart =/usr/bin/python3 /root/scripts/glasshouse/glasshouse.py [Nruab Nrab] WantedBy = ntau tus neeg siv.tag

Koj tuaj yeem ua qhov haujlwm no ua qhov kev pabcuam siv cov lus txib hauv qab no:

systemctl pab kom glasshouse

thiab pib nws siv:

systemctl pib iav tsev

Kauj ruam 4: MySQL Server

Koj yuav tsum tsim MySQL database tshiab nrog tsuas yog ib lub rooj hauv nws.

Txoj cai tsim lub rooj yog:

Tsim cov ntawv `sensordata` (`idx` int (11) DEFAULT NULL,` kub 'float DEFAULT NULL, `av noo' ntab DEFAULT NULL,` timestamp` datetime DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8;

Kauj ruam 5: Lub vev xaib

Lub vev xaib muaj ob daim ntawv, cov ntawv index.php thiab ib daim ntawv config.ini

Cov ntsiab lus ntawm cov ntaub ntawv config.ini yog:

[cov ntaub ntawv]

db_host = "localhost" db_name = "glasshouse" db_table = "sensordata" db_user = "*** DATABASE_USER ***" db_password = "*** DATABASE_PASSWORD ***"

Qhov twg offcourse koj hloov *** DATABASE_USER *** thiab *** DATABASE_PASSWORD *** nrog koj tus kheej daim ntawv pov thawj.

google.charts.load ('tam sim no', {'pob khoom': ['corechart']}); google.charts.setOnLoadCallback (drawChart); muaj nuj nqi drawChart () {var data = google.visualization.arrayToDataTable ([// ['Timestamp', 'Kub', 'Humidity', 'Heat Index'], ['Timestamp', 'Kub', 'Humidity'], lus nug ($ sql); # Lub sijhawm no - lub voj voos thiab muab tag nrho cov ntaub ntawv khaws cia rau hauv ['timestamp', 'kub', 'av noo'] txoj kev. thaum ($ kab = $ tshwm sim-> fetch_assoc ()) {$ timestamp_rest = substr ($ row ["timestamp"], 10, 6); ncha "['". $ timestamp_rest. "',". $ row ['kub']. ",". $ row ['humidity']. "],"; // ncha "['". $ timestamp_rest. "',". $ row ['kub']. ",". $ row ['humidity']. ",". $ row ['heatindex ']. "],";}?>])); // Kab txoj kab sib txawv xaiv = {npe: 'Kub thiab av noo', nkhausType: 'ua haujlwm', lus dab neeg: {txoj haujlwm: 'hauv qab'}, hAxis: {slantedText: muaj tseeb, slantedTextAngle: 45}}; // Curved chart var chart = new google.visualization. LineChart (document.getElementById ('curve_chart')); chart.draw (cov ntaub ntawv, kev xaiv); } // Xaus kab ntawv los ntawm drawChart //

Kauj Ruam 6: 3D Luam Vaj Tsev

Rau vaj tsev, kuv siv ob lub tsev cais, ib qho rau ESP32-CAM thiab DHT22 ua ke thiab ib qho rau 18650 roj teeb thaiv.

Kauj Ruam 7: Qhov Kawg Kawg

Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!
Qhov Kawg Nkaus!

Qhov kawg tshwm sim tseem pom hauv cov duab saum toj no.

Thiab thaum twg lub roj teeb tsis tas, koj tuaj yeem them nws nrog lub mini USB cable.

Pom zoo: