I bought a Raspberry Pi and an Arduino several months ago and they basically sat on my desk, collecting dust, until I made a concentrated effort to get them up and running. Since they’re not a plug-and-play type of device, there’s an entrance barrier to getting anything working on them and they need a little TLC. It’s worth it, however, to take the time to get to know them as you can do amazing things with the Internet of Things once you have the devices that support it up and running!
I thought I’d write a few posts describing what I’m doing with them as I gradually learn this new technology. I originally bought the Pi because I wanted to learn python, but found that you can run many languages on the machine, including lua! So that led me to believe that I might be able to create a link between my Raspberry Pi configured as a web server and a Corona app written in lua, ideally to make the mobile app control some hardware activity. This has actually been done by the fabulous Benjamin Cabé of the Eclipse Foundation; you can read more about his projects here.
So what we want to do in this project is to set up our Raspberry Pi in such a way that it will flash LED lights when we run a “Red Light / Green Light” code snippet on an external computer. This code snippet is written in lua. This will at least allow us to demo the toolsets that we need to connect the Pi with lua code.
Like a lot of these hardware/software projects, there is a little ‘install-fest’ that needs to happen. First, you have to get your Raspberry Pi set up and working correctly. To do this, I recommend grabbing a copy of Carrie Ann Philbin’s book. Do everything she says to set up your Pi. You need a monitor and a keyboard and some way to hook it up to your internet connection (I use a monitor that I found at our town’s dump, or you can hook up your Pi to your TV although that’s a lot less convenient). I repurposed a keyboard with USB hookup to the Pi, went to Radio Shack and bought a pack of LEDs, Resistors, a Breadboard and Jumper Cables (it ran about $40), and got the system running the NOOBs OS. So far, so good!
Now, configure some hardware. You want to hook up your LEDs, resistors, a breadboard, and jumper cables to your Pi like this:
Taken from RasPi.tv. Use a green light instead of that white color, so that our “Red Light, Green Light” game makes sense.
Now, we need to turn our attention to an install-fest on our laptop. I have a MacBook Pro. Here’s where the tools introduced by Benjamin Cabé come in useful!
Install the following on your laptop:
1. An IDE to support your lua development for the Internet of Things. Eclipse has Konecki, which is bundled with Lua Development Tools so you can use that familiar language. http://www.eclipse.org/koneki/ldt/
2. Mihini – after you install Konecki, install Mihini, an embeddable runtime with a high-level API with which you can connect your lua code to machines within the IOT sphere. Also install Mihini on your Raspberry Pi: http://wiki.eclipse.org/Mihini/Run_Mihini_on_an_Open_Hardware_platform#Sources_to_compile and start it up.
Now, you can open Konecki and use its ‘connect’ capability to hook your laptop with your Pi over ethernet. To do this, type ‘ifconfig’ on your Pi’s LXTerminal to determine your IP address. Mine tends to change, so this is just an interim solution.
Create a connection in Konecki between your laptop and your Pi:
Open the perspective “Remote System explorer”
– “Define a connection to remote system” -> “Mihini Device”
– Fill the “Host name” with your Raspberry Pi’s IP address, and “Finish”
– Right click on “Applications”, then “Connect…”, and fill in your credentials
Now, you should be able to run lua code from your laptop right on your Pi.
Here’s some code to try, assuming you have all these installations done, your are running Mihini on your Pi and have Konecki all configured and your Pi is hooked up with the circuit as described above, with GPIO pins in 24 and 25. You may have to change the paths to the Mihini libraries in the first few lines, as those are configured on your Pi.
--when run, blink an LED as directed package.path = '/home/pi/mihini/lua/?.lua;/home/pi/mihini/lua/?/init.lua;' .. package.path package.cpath = '/home/pi/mihini/lua/?.so;' .. package.cpath local gpio = require"gpio" local shutOffAll,flashRed,flashGreen,main,sleep local clock = os.clock function shutOffAll() --turn off all pins gpio.write(24,0) gpio.write(25,0) end function sleep(n) -- seconds local t0 = clock() while clock() - t0 <= n do end end function flashRed() shutOffAll() gpio.write(24,1) sleep(1) main() end function flashGreen() shutOffAll() gpio.write(25,1) sleep(1) main() end function main() shutOffAll() print("red light (r) or green light (g)") local input = io.read() if string.lower(input) == "r" then flashRed() elseif string.lower(input) == "g" then flashGreen() end end main()
Running this code will allow you to type in either ‘r’ or ‘g’ in the prompt in Konecki’s console so you can light up red and green lights. Cool, huh?
Let me know if you get it working. Next, we’ll get a Corona app to blink those lights!