Orodha ya maudhui:

(Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API: Hatua 5
(Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API: Hatua 5

Video: (Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API: Hatua 5

Video: (Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API: Hatua 5
Video: Lesson 45: Measure temperature and Pressure with BME280 display on LCD1602 and LCD2004 with Arduino 2024, Novemba
Anonim
(Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API
(Mradi wa IOT) Pata Takwimu za Hali ya Hewa Kutumia ESP8266 na Openweather API

Katika hili tunaweza kufundisha mradi rahisi wa IOT ambao utachukua data ya hali ya hewa ya jiji letu kutoka openweather.com/api na kuionyesha kwa kutumia programu ya Usindikaji.

Ugavi:

  1. Arduino
  2. ESP8266 au moduli nyingine yoyote ya esp
  3. Arduino IDE
  4. Inasindika programu
  5. Bodi ya mkate
  6. Jumperwires Mwanaume kwa Mwanaume na Mwanaume kwa Mwanamke

Hatua ya 1: Pata Ufunguo wa API na URL Kutoka Openweather.org

Pata Ufunguo wa API na URL Kutoka Openweather.org
Pata Ufunguo wa API na URL Kutoka Openweather.org
Pata Ufunguo wa API na URL Kutoka Openweather.org
Pata Ufunguo wa API na URL Kutoka Openweather.org
Pata Ufunguo wa API na URL Kutoka Openweather.org
Pata Ufunguo wa API na URL Kutoka Openweather.org
  1. Unda akaunti katika https://openweathermap.org (Picha 1)
  2. Baada ya kuingia ingia kwenye funguo za API na utapata kitufe cha API kama inavyoonekana kwenye picha. (Picha 2)
  3. Nakili kitufe cha API na uihifadhi kwenye faili ya maandishi. (Picha 3)
  4. Nenda kwa chaguo la API (Picha 4)
  5. Nenda kwa chaguo la hati ya API kama inavyoonekana kwenye picha (Picha 5)
  6. Nakili url kutoka kwa na iliyoonyeshwa na uihifadhi kwenye faili ya daftari (Picha 6)

Hatua ya 2: Mchoro wa Uunganisho

Mchoro wa Uunganisho
Mchoro wa Uunganisho

Hatua ya 3: Msimbo wa Arduino

Kabla ya kunakili nambari hii ndani ya Arduino hakikisha umepakua bodi ya ESP8266 kwenye ideu ya ideuino ukitumia meneja wa Bodi.

# pamoja

# pamoja na # pamoja na # pamoja na const char char * ssid = "SSID Yako"; const char * password = "NENO LAKO LA SSID"; // Jina lako la Kikoa lenye njia ya URL au anwani ya IP iliyo na njia String openWeatherMapApiKey = "Bandika ufunguo wako wa API hapa"; // Badilisha na nambari yako ya nchi na jiji String city = "Mumbai"; Nambari ya nchi ya kamba = "IN"; Data ya kamba [16]; // KIWANGO CHA KOSA KIMEWEKWA KWA SEKUNDE 10 KWA MADHUMUNI YA KUJARIBU // Kwa programu ya mwisho, angalia mipaka ya simu za API kwa saa / dakika ili kuzuia kuzuiwa / kupigwa marufuku kutosainiwa muda wa mwisho = 0; // Timer imewekwa kwa dakika 10 (600000) // muda usiosainiwaDelay = 600000; // Weka kipima muda kwa sekunde 10 (10000) kipima muda kisichosainiwaDelay = 10000; Kamba jsonBuffer; kuanzisha batili () {Serial.begin (115200); Kuanza kwa WiFi (ssid, password); //Serial.println ("Kuunganisha"); wakati (WiFi.status ()! = WL_CONNECTED) {kuchelewa (500); // Serial.print ("."); } // Serial.println (""); // Serial.print ("Imeunganishwa kwenye mtandao wa WiFi na Anwani ya IP:"); // Serial.println (WiFi.localIP ()); // // Serial.println ("Timer imewekwa sekunde 10 (timerDelay variable), itachukua sekunde 10 kabla ya kuchapisha usomaji wa kwanza."); } kitanzi batili () {// Tuma ombi la kupata HTTP ikiwa ((millis () - Mara ya mwisho)> timerDelay) {// Angalia hali ya unganisho la WiFi ikiwa (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) inaweza kutumika kupata aina ya var ikiwa (JSON.typeof (myObject) == "haijafafanuliwa") {//Serial.println ("Kuweka pembejeo kumeshindwa!"); kurudi; } // Serial.print ("JSON object ="); // Serial.println (myObject); // Serial.print ("Joto:"); // Serial.println (myObject ["kuu"] ["temp"]); // Serial.print ("Shinikizo:"); // Serial.println (myObject ["kuu"] ["shinikizo"]); // Serial.print ("Unyevu:"); // Serial.println (myObject ["kuu"] ["unyevu"]); // Serial.print ("Kasi ya Upepo:"); // Serial.println (myObject ["upepo"] ["kasi"]); int temp = myObject ["kuu"] ["temp"]; pres ya muda mrefu = myObject ["kuu"] ["shinikizo"]; hum hum = myObject ["kuu"] ["unyevu"]; upepo int = myObject ["upepo"] ["kasi"]; Nchi ya kamba = JSON.stringify (myObject ["sys"] ["nchi"]); String city1 = JSON.stringify (myObject ["jina"]); Hali ya hewa ya kamba = JSON.stringify (myObject ["hali ya hewa"] [0] ["maelezo"]); String icon = JSON.stringify (myObject ["hali ya hewa"] [0] ["icon"]); data [0] = Kamba (temp); data [1] = "/"; data [2] = Kamba (pres); data [3] = "/"; data [4] = Kamba (unyevu); data [5] = "/"; data [6] = Kamba (upepo); data [7] = "/"; data [8] = nchi; data [9] = "/"; data [10] = jiji1; data [11] = "/"; data [12] = hali ya hewa; data [13] = "/"; data [14] = ikoni; data [15] = "\ n"; kwa (int i = 0; i0) {// Serial.print ("Msimbo wa Majibu wa HTTP:"); // Serial.println (httpResponseCode); malipo = http.getString (); } mwingine {Serial.print ("Nambari ya hitilafu:"); Serial.println (httpResponseCode); } // Rasilimali za bure http.end (); malipo ya malipo; }

Hatua ya 4: Msimbo wa Usindikaji

Msimbo wa usindikaji
Msimbo wa usindikaji
Msimbo wa usindikaji
Msimbo wa usindikaji
Msimbo wa usindikaji
Msimbo wa usindikaji

Kabla ya kutumia nambari hii pakua picha za ikoni ambazo zitatumika kuonyesha hali ya hewa. Na weka picha na nambari kwenye folda moja.

usindikaji wa kuagiza.serial. *;

Serial myPort; Picha ya PImage; PImage img2; PImage img3; PImage img4; PImage img5; PImage img6; PImage img7; PImage img8; PImage img9; PImage img10; PImage img11; PImage img12; PImage img13; PImage img14; PImage img15; PImage img16; PImage img17; PImage img18; muda; int pres; unyevu wa ndani; upepo; Mji wa kamba = ""; Nchi ya kamba = ""; Hali ya hewa ya kamba = ""; Ikoni ya kamba = ""; usanidi batili () {saizi (500, 500); myPort = mpya Serial (hii, "COM3", 115200); img = mzigoImage ("01d.png"); img2 = mzigoImage ("01n.png"); img3 = mzigoImage ("02d.png"); img4 = mzigoImage ("02n.png"); img5 = mzigoImage ("03d.png"); img6 = mzigoImage ("03n.png"); img7 = mzigoImage ("04d.png"); img8 = mzigoImage ("04n.png"); img9 = mzigoImage ("09d.png"); img10 = mzigoImage ("09n.png"); img11 = mzigoImage ("10d.png"); img12 = mzigoImage ("10n.png"); img13 = mzigoImage ("11d.png"); img14 = mzigoImage ("11n.png"); img15 = mzigoImage ("13d.png"); img16 = mzigoImage ("13n.png"); img17 = mzigoImage ("50d.png"); img18 = mzigoImage ("50n.png"); } sare batili () {msingi (72, 209, 204); maandishiSize (22); jaza (54, 69, 79); maandishi ("Joto:", 25, 100); maandishi (temp + "° C", 200, 100); maandishi ("Shinikizo:", 25, 150); maandishi (pres + "hpa", 200, 150); maandishi ("Unyevu:", 25, 200); maandishi (baridi + "%", 200, 200); maandishi ("Upepo:", 25, 250); maandishi (upepo + "m / s", 200, 250); maandishi ("Nchi / Jiji:", 25, 300); maandishi (nchi + "-" + jiji, 200, 300); maandishi ("Hali ya Hewa:", 25, 350); maandishi (hali ya hewa, 200, 350); ikiwa (icon.contains ("01d")) {picha (img, 380, 15); } mwingine ikiwa (icon.contains ("01n")) {picha (img2, 380, 15); } mwingine ikiwa (icon.contains ("02d")) {picha (img3, 380, 15); } mwingine ikiwa (icon.contains ("02n")) {picha (img4, 380, 15); } mwingine ikiwa (icon.contains ("03d")) {picha (img5, 380, 15); } mwingine ikiwa (icon.contains ("03n")) {picha (img6, 380, 15); } mwingine ikiwa (icon.contains ("04d")) {picha (img7, 380, 15); } mwingine ikiwa (icon.contains ("04n")) {picha (img8, 380, 15); } mwingine ikiwa (icon.contains ("09d")) {picha (img9, 380, 15); } mwingine ikiwa (icon.contains ("09n")) {picha (img10, 380, 15); } mwingine ikiwa (icon.contains ("10d")) {picha (img11, 380, 15); } mwingine ikiwa (icon.contains ("10n")) {picha (img12, 380, 15); } mwingine ikiwa (icon.contains ("11d")) {picha (img13, 380, 15); } mwingine ikiwa (icon.contains ("11n")) {picha (img14, 380, 15); } mwingine ikiwa (icon == "13d") {picha (img15, 380, 15); } mwingine ikiwa (icon.contains ("13n")) {picha (img16, 380, 15); } mwingine ikiwa (icon.contains ("50d")) {picha (img17, 380, 15); } mwingine ikiwa (icon.contains ("50n")) {picha (img18, 380, 15); }} tupu serialEvent (Serial myPort) {if (myPort.available ()> 0) {String data = myPort.readStringUntil ('\ n'); ikiwa (data! = batili) {data = trim (data); Vitu vya kamba = mgawanyiko (data, '/'); ikiwa (items.length> 1) {temp = int (items [0]); pres = int (vitu [1]); unyevu = int (vitu [2]); upepo = int (vitu [3]); jiji = vitu [4]. mahali ("\" "," "); nchi = vitu [5]. mahali (" / "", ""); hali ya hewa = vitu [6]. mahali ("\" "," "); ikoni = vitu [7]. mahali (" / "", ""); }}}}

Ilipendekeza: