Weather Station Application – APIs

I have always been intrigued by REST APIs, how they work, how apps use them, and how I could play with APIs in practical projects of my own. In sleuthing the internet, I found that not only are others interested in the same thing, but many people have made open source projects that are useful in production environments and use API data from generously free data sources.

Weather Station ApplicationA project that really caught my attention was Eric Elewin’s Pi Weather Station application. This application was intended to be built on and run on a RaspberryPi single-board computer (SBC). The application aggregates API data from 3 different sources and outputs a fantastic visual map charting weather data and forecasts. In the current environment, SBCs are very hard to come by, or prices are so high that they are not worth purchasing. However, I wondered if I could get this to run on a virtual machine (VM) before I moved it to a spare RaspberryPi I have laying around waiting for a project.

The GitHub App could have been titled “Weather Station APP” instead of Pi-Weather-Station. To my delight, it works on every OS I tried it on (Linux based), including different VMs. I could quickly get this application to run on VMs with ARM and AMD processors running ubuntu, a RaspberryPi running Raspian OS, Ubuntu, and Linux KDE Plasma. I think any Linux distribution will work as long as you can install nodejs, NPM, and a GUI.

My Use Case

For my use case, I spun up the application on a VM and provided APIs from 3 recommended data sources. After exploring the program, playing with how to access the application on my home network (outside the local machine), and adding a CRON to automatically start the App on reboot, I replicated the process on one of my spare RaspberryPis. It’s as easy (after updating index.js) as navigating to the host IP with port 8080, and you can access the application! I have an old … I mean, old iPad lying around the house. It does not even have any trade-in value. So, now, I will use it as a constant display for my weather station.

Installation

If you try to install a package you don’t need, your OS will tell you that it is installed, and you can move to the next step. To show the installation process, I will walk through the process I used on my virtual machine. You can follow this for an installation on a RaspberryPi as well.

If you want to try this on a VM first and are interested in using a no-cost VM through Oracle Cloud, check out my recent post here.

Log into or ssh into your machine on which you will install the application.

SSH ScreenInstall NPM:

sudo apt install npm

Install the latest nodejs instance:

curl -sL https://deb.nodesource.com/setup_14.x | sudo bash - 

Now run the command to install Nodejs:

sudo apt install nodejs

NPM INSATALL

Now we can clone the application from GitHub:

git clone https://github.com/elewin/pi-weather-station.git

Clone into desired foldercd into pi weather folder:

cd pi-weather-station

Now we can start the application:

npm start

If you get any NPM module errors, then run:

sudo npm audit fix --force

Now you can restart your App (if needed) with:

npm start

You should see the App confirm starting on port 8080, and a web browser will automatically launch and display the application settings page.

You can stop the App from the terminal screen by pressing CTL C.

With the App running, we can start configuring our application to pull data from various sources.

You are going to need to set up free accounts and create API Keys at the following sites:

Also, you will want to go to google maps, place a PIN at your desired selected home location, and right-click to get your latitude and longitude location. The Lat/Long format should be no more than 4 decimal places (i.e., 42.3123, -107.2312). Custom coordinates are optional, but you may not get an API pull or a “did you input your API Key” error if you don’t add the cords (I did on all OS I installed).

https://www.mapbox.com/I will not walk through setting up accounts on these sites as it is straightforward. after creating an account at the respective sites, you simply need to navigate to the developer or API section and create an API Key, then record it for placement into the application. After inputting your API keys into the App and entering your Lat/Long coordinates, click the save button. You should see data populate like a map and sidebar with weather data.

I will not walk through setting up accounts on these sites as it is straightforward. after creating an account at the respective sites, you simply need to navigate to the developer or API section and create an API Key, then record it for placement into the application. After inputting your API keys into the App and entering your Lat/Long coordinates, click the save button. You should see data populate like a map and sidebar with weather data.

Access from Another Machine

It’s possible to access the App from another machine/device on the same network, but beware that by doing so, you’ll be exposing the App to your entire network. Someone on your network could access the App and receive your API keys from the settings page. By default, the App is only accessible to localhost, but if you would like to open it up to your network (at your own risk, as I did!), open the file located at pi-weather-station/server/index.js and remove “localhost” from the line that contains:

app.listen(PORT, "localhost", async () => {

So that it becomes:

app.listen(PORT, async () => {

 

Nano File for indexjsThe server will now serve the App across your network.

Before updating the above code, you want to stop the application, then start the application again after you have saved the /server/index.js file.

For those new to editing these files, you can change into the /server directory (from the pi-weather-station directory) and perform:

sudo nano index.js

This will open the file, and you can make your changes, save with CTL X, confirm with Y, then Enter to save over the file name.

getting to server folder

Now, using another device connected to the same local network, navigate to the IP address of the host machine running the weather app and port 8080 (i.e., 192.168.122.13:8080). If the configurations are set up correctly, you will see your application populate in the web browser. Hit the play button in the bottom right-hand sidebar and watch it send predictive weather traffic on screen.

Auto Start Weather App on Reboot

With my Weather App up and running, I decided I did want to keep it running full time on a RaspberryPi. I spun up the App on my Pi, entered my API data, and ensured I could see it on my local network from the iPad I wanted to use as my “always on” display. Worked perfectly, just like on the VMs I tested it on.

I wanted to be sure my weather app would auto start when my Pi lost power, updated and rebooted, etc. A fun way would be to containerize this App into a docker container with a restart tag, but I don’t know how to do that yet … maybe you do and want to share? However, I do know we could do this with a CRON job.

To create an autostart config, I created a file on the desktop named start-weather (with mousepad text editor) and typed the following script:

#!/bin/bash

#Auto Start Weather Application

cd pi-weather-station/
npm start

I saved the file on the Desktop.
Now to make the file executable open a terminal, cd to the Desktop folder, and type ls to ensure your file is listed. Then type:

chmod +x start-weather

Rerun the “ls” command, and you should see the file listed in green instead of blue, showing it is now executable.

Now in the terminal, type the following to create the CRON:

crontab -e

if it is your first time opening the editor, you will be given a choice of the editor to use. Select nano.
CRON TAB Editor
when the nano editor displays, scroll to the bottom of the file and enter:

@reboot sleep 20 && export DISPLAY=:0 && /home/pi/Desktop/start-weather

Cron add and start scriptYou would need to change the user name of the path (if different) and the file’s location if you saved it somewhere else. This command says that at reboot, use display 0 after waiting 20 secs (from reboot) and running the start-weather script.

To Sum it Up

I really liked setting up this project. Not only was it fun to retrieve my own “developer APIs” from different sources, but being able to plug them in and generate results was really cool! I have been running the Application for over a week now. It seems to be very accurate with all the “current” weather and the storm cell mapping matches what you see with your own eyes. However, I will say that the predictive % for rain, etc. is like the local news sources, sometimes it is right, sometimes it’s really off! For example, it showed a 2% chance of rain, and it was downpouring! To be fair though it did say it was raining and humidity was correct, and storm cell graphics confirmed as well. 

Resources ConsumedThis is a fast project that anyone can really do. Again, I am displaying the weather app on an old iPad I hung on the wall. The iPad is always doing its thing. I had a power reboot and everything came right back up. Also, I am still running this on a RaspberryPi SBC (using my VM for other fun testing now), and it really is not taking up very many resources. I will probably add additional applications to the Pi. Have fun trying this out and learning about how APIs are used in applications. This is a good stepping stone to creating your own application and aggregating data from multiple sources!

Always on Display

Resources

I used various resources to get this App running how I wanted it to. First and foremost, I used the detailed instructions from Eric’s GitHub found here:

https://github.com/elewin/pi-weather-station

I also used information found from a YouTube video done by Novaspirit Tech:

https://youtu.be/2-9yXwSEu6g

Finally, for the auto start and cron features, I used information found in
KM4ACK’s YouTube video on the application that is located here:

https://youtu.be/cTGTB1sD3MQ

2 thoughts on “Weather Station Application – APIs

  1. Ezzy says:

    This is such an awesome and cool project! Thank you for sharing these instructions. Love how it looks on your iPad by the way!

    Reply
    1. Sean says:

      Thank you Ezzy! It really was fun! I hope others try it and like it as well!

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.