Smart switch: Orange PI one (arm Linux) control relays

Source: Internet
Author: User

As we all know, the relay is a small current to control the operation of a large current "automatic switch", in our lives, work everywhere. Now the "smart home" concept, there are many functional modules is actually "intelligent switch", remote switch, timing switch, condition trigger switch and so on.

The Orange Pi one (arm Linux) control relay, described below, makes it easy to implement an "intelligent switch".

Like Orange pi and Raspberry Pi, it is based on the development Board of ARM Linux, which is used as a common Linux computer. Of course, compared to ordinary Linux computers, it has its own advantages and disadvantages. Simply put, the disadvantage is that the hardware configuration is low, based on the ARM architecture, the development is more difficult than x86, the advantage is: cheap, low power, can be run all year round.

I bought a "USB control Relay Module" online, it integrates "USB to TTL serial" and "Relay" module, and in most modern operating system is free-drive, directly into the USB interface, the operating system can be identified and loaded as "serial device." In Windows XP, Windows 7 and other operating systems directly with the serial debugging software can send instructions to manipulate it. Of course, we have Raspberry Pi, orange, and so on.

The relay has no, NC, COM three terminals.

No is normally open (normal open), which is normally not energized state, is disconnected, after power on the electromagnetic coil under the action (suction) in a closed state. NC is normally closed (normal close), which is normally not in power state, is closed, power after the action of the electromagnetic coil (suction) is in a disconnected state. can choose to connect no+com or nc+com, note: No, NC can not be connected at the same time, otherwise a short circuit will occur.

I used an orange pi, downloaded the Armbian for Orange Pi One from the official website and burned it into the TF card, plugged into the development Board, and the power-up was ready to start.

Plug the USB relay module into the USB interface of the Development Board, then enter "Cd/dev" in the console and enter "LS" to see "ttyUSB0", which is the USB serial port loaded by the system.

Through the serial port to send "A0 A2" can control the relay suction, send "A0 A1" is disconnected.

Now try, input echo-n-E "\xa0\x01\x01\xa2" >/dev/ttyusb0, you can hear the USB relay module "Kata" sound, indicating that the relay has been sucked in, if the connection mode select "Nc+com", now equivalent to send "open" Instructions.

Re-enter Echo-n-E "\xa0\x01\x00\xa1" >/dev/ttyusb0, the USB relay module will again "Kata" sound, indicating that the switch status has changed, if the connection mode select "Nc+com", now equivalent to send "off" instructions.

Write the "on" and "off" instructions in a script file as follows:

ttyusb0open.sh

#!/bin/bash

Echo-n-E "\xa0\x01\x01\xa2" >/dev/ttyusb0

ttyusb0close.sh

#!/bin/bash

Echo-n-E "\xa0\x01\x00\xa1" >/dev/ttyusb0

Then the./ttyusb0open.sh and./ttyusb0close.sh can also be implemented directly on the console to control the relay switch.

To this step, I believe that the program ape friends already know how to implement the "smart switch", using their own good language development, such as C/D, Java, Python, Nodejs, Golang, etc., can be developed under the Arm+linux "on and web communications, communication with the serial port," the remote and intelligent applications.

Of course, you can also not call the script file, directly in the application code to send serial data.

I'm using Nodejs, which lists the main code:

/****************************************************************************************************/

function util_execmd (cmd, param, callback) {

var rst = {};

var spawn =require (' child_process '). Spawn;

var free = spawn (Cmd,param);

Free.stdout.on (' Data ', function (data) {

Rst.out = "" + data;

});

Free.stderr.on (' Data ', function (data) {

Rst.err = "" + data;

});

Free.on (' Exit ', function (code, signal) {

Rst.exit = "" + code;

Callback (RST);

});

}

Router.get ('/ttyusb/open ', function (req, res, next) {

Console.log ("open");

Util_execmd ("ttyusb0open.sh", [], function (RST) {

Res.send (Json.stringify (RST));

});

});

Router.get ('/ttyusb/close ', function (req, res, next) {

Util_execmd ("ttyusb0close.sh", [], function (RST) {

Res.send (Json.stringify (RST));

});

});

/****************************************************************************************************/

I have been through the above steps to control a USB light, placed in the bathroom door:) but have not access to 220V power to control the washing machine, rice cooker, air conditioning, hope that the brave friend, Access 220V test, can tell me, thank you! Please use 220V power in case of safety first.

Smart switch: Orange PI one (arm Linux) control relays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.