INHOUDSOPGAWE:
- Stap 1: Koppel u sensors
- Stap 2: Kode
- Stap 3: Ibm Cloud
- Stap 4: Node-ROOI
- Stap 5: Node-RED Setup: Light
- Stap 6: Node-ROOI-opstelling: temperatuur
- Stap 7: Node-ROOI-opstelling: humiditeit
- Stap 8: Node-RED Setup: Vog
- Stap 9: U is klaar
Video: PInt@t10n: Smart Plant Monitoring System: 9 stappe
2024 Outeur: John Day | [email protected]. Laas verander: 2024-01-30 07:26
Hierdie projek is geskep as 'n toets vir die ibm iot -wolk. Ons gebruik 'n esp-8266 om data van en na die ibm-wolk te stuur en te ontvang. Die kommunikasie tussen die esp en die ibm -wolk geskied deur middel van MQTT.
Ons gebruik node-rooi om al die data te hanteer en om 'n gebruikerskoppelvlak aan die gebruiker voor te stel. Dit is baie beginner vriendelik.
Om aan die gang te kom, benodig u:
- Esp-8266
- dht 11/22 temp/vog sensor
- Fotosensitiewe diode (ligmeting)
- gonser
- grondvog sensor
- ibm -wolkrekening
Stap 1: Koppel u sensors
Soos ons vroeër genoem het, gaan ons die esp8266 gebruik om sensordata te lees en te stuur. Vir hierdie tutoriaal gaan ons aan dat u weet hoe u u sensors aan u mikrobeheerder moet koppel. U moet ook reeds weet hoe u kode na 'n esp8266 moet oplaai.
Ons esp -model het slegs een analoog poort, so ons het gekies om analoog inligting van ons ligsensor te ontvang, want dan kan ons monitor hoeveel lig die plant kry.
Alle ander sensors is digitaal gekoppel. Ons DHT11 -sensor stuur data digitaal en ons gebruik die dht -biblioteek om die sensorsdata te interpreteer.
Hierbo kan u 'n stroombaan -diagram vind vir die sensors wat ons gebruik het. Hou egter in gedagte dat die sensors wat ons gebruik het gereeld 'n module bevat, sodat u dit maklik aan u mikrobeheerder kan koppel sonder al die ekstra weerstande wat ons in die diagram bevat.
Stap 2: Kode
Dit is die arduino -skets wat ons gebruik het. U kan leer hoe die kode werk as u die opmerkings lees.
#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; vlot temp = 0; vlot humiditeit = 0; booleaanse vog = vals; int lig = 0; const char* ssid = STASSID; const char* wagwoord = STAPSK; const char* mqtt_server = "cmfwqk.messaging.internetofthings.ibmcloud.com"; WiFiClient espClient; PubSubClient -kliënt (espClient); lang laasteMsg = 0; char msg [50]; int waarde = 0; #define MQTT_HOST "cmfwqk.messaging.internetofthings.ibmcloud.com" "#define MQTT_PORT 1883 #define MQTT_DEVICEID" d: cmfwqk: ESP8266: ESP "#define MQTT_UST" UTT_UST "MUT_TYT" #TeT_TT_UT " #" TKT_UST "#TK_TYD" #TKT_UIT " #" Teken_MENT "#TeK_Tik" iot-2/evt/status/fmt/json "#define MQTT_TOPIC_DISPLAY" iot-2/cmd/update/fmt/json "#include" pitches.h "// note in die melodie: // nootduur: 4 = kwart noot, 8 = agtste noot, ens.: int shortTone = 80; int longTone = 200; int standardDelay = 50; int delayBetweenBars = 50; ongeldig setup_wifi () {vertraging (10); // Ons begin deur aan te sluit op 'n WiFi -netwerk Serial.println (); Serial.print ("Koppel aan"); Serial.println (ssid); WiFi.begin (ssid, wagwoord); terwyl (WiFi.status ()! = WL_CONNECTED) {vertraging (500); Serial.print ("."); } Serial.println (""); Serial.println ("WiFi gekoppel"); Serial.println ("IP -adres:"); Serial.println (WiFi.localIP ()); } ongeldige terugbel (char* onderwerp, greep* laai, ongetekende int lengte) {Serial.print ("Boodskap het gekom ["); Serial.print (onderwerp); Serial.print ("]"); for (int i = 0; i <length; i ++) {Serial.print ((char) laai ); } Serial.println (); StaticJsonDocument jsonBuffer; deserializeJson (jsonBuffer, loonvrag); JsonObject root = jsonBuffer.as (); if (root ["song"] == "darude") {darude (); } if (root ["song"] == "starwars") {starwars (); } if (root ["song"] == "jacques") {Serial.println ("jakakakakakkakaka"); jacques (); }} ongeldig herkoppel () {// Lus totdat ons weer verbind is terwyl (! client.connected ()) {Serial.print ("Poging tot MQTT -verbinding …"); // Probeer om aan te sluit as (client.connect (MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {Serial.println ("verbind"); kliënt.inteken (MQTT_TOPIC_DISPLAY); } anders {Serial.print ("misluk, rc ="); Serial.print (client.state ()); Serial.println ("probeer weer oor 5 sekondes"); // Wag 5 sekondes voordat die vertraging weer probeer (5000); }}} ongeldige opstelling () {Serial.begin (9600); pinMode (moistPin, INPUT); dht.begin (); pinMode (0, UITGANG); setup_wifi (); client.setServer (mqtt_server, MQTT_PORT); client.setCallback (terugbel); vertraging (20); } leemte -lus () {as (! client.connected ()) {herkoppel (); } client.loop (); vog = digitalRead (vogPin); lig = analogRead (A0); temp = dht.readTemperature (); humiditeit = dht.readHumidity (); Serial.println ("Temperature =" + String (temp, 1) + "\ nHumidity =" + String (humiditeit, 1) + "\ nVog:" + String (vog) + "\ nLicht:" + String (lig)); String payload = "{" ts / ": {" t / ":"+String (temp, 1)+", \" h / ":"+String (humiditeit, 1)+"}, \" ms / ": {" m / ":"+Tou (vog)+"}, \" ls / ": {" l / ":"+Tou (lig)+"}}"; Serial.println (loonvrag); if (client.publish (MQTT_TOPIC, (char*) payload.c_str ())) {Serial.println ("publiseer ok"); } anders {Serial.println ("publiseer misluk"); } vertraging (2000); } void darude () {toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (delayBetweenBars); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_D4, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_A3, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (delayBetweenBars); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (delayBetweenBars); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, shortTone); vertraging (shortTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_B3, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); toon (0, NOTE_E4, longTone); vertraging (longTone); noTone (0); vertraging (standaardvertraging); } ongeldig starwars () {piep (NOTE_A4, 500); piep (NOTE_A4, 500); piep (NOTE_A4, 500); piep (NOTE_F4, 350); piep (NOTE_C5, 150); piep (NOTE_A4, 500); piep (NOTE_F4, 350); piep (NOTE_C5, 150); piep (NOTE_A4, 650); vertraging (500); piep (NOTE_E5, 500); piep (NOTE_E5, 500); piep (NOTE_E5, 500); piep (NOTE_F5, 350); piep (NOTE_C5, 150); piep (NOTE_GS4, 500); piep (NOTE_F4, 350); piep (NOTE_C5, 150); piep (NOTE_A4, 650); } ongeldig piep (int noot, int duur) {// Speel toon op buzzerPin toon (0, noot, duur); vertraging (duur); noTone (0); vertraging (standaardvertraging); } void heartOfCourage () {} void jacques () {piep (NOTE_C4, 500); piep (NOTE_D4, 500); piep (NOTE_E4, 500); piep (NOTE_C4, 500); piep (NOTE_C4, 500); piep (NOTE_D4, 500); piep (NOTE_E4, 500); piep (NOTE_C4, 500); piep (NOTE_E4, 500); piep (NOTE_F4, 500); piep (NOTE_G4, 500); piep (NOTE_E4, 500); piep (NOTE_F4, 500); piep (NOTE_G4, 500); piep (NOTE_G4, 500); piep (NOTE_A4, 500); piep (NOTE_G4, 500); piep (NOTE_F4, 500); piep (NOTE_E4, 500); piep (NOTE_C4, 500); piep (NOTE_G4, 500); piep (NOTE_A4, 500); piep (NOTE_G4, 500); piep (NOTE_F4, 500); piep (NOTE_E4, 500); piep (NOTE_C4, 500); piep (NOTE_C4, 500); piep (NOTE_G4, 500); piep (NOTE_C4, 500); piep (NOTE_C4, 500); piep (NOTE_G4, 500); piep (NOTE_C4, 500); }
Stap 3: Ibm Cloud
U moet 'n IBM-wolk-instansie opstel. U kan inligting hieroor vind via hierdie skakel: github.
Stap 4: Node-ROOI
Ons gebruik node-rooi vir die hantering van ons algemene sakelogika en om hierdie data in 'n intuïtiewe gebruikerskoppelvlak te visualiseer.
Stap 5: Node-RED Setup: Light
Voer die volgende kode in by u projek.
[{"id": "deb0d57.1c46528", "type": "tab", "label": "Light", "disabled": false, "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", "name": "Kry data van ESP", "service": "geregistreer", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 120, "y": 62, "wires":
Stap 6: Node-ROOI-opstelling: temperatuur
Voer die volgende kode in by u projek.
[{"id": "fbad3799.f0e0e", "type": "tab", "label": "Temperatuur", "gedeaktiveer": vals, "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", "name": "Kry data van ESP", "service": "geregistreer", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 145.5, "y": 49, "wires":
Stap 7: Node-ROOI-opstelling: humiditeit
Voer die volgende kode in by u projek.
[{"id": "144cd53b.c00473", "type": "tab", "label": "Humidity", "disabled": false, "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", "name": "Kry data van ESP", "service": "geregistreer", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 142, "y": 87, "wires":
Stap 8: Node-RED Setup: Vog
Voer die volgende kode in by u projek.
[{"id": "4ee73536.ec6fc4", "type": "tab", "label": "Vog", "gedeaktiveer": vals, "info": ""}, {"id": "a6daac3e. 2d5268 "," type ":" ibmiot out "," z ":" 4ee73536.ec6fc4 "," authentication ":" boundService "," apiKey ":" "," outputType ":" cmd "," deviceId ":" ESP "," deviceType ":" ESP8266 "," eventCommandType ":" update "," format ":" json "," data ":" msg "," qos ": 0," name ":" Stuur opdrag na ESP "," diens ":" geregistreer "," x ": 804.1944580078125," y ": 383.0000305175781," drade ": }, {" id ":" 4134fdb7.18e9b4 "," tipe ":" ibmiot in ", "z": "4ee73536.ec6fc4", "authentication": "boundService", "apiKey": "", "inputType": "evt", "logicalInterface": "", "ruleId": "", "deviceId": "ESP", "applicationId": "", "deviceType": "ESP8266", "eventType": "update", "commandType": "", "format": "json", "name": "Kry data van ESP "," service ":" geregistreer "," allDevices ": false," allApplications ":" "," allDeviceTypes ": false," allLogicalInterfaces ":" "," allEvents ": true," allCommands ":" ", "allFormats": false, "qos": 0, "x": 115, "y": 76, "wires":
Stap 9: U is klaar
Baie geluk! u het nou 'n werkende waarnemingstelsel vir aanlegte.
As u enige vrae het, moet asseblief nie huiwer om ons te kontak nie!