Wayback Machine
17 captures
04 Apr 2017 - 12 Jul 2025
Apr DEC Jun
Previous capture 05 Next capture
2022 2023 2025
success
fail
About this capture
COLLECTED BY
Collection: Common Crawl
Web crawl data from Common Crawl.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20231205230558/https://dzone.com/articles/control-humidity-with-a-raspberry-pi-and-iot-devic
DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • How to Get Started With the Smart Parking
  • Building an IoT Security Camera With Raspberry Pi and Render
  • Controlling IoT Cameras With libptp
  • Introduction to IoT Sensors

Trending

  • Data Consistency in Distributed Systems: Transactional Outbox
  • How To Use KubeDB and Postgres Sidecar for Database Integrations in Kubernetes
  • Evolution of Software Architecture: From Monoliths to Microservices and Beyond
  • Quarkus 3: The Future of Java Microservices With Virtual Threads and Beyond
  1. DZone
  2. Data Engineering
  3. IoT
  4. Control Humidity With a Raspberry Pi and IoT Devices

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.

Gonzalo Ayuso user avatar by
Gonzalo Ayuso
·
Apr. 04, 17 · Tutorial
Like (5)
Save
Tweet
Share
11.6K Views

Join the DZone community and get the full member experience.

Join For Free

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.

raspberry pi IoT

Published at DZone with permission of Gonzalo Ayuso, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Get Started With the Smart Parking
  • Building an IoT Security Camera With Raspberry Pi and Render
  • Controlling IoT Cameras With libptp
  • Introduction to IoT Sensors

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: