making a weather station screen using Termux and Node-RED

Hi all!

Now I’ll show you how to transfer a weather station project, originally made under the Raspberry Pi, to a smartphone. To do this, we will install the Termux program – a Linux terminal emulator -, then install the Node-RED visual programming environment on it, and in 5 minutes we will implement a finished project – a weather station, that is, a screen that displays the current weather and forecast. We will use only Free/Open Source-tools.

This text is based on my video:

And we continue to dive into the topic of reuse of technology in course “Galaxy Upcycling – a new life for an old smartphone.” What we just haven’t done: physical experiments, useful gadgets, and funny special effects. But in today’s lesson, it will be important not WHAT we will do, but HOW we will do it.

What is Termux: a Linux console on a smartphone

Termux mimics the console of the Linux operating system on an Android smartphone and does not even require Root access.

New opportunities open up:

  • try many variations of the software that already exists under Linux

  • raise your server for backup or for playing minecraft with friends

  • check systems for vulnerabilities

  • host your home page

  • and even turn your smartphone into an IoT device! What are we going to do today

Source: https://github.com/AKXX/termux
Source: https://github.com/AKXX/termux

Termux installation

Termux is distributed openly and free of charge. Simply download the installation APK file from the F-Droid website: https://f-droid.org/en/packages/com.termux/

Requires Android OS version 5 or higher. On my Galaxy S7 (2016) everything started without problems. You need to give the application various permissions during installation. And now, running Termux, you see the familiar Linux console window! Here you can use all the usual commands, such as go to a directory (cd), list a directory (ls), install new packages (apt), autocomplete (tab), command history (↑, ↓). The user experience when working in the console turned out to be quite tolerable, and even if you connect a Bluetooth keyboard, you can’t distinguish it from the desktop at all.

This is what the home directory looks like.
This is what the home directory looks like.
And so is the file system.
And so is the file system.

What useful things can be done here? Since we undertook to make a weather station, we need some kind of development environment, simple and convenient for working with physical devices. I suggest installing the Node-RED environment.

What is Node-RED

Node-RED is an open and free platform for integrating systems with each other, a kind of universal glue for quickly connecting software and hardware components. It is often used in the study of the Internet of things. The platform encourages cooperation: over 4,000 ready-made nodes and more than 2,000 ready-made user programs are posted on the official website.

Programs in Node-RED are created in a visual language, and the finished program looks like a flowchart. The environment teaches you to think in the event-driven programming paradigm, it makes extensive use of multithreading. But such an organization of the code has its own characteristics: it is easy to get confused in the visual scheme as soon as it grows even a little.

Node-RED is so named because it is basically written in the NodeJS server programming language. Custom nodes are written in JavaScript. In this lesson, we will use a completely ready-made code, that is, you do not have to program.

Install Node-RED

The Node-RED website has management for installation under Termux, but in short it all comes down to a few lines:

apt update
apt upgrade
apt install coreutils nano nodejs
npm i -g --unsafe-perm node-red
node-red

Server started:

The Node-RED server will be available on a smartphone at http://127.0.0.1:1880. Everything is quite clear: the address is localhost and port 1880. You can open the environment directly in the browser on your smartphone, but it is inconvenient to edit there.

The environment is clearly not designed for programming through the smartphone screen
The environment is clearly not designed for programming through the smartphone screen

I prefer to work on code from my laptop. To do this, the laptop must be on the same WiFi network. Exit Node-RED using the standard Linux command to interrupt the process Ctrl+C. We find out the IP address of the smartphone through the standard ifconfig command. And again we start the node-red server. My smartphone turned out to have the address 192.168.0.14, so in the browser I went to the address http://192.168.0.14:1880/ and saw the convenient large window of the Node-RED environment.

Quite another matter!
Quite another matter!

Overview of the finished project – weather station

I decided to take for educational purposes a ready-made project of a weather station, which found on the official site of Node-RED. This station shows the current weather, future forecast, humidity, cloudiness, and has a nice interface.

The author originally made it for the Raspberry Pi, but it turns out that the same program can be run on a smartphone, because the development platform is the same – Node-RED.

This is what the Raspberry Pi project looked like.
This is what the Raspberry Pi project looked like.
This is what it looks like on my smartphone
This is what it looks like on my smartphone

I simplified this project a little: I removed third-party icons so that they did not have to be installed separately, and added my own parameters that I lacked here.

What steps do you need to take to make exactly the same station on your smartphone? Very simple! I need to import my code into Node-RED. Download from above link flow.json file and click Import.

It is convenient that the entire project in Node-RED is JSON text, and it can be imported even with a simple copy-paste
It is convenient that the entire project in Node-RED is JSON text, and it can be imported even with a simple copy-paste

After importing, the program itself will write what other packages it lacks. All of these additional packages are easy to install through the palette manager. For example, this is the dashboard package, which is responsible for the visual interface of your application.

After everything is installed and configured, I launch the project with the Deploy button and open the visual interface at http://192.168.0.14:1880/ui. To make everything look nice, enable the dark theme in the Dashboard settings.

So far, no weather indications are visible. Why? This is easy to understand using the debug node. I put it on and immediately after the error message I understand that an API key is needed.

The weather station takes data from a weather forecast website. Openweathermap. This site has an API for external requests, that is, a format for communicating with other programs. Knowing this format, you can get weather readings for a specific geographic point.

So that users do not overload the weather server, each user has a strictly limited number of requests, but we have more than enough of them, we will have infrequent requests. Each user has their own API key, so register on the Openweathermap website, it is free, and in your profile settings you will see a long line with this key. It is important that in the first few hours after registration the key may be inactive, then you need to wait.

With the key, we return to Node-RED. In the settings of the Current Weather node, I prescribe my parameters: geographic location (latitude and longitude) and my API key. I do the same in the settings of the 5 Day Forecast node.

After that everything will work!

Parsing the weather station code

Now that everything is up and running for me, let’s see how it all works.

The code is quite simple. The Current Weather node sends a request to the weather server. The response from this server is in the form of a long string in JSON format.

JavaScript converts JSON text into a program object containing data fields. You can see the object and its contents if you put the Debug node immediately after the Current Weather node. Let’s look at the data. They are quite clear: there is also the wind speed, and its direction, and the current temperature with a textual description, and cloudiness, and much more.

Further, in the Prepare Dashboard Data node, the following happens: we take this data and process it as we need. For example, if it is not important for us to know the exact direction of the wind in degrees, we can convert this into a letter designation for the cardinal points – S, W, SW, and so on. Much of this data is too detailed for everyday use. And here we select the icons to display on the screen. The icons are taken from the ready-made basic set for the weather, which is already built into NodeRed. I added some indicators here (initially they were not displayed): cloudiness, humidity, pressure. You can add something at your discretion, everything is done completely by analogy.

And in the 5 Day Forecast node, exactly the same thing happens, only the request is no longer made for the current weather, but for the forecast – for the next hours or days.

What happened:

The first screen shows the sunrise/sunset time and you can update the data (manually send a request to the weather server)
The first screen shows the sunrise/sunset time and you can update the data (manually send a request to the weather server)
On the second screen, the current weather and the forecast for the next few hours are updated by a timer
On the second screen, the current weather and the forecast for the next few hours are updated by a timer

Conclusion

We took a look at the Termux program, and it turns out that Linux is closer than you think. For small tasks, it is not necessary to install an entire operating system on your computer, you can simply have Linux in your pocket on your smartphone. We also installed Node-RED and realized that some information processing tasks do not even require programming, but still require algorithmic thinking.

If the example shown is not enough for you and you want to learn how to make full-fledged applications for Android, then you will be helped by practical Java programming skills that can be obtained in «IT School Samsung».

Well, do your homework – add your own widgets to the dashboard so that you can display the indicators that are important to you. This could be the view count of your YouTube channel, or the dates of the next events on the calendar, or the train schedule. And you can further develop the weather station. Now that you understand the principle, you can connect your own homemade or purchased sensors to display readings not only from the Internet, but also directly from the environment, for example, to measure indoor temperature and humidity.

Bye everyone! We will continue this topic in the next tutorial with a completely different project.

Other articles in this series:

Tatyana Volkova

Leading Specialist of the Department for the Development of Technological Projects and Educational Programs at the Samsung Research Center in Russia

Similar Posts

Leave a Reply

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