Mundarija:
2025 Muallif: John Day | [email protected]. Oxirgi o'zgartirilgan: 2025-01-13 06:58
Bu yo'riqnomada biz oddiy IOT loyihasini qurmoqchimiz, unda openweather.com/api saytidan shahrimiz ob -havo ma'lumotlarini olish va uni ishlov berish dasturi yordamida ko'rsatish.
Ta'minot:
- Arduino
- ESP8266 yoki boshqa har qanday esp moduli
- Arduino IDE
- Qayta ishlash dasturi
- Non paneli
- Jumper simlari erkakdan erkakgacha va erkakdan ayolgacha
1 -qadam: Openweather.org saytidan API kaliti va URL manzilini oling
- Https://openweathermap.org saytida hisob yarating (1 -rasm)
- Kirishdan so'ng API kalitlariga o'ting va rasmda ko'rsatilgandek API kalitini olasiz. (2 -rasm)
- API kalitini nusxa ko'chiring va uni bloknot fayliga saqlang. (3 -rasm)
- API variantiga o'ting (4 -rasm)
- Rasmda ko'rsatilgandek API doc variantiga o'ting (5 -rasm)
- Ko'rsatilgan va ko'rsatilgandan urlni nusxalash va bloknot fayliga saqlash (6 -rasm)
2 -qadam: ulanish diagrammasi
3 -qadam: Arduino kodi
Ushbu kodni Arduino -ga nusxalashdan oldin, ESP8266 kartasini taxtalar menejeri yordamida arduino ide -ga yuklaganingizga ishonch hosil qiling.
#qo'shing
#include #include #include const char* ssid = "Sizning SSID"; const char* password = "Sizning SSID parolingiz"; // URL yo'lli domen nomingiz yoki yo'lli IP -manzil String openWeatherMapApiKey = "API kalitingizni bu erga joylashtiring"; // Mamlakat kodi va shahar bilan almashtiring String city = "Mumbay"; String countryCode = "IN"; String ma'lumotlari [16]; // MAKSADLARNI TESTLASH UCHUN 10 DEBUNIYa BOSHLANGAN TAYMER o'rnatildi // Oxirgi dastur uchun, bloklanmagan yoki taqiqlanmagan, uzoq vaqt davomida API qo'ng'iroqlar chegarasini tekshiring/lastTime = 0; // Taymer 10 daqiqaga o'rnatildi (600000) // imzosiz uzun taymerDelay = 600000; // Taymerni 10 soniyaga o'rnating (10000) belgisiz uzun taymerDelay = 10000; JsonBuffer satri; void setup () {Serial.begin (115200); WiFi.begin (ssid, parol); //Serial.println("Ulanish"); while (WiFi.status ()! = WL_CONNECTED) {kechiktirish (500); // Serial.print ("."); } // Serial.println (""); // Serial.print ("IP -manzil bilan WiFi tarmog'iga ulangan:"); // Serial.println (WiFi.localIP ()); // // Serial.println ("Taymer 10 soniyaga o'rnatildi (timerDelay o'zgaruvchisi), birinchi o'qishni e'lon qilish uchun 10 soniya kerak bo'ladi."); } void loop () {// HTTP GET so'rovini yuboring, agar ((millis () - lastTime)> timerDelay) {// Wi -Fi ulanish holatini tekshiring, agar (WiFi.status () == WL_CONNECTED) {String serverPath = "https://api.openweathermap.org/data/2.5/weather?q= " + shahar +", " + countryCode +" & APPID = " +" d5b56fd07988143ae141503ed9d81742 " +" & birliklar = metrik "; jsonBuffer = httpGETRequest (serverPath.c_str ()); //Serial.println(jsonBuffer); JSONVar myObject = JSON.parse (jsonBuffer); Var turini olish uchun // JSON.typeof (jsonVar) dan foydalanish mumkin, agar (JSON.typeof (myObject) == "aniqlanmagan") {//Serial.println("Tashkil etilmadi! "); qaytish; } // Serial.print ("JSON obyekti ="); // Serial.println (myObject); // Serial.print ("Harorat:"); // Serial.println (myObject ["asosiy"] ["temp"]); // Serial.print ("Bosim:"); // Serial.println (myObject ["asosiy"] ["bosim"]); // Serial.print ("Namlik:"); // Serial.println (myObject ["asosiy"] ["namlik"]); // Serial.print ("Shamol tezligi:"); // Serial.println (myObject ["shamol"] ["tezlik"]); int temp = myObject ["asosiy"] ["temp"]; long pres = myObject ["asosiy"] ["bosim"]; int humid = myObject ["asosiy"] ["namlik"]; int shamol = myObject ["shamol"] ["tezlik"]; String mamlakat = JSON.stringify (myObject ["sys"] ["mamlakat"]); String city1 = JSON.stringify (myObject ["nomi"]); String ob -havo = JSON.stringify (myObject ["ob -havo"] [0] ["tavsif"]); String belgisi = JSON.stringify (myObject ["ob -havo"] [0] ["belgi"]); ma'lumotlar [0] = String (temp); ma'lumotlar [1] = "/"; ma'lumotlar [2] = String (pres); ma'lumotlar [3] = "/"; ma'lumotlar [4] = String (nam); ma'lumotlar [5] = "/"; ma'lumotlar [6] = Ip (shamol); ma'lumotlar [7] = "/"; ma'lumotlar [8] = mamlakat; ma'lumotlar [9] = "/"; ma'lumotlar [10] = shahar1; ma'lumotlar [11] = "/"; ma'lumotlar [12] = ob -havo; ma'lumotlar [13] = "/"; ma'lumotlar [14] = belgi; ma'lumotlar [15] = "\ n"; for (int i = 0; i0) {// Serial.print ("HTTP javob kodi:"); // Serial.println (httpResponseCode); yuk = http.getString (); } else {Serial.print ("Xato kodi:"); Serial.println (httpResponseCode); } // Bepul resurslar http.end (); yukni qaytarish; }
4 -qadam: kodni qayta ishlash
Ushbu kodni ishga tushirishdan oldin ob -havoni ko'rsatish uchun ishlatiladigan tasvir tasvirini yuklab oling. Va rasm va kodni bitta papkada saqlang.
importni qayta ishlash.serial.*;
Seriyali myPort; PI tasvir img; Img2 tasviri; PImg img3; PImg img4; PImg img5; PImg img6; PImg img7; PImg img8; Img9 tasviri; Img10 tasviri; PImg img11; PImg img12; PImg img13; PImg img14; Img15 tasviri; Img16 tasviri; Rasm img17; Img18 tasviri; int temp; int pres; nam nam; kuchli shamol; String shahar = ""; String mamlakat = ""; Qattiq ob -havo = ""; String belgisi = ""; void setup () {size (500, 500); myPort = yangi seriyali (bu, "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 draw () {background (72, 209, 204); textSize (22); to'ldirish (54, 69, 79); matn ("Harorat:", 25, 100); matn (temp +"° C", 200, 100); matn ("Bosim:", 25, 150); matn (pres + "hpa", 200, 150); matn ("Namlik:", 25, 200); matn (nam+" %", 200, 200); matn ("Shamol:", 25, 250); matn (shamol+"m/s", 200, 250); matn ("Mamlakat/shahar:", 25, 300); matn (mamlakat + "-" + shahar, 200, 300); matn ("Ob -havo:", 25, 350); matn (ob -havo, 200, 350); if (icon.contains ("01d")) {tasvir (img, 380, 15); } if if (icon.contains ("01n")) {image (img2, 380, 15); } if if (icon.contains ("02d")) {image (img3, 380, 15); } if if (icon.contains ("02n")) {image (img4, 380, 15); } if if (icon.contains ("03d")) {image (img5, 380, 15); } if if (icon.contains ("03n")) {image (img6, 380, 15); } if if (icon.contains ("04d")) {image (img7, 380, 15); } if if (icon.contains ("04n")) {image (img8, 380, 15); } if if (icon.contains ("09d")) {image (img9, 380, 15); } if if (icon.contains ("09n")) {image (img10, 380, 15); } if if (icon.contains ("10d")) {image (img11, 380, 15); } if if (icon.contains ("10n")) {image (img12, 380, 15); } if if (icon.contains ("11d")) {image (img13, 380, 15); } if if (icon.contains ("11n")) {image (img14, 380, 15); } if if (icon == "13d") {image (img15, 380, 15); } if if (icon.contains ("13n")) {image (img16, 380, 15); } if if (icon.contains ("50d")) {image (img17, 380, 15); } if if (icon.contains ("50n")) {image (img18, 380, 15); }} void serialEvent (Serial myPort) {if (myPort.available ()> 0) {String data = myPort.readStringUntil ('\ n'); if (ma'lumotlar! = null) {ma'lumotlar = trim (ma'lumotlar); String elementlari = ajratish (ma'lumotlar, '/'); if (items.length> 1) {temp = int (items [0]); pres = int (elementlar [1]); nam = int (elementlar [2]); shamol = int (elementlar [3]); shahar = punktlar [4].jozib ("\" "," "); mamlakat = elementlar [5].rash (" / "", ""); ob -havo = elementlar [6].jozib ("\" "," "); ikonka = elementlar [7].rash (" / "", ""); }}}}