Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Control Humidity With a Raspberry Pi and IoT Devices

DZone's Guide to

Control Humidity With a Raspberry Pi and IoT Devices

In this post we take a look at how you can control the humidity in a room using a Raspberry Pi, a switch, and a sensor with a dash of JavaScript and Python.

· IoT Zone ·
Free Resource

Nowadays I’m involved with Arduino and IoT, so I wanted to do something with affordable Arduino stuff. I’ve got a Wemo switch and a BeeWi temperature/humidity sensor, which I’ve used in previous projects. Today I want to control the humidity level in a room. The idea is to switch on/off a dehumidifier (plugged into the Wemo switch) depending on the humidity (from the BeeWi sensor). Let’s start.

I’ve got one script (node) that reads humidity from the sensor (via BTLE):

#!/usr/bin/env node
noble = require('noble');

var status = false;
var address = process.argv[2];

if (!address) {
    console.log('Usage "./reader.py <sensor mac address>"');
    process.exit();
}

function hexToInt(hex) {
    var num, maxVal;
    if (hex.length % 2 !== 0) {
        hex = "0" + hex;
    }
    num = parseInt(hex, 16);
    maxVal = Math.pow(2, hex.length / 2 * 8);
    if (num > maxVal / 2 - 1) {
        num = num - maxVal;
    }

    return num;
}

noble.on('stateChange', function(state) {
    status = (state === 'poweredOn');
});

noble.on('discover', function(peripheral) {
    if (peripheral.address == address) {
        var data = peripheral.advertisement.manufacturerData.toString('hex');
        console.log(Math.min(100,parseInt(data.substr(14, 2),16)));
        noble.stopScanning();
        process.exit();
    }
});

noble.on('scanStop', function() {
    noble.stopScanning();
});

setTimeout(function() {
    noble.stopScanning();
    noble.startScanning();
}, 3000);


Now I’ve got another script to control the switch. It's a Python script using the ouimeaux library:

#!/usr/bin/env python
from ouimeaux.environment import Environment
from subprocess import check_output
import sys
import os

threshold = 3

def action(switch):
    humidity = int(check_output(["%s/reader.js" % os.path.dirname(sys.argv[0]), sensorMac]))
    if "Switch1" == switch.name:
        botton = expected - threshold
        isOn = False if switch.get_state() == 0 else True
        log = ""

        if isOn and humidity < botton:
            switch.basicevent.SetBinaryState(BinaryState=0)
            log = "humidity < %s Switch to OFF" % botton
        elif not isOn and humidity > expected:
            switch.basicevent.SetBinaryState(BinaryState=1)
            log = "humidity > %s Switch to ON" % expected

        print "Humidity: %s Switch is OK (%s) %s" % (humidity, 'On' if isOn else 'Off', log)

if __name__ == '__main__':
    try:
        sensorMac = sys.argv[1]
        mySwitch = sys.argv[2]
        expected = int(sys.argv[3])
    except:
        print 'Usage "./dehumidifier.py <sensorMac> <switch name> <expected humidity>"'
        sys.exit()

    env = Environment(action)
    env.start()
    env.discover(seconds=3)


And that’s all. Now I only need to configure my Raspberry Pi’s crontab and run the script each minute:

*/1 * * * *     /mnt/media/projects/hum/dehumidifier.py ff:ff:ff:ff:ff:ff Switch1 50


If you're interested, the project is available on my GitHub account.

Topics:
raspberry pi ,iot ,home automation ,tutorial ,sensor data

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}