GPIO configuration
The default GPIO configuration is configured to follow the GL-iNet wireless router pinout (i.e. GPIO 20, 19, 18, 22 & 21), however it is very easily re-configure the layout to suit yourself.
Below is the pin connections between GL-iNet wireless router and 74HC595 Shift Register. Click here if you want to know more about GPIO.
Router GPIO 20 - 74HC595 Pin 12 (Latch)
Router GPIO 19 - 74HC595 Pin 14 (Data)
Router GPIO 18 - 74HC595 Pin 11 (Clock)
Router GPIO 22 - 74HC595 Pin 13 (Output Enable)
Note: You can always disconnect OE (74HC595 Pin13) from GPIO21 and connect it to groud if you want to use only 3 signals to control the Shift Register.
Code Samplers
With only two lines of code, you can shift any values to the Shift Register. Example below send a value 6 (00000110) to the Shift Register starting from least significant bit (LSBFIRST).
require "shiftOut" update_ShiftRegister(6, LSBFIRST) --alternative: update_ShiftRegister(6)
Example below will send the value starting from most significant bit (MSBFIRST) to the Shift Register:
require "shiftOut" update_ShiftRegister(255, MSBFIRST) --turn on all outputs
Example using binary number
require "shiftOut" update_ShiftRegister_Binary("10100010",LSBFIRST)
List of default values
You can overwrite the following settings in order to use with different router.
LatchPin = 20 (LatchPin connected to GPIO20)
DataPin = 19 (DataPin connected to GPIO19)
ClockPin = 18 (ClockPin connected to GPIO18)
OutputEnablePin = 22 (Future use for PWM control)
Number_of_bit = 8 (8=single shift register, 16=two shift register, etc)
Example below will send a 16bits value to the Shift Register, 321 will output 0000000101000001
require "shiftOut" Number_of_bit = 16 update_ShiftRegister(321)
Here is an example to re-configure pin configuration to match with your router. You can use other pins, like those connected to the LEDs.
require "gpio" --require if you want to re-configure GPIO require "shiftOut" LatchPin = 7 DataPin = 29 ClockPin = 26 OutputEnablePin = 22 pinMode(LatchPin, OUTPUT) pinMode(DataPin, OUTPUT) pinMode(ClockPin, OUTPUT) pinMode(OutputEnablePin, OUTPUT) update_ShiftRegister(6)
Step by step
I'm using a GL-iNet wireless router for this project, it is preloaded with the OpenWRT firmware. You must upgrade your router to OpenWRT firmware if your router does not already have OpenWRT. Below is a detailed guide to use the Lua_ShiftOut library.
Prerequisite
- A router (eg. TL-WR1043ND, TL-MR3020, GL.iNet) flashed with OpenWRT, I suggested to use GL.iNet, it is preloaded with OpenWRT.
- SSH client software such as PuTTy
- FTP/SCP client software such as WinSCP
Source Code
- Lua_ShiftOut depend on Lua_GPIO to control the router GPIO
- Lua_ShiftOut source code
Find the Router IP address
Now, you must know your router IP address and its login user name, most of the OpenWRT firmware router is configured to 192.168.1.1 with user name root. For my GL.iNet wireless router, the default IP address and user name is 192.168.8.1 and root respectively.
If you don't know your router IP address, I would suggested to use Soft Perfect Network Scanner to detect your router IP address.
Uploading Files and Folders to Router
I'm using WinSCP to copy files/folders from computer to router. You need to download the source code (Lua_GPIO & Lua_ShiftOut libraries) and copy them to OpenWRT search path (eg. /usr/lib/lua) as shown in figure below.
Enable SSH
Before enabling SSH, you must set a password for your router.
- Open up a browser such as Internet Explorer.
- Navigate to your router IP (eg. http://192.168.1.1)
- Login to the router and change the password, SSH is enabled automatically.
Testing
Below is an example (filename: shiftOut_example.lua) to show you how to control the LED from router, it is already included in your download.
- Upload the file (shiftOut_example) to your router. I copy it to router /tmp directory.
- SSH to router
Lo
- Login and change the file access permission to 755. Finally, run the example
login as: root
root_192.168.8.1's password:
BusyBox v1.22.1 (2014-10-08 16:34:50 HKT) built-in shell (ash)
Enter 'help' for a list of built-in commands._______ ________ __
| |.-----.-----.-----.| | | |.----.| |_
| - || _ | -__| || | | || _|| _|
|_______|| __|_____|__|__||________||__| |____|
|__| W I R E L E S S F R E E D O M
-----------------------------------------------------
BARRIER BREAKER (14.07, r42853)
-----------------------------------------------------
* 1/2 oz Galliano Pour all ingredients into
* 4 oz cold Coffee an irish coffee mug filled
* 1 1/2 oz Dark Rum with crushed ice. Stir.
* 2 tsp. Creme de Cacao
-----------------------------------------------------
root@GL-iNet:~# chmod 755 /tmp/shiftOut_example
root@GL-iNet:~# /tmp/shiftOut_example.lua
Using the browser
Save the following file to /www/cgi-bin and give it a name (eg. control)
Now, you can control it from internet.
- Turn on first LED
http://192.168.8.1/cgi-bin/control?1 - Turn on all LED
http://192.168.8.1/cgi-bin/control?255