Introduction: ESP8266 Weather Server
If you just bought the ESP8266 and have no idea of where to start, or just wish to have the ESP (I will call the ESP8266 as ESP from now on for the sake of brevity) serve you local weather info for you and people connected to your WiFi, then this post is just for you. The ESP8266 is a low-cost Wi-Fi chip with a full TCP/IP stack and MCU (microcontroller unit) all on a single board that is available for purchase at only about $5 (varies from vendor to vendor). The ESP can do some very cool stuff, like being a web client and a server at the same time. In this instructable, we will be using the ESP to fetch local weather information from OpenWeatherMap using their free API (limited to only 60 requests per minute) and serve it to users connected to our WiFi network. Let's get started.
This project was done by me, Nikhil Raghavendra, a Diploma in Computer Engineering student from Singapore Polytechnic, School of Electrical and Electronic Engineering, under the guidance of my mentor Mr Teo Shin Jen.
Step 1: Obtaining an API Key
To use the free API provided by OpenWeatherMap, you need to know your current location (or target location if you are doing remote monitoring) and the API key. To obtain an API key head over to OpenWeatherMap and sign in. If you are not a member, register yourself and sign in. Once you have signed in, click on the API keys tab, key in the name of your API key and hit the "Generate" button. Then copy your key and save it into a text file (highly recommended) in your working directory.
Step 2: Obtaining Coordinates of Your Location
The API works well with city names, but I like to be specific and if you want to be specific as well, head over to Google Maps and check the search bar of your browser. The numbers after the '@' symbol are your coordinates and if you don't see them for some reason (you might have blocked location access), type in your city name or street address and you should have them there. Copy and paste the coordinates into the same text file where you saved your API key earlier on.
Step 3: Writing the Code
Now, connect the ESP to your computer using a USB to micro USB cable. Then open up the Arduino IDE and head over to the library manager and install the ArduinoJson library. Download the attached code and run it. Before you run it on your ESP, make sure to provide your WiFi SSID and password. The API URL is also needed for the application to work properly, if not you won't even have the weather info to serve. The API URL is in the following format:
As you can see, you need the coordinates of your current/target location and the API key in order for the API request to function properly. To get the predicted weather info, we perform a GET request using the API URL with the appropriate parameters and then parse the response string using the ArduinoJson library. I will be covering more about the ArduinoJson library in detail, in another instructable.
Step 4: The Results
To check the IP address of your ESP, open the serial monitor and take note of the address. If you don't see anything, press the reset button on your ESP. Type in the IP address of your ESP into the browser of your choice and *boom*, you should see the predicted weather condition at your location.