Redis in-memory storage
Arduino Redis Library
The Arduino ESP8266 family of modules can send sensor data to a Redis server. To install the latest Redis version, use the Arduino Library Manager (Figure 3) and the DHT temperature sensor library.
Infrared flame sensors measure IR light generated by a flame, which makes them great for indoor projects; however, for outdoor projects, you will need to add some shielding against ambient light. The flame scanner has both an analog (A0) and a digital (D0) connection. The analog A0 pin returns the flame intensity, where 1024=no flame and 0=hot flame. The digital D0 connection returns a 1 for no flame and 0 for a flame.
For Arduino code (Listing 2), three values are published to a remote Redis server: roomtemp
, flamestatus
, and flameintensity
.
Listing 2
Redis_flame.ino
The redis-cli psubscribe
command can verify that the Arduino values are being received at the Redis server. For example, Listing 3 shows how to subscribe to the roomtemp
value.
Listing 3
Subscribing to a Value
Node-RED and Redis
Node-RED [4] is a visual programming environment that lets you create applications by dragging and dropping nodes onto a code flow window. Logic flows are then created by connecting the different nodes together.
Node-RED has been preinstalled on Raspbian Jesse since the November 2015 release, but Node-RED can be installed on Windows, macOS, and other Linux distros, as well, and you can find instructions online [5] for installing and running Node-RED on your specific system.
To install the Redis components, select the Manage palette option from the hamburger menu on the right (Figure 4), then search for redis and install node-red-contrib-redis. If you haven't already installed the dashboard library, then search for and install node-red-dashboard.
For my Node-RED flame scanner example, I'll look at subscribing to the three items (Figure 5) and show them on a web dashboard. The logic will require the following nodes: three redis in
, two chart
, and one text
.
To configure the redis in
node, double-click on the node to get the edit dialog (Figure 6), so you can configure the Redis server and define the topic to which to subscribe. For this project, I have three topics: roomtemp, flameintensity, and flamestatus. Finally, the method needs to be set to SUBSCRIBE.
Next, edit the chart
(Figure 7) and text
nodes. The key here is to create and define the dashboard group. Time spans, labels, colors, and sizing are also configured in this dialog box.
After the logic is complete, hit the Deploy button on the right side of the menubar to run the logic. The Node-RED dashboard user interface is accessed at http://<ipaddress>:1880/ui. Figure 8 shows my Node-RED dashboard with the flame scanner.
Weather Station Webcam
A Raspberry Pi can make an excellent weather station node. In addition to sensors (e.g., temperature and humidity), you can include webcams images (Figure 9).
For my test, I used low-cost USB webcams, but you can find some excellent Pi cameras, too. Images are generated with the fswebcam
command-line tool, which you can install with:
sudo apt-get install fswebcam
The Python script in Listing 4 calls fswebcam
and uploads an image to a Redis server. The webcam image is passed to the Redis server as a large binary variable using the SET method.
Listing 4
image_2_redis.py
For my weather station example, the Python code periodically reads the temperature and humidity sensors and then takes a picture. This data was SET into a Redis server as temp1
, humdity1
, and webcam1
.
I loaded the Redis server on the same hardware as my PHP/Apache web server, which allowed single-line testing with the redis-cli
tool. Once I was happy with the results, I moved the command-line syntax directly into my PHP script.
To pull the image data from Redis, I issue a redis-cli
command to GET raw webcam1
binary data and write it to a JPEG file. Listing 5 is the complete PHP example, and Figure 10 shows a sample weather station web page.
Listing 5
redis1.php
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.