Source code and download
- Download the source code (Lua_2W_Motor.zip) to your computer.
- Extract the zip file to your computer.
- Upload all files to router \usr\lib\lua directory, I'm using WinSCP to upload files from computer to wireless router as shown in figure below.
- Right click the two files and change its permissions to 0755 as shown in figure below.
GPIO Configuration
The default configuration is configured to follow the GL-iNet wireless router pinout (i.e. GPIO 20, 19, 18, 22 & 21), you may change the following value to suit yourself.
Motor[1].pinB = 22
Motor[2].pinA = 18
Motor[2].pinB = 19
Using the library
Lua comes with the default installation and support web service. Following steps show you how to test the Lua_motor library under the Lua command line interpreter.
- SSH to wireless router.
- Enter router user name and password.
- Type lua to bring up the Lua command line interpreter as shown in figure below. Each command enter into the Lua interpreter is executed immediately.
login as: root
root@@192.168.2.1's password:BusyBox v1.22.1 (2014-09-20 22:01:35 CEST) 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, r42625)
-----------------------------------------------------
* 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@OpenWrt:~# lua
Lua 5.1.5 Copyright (C) 1994-2012 Lua.org, PUC-Rio (double int32)
>
Controlling a Motor
Lua_2W_Motor library must loaded before you can controlling a motor.
Syntax: motor_run(motorNumber, direction)
Where motorNumber can be 1 or 2, direction must be either FORWARD, BACKWARD or STOP
- Example 1: Motor 1 forward
>require "2w_motor" --import the Lua_2W_Motor library
>motor=1
>motor_run(motor, FORWARD) --or motor_run(motor, 8)
- Example 2: Motor 2 backward
>motor=2
>motor_run(motor, BACKWARD) --or motor_run(motor, 2)
- Example 3: Motor 1 stop
>motor1
>motor_run(motor, STOP) --or motor_run(motor, 5)
Controlling a car
Syntax: car_run(direction)
Where direction can be FORWARD, BACKWARD, LEFT, RIGHT & STOP
- Example 4: Car forward
>require "2d_motor" --import the Lua_2W_Motor library
>car_run(FORWARD) --or car_run(8)
- Example 5: Car stop
>car_run(STOP) --or car_run(5)
- Example 6: Car left
>car_run(LEFT) --or car_run(4)
Controlling a car from web browser
Create a new file with the following codes and save it as \www\cgi-bin\car. Remember to change its file permission to 0755.
#!/usr/bin/lua local str= os.getenv("QUERY_STRING") -- query string from URL |
Now you can use the following url to control the car
http://routerIP/cgi-bin/car?NUMBER
Where NUMBER is ranged from 1 to 9
- Example 1: Car forward
http://192.168.8.1/cgi-bin/car?8
- Example 2: Car Stop
http://192.168.8.1/cgi-bin/car?5