Dec 18, 2017

CONTROLLING ANY DEVICE USING A RASPBERRY PI
AND A RELAY MODULE

alt=

You are going to learn how you can control a relay that is attached to your Raspberry Pi, from any device like your computer or tablet. To add scripts to OctoPrint and turn on and off LED lights, of course this LED could be replaced by anything like a lamp, or the motor of your electric curtains.

HARDWARE & SOFTWARE REQUIREMENTS

For this tutorial, you will need a Raspberry Pi board, the version of the board or the model (A or B) doesn’t really matter, but to make sure you are using the correct pin mapping. You will have to connect it to your local network. For the components, you also will need a Two Channels 5V Relay Module, and LED light. On the software side, you will need a Raspberry Pi configured with the Raspbian Linux distribution installed on it. There are so many tutorials on the web that will guide you through the installation of Raspbian on your Pi. Download and unzip the latest OctoPi Image. There is quite a lot of hardware to connect for this project, so pay attention. First, What are relays? A relay is an electromagnetic switch operated by a relatively small electric current that can turn on or off a much larger electric current. The heart of a relay is an electromagnet (a coil of wire that becomes a temporary magnet when electricity flows through it). You can think of a relay as a kind of electric lever: switch it on with a tiny current and it switches on ("leverages") another appliance using a much bigger current. Activating the low-power part by applying 5V on the coil will activate the switch and change the state of the relay. To monitor in which state the relay is, I used one LED on one part of the switch. Of course, the LED can be replaced by any device you want to switch on or off, for example a lamp. Also note that I am using a 5v board, as is the case with most of the relay boards. However, there are 3v boards out there, if that is what you have, just make sure you change the power accordingly to the 3v pin. It is not a very popular option because the 3.3v on the pi is limited to 50ma, so it can take 1 maybe 2 relays of the 3v types, but as many 5v relays as the board can take.

Two Channels 5V Relay Module

Each relay has three connection points. The relay is designed to connect, and switch a circuit between these connection points. Most circuits will use two of these points only: either NO & COM or NC & COM. In most applications, you will only have two wires into each relay (unless you're switching between two common circuits). Each relay is electrically disconnected from the next, so you can wire in multiple discreet circuits into to a single PiOT relay board.

NO - Normally Open

In a normally open circuit, there is no contact between NO & COM. Therefore the circuit you are switching will be "Open", and the connected circuit will be turned off. When you trigger, the relay will connect the NO and COM points, closing the circuit and turning it on.

NC - Normally Closed

In a normally closed circuit, there is constant contact between NC & COM. Therefore the circuit you are switching is "Closed", and the connected circuit will be constantly turned on. when you trigger, the relay disconnects the NC and COM points, opening the circuit and turning it off.

COM - Common Point

The common point is your Ground, or common point - the switching point of your circuit.

We will be using pins 1 (5v), 6 (ground), and 18. They are not side by side, and so I need to rearrange the cable a bit so it has the correct connectors. Also, note that 18 are the "header numbers" (physical location), but the wiringpi software that controls them calls them pins 0 and 1.

Raspberry Pi 2 GPIO Header
Two Channels 5V Relay
Raspberry Pi 2

Wire 2-Channel 5V Relay Module to the Board (Basics)

Connect the INx pin to the Raspberry Pi pin

1 Connect the 5V supply to the Vcc and GND pins on the relay board.
2 Connect the INx pin to the Raspberry Pi pin that you want to use to activate the relay.
3
- The 8 relay board have diodes beside the relays (D1, D2, D3...) that tell you which IN pin corresponds to which relay. The 2 relay board, the IN1 is the top relay, the IN2 is the bottom relay (top and bottom based on the GND, INx, and Vcc pins being on the left). The 4 relay board is clearly labeled.
4 The relays are activated when the corresponding INx pin is connected to GND.

Installation

First turn off your pi, connect everything in place, and then turn the power back on.

Login to RetroPie

ssh pi@ your pi's IP address

Password "raspberry"

I am using wiringpi. Many builds come with wiringpi installed. To test this, use the commands

gpio -v

gpio readall

If it did not succeed, then try installing wiring pi. More information is provided on the wiringpi website download-and-install.

Testing

We can now test the relay by sending the commands to turn them on\off.

Terminal screen shot

First use gpio readall to see everything mapped out (useful to know, and good for diagnostics... more on that lower) Second, assuming you used the same pins as me (otherwise look at your schematic and figure it out), use the commands gpio read 0 and gpio read 1 (this will read the states of pins 0 and 1). With my module, 0 is on, 1 is off. Third, we want to change the pin mode. We want an out pin, not an in pin. The commands are gpio mode 0 out and gpio mode 1 out. Lastly we want to actually use the pin, so we will send gpio write 1 0 (the first number is the pin number, the second number is the state). If it turns on and off as expected, then you are done. If like me, you have some issues, the next step is to read back the pin you just wrote to. It turns out I have problems on my pin 1, and only my pin 0 worked properly. As it turns out, it is because my pin was still set to IN rather than OUT. That's it, now you can use your devices via SSH terminals.

Adding the On/off Command to Octopi

OCTOPI

  • First edit the file ~/.octoprint/config.yam I use nano so it goes
  • sudo nano ~/.octoprint/config.yaml
  • Then go down to actions and add the folowing (obviously naming it as you want)
  • - action: LED on
  • command: gpio export >>Your GPIO pin number in hire<< out
  • name: Turn on LEDs
  • - action: LED off
  • command: gpio export >>Your GPIO pin number in hire<< in
  • name: Turn off LEDs
  • SSH Test GPIO SSH Test GPIO
    SSH Test GPIO
  • Action LED On Action LED On
    Action LED On
  • GPIO Test Light GPIO Test Light
    GPIO Test Light

Access The Interface From Anywhere

You can now control the relay from anywhere on your network, that’s nice but it would be much better if you could do the same from outside of your home, right? You can port forward on your router to allow the the use of octopi anywhere, just make sure you enable password protection on the pi so no one but you can mess with it.

Depending on what kind of router you have, you setup port forward using one port and then forward it to the IP of your RPi. Then when you connect through the port from the outside to your outside IP, you will be passed onto the IP of your Raspberry to use octopi.

Enjoy!