This is only a preview of the March 2013 issue of Silicon Chip. You can view 20 of the 96 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "High Performance CLASSiC DAC; Pt.2":
Items relevant to "Infrasound Detector For Low Frequency Measurements":
Items relevant to "Automatic Points Controller For Model Railways":
Items relevant to "Capacitor Discharge Unit For Twin-Coil Points Motors":
Items relevant to "AAA-Cell LED Torch Driver":
|
Control relays over the
Internet with Arduino
Turning items on and off remotely via the internet has generally
been a complex and expensive task due to the hardware and
knowledge required. Not any more. Here we show how easy
and inexpensive it can be to control four or more relays over the
Internet using open-source Arduino-based hardware.
By JOHN BOXALL
Fig.2: the Freetronics
RELAY4 relay driver
module. It interfaces
directly to the EtherTen
module & uses FETs to
switch external relays.
Fig.1 (left): the Freetronics EtherTen module is Arduino
Uno-compatible and has an onboard Ethernet interface.
I
T’S NOT DIFFICULT to remotely control relays via the
internet. In this article, we’ll first look at the hardware
required, then explain the software and network requirements. After that, we’ll look at how commands are sent
over the internet using a web browser to control the relays.
If you are unfamiliar with the Arduino environment,
please refer to the article by Jonathan Oxer titled “Arduino – What’s All The Fuss About?” in the January 2012
issue of SILICON CHIP or visit the homepage at http://www.
arduino.cc
Arduino board and uses FETs to switch the relay coils. It
also includes reverse-connected power diodes to suppress
back-EMF pulses when the relays are turned off.
Connecting the relay module to the Arduino board is
very simple:
The hardware
Note that when using an Ethernet-enabled Arduino
board, digital pins 10-13 are used by the Ethernet interface
and can’t be used for other purposes. And in the case of the
EtherTen board, digital pin 4 is used for the microSD card.
Note that, for this project, we don’t use digital pin 8
either and we’ll explain the reason for this shortly.
The next consideration is the power supply for the relay
coils. Although there is a 5V power supply available from
the Arduino board, it’s unable to supply enough current to
The heart of the system is an Arduino Uno-style board
with an Ethernet shield. In this case, we have used the
Freetronics EtherTen board which conveniently combines
both into a single unit, thereby saving space and money
– see Fig.1.
The EtherTen board can control up to four relays via
a Freetronics RELAY4 4-channel relay control module –
see Fig.2. This module interfaces directly to the EtherTen
70 Silicon Chip
•
•
•
•
•
Input 1 to Arduino D2
Input 2 to Arduino D3
Input 3 to Arduino D5
Input 4 to Arduino D6
Logic GND to Arduino GND
siliconchip.com.au
drive most conventional relay coils. What’s more, it cannot be used to power relays with 12V (or higher) coils. In
either case, you will have to connect an external DC power
supply with the required ratings to the RELAY4 board’s
power terminals (bottom-left of Fig.3).
On the other hand, if you can keep the current draw
under 150mA and are using solid-state 5V relays (such
as Jaycar SY4092) with very low switching currents, the
on-board Arduino 5V supply will be enough.
With a 5V supply, the RELAY4 board itself draws around
13mA with all LEDs on. Add four relays drawing just
20mA each and you can comfortably power the lot from
the Arduino. In that case, connect the positive pin from
the RELAY4 power terminal to the Arduino +5V pin.
Testing
You can then test the connections to the RELAY4 board
with a simple Arduino sketch (software program) that
turns the outputs on and off – as indicated by the on-board
RELAY4 LEDs. Once your hardware has been connected,
enter and upload the following sketch using the Arduino
IDE (Integrated Development Environment):
void setup()
{
DDRD = B11111111; // set PORTD (digital 7~0) to outputs
}
void loop()
{
PORTD = B01101100; // set D2, D3, D5, D6 HIGH
delay(250);
PORTD = B00000000; // set D2, D3, D5, D6 LOW
delay(250);
}
At this stage, all four LEDs should be blinking on and off
at 2Hz. If not, check the wiring between the two boards,
including the GND line.
Software & network requirements
To control our Arduino over the Internet, we use a free
online service called “Teleduino”. It allows us to send
commands to an Arduino board (via the Internet) using
simple commands in the form of URLs similar to that used
to refer to a web page.
You can find out more at the Teleduino website at www.
teleduino.org
To identify an individual Arduino board to the Tele
duino service, we use a unique key in the form of a long
hexadecimal number. This key is issued by the Teleduino
service and is inserted into the Arduino sketch and also
into the commands issued to control the board.
To generate a key, simply go to https://www.teleduino.
org/tools/request-key and complete the required fields. A
short time later, your key will arrive via email – remember
to store this for later retrieval. It will be a long string of
characters, eg, 18F5F4749B058F952ABCDEF8534B2BBF.
The next step is to download and install the Teleduino
Arduino library into the IDE. The latest library can be
found at https://www.teleduino.org/downloads/ Extract
the library folder and copy it to the arduino-1.0.1/libraries
folder in your IDE installation. If your IDE is running, you
will need to restart it in order to use the library.
siliconchip.com.au
Fig.3: here’s how to connect the external hardware & wire
the power supply to the relay driver module.
You now have to prepare the Teleduino sketch for the
Arduino board. This sketch connects the Arduino to the
Teleduino server and also executes received commands
via the service. The sketch is included with the library,
so in the IDE select File –> Examples –> Teleduino328 –>
TeleduinoEthernetClientProxy.
Before uploading the sketch, the unique Teleduino key
needs to be inserted so the Arduino can identify itself to
the service. To do this, go to https://www.teleduino.org/
tools/arduino-sketch-key, enter your Teleduino key into
the field and click “Generate Code”. This will appear as an
array in Arduino format as shown, for example, in Fig.4.
That done, scan through the Arduino sketch currently
loaded in the IDE, locate the same byte variable (it should
start on line 36) and replace the array full of zeros with
your Teleduino key array – see Fig.5 (for example).
Once you have modified the sketch as above, upload it to
your Arduino as normal. You should also save the sketch
so you don’t need to repeat the key-insertion process in
the future. Note that if you are going to control multiple
Arduino boards, you will need multiple Teleduino keys.
Just remember to keep track of the key uploaded to each
board.
The next step is to test that the Arduino is connecting to
the Teleduino service by monitoring the connection status.
This can be done using a LED indicator connected via a
560Ω resistor between the Arduino’s D8 pin and GND, as
shown in Fig.6.
Once the indicator LED is in place, connect the Arduino
to your router via a network cable, apply power and watch
the LED. After a few moments, the LED will start blinking
to indicate the status of the connection to the Teleduino
service.
Fig.4: a Teleduino key array in Arduino sketch format.
Fig.5: the Teleduino key array after insertion into the
Arduino sketch.
March 2013 71
(3.3V)
(5V)
POWER
Fig.6: the status LED
is connected between
D8 and GND of the
Arduino module as
shown here.
(Vin)
SCK
(RST)
(AREF)
MISO
MOSI
SS
(D9)
(A1)
(A2)
(A3)
(A4)
ANALOG INPUTS
(A0)
(A5)
(D8)
(D7)
DIGITAL INPUTS/OUTPUTS
ARDUINO
ETHERNET
SHIELD
PWM
(D6)
PWM
(D5)
PWM
R1
560
SS
(D3)
PWM
A
LED1
(D2)
(D1)
Tx
(D0)
Rx
K
(GND)
At the time of writing, the following blink parameters
are used:
•
•
•
•
•
•
•
1 blink:
2 blinks:
3 blinks:
4 blinks:
5 blinks:
6 blinks:
10 blinks:
initialising
starting network connection
connecting to the Teleduino server
authentication successful
session already exists for supplied key
invalid or unauthorised key
connection dropped
It is normal for the LED to work its way up from one
to four blinks. After the connection and authentication is
successful, the LED will then blink very briefly every 10
seconds or so. This signifies that all is well.
If your LED shows five blinks, just reset the Arduino
board. If your LED shows six blinks, check your Teleduino
key in the control sketch and re-upload it to the Arduino.
And finally, if it blinks 10 times, the Internet connection
has dropped out.
Although the above procedure may seem somewhat
tedious, it is necessary to establish that everything is
working correctly. Once you’ve done that, the status LED
can be removed if desired but we suggest keeping it to
aid troubleshooting if you strike problems in the future.
Default relay settings
The final step in setting up the Teleduino service is to
decide what the default settings will be for each of the
relays. These are the settings that the relays revert to when
the Arduino board is turned on or reset, loses the Internet
connection or the network cable is removed.
You can set the defaults after your Arduino has connected to Teleduino by browsing to https://www.teleduino.
org/tools/manage-presets After entering your Teleduino
key, a large selection of options will be displayed. Scroll
down to the “Pins” section (see Fig.7) and change the
72 Silicon Chip
Fig.7: this section of the Teleduino presets page allows you
to set the defaults for the Arduino’s digital I/O pins.
mode of the Arduino pins you’re using to OUTPUT. Then,
depending on your needs, you can set the default relay
status with the value parameter.
Controlling the RELAY4 module
To control the RELAY4 module, first launch your web
browser (on a computer, smartphone or tablet). You can
then control the Arduino’s digital pins and thus the relays
by going to http://us01.proxy.teleduino.org/api/1.0/328.php
?k=999999&r=setDigitalOutput&pin=X&output=Y
There are three parameters you need to enter into this
page. The first is your Teleduino key – simply replace
999999 with your key. The next is the Arduino digital
pin to control – replace “X” with the pin number. And
finally, to turn the pin on or off, replace Y with a “0” for
off or a “1” for on.
For example, to turn on relay 1, you would use http://us01.
proxy.teleduino.org/api/1.0/328.php?k=999999&r=set
DigitalOutput&pin=2&output=1
To turn it off again, simply change the final “1” to “0”.
You may find it convenient to bookmark the various
URLs to make sending commands much easier. Furthermore, the use of URL-shortening services such as http://
bit.ly can reduce their length to more manageable sizes.
By checking the status LEDs on the RELAY4 board, you
can test the pin control without needing to wire up your
entire project at the start. Also, when you send a command,
the Teleduino server will return a message if the action
has been successful or not. If the command worked, an
output similar to the following will appear in the web page:
{"status":200,"message":"OK","response":{"result":1,"time":
0.2338559627533,"values":[]}}
Conversely, if it was not successful, you will see:
{"status":403,"message":"Key is offline or invalid.","response":[]}
This tells you that the Arduino has lost connection to
the Teleduino servers.
Conclusion
Once you have run through the set-up procedure, controlling the relays remotely is quite simple. If you need to
control more relays, either add another RELAY4 board or
check out the Freetronics RELAY8 board.
Finally, the Teleduino service allows web-based control
of much more than your Arduino’s digital outputs – refer
SC
to http://www.teleduino.org for more information.
siliconchip.com.au
|