
Limited Time Resource Download:Reply “Tutorial” to get the microcontroller eBook, reply “Simulation“ to get Proteus simulation materials, Baidu Pan group share link update time: 2016-02-14, if expired, please leave a message at the end of the article, do not leave a message in the background
Source: http://www.freebuf.com/news/special/72796.html
I have recently been researching microcontrollers and the security of IoT devices. Therefore, I started considering building a small home automation system. Although it is not yet complete, I want to share how I use Raspberry Pi 2 and some other electronic components to control the lights in my room. Of course, I will not introduce the initial setup of Raspberry Pi here, as you can find various tutorials online.
Important Notes
Before we proceed with the experiment, I think it is necessary to remind you about the dangers of “current” in the experiment. In the worst-case scenario, you could die or burn down your house. So, please do not attempt to complete anything mentioned in this article that you do not understand, or seek help from an experienced electrician while making.
Alright, let’s start the DIY!
Experiment Preparation
Hardware Requirements
1. Raspberry Pi 2 (or any model with 5V output power)
2. USB wireless dongle
3. 8-channel relay
4. Some Female-Female jumper wires (40 PCS FEMALE TO FEMALE JUMPER WIRES)
5. Light socket wire
(All the above hardware can be purchased on Taobao)
Other Requirements
1. Basic knowledge of Python or any other language (I will use Python)
2. Basic understanding of Linux systems
3. Focus and dedication
Process Requirements
First, ssh into the Raspberry Pi and install “apache” and “php5”:
You will need to install the python GPIO library to control the GPIO pins of the Raspberry Pi:
Understanding Components
Now, before we continue with the production, you need to understand the electronic components we will be using.
1. Relay
A relay is an electrical device that uses a very low voltage input to control high voltage electricity. It consists of a closed circuit made up of a metal rod wound with a coil and two small metal nodes. One node is fixed, while the other is movable. Whenever current flows through the coil, it generates a magnetic field that attracts the movable node towards the static node, completing the circuit. By supplying a small voltage to the coil, we can complete the high-voltage circuit. At the same time, the static node is not physically connected to the coil, so if something goes wrong, the microcontroller-driven coil rarely fails.
In the experiment, I used an 8-channel relay that can control 8 devices simultaneously. You can choose your own relay or relay board, but make sure you handle it within the rated voltage of the relay to avoid any accidents.
2. Jumper Wires
Jumper wires are simple connecting wires that link the GPIO pins of the Raspberry Pi to the relay.
3. Raspberry Pi 2
We use Raspberry Pi 2 as a microcontroller to manipulate the relay. It has 40 GPIO (General Purpose Input/Output) pins. You can see the layout of these pins below, and we will use these interfaces to power the relay and control the switch.
Connecting the Circuit
The circuit is very simple. We will connect the GPIO pins to the relay board. First, connect the “GND” on the relay board to any “GND” on the Raspberry Pi. Then connect the relay’s “IND1” to GPIO PIN 17; we will use GPIO PIN 17 as an output to control the first relay. Finally, connect the relay’s “VCC” to the “5V” GPIO pin on the Raspberry Pi. Let’s set it up simply and directly:
Now we come to the most tricky part, where we need to connect the relay to the light socket that powers the main circuit. However, I want to first introduce a simple operation to turn the light on and off using a DC power supply.
We typically connect two wires to the light bulb to provide current. One wire is the “neutral” wire, while the other is the “live” wire that carries current. Similarly, there is a switch that controls the entire circuit. Thus, when the switch (closed) connects to the current flowing through the light bulb and the live wire, the circuit is complete. The light bulb lights up. When the switch (open) breaks the circuit and the current to the light bulb, the light bulb does not light up. Here is a small circuit diagram to explain the situation:
In our experiment, we need the “live wire” to break the circuit through our relay, thus controlling the current flowing through the light bulb. Therefore, when the relay is on, the light bulb should light up in the closed circuit, and vice versa. Please refer to the complete circuit:
Control Script
Now, we finally come to the software part. I wrote a simple Python script to control the relay switch, using GPIO PIN 17, and a PHP code that can run the Python script on any mobile terminal. You can find this code on my GitHub (and CSS).
Note: You will need to add the “www-data” user to the sudoers file.
Note+: The PHP code is just for testing; we do not recommend running it in a public environment.
I will soon complete this setup and hope to come back to update a new post. Please try this light control yourself before that, but be sure to pay attention to safety.
Video Demonstration: demo1 :
demo2:
References
· GPIO Diagram: http://data.designspark.info/uploads/images/53bc258dc6c0425cb44870b50ab30621
·https://www.youtube.com/watch?v=Z2B67hybdAA
·https://elementztechblog.wordpress.com/2014/09/09/controlling-relay-boards-using-raspberrypi/
·https://www.raspberrypi.org/forums/viewtopic.php?t=36225
·https://github.com/TheGreenToaster/web-pins/
Limited Time Resource Download: Reply “Tutorial” to get the microcontroller eBook, reply “Simulation” to get Proteus simulation materials。
> > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >
Click the lower left corner “Read Original” to enterthe forum for communication!!!