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.

Figure 3: Installing Redis with the Arduino Library Manager.

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



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.

Figure 4: Installing Redis on Node-RED.

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.

Figure 5: Node-RED flame scanner logic.

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.

Figure 6: Configuring the Redis server in the redis in dialog.

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.

Figure 7: Creating and defining the dashboard in the chart dialog.

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.

Figure 8: Node-RED flame scanner dashboard.

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).

Figure 9: Raspberry Pi/Redis weather station.

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


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



Figure 10: PHP IoT weather station page.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Logstash

    When something goes wrong on a system, the logfile is the first place to look for troubleshooting clues. Logstash, a log server with built-in analysis tools, consolidates logs from many servers and even makes the data searchable.

  • Monitoring Old Devices

    Create monitoring dashboards with SSH, command-line tools, and Node-RED.

  • IoT with RabbitMQ

    Connect multiple protocols and servers together on your IoT projects.

  • Node-RED on Android

    We show you how to control devices connected to Rasp Pi GPIO pins with text messages from an Android phone.

  • WiFi Thermo-Hygrometer

    A WiFi sensor monitors indoor humidity and temperature and a Node-RED dashboard reports the results, helping you to maintain a pleasant environment.

comments powered by Disqus

Direct Download

Read full article as PDF:

Price $2.95