Innholdsfortegnelse:
- Trinn 1: Koble til sensorene
- Trinn 2: Kode
- Trinn 3: Ibm Cloud
- Trinn 4: Node-RØD
- Trinn 5: Node-RED Setup: Light
- Trinn 6: Node-RED Setup: Temperatur
- Trinn 7: Node-RED Setup: Fuktighet
- Trinn 8: Node-RED Setup: Fuktighet
- Trinn 9: Du er ferdig
Video: PInt@t10n: Smart Plant Monitoring System: 9 trinn
2024 Forfatter: John Day | [email protected]. Sist endret: 2024-01-30 11:23
Dette prosjektet ble opprettet som en test for ibm iot -skyen. Vi bruker en esp-8266 til å sende og motta data til og fra ibm-skyen. Kommunikasjonen mellom esp og ibm -skyen skjer gjennom MQTT.
For å håndtere alle dataene og for å presentere brukeren et brukergrensesnitt bruker vi node-rødt. Det er veldig nybegynnervennlig.
For å komme i gang trenger du:
- Esp-8266
- dht 11/22 temp/fuktighetssensor
- lysfølsom diode (lysmåling)
- summer
- jordfuktighetssensor
- ibm cloud -konto
Trinn 1: Koble til sensorene
Som vi nevnte før, skal vi bruke esp8266 til å lese og sende sensordata. I denne opplæringen antar vi at du vet hvordan du kobler sensorene til mikrokontrolleren. Du må også allerede vite hvordan du laster opp kode til en esp8266.
Vår esp -modell har bare en analog port, så vi valgte å motta analog informasjon fra lyssensoren vår, for da kan vi overvåke hvor mye lys planten får.
Alle andre sensorer er koblet digitalt. Vår DHT11 -sensor sender data digitalt, og vi bruker dht -biblioteket til å tolke sensordata.
Over finner du et kretsdiagram for sensorene vi brukte. Men vær oppmerksom på at sensorene vi brukte ofte kommer med en modul, slik at du enkelt kan koble den til mikrokontrolleren din uten alle de ekstra motstandene som vi inkluderte i diagrammet.
Trinn 2: Kode
Dette er arduino -skissen vi brukte. Du kan lære om hvordan koden fungerer hvis du leser kommentarene.
#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; flyte temp = 0; flytefuktighet = 0; boolsk fuktighet = falskt; int lys = 0; const char* ssid = STASSID; const char* passord = STAPSK; const char* mqtt_server = "cmfwqk.messaging.internetofthings.ibmcloud.com"; WiFiClient espClient; PubSubClient -klient (espClient); lang sisteMsg = 0; røykemelding [50]; int verdi = 0; #define MQTT_HOST "cmfwqk.messaging.internetofthings.ibmcloud.com" "#define MQTT_PORT 1883 #define MQTT_DEVICEID" d: cmfwqk: ESP8266: ESP "#define MQTT_UST" Md "MOT_TI_TT_UST" Md_TK_TK_TT_UST "MTK_TK_TK_TIL_TIL_TILT_UST" #" iot-2/evt/status/fmt/json "#define MQTT_TOPIC_DISPLAY" iot-2/cmd/update/fmt/json "#include" pitches.h "// notater i melodien: // notatvarighet: 4 = kvartal note, 8 = åttende note, etc.: int shortTone = 80; int longTone = 200; int standardDelay = 50; int delayBetweenBars = 50; ugyldig setup_wifi () {forsinkelse (10); // Vi starter med å koble til et WiFi -nettverk Serial.println (); Serial.print ("Koble til"); Serial.println (ssid); WiFi.begin (ssid, passord); mens (WiFi.status ()! = WL_CONNECTED) {forsinkelse (500); Serial.print ("."); } Serial.println (""); Serial.println ("WiFi tilkoblet"); Serial.println ("IP -adresse:"); Serial.println (WiFi.localIP ()); } ugyldig tilbakeringing (tegn* emne, byte* nyttelast, usignert int -lengde) {Serial.print ("Melding kom ["); Serial.print (emne); Serial.print ("]"); for (int i = 0; i <length; i ++) {Serial.print ((char) nyttelast ); } Serial.println (); StaticJsonDocument jsonBuffer; deserializeJson (jsonBuffer, nyttelast); JsonObject root = jsonBuffer.as (); if (root ["sang"] == "darude") {darude (); } if (root ["song"] == "starwars") {starwars (); } if (root ["song"] == "jacques") {Serial.println ("jakakakakakkakaka"); jacques (); }} ugyldig koble til igjen () {// Sløyfe til vi er tilkoblet igjen mens (! client.connected ()) {Serial.print ("Forsøk på MQTT -tilkobling …"); // Prøv å koble til hvis (client.connect (MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {Serial.println ("tilkoblet"); klient. abonnere (MQTT_TOPIC_DISPLAY); } annet {Serial.print ("mislyktes, rc ="); Serial.print (client.state ()); Serial.println ("prøv igjen om 5 sekunder"); // Vent 5 sekunder før forsøk på nytt (5000); }}} ugyldig oppsett () {Serial.begin (9600); pinMode (fuktighetsnål, INNGANG); dht.begin (); pinMode (0, OUTPUT); setup_wifi (); client.setServer (mqtt_server, MQTT_PORT); client.setCallback (tilbakeringing); forsinkelse (20); } void loop () {if (! client.connected ()) {reconnect (); } client.loop (); fuktighet = digitalRead (fuktighetsPin); light = analogRead (A0); temp = dht.readTemperature (); fuktighet = dht.readHumidity (); Serial.println ("Temperatur =" + streng (temp, 1) + "\ nFuktighet =" + streng (fuktighet, 1) + "\ nFukt:" + streng (fuktighet) + "\ nLys:" + streng (lys)); String nyttelast = "{" ts / ": {" t / ":"+String (temp, 1)+", \" h / ":"+String (fuktighet, 1)+"}, \" ms / ": {" m / ":"+streng (fuktighet)+"}, \" ls / ": {" l / ":"+streng (lys)+"}}"; Serial.println (nyttelast); if (client.publish (MQTT_TOPIC, (char*) payload.c_str ())) {Serial.println ("publiser ok"); } annet {Serial.println ("publisering mislyktes"); } forsinkelse (2000); } void darude () {tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (delayBetweenBars); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_D4, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_A3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (delayBetweenBars); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (delayBetweenBars); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, shortTone); forsinkelse (shortTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_B3, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); tone (0, NOTE_E4, longTone); forsinkelse (longTone); noTone (0); forsinkelse (standardDelay); } ugyldige starwars () {pip (NOTE_A4, 500); pip (NOTE_A4, 500); pip (NOTE_A4, 500); pip (NOTE_F4, 350); pip (NOTE_C5, 150); pip (NOTE_A4, 500); pip (NOTE_F4, 350); pip (NOTE_C5, 150); pip (NOTE_A4, 650); forsinkelse (500); pip (NOTE_E5, 500); pip (NOTE_E5, 500); pip (NOTE_E5, 500); pip (NOTE_F5, 350); pip (NOTE_C5, 150); pip (NOTE_GS4, 500); pip (NOTE_F4, 350); pip (NOTE_C5, 150); pip (NOTE_A4, 650); } ugyldig pip (int notat, int varighet) {// Spill tone på buzzerPin tone (0, notat, varighet); forsinkelse (varighet); noTone (0); forsinkelse (standardDelay); } void heartOfCourage () {} void jacques () {pip (NOTE_C4, 500); pip (NOTE_D4, 500); pip (NOTE_E4, 500); pip (NOTE_C4, 500); pip (NOTE_C4, 500); pip (NOTE_D4, 500); pip (NOTE_E4, 500); pip (NOTE_C4, 500); pip (NOTE_E4, 500); pip (NOTE_F4, 500); pip (NOTE_G4, 500); pip (NOTE_E4, 500); pip (NOTE_F4, 500); pip (NOTE_G4, 500); pip (NOTE_G4, 500); pip (NOTE_A4, 500); pip (NOTE_G4, 500); pip (NOTE_F4, 500); pip (NOTE_E4, 500); pip (NOTE_C4, 500); pip (NOTE_G4, 500); pip (NOTE_A4, 500); pip (NOTE_G4, 500); pip (NOTE_F4, 500); pip (NOTE_E4, 500); pip (NOTE_C4, 500); pip (NOTE_C4, 500); pip (NOTE_G4, 500); pip (NOTE_C4, 500); pip (NOTE_C4, 500); pip (NOTE_G4, 500); pip (NOTE_C4, 500); }
Trinn 3: Ibm Cloud
Du må konfigurere en IBM-sky-forekomst. Du finner informasjon om dette via denne lenken: github.
Trinn 4: Node-RØD
Vi bruker node-red for å håndtere vår generelle forretningslogikk og for å visualisere disse dataene i et intuitivt brukergrensesnitt.
Trinn 5: Node-RED Setup: Light
Importer følgende kode til prosjektet ditt.
[{"id": "deb0d57.1c46528", "type": "fane", "label": "Lys", "deaktivert": 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": "Hent data fra ESP", "service": "registrert", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 120, "y": 62, "wires":
Trinn 6: Node-RED Setup: Temperatur
Importer følgende kode til prosjektet ditt.
[{"id": "fbad3799.f0e0e", "type": "fane", "label": "Temperatur", "deaktivert": false, "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": "Hent data fra ESP", "service": "registrert", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 145.5, "y": 49, "wires":
Trinn 7: Node-RED Setup: Fuktighet
Importer følgende kode til prosjektet ditt.
[{"id": "144cd53b.c00473", "type": "fane", "label": "Fuktighet", "deaktivert": 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": "Hent data fra ESP", "service": "registrert", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 142, "y": 87, "wires":
Trinn 8: Node-RED Setup: Fuktighet
Importer følgende kode til prosjektet ditt.
[{"id": "4ee73536.ec6fc4", "type": "tab", "label": "Fuktighet", "deaktivert": false, "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 ":" Send kommando til ESP "," service ":" registrert "," x ": 804.1944580078125," y ": 383.0000305175781," wires ": }, {" id ":" 4134fdb7.18e9b4 "," type ":" ibmiot in ", "z": "4ee73536.ec6fc4", "authentication": "boundService", "apiKey": "", "inputType": "evt", "logicalInterface": "", "ruleId": "", "deviceId": "ESP", "applicationId": "", "deviceType": "ESP8266", "eventType": "oppdatering", "commandType": "", "format": "json", "name": "Hent data fra ESP "," service ":" registrert "," allDevices ": false," allApplications ":" "," allDeviceTypes ": false," allLogicalInterfaces ":" "," allEvents ": true," allCommands ":" ", "allFormats": false, "qos": 0, "x": 115, "y": 76, "wires":
Trinn 9: Du er ferdig
Gratulerer! du har nå et fungerende planteobservasjonssystem.
Hvis du har spørsmål, ikke nøl med å kontakte oss!