Tabla de contenido:

(Proyecto IOT) Obtenga datos meteorológicos utilizando ESP8266 y Openweather API: 5 pasos
(Proyecto IOT) Obtenga datos meteorológicos utilizando ESP8266 y Openweather API: 5 pasos

Video: (Proyecto IOT) Obtenga datos meteorológicos utilizando ESP8266 y Openweather API: 5 pasos

Video: (Proyecto IOT) Obtenga datos meteorológicos utilizando ESP8266 y Openweather API: 5 pasos
Video: SENSOR de TEMPERATURA con ESP32 THINGSPEAK IOT -SENDING data to THINGSPEAK using ESP32 ESP8266 🌡️ 2024, Septiembre
Anonim
(Proyecto IOT) Obtenga datos meteorológicos mediante ESP8266 y la API de Openweather
(Proyecto IOT) Obtenga datos meteorológicos mediante ESP8266 y la API de Openweather

En este instructivo, vamos a construir un proyecto IOT simple en el que obtendremos los datos meteorológicos de nuestra ciudad de openweather.com/api y los mostraremos usando el software Processing.

Suministros:

  1. Arduino
  2. ESP8266 o cualquier otro módulo esp
  3. IDE de Arduino
  4. Software de procesamiento
  5. Tablero de circuitos
  6. Cables de puente macho a macho y macho a hembra

Paso 1: Obtenga la clave API y la URL de Openweather.org

Obtenga la clave API y la URL de Openweather.org
Obtenga la clave API y la URL de Openweather.org
Obtenga la clave API y la URL de Openweather.org
Obtenga la clave API y la URL de Openweather.org
Obtenga la clave API y la URL de Openweather.org
Obtenga la clave API y la URL de Openweather.org
  1. Crear cuenta en https://openweathermap.org (Imagen 1)
  2. Después de iniciar sesión, vaya a las claves API y obtendrá la clave API como se muestra en la imagen. (Imagen 2)
  3. Copie la clave API y guárdela en un archivo de bloc de notas (Imagen 3)
  4. Vaya a la opción API (Imagen 4)
  5. Vaya a la opción de documento API como se muestra en la imagen (Imagen 5)
  6. Copie la URL de y que se muestra y guárdela en un archivo de bloc de notas (Imagen 6)

Paso 2: diagrama de conexión

Diagrama de conexión
Diagrama de conexión

Paso 3: Código Arduino

Antes de copiar este código en Arduino, asegúrese de haber descargado la placa ESP8266 en arduino ide usando Boards Manager.

#incluir

#include #include #include const char * ssid = "Su SSID"; const char * password = "Your SSID PASSWORD"; // Su nombre de dominio con ruta URL o dirección IP con ruta String openWeatherMapApiKey = "Pegue su clave API aquí"; // Reemplace con su código de país y ciudad String city = "Mumbai"; String countryCode = "IN"; Datos de cadena [16]; // EL TEMPORIZADOR POR DEFECTO ESTA A 10 SEGUNDOS PARA PROPÓSITOS DE PRUEBA // Para una aplicación final, verifique los límites de llamadas de API por hora / minuto para evitar ser bloqueados / prohibidos unsigned long lastTime = 0; // Temporizador establecido en 10 minutos (600000) // Temporizador largo sin firmarDelay = 600000; // Establece el temporizador en 10 segundos (10000) unsigned long timerDelay = 10000; String jsonBuffer; configuración vacía () {Serial.begin (115200); WiFi.begin (ssid, contraseña); //Serial.println("Connecting "); while (WiFi.status ()! = WL_CONNECTED) {retraso (500); // Serial.print ("."); } // Serial.println (""); // Serial.print ("Conectado a la red WiFi con dirección IP:"); // Serial.println (WiFi.localIP ()); // // Serial.println ("Temporizador establecido en 10 segundos (variable timerDelay), tomará 10 segundos antes de publicar la primera lectura."); } void loop () {// Envía una solicitud HTTP GET if ((millis () - lastTime)> timerDelay) {// Verifica el estado de la conexión WiFi if (WiFi.status () == WL_CONNECTED) {String serverPath = "https://api.openweathermap.org/data/2.5/weather?q= "+ ciudad +", "+ countryCode +" & APPID = "+" d5b56fd07988143ae141503ed9d81742 "+" & units = metric "; jsonBuffer = httpGETRequest (ruta del servidor.c_str ()); //Serial.println(jsonBuffer); JSONVar myObject = JSON.parse (jsonBuffer); // JSON.typeof (jsonVar) se puede usar para obtener el tipo de var if (JSON.typeof (myObject) == "undefined") {//Serial.println("Parsing input failed! "); regreso; } // Serial.print ("Objeto JSON ="); // Serial.println (myObject); // Serial.print ("Temperatura:"); // Serial.println (myObject ["principal"] ["temp"]); // Serial.print ("Presión:"); // Serial.println (myObject ["principal"] ["presión"]); // Serial.print ("Humedad:"); // Serial.println (myObject ["principal"] ["humedad"]); // Serial.print ("Velocidad del viento:"); // Serial.println (myObject ["viento"] ["velocidad"]); int temp = myObject ["principal"] ["temp"]; long pres = myObject ["principal"] ["presión"]; int húmedo = miObjeto ["principal"] ["humedad"]; int viento = miObjeto ["viento"] ["velocidad"]; Cadena país = JSON.stringify (myObject ["sys"] ["país"]); String city1 = JSON.stringify (myObject ["nombre"]); Cadena weather = JSON.stringify (myObject ["weather"] [0] ["description"]); String icon = JSON.stringify (myObject ["weather"] [0] ["icon"]); datos [0] = Cadena (temp); datos [1] = "/"; datos [2] = Cadena (pres); datos [3] = "/"; datos [4] = String (húmedo); datos [5] = "/"; datos [6] = String (viento); datos [7] = "/"; datos [8] = país; datos [9] = "/"; datos [10] = ciudad1; datos [11] = "/"; datos [12] = clima; datos [13] = "/"; datos [14] = icono; datos [15] = "\ n"; for (int i = 0; i0) {// Serial.print ("Código de respuesta HTTP:"); // Serial.println (httpResponseCode); payload = http.getString (); } else {Serial.print ("Código de error:"); Serial.println (httpResponseCode); } // Recursos gratuitos http.end (); devolver la carga útil; }

Paso 4: Código de procesamiento

Código de procesamiento
Código de procesamiento
Código de procesamiento
Código de procesamiento
Código de procesamiento
Código de procesamiento

Antes de ejecutar este código, descargue las imágenes de iconos dadas que se utilizarán para mostrar el clima. Y mantenga las imágenes y el código en la misma carpeta.

procesamiento de importación.serie. *;

Serial myPort; PImage img; 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; int temp; int pres; int húmedo; viento int; Cadena ciudad = ""; Cadena país = ""; String weather = ""; Icono de cadena = ""; configuración vacía () {tamaño (500, 500); myPort = new Serial (este, "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"); } vacío dibujar () {fondo (72, 209, 204); textSize (22); llenar (54, 69, 79); texto ("Temperatura:", 25, 100); texto (temp + "° C", 200, 100); texto ("Presión:", 25, 150); texto (pres + "hpa", 200, 150); texto ("Humedad:", 25, 200); texto (húmedo + "%", 200, 200); texto ("Viento:", 25, 250); texto (viento + "m / s", 200, 250); texto ("País / Ciudad:", 25, 300); texto (país + "-" + ciudad, 200, 300); texto ("Clima:", 25, 350); texto (clima, 200, 350); if (icon.contains ("01d")) {imagen (img, 380, 15); } else if (icon.contains ("01n")) {imagen (img2, 380, 15); } else if (icon.contains ("02d")) {imagen (img3, 380, 15); } else if (icon.contains ("02n")) {imagen (img4, 380, 15); } else if (icon.contains ("03d")) {imagen (img5, 380, 15); } else if (icon.contains ("03n")) {imagen (img6, 380, 15); } else if (icon.contains ("04d")) {imagen (img7, 380, 15); } else if (icon.contains ("04n")) {imagen (img8, 380, 15); } else if (icon.contains ("09d")) {imagen (img9, 380, 15); } else if (icon.contains ("09n")) {imagen (img10, 380, 15); } else if (icon.contains ("10d")) {imagen (img11, 380, 15); } else if (icon.contains ("10n")) {imagen (img12, 380, 15); } else if (icon.contains ("11d")) {imagen (img13, 380, 15); } else if (icon.contains ("11n")) {imagen (img14, 380, 15); } más si (icono == "13d") {imagen (img15, 380, 15); } else if (icon.contains ("13n")) {imagen (img16, 380, 15); } else if (icon.contains ("50d")) {imagen (img17, 380, 15); } else if (icon.contains ("50n")) {imagen (img18, 380, 15); }} void serialEvent (Serial myPort) {if (myPort.available ()> 0) {String data = myPort.readStringUntil ('\ n'); if (datos! = nulo) {datos = recortar (datos); Elementos de cadena = dividir (datos, '/'); if (items.length> 1) {temp = int (items [0]); pres = int (elementos [1]); húmedo = int (elementos [2]); viento = int (elementos [3]); ciudad = elementos [4].replace ("\" "," "); país = elementos [5].replace (" / "", ""); clima = elementos [6].replace ("\" "," "); icono = elementos [7].replace (" / "", ""); }}}}

Recomendado: