Home > Projects > Getting weather data

Getting weather data

Summary of Getting weather data


This tutorial shows how to use an Arduino MKR1000 to fetch weather forecasts from OpenWeatherMap via WiFi, parse the JSON response with ArduinoJson, and use the forecast (e.g., detect rain) to control a relay for an irrigation pump. It explains obtaining an API key, testing the API in a browser, making HTTP GET requests from the MKR1000, parsing the returned JSON, and extracting the next three forecast entries.

Parts used in the A very savvy irrigation system:

  • Arduino MKR1000
  • WiFi network (SSID and password) — credentials for connection
  • OpenWeatherMap API key
  • Relay (connected to water pump)
  • Water pump (controlled by relay)
  • USB cable (for programming and serial monitor)
  • Computer with Arduino IDE and ArduinoJson library

With this tutorial, you will learn how to get the weather data from a web service to your Arduino.

Things used in this project

Hardware components

Arduino MKR1000
Arduino MKR1000
× 1

Story

Get the code here

#include <SPI.h> 
#include <WiFi101.h> 
char ssid[] = SECRET_SSID; //  your network SSID (name) 
char pass[] = SECRET_PSW;//  your network PASSWORD () 
//open weather map api key 
String apiKey= SECRET_APIKEY; 
//the city you want the weather for 
String location= "torino,IT"; 
int status = WL_IDLE_STATUS; 
char server[] = "api.openweathermap.org";     
WiFiClient client; 
void setup() { 
 //Initialize serial and wait for port to open: 
 Serial.begin(9600); 
 // attempt to connect to Wifi network: 
 while (status != WL_CONNECTED) { 
   Serial.print("Attempting to connect to SSID: "); 
   Serial.println(ssid); 
   status = WiFi.begin(ssid); 
   //use the line below if your network is protected by wpa password  
   //status = WiFi.begin(ssid, pass); 
   // wait 10 seconds for connection: 
   delay(1000); 
 } 
 Serial.println("Connected to wifi"); 
} 
void loop() { 
 getWeather(); 
 delay(10000); 
} 
void getWeather() { 
 Serial.println("\nStarting connection to server..."); 
 // if you get a connection, report back via serial: 
 if (client.connect(server, 80)) { 
   Serial.println("connected to server"); 
   // Make a HTTP request: 
   client.print("GET /data/2.5/forecast?"); 
   client.print("q="+location); 
   client.print("&appid="+apiKey); 
   client.print("&cnt=3"); 
   client.println("&units=metric"); 
   client.println("Host: api.openweathermap.org"); 
   client.println("Connection: close"); 
   client.println(); 
 } else { 
   Serial.println("unable to connect"); 
 } 
 delay(1000); 
 String line = ""; 
 while (client.connected()) { 
   line = client.readStringUntil('\n'); 
   Serial.println(line); 
 } 
} 

If everything goes well you should be able to open the serial port and see the json data printed in the serial monitor.

Notice, inside the sketch, that most of the code needed to do the HTTP get request is inside the getWeather function. it should look very similar to the url we were using to get the data in the browser.

Parse the Json data

Now we just need to get only the information that we need from the sketch.

To parse the data we are going to use the Arduino Json library, therefore, if you don’t have it already just go ahead and download it from the Arduino library manager.

Schematics

Code

A very savvy irrigation system

 

Quick Solutions to Questions related to A very savvy irrigation system:

  • How do I get an API key for OpenWeatherMap?
    Create an account on OpenWeatherMap and generate a new api key from your account as described in the tutorial.
  • Can the MKR1000 fetch weather data from OpenWeatherMap?
    Yes, the tutorial shows using the MKR1000 WiFiConnection to query the OpenWeatherMap forecast API.
  • What URL is used to test the forecast in a browser?
    Use http://api.openweathermap.org/data/2.5/forecast?q=city,countryCode&cnt=3&appid=your_api_key replacing city,countryCode and your_api_key.
  • How many forecast entries does the example request from the API?
    The example requests cnt=3 so it fetches the next three forecast entries.
  • How is the HTTP request made from the MKR1000 sketch?
    The sketch connects to api.openweathermap.org on port 80 and sends a GET /data/2.5/forecast?... request including q, appid, cnt, and units parameters.
  • How do you parse the JSON response on Arduino?
    The tutorial uses the ArduinoJson library and StaticJsonBuffer to parse the JSON and access fields like root["list"][0]["weather"][0]["main"].
  • What weather field provides the plain-word forecast like Rain or Clear?
    The main field under weather, accessed as data["list"][0]["weather"][0]["main"], returns the plain-word forecast.
  • Can the parsed forecast be used to control a relay for irrigation?
    Yes, the tutorial describes activating the relay connected to the water pump only if there is no rain forecasted.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top