Indholdsfortegnelse:
- Trin 1: Tilslut dine sensorer
- Trin 2: Kode
- Trin 3: Ibm Cloud
- Trin 4: Node-RØD
- Trin 5: Node-RØD opsætning: Lys
- Trin 6: Node-RØD opsætning: Temperatur
- Trin 7: Node-RØD opsætning: Fugtighed
- Trin 8: Node-RØD opsætning: Fugt
- Trin 9: Du er færdig
Video: PInt@t10n: Smart Plant Monitoring System: 9 trin
2024 Forfatter: John Day | [email protected]. Sidst ændret: 2024-01-30 08:28
Dette projekt blev oprettet som en test for ibm iot cloud. Vi bruger en esp-8266 til at sende og modtage data til og fra ibm-skyen. Kommunikationen mellem esp og ibm -skyen sker gennem MQTT.
For at håndtere alle data og for at præsentere brugeren for en brugergrænseflade bruger vi node-rød. Det er meget begyndervenligt.
For at komme i gang har du brug for:
- Esp-8266
- dht 11/22 temp/fugtføler
- lysfølsom diode (lysmåling)
- summer
- jordfugtighedsføler
- ibm cloud -konto
Trin 1: Tilslut dine sensorer
Som vi nævnte før, vil vi bruge esp8266 til at læse og sende sensordata. I denne vejledning antager vi, at du ved, hvordan du tilslutter dine sensorer til din mikrocontroller. Du skal også allerede vide, hvordan du uploader kode til en esp8266.
Vores esp -model har kun en analog port, så vi valgte at modtage analoge oplysninger fra vores lyssensor, for så kan vi overvåge, hvor meget lys planten får.
Alle andre sensorer er forbundet digitalt. Vores DHT11 -sensor sender data digitalt, og vi bruger dht -biblioteket til at fortolke sensordata.
Ovenfor kan du finde et kredsløbsdiagram for de sensorer, vi brugte. Men husk, at de sensorer, vi brugte, ofte kommer med et modul, så du nemt kan slutte det til din mikrokontroller uden alle de ekstra modstande, som vi inkluderede i diagrammet.
Trin 2: Kode
Dette er den arduino -skitse, vi brugte. Du kan lære mere om, hvordan koden fungerer, hvis du læser kommentarerne.
#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; flyde temp = 0; flydefugtighed = 0; boolsk fugt = falsk; int lys = 0; const char* ssid = STASSID; const char* password = STAPSK; const char* mqtt_server = "cmfwqk.messaging.internetofthings.ibmcloud.com"; WiFiClient espClient; PubSubClient -klient (espClient); lang sidsteMsg = 0; char msg [50]; int værdi = 0; #define MQTT_HOST "cmfwqk.messaging.internetofthings.ibmcloud.com" "#define MQTT_PORT 1883 #define MQTT_DEVICEID" d: cmfwqk: ESP8266: ESP "#define MQTT_UST" MUT_DK_TK_TIL_TIL_TIL_TIL_TIL_TIL_TIL_TIL_TIL_TILT_UST " #" iot-2/evt/status/fmt/json "#define MQTT_TOPIC_DISPLAY" iot-2/cmd/update/fmt/json "#include" pitches.h "// noter i melodien: // note varigheder: 4 = kvartal note, 8 = ottende note osv.: int shortTone = 80; int longTone = 200; int standardDelay = 50; int delayBetweenBars = 50; ugyldig setup_wifi () {forsinkelse (10); // Vi starter med at oprette forbindelse til et WiFi -netværk Serial.println (); Serial.print ("Opretter forbindelse til"); Serial.println (ssid); WiFi.begin (ssid, adgangskode); mens (WiFi.status ()! = WL_CONNECTED) {forsinkelse (500); Serial.print ("."); } Serial.println (""); Serial.println ("WiFi tilsluttet"); Serial.println ("IP -adresse:"); Serial.println (WiFi.localIP ()); } ugyldig tilbagekald (char* emne, byte* nyttelast, usigneret int -længde) {Serial.print ("Besked ankommet ["); 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 (); hvis (root ["sang"] == "darude") {darude (); } if (root ["song"] == "starwars") {starwars (); } if (root ["song"] == "jacques") {Serial.println ("jakakakakakkakaka"); jacques (); }} ugyldig forbindelse igen () {// Sløjfe, indtil vi genopretter forbindelse, mens (! client.connected ()) {Serial.print ("Forsøger MQTT -forbindelse …"); // Forsøg at oprette forbindelse, hvis (client.connect (MQTT_DEVICEID, MQTT_USER, MQTT_TOKEN)) {Serial.println ("forbundet"); client.subscribe (MQTT_TOPIC_DISPLAY); } ellers {Serial.print ("mislykkedes, rc ="); Serial.print (client.state ()); Serial.println ("prøv igen om 5 sekunder"); // Vent 5 sekunder, før forsinkelsen forsøger igen (5000); }}} ugyldig opsætning () {Serial.begin (9600); pinMode (moistPin, INPUT); dht.begin (); pinMode (0, OUTPUT); setup_wifi (); client.setServer (mqtt_server, MQTT_PORT); client.setCallback (tilbagekald); forsinkelse (20); } void loop () {if (! client.connected ()) {reconnect (); } client.loop (); fugt = digitalRead (moistPin); light = analogRead (A0); temp = dht.readTemperature (); fugtighed = dht.readHumidity (); Serial.println ("Temperatur =" + streng (temp, 1) + "\ nFugtighed =" + streng (fugtighed, 1) + "\ nFugt:" + streng (fugt) + "\ nLys:" + streng (lys)); String nyttelast = "{" ts / ": {" t / ":"+streng (temp, 1)+", \" h / ":"+streng (fugtighed, 1)+"}, \" ms / ": {" m / ":"+streng (fugt)+"}, \" ls / ": {" l / ":"+streng (lys)+"}}"; Serial.println (nyttelast); hvis (client.publish (MQTT_TOPIC, (char*) payload.c_str ())) {Serial.println ("publicer ok"); } else {Serial.println ("publicering mislykkedes"); } 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 () {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); forsinkelse (500); bip (NOTE_E5, 500); bip (NOTE_E5, 500); 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); } ugyldigt bip (int note, int varighed) {// Afspil tone på buzzerPin tone (0, note, varighed); forsinkelse (varighed); noTone (0); forsinkelse (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); }
Trin 3: Ibm Cloud
Du skal konfigurere en IBM-cloud-forekomst. Du kan finde oplysninger om dette via dette link: github.
Trin 4: Node-RØD
Vi bruger knude-rød til håndtering af vores generelle forretningslogik og til at visualisere disse data i en intuitiv brugergrænseflade.
Trin 5: Node-RØD opsætning: Lys
Importer følgende kode til dit projekt.
[{"id": "deb0d57.1c46528", "type": "fane", "label": "Lys", "deaktiveret": falsk, "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": "registreret", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 120, "y": 62, "wires":
Trin 6: Node-RØD opsætning: Temperatur
Importer følgende kode til dit projekt.
[{"id": "fbad3799.f0e0e", "type": "fane", "label": "Temperatur", "deaktiveret": falsk, "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": "registreret", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 145.5, "y": 49, "wires":
Trin 7: Node-RØD opsætning: Fugtighed
Importer følgende kode til dit projekt.
[{"id": "144cd53b.c00473", "type": "fane", "label": "Fugtighed", "deaktiveret": falsk, "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": "registreret", "allDevices": false, "allApplications": "", "allDeviceTypes": false, "allLogicalInterfaces": "", "allEvents": true, "allCommands": "", "allFormats": "", "qos": 0, "x": 142, "y": 87, "wires":
Trin 8: Node-RØD opsætning: Fugt
Importer følgende kode til dit projekt.
[{"id": "4ee73536.ec6fc4", "type": "fane", "label": "Fugt", "deaktiveret": falsk, "info": ""}, {"id": "a6daac3e. 2d5268 "," type ":" ibmiot out "," z ":" 4ee73536.ec6fc4 "," authentication ":" boundService "," apiKey ":" "," outputType ":" cmd "," deviceId ":" ESP "," deviceType ":" ESP8266 "," eventCommandType ":" opdatering "," format ":" json "," data ":" msg "," qos ": 0," name ":" Send kommando til ESP "," service ":" registreret "," 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": "opdater", "commandType": "", "format": "json", "name": "Hent data fra ESP "," service ":" registreret "," allDevices ": false," allApplications ":" "," allDeviceTypes ": false," allLogicalInterfaces ":" "," allEvents ": true," allCommands ":" ", "allFormats": false, "qos": 0, "x": 115, "y": 76, "wires":
Trin 9: Du er færdig
Tillykke! du har nu et fungerende anlægsobservationssystem.
Hvis du har spørgsmål, tøv ikke med at kontakte os!