Feature

Getting Salty

Tech Keeps Salt Lamps from Melting Away!

David Such

Issue 5, November 2017

Usually when a lamp is melting it’s from too much heat, not high humidity. But salt lamps are a different beast.

Himalayan salt lamps are popular because they look great, and are reported to provide health benefits by cleansing the air. Regardless of what you think about that, the fact is, when the humidity rises, it can cause your salt lamp to melt. Fortunately for David, he came up with a solution.

This is one of those problems you’d never consider until it happened. When did you first realise your salt lamp was “melting” in the humidity?

When a large amount of water streamed off the corner table and formed a pool on the floor! It happened after we had a solid week of rain and it was the catalyst for me to start investigating the phenomena. Salt lamps allegedly remove dust, pollen, cigarette smoke, and other contaminants from the air. I have no idea how effective they are at this but they look nice! Salt is very hygroscopic - that is, it absorbs water - and this is the basis of the claimed health benefits. The salt absorbs the water in the air along with any foreign particles that water may contain. The water then evaporates when the lamp is switched on, leaving the contaminants behind entrapped in the salt.

The “melting” occurs when there is too much water in the air. Salt is so hygroscopic that it readily dissolves in the water it absorbs; this property is called “deliquescence”. It is, of course, a problem if your expensive Himalayan salt lamp dissolves into a puddle of salty water, especially when it is connected to 240VAC. In our house this melting process starts at relative humidities above 70%.

The solution is to turn your lamp on if humidity gets above 70%. This seemed like a good excuse to introduce the start of a home automation hub and learn about Node-RED. For humidity measurement and control, I used a Raspberry Pi (RPi) with a Sense HAT mounted via the GPIO header. This is definitely overkill as this is the same combination that was sent up to the International Space Station to allow school kids to create experiments! Nevertheless, this is what I had on hand. The Sense HAT has an 8x8 RGB LED matrix, a five-button joystick, and includes the following sensors:

  • Gyroscope
  • Accelerometer
  • Magnetometer
  • Temperature
  • Barometric pressure
  • Humidity
A Raspberry Pi and Sense HAT.
A Raspberry Pi and Sense HAT.

For this project I am only using the humidity sensor, display and joystick button.

Using the Sense HAT Python library, turning a lamp on and off based on humidity and time (lamp goes on at 5pm and off at 10pm), is quite simple. I also wanted to be able to manually control the lamp via the node-RED dashboard and using the joystick push button; this part was a bit trickier.

It’s convenient that you can simply switch the lamp on to keep the salt dry - great idea! Would it still work if the lamp was LED illuminated and didn’t provide much heat?

Yes, it turns out that I wasn’t the first person to have this problem. A quick search of the internet suggested that using a timer to switch on the lamp would stop it from melting. Of course that was much too simple for me!

You might expect that using LED illumination could be a problem for salt lamp longevity, unless you lived in a desert, and you would be right; but perhaps for the wrong reason. The salt lamp uses a 15W pilot incandescent globe (E14 small Edison screw). I don’t have a data sheet for this globe, but typically the energy consumed by an incandescent bulb is converted to around 12% heat, 83% infrared radiation (IR), and only 5% visible light. In contrast, a typical LED produces 15% visible light and 85% heat. So most of the heating effect from the globe is coming from IR.

If you had LED illumination that wasn’t generating much heat, then it would be a problem. You need enough heat to either evaporate the water in the salt before it reaches the deliquescence point, or reduce the relative humidity (RH%) of the air near the lamp. Deliquescence occurs when the vapour pressure of the solution that is formed, is less than the partial pressure of water vapour in the air. All soluble salts will deliquesce if the air is sufficiently humid.

LEDs do generate heat but normally you don’t notice it because the power is so low. For high-power LEDs, thermal management is important. Without good heat sinking, the internal temperature of the LED rises, and this causes the LED characteristics to change. As the junction temperature of an LED is increased, both the forward voltage and the lumen output decreases. The output wavelength increases with rising junction temperature, but also decreases the lifetime of the LED.

So the problem is, you need the LED to generate enough heat to stop the lamp melting but this much heat would also effect the LED, reducing light output, generating a redder colour, and causing the LED to fail earlier. In normal LED lighting applications, a heatsink is used to dissipate this heat and keep the internal LED temperature down, but that wouldn’t be easy for the lamp since you need the light and heat in the same spot. This is one place where an incandescent lamp is a better solution.

Sounds like a little sacrifice in efficiency is worth it here. Did the project take much trial and error to get your settings right?

It wasn’t too bad. As it was damp at the time, it didn’t take long to work out that turning on the lamp at a RH of 70% was the sweet spot. 80% was too late as the lamp had already started “sweating” at that point. The most difficult part was learning how to use Node-RED and interfacing this to the RPi.

It seems little trial and error was forgiving, as long as the lamp didn’t melt first! What are you using for the 240V switching control?

I like to keep my low voltage electronics away from 240VAC as much as possible, so I used a PowerSwitch Tail kit (240VAC version) to switch the lamp. The PowerSwitch includes a single pole relay capable of switching 240V at 20A and includes 5300VRMS isolation with no exposed AC wiring. You can switch it on directly from an Arduino or Raspberry Pi digital output (i.e., 3.3V for the Raspberry Pi). The kit includes resistors to select the actuation voltage from 3 to 24VDC, at 3 to 10mA. It is a 2 wire connection from the Raspberry Pi to the PowerSwitch, there is no need to bring across DC power. The PowerSwitch has a LED indicator which shows when power is being applied to the relay. I sacrificed an extension lead to get the 240V plug and socket.

A PowerSwitch Tail Kit
A PowerSwitch Tail Kit, used for safer mains switching.

What functions does the matrix display provide?

As the RPi is running headless, I wanted to be able to check at a glance that it hadn’t locked up. To that end, I have the Raspberry Pi running a Python version of Conway’s Game of Life. This program is running in a separate thread to the lamp controller. I’m using 7x8 of the RGB matrix to display the various cell generations, and resetting if the cells die out or get stuck in a static pattern. This generates a display which looks like the blinking lights from old SF movies. The last row of the RGB display shows a pulsing green when the lamp has been turned on automatically, either due to humidity or the timer. Finally, the last LED in this row is switched on to blue when the lamp is in manual mode, and has been switched on or off by the joystick switch on the Sense HAT, or via the node-RED dashboard.

To be fair, the RPi has been running for months without a hitch. I’ve been very impressed with how stable and reliable the Raspbian/Raspberry Pi combination has been. I’m not used to running on Linux, hence my caution.

Were there any unexpected challenges creating this project?

There is one thing that caused me a fair amount of frustration and was tricky to fix.

It makes sense to create a Bash script, which is read on start up to run the Python programs required. That way if you lose power, the controller will restart automatically when power is restored. If you don’t do this then you have to work out the IP address of the RPi, SSH in, and then run the programs manually.

Make sure that you add a single trailing ampersand at the end of the command. For example:

./lamp_controller.py &

The trailing ampersand directs the shell to run the command in the background; that is, it is run in a separate sub-shell, asynchronously. The shell will immediately return a status of “0” for true and continue as normal, either processing further commands in the script, or returning the focus to the Linux terminal.

I forgot this when I tried it for the first time. If you don’t include the trailing ampersand and run a Python program which doesn’t end (like mine), then the script will not run past this point and you will be unable to access the terminal. It is then quite difficult to fix, since you can’t get to the terminal to edit the Bash file. Turning the RPi on and off doesn’t help either, since this script runs on start up. In the end I had to remove the SD card from the RPi and edit the Bash file on my laptop.

You have implemented Node-RED for remote monitoring/dashboarding. Had you worked with it previously or was this your first go?

This was a first for me. I was looking for a web-based dashboard and came across Node-RED, which is much more than that. Node-RED is a programming tool for wiring together hardware devices, APIs and online services. It was developed as a visual programming tool for the Internet of Things (IoT). It also allows you to produce and publish a funky web based dashboard with one click.

Node-RED includes a browser-based editor that makes it easy to connect flows using the selection of nodes in the side menu. Flows can be then be deployed to run in a single-click. JavaScript functions can be created within the editor to customise the messages passed between nodes. A built-in library allows you to save useful functions, templates or flows for reuse.

The light-weight runtime is built on Node.js, taking advantage of its event-driven, non-blocking model. This makes it ideal to run on low-cost hardware such as the RPi.

Nodes can be anything from a timer to trigger events to a RPi GPIO output used to turn on a LED (or salt lamp in my example). With over 225,000 modules in Node’s package repository, it is easy to extend the range of nodes to add new capabilities. There are packages available for the RPi and the Sense Hat, which made things very easy. The flows created in Node-RED are stored using JSON.

Node-RED was originally developed by IBM and in 2016, they contributed Node-RED as an open source JS Foundation project.

If you are running Raspbian Jessie on your RPi then you should already have node-RED installed. For those that want to give it a go, before starting the Node-RED server it is worth installing a few packages that you will need. Type the following at the CLI:

sudo apt-get update
sudo apt-get install npm
cd $HOME/.node-red
npm install node-red-dashboard
npm install node-red-node-snmp
npm install node-red-contrib-os

Node-RED is started by running the following command in the terminal:

node-red-start

Once started, you use a browser (either on the RPi or remotely) to build your applications and configure your dashboard; I used my Macbook Air. To do this point your browser at the IP address of your RPi:1880. If you do it on your Pi, the URL would be 127.0.0.1:1880 or localhost:1880. The associated dashboard URL is :1880/ui. So for example, my RPi dashboard is at http://192.168.0.18:1880/ui

Most of the RPi information charted in the dashboard shown is from the node-red-contrib-os package. For example information on the SD card is from the Drives node, which is the node you use to query the hard drives. Values for size, used and available are expressed in KB (1024 bytes). Value for capacity is a number between 0 and 1. Capacity*100 is also known as percentage used.

Some of the flows are shown below. The first step is to drag across a timer, which you can use to poll the Drive node. Our timer sends a timestamp every minute.

flows

Connect the timer to a Drive node and it will start pumping out messages with the size, used, available and capacity values for every drive on your target system. You can use a Debug node to see messages being sent out by any node. This is very useful in debugging your flows. On the Raspberry Pi there will be a few different file systems on your SD card so you have to be specific about which area you want information about.

You can add a Function node to include custom JavaScript to process the messages passed between the nodes. The JavaScript used to extract the various Drive information that I use is shown below. The topic variable is used as the name for charts with multiple inputs.

var msg1,msg2,msg3;
if (msg.payload.filesystem === ‘/dev/root’) {
  msg1 = { payload: msg.payload.used };
  msg2 = { payload: msg.payload.available };
  msg3 = { payload: msg.payload.capacity * 100 };
  msg1.topic = "used"
  msg2.topic = "available"
  msg3.topic = "capacity"
}
return [ msg1, msg2, msg3 ];
chart

To display CPU temperature we use a different technique. On the Raspberry Pi you can display the current CPU temperature by typing:

/opt/vc/bin/vcgencmd measure_temp

You can use an Exec node to run OS commands. So connect the same timer node to an Exec node and input the command above. You then have to do a bit of processing to extract the temperature as a number. Use another function node with the following code.

msg.payload = msg.payload.replace("temp=","").replace("’Cn","");
return msg;
sensor data

There are also nodes available for the Sense Hat. You need to use functions similar to those above to extract the various sensor data values.

sense hat

I wanted to be able to turn the lamp on and off manually from the Node-RED dashboard. Manual control of the RPi GPIO is fairly straight forward. The one trick is that the Switch node outputs true/false, and the Raspberry Pi GPIO out node expects a 1/0 input. So include another Function node to mediate. The relevant code is:

msg.payload = msg.payload ? 1 : 0;
return msg;

Thanks for the detailed run-through. Is there anything you wish for in Node-RED that’s not currently provided?

I am also using Node-RED to display an almost real-time chart of my net worth. Getting share price information is fairly straight forward, using the yahoo Finance API but scrapping bank account balances is a lot harder. There is a module available for CBA’s NetBank, but I wasn’t able to find one for the other banks that I use. I realise this may be a bit of an edge case for most users! The code to get delayed share price information is shown below. The share symbols are passed in from the previous node.

var yahooFinance = global.get(‘yahooFinance’);
var msg1;
var SYMBOLS = msg.payload;
var FIELDS = [‘s’, ‘l1’]
node.status({fill:"red",shape:"ring",text:
"disconnected"});
yahooFinance.snapshot({
  fields: FIELDS,
  symbols: SYMBOLS
}).then(function (snapshot) {
  node.status({fill:"green",shape:"dot",text:
"connected"});
  msg1 = {payload : snapshot, topic : "share_prices" }
  node.send(msg1);
  console.log(util.format(‘=== %s ===’, SYMBOL).cyan);
  console.log(JSON.stringify(snapshot, null, 2));
});
return;

A real-time(ish) dashboard of net worth - that’s a novel idea! If you tackled this project now with the knowledge you have, would you have used different hardware or made any changes to your approach?

If I was only looking to control the lamp, then an Arduino-based solution would probably make more sense; however, this is the first step in a much wider home automation project. I have mounted an iPad on the living room wall to act as my central control centre. I will then be able to extend the Node-RED dashboard to monitor and control these projects - you can add as many dashboard pages as you like.

insitu

Arduino could certainly tackle much of this core functionality, we agree. Anything else you would like us to know about this project?

For people interested in the IoT, I would really recommend that they have a look at Node-RED; particularly if they want to combine their local data with something on the web. The only problem is that it helps if you know JavaScript - another language to add to the pile to learn.

I’m sure many readers have solid JavaScript knowledge. What are you working on now?

I am now looking at the remote control and monitoring of our two garage doors. They are some way from the house and outside WiFi range, so I am experimenting with the 433MHz wireless transmitters. Of course, it’s really just an excuse to buy more kit!

If you’re like us, no excuse is really needed is it?! That sounds awesome, and we look forward to seeing what you come up with!