Cov txheej txheem:
- Kauj Ruam 1: Txuas Koj Cov Sensors
- Kauj ruam 2: Code
- Kauj Ruam 3: Ibm Huab
- Kauj Ruam 4: Node-RED
- Kauj Ruam 5: Node-RED Teeb: Teeb
- Kauj Ruam 6: Node-RED Teeb: Kub
- Kauj Ruam 7: Node-RED Teeb: Vaum
- Kauj Ruam 8: Node-RED Teeb: Hws noo
- Kauj Ruam 9: Koj Ua tiav
Video: PInt@t10n: Ntse Kev Saib Xyuas Cog Ntoo: 9 Cov Kauj Ruam
2024 Tus sau: John Day | [email protected]. Kawg hloov kho: 2024-01-30 09:27
Txoj haujlwm no tau tsim los ua kev sim rau ibm iot huab. Peb siv tus esp-8266 xa thiab txais cov ntaub ntawv mus thiab los ntawm ibm huab. Kev sib txuas lus ntawm esp thiab ibm huab tshwm sim los ntawm MQTT.
Txhawm rau daws txhua cov ntaub ntawv thiab nthuav qhia tus neeg siv nrog tus neeg siv cuam tshuam peb siv node-liab. Nws yog tus pib zoo heev.
Txhawm rau pib koj xav tau:
- Esp-8266
- dht 11/22 ntsuas kub/noo noo
- photosensitive diode (ntsuas lub teeb)
- buzzer
- av noo sensor
- ibm cloud account
Kauj Ruam 1: Txuas Koj Cov Sensors
Raws li peb tau hais ua ntej peb yuav siv lub esp8266 los nyeem thiab xa cov ntaub ntawv sensor. Txog qhov kev qhia no peb yuav xav tias koj paub yuav txuas koj lub ntsuas pa li cas rau koj lub micro tswj. Koj kuj tseem xav paub yuav ua li cas xa cov lej xa mus rau esp8266.
Peb tus qauv esp tsuas muaj ib qho chaw nres nkoj sib piv yog li peb tau xaiv kom tau txais cov ntaub ntawv analog los ntawm peb lub teeb pom kev vim tias tom qab ntawd peb tuaj yeem saib xyuas lub teeb pom kev ntau npaum li cas.
Tag nrho lwm cov sensors tau txuas nrog digitally. Peb DHT11 sensor xa cov ntaub ntawv digitally thiab peb siv lub tsev qiv ntawv dht los txhais cov ntaub ntawv ntsuas.
Saum toj no koj tuaj yeem pom daim duab qhia chaw rau cov ntsuas peb tau siv. Tab sis thov nco ntsoov tias cov ntsuas uas peb siv feem ntau tuaj nrog tus qauv yog li koj tuaj yeem yooj yim txuas nws mus rau koj lub micro tswj yam tsis muaj txhua qhov txuas ntxiv uas peb suav nrog hauv daim duab.
Kauj ruam 2: Code
Nov yog daim duab arduino peb tau siv. Koj tuaj yeem kawm paub txog txoj cai ua haujlwm li cas yog tias koj nyeem cov lus.
#suav nrog ArduinoJson.h #suav nrog DHT_U.h #suav nrog DHT.h #suav nrog ESP8266WiFi.h #suav nrog ESP8266WiFiMulti.h #suav nrog PubSubClient.h
#define STASSID "ucll-projectweek-IoT" #define STAPSK "Foo4aiHa" #define DHTPin 5 #define moisturePin 4 #define DHTType DHT11 DHT dht (DHTPin, DHTType); ESP8266WiFiMulti WiFiMulti; ntab temp = 0; float av noo = 0; boolean ya raws = tsis tseeb; int lub teeb = 0; const char* ssid = STASSID; const char* password = STAPSK; const char* mqtt_server = "cmfwqk.messaging.internetofthings.ibmcloud.com"; WiFiClient espClient; PubSubClient tus thov kev pab (espClient); ntev lastMsg = 0; char lus [50]; int tus nqi = 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" #define MTTTTTTT8TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTFTKTemTidTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTFIKTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTFIKTFTTTTTTTTT8TTTTTT8 iot-2/evt/status/fmt/json "#define MQTT_TOPIC_DISPLAY" iot-2/cmd/hloov tshiab/fmt/json "#include" pitches.h "// sau hauv suab paj nruag: // nco lub sijhawm: 4 = lub hlis ceeb toom, 8 = daim ntawv thib yim, thiab lwm yam: int shortTone = 80; int longTone = 200; int standardDelay = 50; int delayBetweenBars = 50; tsis muaj teeb meem setup_wifi () {ncua (10); // Peb pib los ntawm kev txuas mus rau WiFi network Serial.println (); Serial.print ("Txuas rau"); Serial.println (ssid); WiFi.begin (ssid, password); thaum (WiFi.status ()! = WL_CONNECTED) {ncua (500); Serial.print ("."); } Serial.println (""); Serial.println ("Txuas nrog WiFi"); Serial.println ("IP chaw nyob:"); Serial.println (WiFi.localIP ()); } tsis muaj kev hu rov qab (char* ntsiab lus, byte* them nyiaj, tsis tau kos npe ntev) {Serial.print ("Cov lus tuaj txog ["); Serial.print (ncauj lus); Serial.print ("]"); rau (int i = 0; i <ntev; i ++) {Serial.print ((char) payload )); } Serial.println (); StaticJsonDocument jsonBuffer; deserializeJson (jsonBuffer, them nyiaj); JsonObject hauv paus = jsonBuffer.as (); yog (hauv paus ["nkauj"] == "darude") {darude (); } yog (hauv paus ["nkauj"] == "starwars") {starwars (); } yog (hauv paus ["nkauj"] == "jacques") {Serial.println ("jakakakakakakakakaka"); jacques (); }} void reconnect () {// Loop kom txog thaum peb rov txuas dua thaum (! // Sim txuas mus yog (client.connect (MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {Serial.println ("txuas")); client.subscribe (MQTT_TOPIC_DISPLAY); } 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); }}} void teeb () {Serial.begin (9600); pinMode (moisturePin, INPUT); dht.begin (); pinMode (0, NTAWV); teeb_wifi (); client.setServer (mqtt_server, MQTT_PORT); client.setCallback (hu rov qab); ncua (20); } void loop () {yog (! client.connected ()) {reconnect (); } client.loop (); ya raws = digitalRead (moisturePin); teeb = analogRead (A0); temp = dht.readTemperature (); vaum = dht.readHumidity (); Serial.println ("Kub =" + String (temp, 1) + "\ nHumidity =" + String (humidity, 1) + "\ nMoisture:" + String (moisture) + "\ nLight:" + String (lub teeb)); String payload = "{" ts / ": {" t / ":"+String (temp, 1)+", \" h / ":"+String (humidity, 1)+"}, \" ms / ": {" m / ":"+String (moisture)+"}, \" ls / ": {" l / ":"+String (light)+"}}"; Serial.println (them nyiaj); yog (client.publish (MQTT_TOPIC, (char*) payload.c_str ())) {Serial.println ("tshaj tawm ok"); } lwm {Serial.println ("luam tawm ua tsis tiav"); } ncua (2000); } void darude () {tone (0, NOTE_B3, shortTone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (delayBetweenBars); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_D4, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_D4, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_A3, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (delayBetweenBars); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (delayBetweenBars); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab (0, NOTE_B3, luv Tone); ncua (shortTone); noTone (0); ncua (standardDelay); suab nrov (0, NOTE_B3, longTone); ncua (longTone); noTone (0); ncua (standardDelay); suab (0, NOTE_E4, longTone); ncua (longTone); noTone (0); ncua (standardDelay); } tsis muaj starwars () {beep (NOTE_A4, 500); beep (NOTE_A4, 500); beep (NOTE_A4, 500); beep (NOTE_F4, 350); beep (NOTE_C5, 150); beep (NOTE_A4, 500); beep (NOTE_F4, 350); beep (NOTE_C5, 150); beep (NOTE_A4, 650); ncua (500); beep (NOTE_E5, 500); beep (NOTE_E5, 500); beep (NOTE_E5, 500); beep (NOTE_F5, 350); beep (NOTE_C5, 150); beep (NOTE_GS4, 500); beep (NOTE_F4, 350); beep (NOTE_C5, 150); beep (NOTE_A4, 650); } void beep (int note, int duration) {// Play tone on buzzerPin tone (0, nco, sijhawm); ncua (sijhawm); noTone (0); ncua (standardDelay); } void heartOfCourage () {} tsis muaj jacques () {beep (NOTE_C4, 500); beep (NOTE_D4, 500); beep (NOTE_E4, 500); beep (NOTE_C4, 500); beep (NOTE_C4, 500); beep (NOTE_D4, 500); beep (NOTE_E4, 500); beep (NOTE_C4, 500); beep (NOTE_E4, 500); beep (NOTE_F4, 500); beep (NOTE_G4, 500); beep (NOTE_E4, 500); beep (NOTE_F4, 500); beep (NOTE_G4, 500); beep (NOTE_G4, 500); beep (NOTE_A4, 500); beep (NOTE_G4, 500); beep (NOTE_F4, 500); beep (NOTE_E4, 500); beep (NOTE_C4, 500); beep (NOTE_G4, 500); beep (NOTE_A4, 500); beep (NOTE_G4, 500); beep (NOTE_F4, 500); beep (NOTE_E4, 500); beep (NOTE_C4, 500); beep (NOTE_C4, 500); beep (NOTE_G4, 500); beep (NOTE_C4, 500); beep (NOTE_C4, 500); beep (NOTE_G4, 500); beep (NOTE_C4, 500); }
Kauj Ruam 3: Ibm Huab
Koj yuav tsum teeb tsa IBM-huab piv txwv. Koj tuaj yeem nrhiav cov ntaub ntawv hais txog qhov no ntawm qhov txuas no: github.
Kauj Ruam 4: Node-RED
Peb siv node-red rau kev tswj hwm peb cov kev lag luam dav dav thiab kom pom cov ntaub ntawv no hauv kev siv tus neeg siv yooj yim.
Kauj Ruam 5: Node-RED Teeb: Teeb
Ntshuam cov cai hauv qab no rau hauv koj qhov project.
[{"id": "deb0d57.1c46528", "type": "tab", "label": "Teeb", "xiam oob qhab": tsis tseeb, "info": ""}, {"id": "8a0fcaac. 4e54 "," type ":" ibmiot in "," z ":" deb0d57.1c46528 "," authentication ":" boundService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":"+"," commandType ":" "," format ": "json", "npe": "Tau txais cov ntaub ntawv los ntawm ESP", "kev pabcuam": "sau npe", "allDevices": tsis tseeb, "txhua daim ntawv thov": "", "allDeviceTypes": tsis tseeb, "allLogicalInterfaces": "", "allEvents": muaj tseeb, "allCommands": "", "allFormats": "", "qos": 0, "x": 120, "y": 62, "xov hlau":
Kauj Ruam 6: Node-RED Teeb: Kub
Ntshuam cov cai hauv qab no rau hauv koj qhov project.
[{"id": "fbad3799.f0e0e", "type": "tab", "label": "Kub", "xiam oob qhab": tsis tseeb, "info": ""}, {"id": "b8618eb9. 1c9288 "," type ":" ibmiot in "," z ":" fbad3799.f0e0e "," authentication ":" boundService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":"+"," commandType ":" "," format ": "json", "npe": "Tau txais cov ntaub ntawv los ntawm ESP", "kev pabcuam": "sau npe", "allDevices": tsis tseeb, "txhua daim ntawv thov": "", "allDeviceTypes": tsis tseeb, "allLogicalInterfaces": "", "allEvents": tseeb, "allCommands": "", "allFormats": "", "qos": 0, "x": 145.5, "y": 49, "xov hlau":
Kauj Ruam 7: Node-RED Teeb: Vaum
Ntshuam cov cai hauv qab no rau hauv koj qhov project.
[{"id": "144cd53b.c00473", "type": "tab", "label": "Vaum", "xiam oob qhab": tsis tseeb, "info": ""}, {"id": "d958f58b. 2678e "," type ":" ibmiot in "," z ":" 144cd53b.c00473 "," authentication ":" boundService "," apiKey ":" "," inputType ":" evt "," logicalInterface ":" "," ruleId ":" "," deviceId ":" ESP "," applicationId ":" "," deviceType ":" ESP8266 "," eventType ":"+"," commandType ":" "," format ": "json", "npe": "Tau txais cov ntaub ntawv los ntawm ESP", "kev pabcuam": "sau npe", "allDevices": tsis tseeb, "txhua daim ntawv thov": "", "allDeviceTypes": tsis tseeb, "allLogicalInterfaces": "", "allEvents": tseeb, "allCommands": "", "allFormats": "", "qos": 0, "x": 142, "y": 87, "xov hlau":
Kauj Ruam 8: Node-RED Teeb: Hws noo
Ntshuam cov cai hauv qab no rau hauv koj qhov project.
[{"id": "4ee73536.ec6fc4", "type": "tab", "label": "Moisture", "disabled": cuav, "info": ""}, {"id": "a6daac3e. 2d5268 "," type ":" ibmiot out "," z ":" 4ee73536.ec6fc4 "," authentication ":" boundService "," apiKey ":" "," outputType ":" cmd "," deviceId ":" ESP "," deviceType ":" ESP8266 "," eventCommandType ":" hloov tshiab "," format ":" json "," data ":" msg "," qos ": 0," name ":" Xa lus txib rau ESP "," kev pabcuam ":" sau npe "," x ": 804.1944580078125," y ": 383.0000305175781," xov ": }, {" id ":" 4134fdb7.18e9b4 "," type ":" ibmiot in ", "z": "4ee73536.ec6fc4", "authentication": "boundService", "apiKey": "", "inputType": "evt", "logicalInterface": "", "ruleId": "", "deviceId": "ESP", "applicationId": "", "deviceType": "ESP8266", "eventType": "hloov tshiab", "commandType": "", "format": "json", "name": "Tau txais cov ntaub ntawv los ntawm ESP "," kev pabcuam ":" sau npe "," allDevices ": tsis tseeb," txhua daim ntawv thov ":" "," allDeviceTypes ": tsis tseeb," allLogicalInterfaces ":" "," allEvents ": tseeb," allCommands ":" ", "allFormats": tsis tseeb, "qos": 0, "x": 115, "y": 76, "xov hlau":
Kauj Ruam 9: Koj Ua tiav
Nrog koj zoo siab! koj tam sim no muaj cov txheej txheem tshuaj xyuas tsob ntoo ua haujlwm.
Yog tias koj muaj lus nug, tsis txhob yig tiv tauj peb!