Reader small image

You're reading from  Building Wireless Sensor Networks Using Arduino

Product typeBook
Published inOct 2015
Publisher
ISBN-139781784395582
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
Matthijs Kooijman
Matthijs Kooijman
author image
Matthijs Kooijman

Matthijs Kooijman is an independent embedded software developer who is firmly connected with the maker movement through a local fab lab and his work on the Arduino project. Since his youth, Matthijs has been interested in making things; for example, he built his first television remote control before the age of 10 (using a piece of rope to pull on the volume slider, not a solution that he would choose today). Matthijs has a firm belief in the merits of open source software and enjoys contributing to the software that he uses—both by coding and helping out other users. His work experience is broad—ranging from Web development to Linux driver hacking, from tech support to various forms of wireless networking, but almost always related to open source software in some way.
Read more about Matthijs Kooijman

Right arrow

Chapter 4. Controlling the World

By now, you have seen your Arduinos read their sensors, collect that data in the coordinator, and even store it in the cloud or on your computer. In this chapter, you will make your network take control of part of your surroundings.

The goal of this chapter is turn your network into a thermostat that can control the heating or cooling of (part of) your house. For this, there are two elements that need to be added:

  • Some way to configure the intended temperature (setpoint)

  • Some way to turn the heating and cooling on or off

The setpoint will be controlled through the Beebotte dashboard, allowing you to control your house's temperature from any browser (including one on a smartphone). If you prefer to build an actual thermostat device using a display and a knob, that is also possible (you can even combine both), but this is beyond the scope of this book.

Controlling the heating or cooling is a bit more tricky and depends on what kind of system you have. Some options...

Controlling your heating and/or cooling system


To make a working thermostat, you will need some way to let it control the temperature in your house. One way to approach this is to let an Arduino replace your existing thermostat and have it control the HVAC (heating, ventilation and/or air conditioning) system in your house in the same way the thermostat did. Alternatively, you could have your network control mains power to a device, such as an electrical heater or standalone air conditioning unit, and control the room temperature through that.

Ideally, you could toggle heating or cooling for each room in your house separately. Combined with a temperature sensor in each room, you would have detailed control over the temperature in every room of your house.

For simplicity, these examples assume that you will control the heating using a single on/off switch for the entire house. If you (instead or additionally) want to control cooling, the examples will be easy to modify. If you have a way to...

Control systems


A thermostat, as well as the system you are about to implement is an example of a control system. Simply put, a control system is a system that, based on a number of inputs, continuously decides on a value for one or more outputs. Most control systems are closed loop control systems (also known as feedback control systems), meaning that their outputs also influence the inputs, providing the system with feedback about the decisions it makes.

In this case, the inputs are the current temperatures and the setpoint temperature, and the single output is whether the heating system should be on or off. The feedback happens when the temperature changes due to the heating being on or off.

A lot of different sorts of control systems have been designed and documented but, like most on/off thermostats, this example will use a simple hysteresis controller. Consider the following approach: If any rooms have a temperature below the setpoint, turn the heating on; if all rooms are above the...

Adding setpoint control


As a first step to turn your coordinator into a thermostat, you will need some way to control the setpoints. In this example, a single setpoint for the entire house is used, but it should be easy to add one per room and adapt the code to check each room temperature against the appropriate setpoint.

To store the setpoint, create a new channel in Beebotte called House. Inside, add a Setpoint resource, using the temperature type from the dropdown.

For this resource, you should enable the Send on Subscribe (SoS) option. Normally, when you subscribe to a resource, you will only receive new values published to it. For the setpoint, this will mean that the coordinator will get notified whenever the setpoint is changed. However, when the coordinator Arduino is first turned on or resets, it will not know about the current setpoint until it changes. The SoS option solves this by automatically sending the current value of the setpoint whenever the coordinator subscribes to it...

Controlling a relay


Controlling a relay module or shield is fairly simple: Connect it to an Arduino using 5V, GND and a single I/O pin. Then write HIGH to that I/O pin to turn the relay on, write LOW to switch it off again. Using the PowerSwitch Tail is identical, except you only connect GND and an I/O pin; the Tail has its own power supply built in.

In these examples, we will assume the relay is controlled through pin 4 (as used by the SparkFun shield) and use a constant to store the pin number:

const uint8_t RELAY_PIN = 4;

Of course, the pin must be configured as an output in the setup() function:

  pinMode(RELAY_PIN, OUTPUT);

Now, if you can attach the relay directly to the coordinator, the switchHeating() function will be rather trivial:

void switchHeating(bool state) {
  digitalWrite(RELAY_PIN, state);
  heating_state = state;
  publish(F("House/Heating"), state);
}

This version of the sketch is available in the code bundle as Coordinator_Relay.ino.

Things become a little more complicated if...

Controlling off-the-shelf ZigBee devices


Instead of attaching things to an Arduino to expand the input and output capabilities of your network, you could also consider off-the-shelf ZigBee devices. In particular, devices that implement the ZigBee Home Automation specification (covered in more detail next) can typically add useful things, such as on/off control of power outlets, (wall) switches or dials, displays, various sensors, and so on.

In this section, you will see how to remotely control a power outlet that supports the ZigBee Home Automation standard, making it turn on and off. Since the protocols and specifications needed for this are complex and big, this will only cover the most basic use case. Implementing the complete ZigBee Home Automation specification using an Arduino and XBee module could probably fill a book on its own, but this section should give you enough info to allow basic control of an on/off device and give you a starting point to implement other things, as well.

To...

Summary


In this chapter, you have put control over (a small part of) the world in the hands of your Arduinos. Using your self-built wireless relay, or an off-the-shelf wireless power socket, you have enabled your network to switch things on and off. To keep ultimate control in your own hands, you have added control over the intended temperature to your Beebotte dashboard as well.

These examples should provide a good basis for more complex projects, where the network has more fine-grained control over things than just switching them on or off. Anything you can let your Arduino control (and there are plenty of good tutorials and libraries out there), can be done wirelessly using the same approach used here. Similarly, you could be using physical buttons, knobs, and displays instead of, or in addition to, the Beebotte interface for providing setpoint control, or any other kind of control for that matter.

In the next chapter, you will be looking at using XBee modules without an Arduino or other...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Building Wireless Sensor Networks Using Arduino
Published in: Oct 2015Publisher: ISBN-13: 9781784395582
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Matthijs Kooijman

Matthijs Kooijman is an independent embedded software developer who is firmly connected with the maker movement through a local fab lab and his work on the Arduino project. Since his youth, Matthijs has been interested in making things; for example, he built his first television remote control before the age of 10 (using a piece of rope to pull on the volume slider, not a solution that he would choose today). Matthijs has a firm belief in the merits of open source software and enjoys contributing to the software that he uses—both by coding and helping out other users. His work experience is broad—ranging from Web development to Linux driver hacking, from tech support to various forms of wireless networking, but almost always related to open source software in some way.
Read more about Matthijs Kooijman