Cov txheej txheem:

(IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API: 5 Cov Kauj Ruam
(IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API: 5 Cov Kauj Ruam

Video: (IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API: 5 Cov Kauj Ruam

Video: (IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API: 5 Cov Kauj Ruam
Video: Plab Plab Ntswg..(Pluav Pluav Ntswg) Maiv Twm New Song 2023-2024 2024, Kaum ib hlis
Anonim
(IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API
(IOT Project) Tau Txais Cov Ntaub Ntawv Huab Cua Siv ESP8266 thiab Openweather API

Hauv cov lus qhia no peb yuav tsim txoj haujlwm IOT yooj yim uas nqa cov ntaub ntawv huab cua ntawm peb lub nroog los ntawm openweather.com/api thiab tso tawm nws siv software ua.

Khoom siv:

  1. Arduino
  2. ESP8266 lossis lwm yam esp module
  3. Arduino IDE Cov
  4. Ua software
  5. Cov qhob cij
  6. Jumperwires Txiv neej rau Txiv neej thiab Txiv neej rau Poj Niam

Kauj ruam 1: Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org

Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
Tau txais API Ntsiab Cai thiab URL Los Ntawm Openweather.org
  1. Tsim tus account hauv https://openweathermap.org (Duab 1)
  2. Tom qab koj nkag mus rau API yuam sij thiab koj yuav tau txais tus lej API raws li qhia hauv duab. (Duab 2)
  3. Luam tus lej API thiab khaws nws hauv cov ntawv sau cia. (Duab 3)
  4. Mus rau qhov kev xaiv API (Duab 4)
  5. Mus rau API doc xaiv raws li qhia hauv duab (Duab 5)
  6. Luam qhov url los ntawm thiab qhia thiab khaws nws hauv cov ntawv sau cia (Duab 6)

Kauj ruam 2: Daim duab txuas

Daim duab sib txuas
Daim duab sib txuas

Kauj Ruam 3: Arduino Code

Ua ntej theej cov cai no rau hauv Arduino nco ntsoov tias koj tau rub tawm ESP8266 pawg thawj coj mus rau hauv arduino lub tswv yim siv Boards manager.

#suav nrog

#include #include #include const char* ssid = "Koj SSID"; const char* password = "Koj SSID PASSWORD"; // Koj lub npe sau nrog URL kab lossis IP chaw nyob nrog txoj hlua Txoj hlua openWeatherMapApiKey = "Muab koj tus lej API tso rau ntawm no"; // Hloov nrog koj lub tebchaws cov cai thiab lub nroog Txoj hlua lub nroog = "Mumbai"; Txoj hlua countryCode = "IN"; Cov ntaub ntawv xov [16]; // Lub Sijhawm DEFAULT TSEV YOG 10 SECONDS RAU KOOM TSEV KAWM NTAWV // Rau daim ntawv thov zaum kawg, txheeb xyuas qhov API hu txwv ib teev/feeb kom tsis txhob raug thaiv/txwv tsis pub kos npe ntev lastTime = 0; // Timer teem rau 10 feeb (600000) // tsis tau kos npe ntev timerDelay = 600000; // Teeb tsa lub sijhawm rau 10 vib nas this (10000) tsis tau kos npe ntev timerDelay = 10000; Txoj hlua jsonBuffer; teeb tsa tsis muaj dab tsi () {Serial.begin (115200); WiFi.begin (ssid, password); //Serial.println("Txuas"); thaum (WiFi.status ()! = WL_CONNECTED) {ncua (500); // Serial.print ("."); } // Serial.println (""); // Serial.print ("Txuas nrog WiFi network nrog IP Chaw Nyob:"); // Serial.println (WiFi.localIP ()); // // Serial.println ("Timer set to 10 seconds (timerDelay variable), nws yuav siv 10 vib nas this ua ntej tshaj tawm thawj zaug nyeem ntawv."); } void loop () {// Xa HTTP GET thov yog ((millis () - lastTime)> timerDelay) {// Txheeb xyuas WiFi li xwm txheej txuas yog (WiFi.status () == WL_CONNECTED) {String serverPath = "https://api.openweathermap.org/data/2.5/weather?q= " + city +", " + countryCode +" & APPID = " +" d5b56fd07988143ae141503ed9d81742 " +" & units = metric "; jsonBuffer = httpGETRequest (serverPath.c_str ()); //Serial.println(jsonBuffer); JSONVar myObject = JSON.parse (jsonBuffer); // JSON.typeof (jsonVar) tuaj yeem siv los ua hom var yog tias (JSON.typeof (myObject) == "undefined") {//Serial.println("Parsing input ua tsis tiav! "); rov qab; } // Serial.print ("JSON kwv ="); // Serial.println (myObject); // Serial.print ("Kub:"); // Serial.println (myObject ["main"] ["temp"]); // Serial.print ("Siab:"); // Serial.println (myObject ["lub ntsiab"] ["siab"]); // Serial.print ("Vaum:"); // Serial.println (myObject ["lub ntsiab"] ["av noo"]); // Serial.print ("Cua Ceev:"); // Serial.println (myObject ["cua"] ["ceev"]); int temp = myObject ["main"] ["temp"]; ntev pres = myObject ["lub ntsiab"] ["siab"]; int ntub = myObject ["lub ntsiab"] ["av noo"]; int cua = myObject ["cua"] ["ceev"]; Txoj hlua lub teb chaws = JSON.stringify (myObject ["sys"] ["lub teb chaws"]); String city1 = JSON.stringify (myObject ["lub npe"]); Txoj hlua huab cua = JSON.stringify (myObject ["huab cua"] [0] ["kev piav qhia"]); String icon = JSON.stringify (myObject ["huab cua"] [0] ["icon"]); data [0] = Txoj hlua (temp); cov ntaub ntawv [1] = "/"; data [2] = Txoj hlua (pres); cov ntaub ntawv [3] = "/"; data [4] = Txoj hlua (ntub); cov ntaub ntawv [5] = "/"; data [6] = Txoj hlua (cua); cov ntaub ntawv [7] = "/"; data [8] = teb chaws; cov ntaub ntawv [9] = "/"; data [10] = nroog 1; cov ntaub ntawv [11] = "/"; data [12] = huab cua; cov ntaub ntawv [13] = "/"; cov ntaub ntawv [14] = icon; data [15] = "\ n"; rau (int i = 0; i0) {// Serial.print ("HTTP Teb chaws tus lej:"); // Serial.println (httpResponseCode); ntsaws = http.getString (); } lwm {Serial.print ("Error code:"); Serial.println (httpResponseCode); } // Cov peev txheej pub dawb http.end (); payload rov qab; }

Kauj Ruam 4: Ua Txoj Cai

Ua Txoj Cai
Ua Txoj Cai
Ua Txoj Cai
Ua Txoj Cai
Ua Txoj Cai
Ua Txoj Cai

Ua ntej khiav cov cai no rub tawm lub cim cim duab uas yuav siv los qhia huab cua. Thiab khaws cov duab thiab cov cai hauv tib daim nplaub tshev.

ntshuam ua.serial.*;

Serial myPort; Cov duab img; Cov duab img2; Cov duab img3; Duab img4; Duab img5; Duab img6; Duab img7; Duab img8; Duab img9; PImage img10; PImage img11; PImage img12; Cov duab img13; Cov duab img14; PImage img15; PImage img16; Cov duab img17; PImage img18; koj temp; koj pres; ntub dej; cua zog; Txoj hlua lub nroog = ""; Txoj hlua lub teb chaws = ""; Txoj hlua huab cua = ""; Txoj hlua cim = ""; teeb tsa tsis muaj dab tsi () {loj (500, 500); myPort = Serial tshiab (qhov no, "COM3", 115200); img = loadImage ("01d.png"); img2 = loadImage ("01n.png"); img3 = loadImage ("02d.png"); img4 = loadImage ("02n.png"); img5 = loadImage ("03d.png"); img6 = loadImage ("03n.png"); img7 = loadImage ("04d.png"); img8 = loadImage ("04n.png"); img9 = loadImage ("09d.png"); img10 = loadImage ("09n.png"); img11 = loadImage ("10d.png"); img12 = loadImage ("10n.png"); img13 = loadImage ("11d.png"); img14 = loadImage ("11n.png"); img15 = loadImage ("13d.png"); img16 = loadImage ("13n.png"); img17 = loadImage ("50d.png"); img18 = loadImage ("50n.png"); } void kos () {keeb kwm yav dhau (72, 209, 204); textSize (22); sau (54, 69, 79); text ("Kub:", 25, 100); ntawv (temp +"° C", 200, 100); text ("Siab:", 25, 150); ntawv nyeem (pres + "hpa", 200, 150); text ("Vaum:", 25, 200); ntawv (ntub+" %", 200, 200); text ("Cua:", 25, 250); ntawv (cua+"m/s", 200, 250); text ("Lub Tebchaws/Lub Nroog:", 25, 300); ntawv (lub tebchaws + "-" + nroog, 200, 300); text ("Huab cua:", 25, 350); ntawv (huab cua, 200, 350); yog (icon.contains ("01d")) {duab (img, 380, 15); } lwm yog (icon.contains ("01n")) {duab (img2, 380, 15); } lwm yog (icon.contains ("02d")) {duab (img3, 380, 15); } lwm yog (icon.contains ("02n")) {duab (img4, 380, 15); } lwm yog (icon.contains ("03d")) {duab (img5, 380, 15); } lwm yog (icon.contains ("03n")) {duab (img6, 380, 15); } lwm yog (icon.contains ("04d")) {duab (img7, 380, 15); } lwm yog (icon.contains ("04n")) {duab (img8, 380, 15); } lwm yog (icon.contains ("09d")) {duab (img9, 380, 15); } lwm yog (icon.contains ("09n")) {duab (img10, 380, 15); } lwm yog (icon.contains ("10d")) {duab (img11, 380, 15); } lwm yog (icon.contains ("10n")) {duab (img12, 380, 15); } lwm yog (icon.contains ("11d")) {duab (img13, 380, 15); } lwm yog (icon.contains ("11n")) {duab (img14, 380, 15); } lwm yog (icon == "13d") {duab (img15, 380, 15); } lwm yog (icon.contains ("13n")) {duab (img16, 380, 15); } lwm yog (icon.contains ("50d")) {duab (img17, 380, 15); } lwm yog (icon.contains ("50n")) {duab (img18, 380, 15); }} void serialEvent (Serial myPort) {yog (myPort.available ()> 0) {String data = myPort.readStringUntil ('\ n'); yog (data! = tsis muaj) {data = trim (data); Cov khoom hlua = cais (cov ntaub ntawv, '/'); yog (items.length> 1) {temp = int (yam khoom [0]); pres = int (cov khoom [1]); ntub = int (cov khoom [2]); cua = int (cov khoom [3]); nroog = cov khoom [4].replace ("\" "," "); lub tebchaws = cov khoom [5].replace (" / "", ""); huab cua = cov khoom [6].replace ("\" "," "); icon = yam khoom [7].replace (" / "", ""); }}}}

Pom zoo: