Isi kandungan:
- Langkah 1: Sambungkan Sensor Anda
- Langkah 2: Kod
- Langkah 3: Ibm Cloud
- Langkah 4: Node-MERAH
- Langkah 5: Persediaan Node-MERAH: Cahaya
- Langkah 6: Persediaan Node-MERAH: Suhu
- Langkah 7: Persediaan Node-MERAH: Kelembapan
- Langkah 8: Persediaan Node-MERAH: Kelembapan
- Langkah 9: Anda Selesai
Video: PInt @ t10n: Sistem Pemantauan Loji Pintar: 9 Langkah
2024 Pengarang: John Day | [email protected]. Diubah suai terakhir: 2024-01-30 11:10
PI @ nt @ t10n
Projek ini dibuat sebagai ujian untuk awan ibm iot. Kami menggunakan esp-8266 untuk mengirim dan menerima data ke dan dari awan ibm. Komunikasi antara esp dan cloud ibm berlaku melalui MQTT.
Untuk menangani semua data dan menampilkan pengguna dengan antara muka pengguna, kami menggunakan simpul-merah. Ia sangat mesra pemula.
Untuk memulakan, anda memerlukan:
- Esp-8266
- dht 11/22 sensor suhu / kelembapan
- diod fotosensitif (pengukuran cahaya)
- pembesar suara
- sensor kelembapan tanah
- akaun awan ibm
Langkah 1: Sambungkan Sensor Anda
Seperti yang telah kami sebutkan sebelumnya, kami akan menggunakan esp8266 untuk membaca dan mengirim data sensor. Untuk tutorial ini, kami akan menganggap anda tahu bagaimana menghubungkan sensor anda ke pengawal mikro anda. Anda juga perlu mengetahui cara memuat naik kod ke esp8266.
Model esp kami hanya mempunyai satu port analog sehingga kami memilih untuk menerima maklumat analog dari sensor cahaya kami kerana dengan itu kami dapat memantau berapa banyak cahaya yang diperoleh kilang.
Semua sensor lain disambungkan secara digital. Sensor DHT11 kami menghantar data secara digital dan kami menggunakan perpustakaan dht untuk mentafsirkan data sensor.
Di atas anda dapat mencari gambarajah litar untuk sensor yang kami gunakan. Tetapi perlu diingat bahawa sensor yang sering kami gunakan dilengkapi dengan modul supaya anda dapat menghubungkannya dengan mudah ke pengawal mikro anda tanpa semua perintang tambahan yang kami sertakan dalam rajah.
Langkah 2: Kod
Ini adalah lakaran arduino yang kami gunakan. Anda boleh mengetahui bagaimana kod tersebut berfungsi sekiranya anda membaca komen.
#include ArduinoJson.h # include DHT_U.h #include DHT.h #include ESP8266WiFi.h #include ESP8266WiFiMulti.h #include PubSubClient.h
#define STASSID "ucll-projectweek-IoT" #define STAPSK "Foo4aiHa" #define DHTPin 5 #define moistPin 4 #define DHTType DHT11 DHT dht (DHTPin, DHTType); ESP8266WiFiMulti WiFiMulti; suhu apungan = 0; kelembapan apungan = 0; kelembapan boolean = palsu; cahaya int = 0; const char * ssid = STASSID; const char * kata laluan = STAPSK; const char * mqtt_server = "cmfwqk.messaging.internetofthings.ibmcloud.com"; WiFiClient espClient; Pelanggan PubSubClient (espClient); lama terakhirMsg = 0; char msg [50]; nilai int = 0; #define MQTT_HOST "cmfwqk.messaging.internetofthings.ibmcloud.com" #define MQTT_PORT 1883 #define MQTT_DEVICEID "d: cmfwqk: ESP8266: ESP" #define MQTT_USER "use-token-auth" #fefTwT " iot-2 / evt / status / fmt / json "#define MQTT_TOPIC_DISPLAY" iot-2 / cmd / update / fmt / json "#include" pitches.h "// nota dalam melodi: // catatan masa: 4 = suku nota, 8 = nota kelapan, dll: int shortTone = 80; int longTone = 200; int standardDelay = 50; int delayBetweenBars = 50; batal setup_wifi () {kelewatan (10); // Kita mulakan dengan menyambung ke rangkaian WiFi Serial.println (); Serial.print ("Menyambung ke"); Serial.println (ssid); WiFi.begin (ssid, kata laluan); sementara (WiFi.status ()! = WL_CONNECTED) {kelewatan (500); Cetakan bersiri ("."); } Serial.println (""); Serial.println ("WiFi disambungkan"); Serial.println ("Alamat IP:"); Serial.println (WiFi.localIP ()); } kekosongan panggilan balik (topik char *, byte * muatan, panjang int yang tidak ditandatangani) {Serial.print ("Pesan tiba ["); Cetakan bersiri (topik); Serial.print ("]"); untuk (int i = 0; i <length; i ++) {Serial.print ((char) muatan ); } Bersiri.println (); StaticJsonDocument jsonBuffer; deserializeJson (jsonBuffer, muatan); JsonObject root = jsonBuffer.as (); if (root ["lagu"] == "darude") {darude (); } if (root ["song"] == "starwars") {starwars (); } if (root ["lagu"] == "jacques") {Serial.println ("jakakakakakkakaka"); jacques (); }} batalkan penyambungan semula () {// Gelung sehingga kita disambungkan semula semasa (! client.connected ()) {Serial.print ("Mencuba sambungan MQTT …"); // Cuba sambung jika (client.connect (MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {Serial.println ("bersambung"); pelanggan.subscribe (MQTT_TOPIC_DISPLAY); } lain {Serial.print ("gagal, rc ="); Serial.print (pelanggan.state ()); Serial.println ("cuba lagi dalam 5 saat"); // Tunggu 5 saat sebelum mencuba penundaan (5000); }}} persediaan tidak sah () {Serial.begin (9600); pinMode (kelembapan, INPUT); dht.begin (); pinMode (0, OUTPUT); persediaan_wifi (); client.setServer (mqtt_server, MQTT_PORT); client.setCallback (panggilan balik); kelewatan (20); } gelung void () {if (! client.connected ()) {sambung semula (); } pelanggan.loop (); kelembapan = digitalRead (moistPin); light = analogRead (A0); temp = dht.readTemperature (); kelembapan = dht.readHumidity (); Serial.println ("Temperature =" + String (temp, 1) + "\ nHumidity =" + String (kelembapan, 1) + "\ nLembapan:" + String (kelembapan) + "\ nLight:" + String (cahaya)); Muatan rentetan = "{" ts / ": {" t / ":" + String (temp, 1) + ", \" h / ":" + String (kelembapan, 1) + "}, \" ms / ": {" m / ":" + String (kelembapan) + "}, \" ls / ": {" l / ":" + String (cahaya) + "}}"; Serial.println (muatan); jika (client.publish (MQTT_TOPIC, (char *) payload.c_str ())) {Serial.println ("terbitkan ok"); } lain {Serial.println ("penerbitan gagal"); } kelewatan (2000); } kekosongan darude () {tone (0, NOTE_B3, shortTone); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (delayBetweenBars); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_D4, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_A3, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (delayBetweenBars); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (delayBetweenBars); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, nada pendek); kelewatan (shortTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_B3, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); nada (0, NOTE_E4, longTone); kelewatan (longTone); noTone (0); kelewatan (standardDelay); } batal bintang bintang () {bip (NOTE_A4, 500); bip (NOTE_A4, 500); bip (NOTE_A4, 500); bip (NOTE_F4, 350); bip (NOTE_C5, 150); bip (NOTE_A4, 500); bip (NOTE_F4, 350); bip (NOTE_C5, 150); bip (NOTE_A4, 650); kelewatan (500); bunyi bip (NOTE_E5, 500); bunyi bip (NOTE_E5, 500); bunyi bip (NOTE_E5, 500); bip (NOTE_F5, 350); bip (NOTE_C5, 150); bip (NOTE_GS4, 500); bip (NOTE_F4, 350); bip (NOTE_C5, 150); bip (NOTE_A4, 650); } bip kosong (not int, durasi int) {// Nada main pada nada buzzerPin (0, nota, tempoh); kelewatan (tempoh); noTone (0); kelewatan (standardDelay); } void heartOfCourage () {} void jacques () {bip (NOTE_C4, 500); bip (NOTE_D4, 500); bip (NOTE_E4, 500); bip (NOTE_C4, 500); bip (NOTE_C4, 500); bip (NOTE_D4, 500); bip (NOTE_E4, 500); bip (NOTE_C4, 500); bip (NOTE_E4, 500); bip (NOTE_F4, 500); bip (NOTE_G4, 500); bip (NOTE_E4, 500); bip (NOTE_F4, 500); bip (NOTE_G4, 500); bip (NOTE_G4, 500); bip (NOTE_A4, 500); bip (NOTE_G4, 500); bip (NOTE_F4, 500); bip (NOTE_E4, 500); bip (NOTE_C4, 500); bip (NOTE_G4, 500); bip (NOTE_A4, 500); bip (NOTE_G4, 500); bip (NOTE_F4, 500); bip (NOTE_E4, 500); bip (NOTE_C4, 500); bip (NOTE_C4, 500); bip (NOTE_G4, 500); bip (NOTE_C4, 500); bip (NOTE_C4, 500); bip (NOTE_G4, 500); bip (NOTE_C4, 500); }
Langkah 3: Ibm Cloud
Anda perlu menyediakan contoh IBM-cloud. Anda boleh mendapatkan maklumat mengenai ini melalui pautan ini: github.
Langkah 4: Node-MERAH
Kami menggunakan node-red untuk menangani logik perniagaan umum kami dan untuk memvisualisasikan data ini dalam antara muka pengguna yang intuitif.
Langkah 5: Persediaan Node-MERAH: Cahaya
Import kod berikut ke dalam projek anda.
[{"id": "deb0d57.1c46528", "type": "tab", "label": "Light", "abled ": false," info ":" "}, {" id ":" 8a0fcaac. 4e54 "," type ":" ibmiot in "," z ":" deb0d57.1c46528 "," authentication ":" bindService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":" + "," commandType ":" "," format ": "json", "name": "Dapatkan data dari ESP", "service": "register", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 120, "y": 62, "wire":
Langkah 6: Persediaan Node-MERAH: Suhu
Import kod berikut ke dalam projek anda.
[{"id": "fbad3799.f0e0e", "type": "tab", "label": "Temperature", "abled ": false," info ":" "}, {" id ":" b8618eb9. 1c9288 "," type ":" ibmiot in "," z ":" fbad3799.f0e0e "," authentication ":" bindService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":" + "," commandType ":" "," format ": "json", "name": "Dapatkan data dari ESP", "service": "register", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 145.5, "y": 49, "wire":
Langkah 7: Persediaan Node-MERAH: Kelembapan
Import kod berikut ke dalam projek anda.
[{"id": "144cd53b.c00473", "type": "tab", "label": "Humidity", "disabled": false, "info": ""}, {"id": "d958f58b. 2678e "," type ":" ibmiot in "," z ":" 144cd53b.c00473 "," authentication ":" bindService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":" + "," commandType ":" "," format ": "json", "name": "Dapatkan data dari ESP", "service": "register", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 142, "y": 87, "wires":
Langkah 8: Persediaan Node-MERAH: Kelembapan
Import kod berikut ke dalam projek anda.
[{"id": "4ee73536.ec6fc4", "type": "tab", "label": "Moisture", "disabled": false, "info": ""}, {"id": "a6daac3e. 2d5268 "," type ":" ibmiot out "," z ":" 4ee73536.ec6fc4 "," authentication ":" bindService "," apiKey ":" "," outputType ":" cmd "," deviceId ":" ESP "," deviceType ":" ESP8266 "," eventCommandType ":" update "," format ":" json "," data ":" msg "," qos ": 0," name ":" Kirim perintah ke ESP "," service ":" berdaftar "," x ": 804.1944580078125," y ": 383.0000305175781," wayar ": }, {" id ":" 4134fdb7.18e9b4 "," type ":" ibmiot in ", "z": "4ee73536.ec6fc4", "pengesahan": "bindService", "apiKey": "", "inputType": "evt", "logicalInterface": "", "ruleId": "", "deviceId": "ESP", "applicationId": "", "deviceType": "ESP8266", "eventType": "update", "commandType": "", "format": "json", "name": "Dapatkan data dari ESP "," service ":" register "," allDevices ": false," allApplications ":" "," allDeviceTypes ": false," allLogicalInterfaces ":" "," allEvents ": true," allCommands ":" ", "allFormats": false, "qos": 0, "x": 115, "y": 76, "wires":
Langkah 9: Anda Selesai
Tahniah! anda kini mempunyai sistem pemerhatian kilang yang berfungsi.
Sekiranya anda mempunyai sebarang pertanyaan, jangan ragu untuk menghubungi kami!