Wednesday, July 31, 2013

HelloWorld in Lua on Raspberry Pi running Mihini on ArchLinux with Koneki as IDE

Here is how you would go about setting up Eclipse's M2M development environment on your Windows Computer & Raspberry Pi. This blog post can be considered as a simplified (and a little bit updated) version of "Run Mihini on an Open Hardware platform" specific to RPi/ArchLinux combination:
  1. Get your Raspberry Pi up and running:

    1. Get the latest ArchLinux SD card image for Raspberry Pi from here
    2. Write the image on to an SD card using either dd (if using Linux) or Win32 Disk Imager (if using Windows)
    3. Boot the Rasberry Pi using the newly created SD card. Make sure it is connected to your LAN (i.e. home WiFi Router) using a straight Ethernet cable. Your Windows PC should also be connected to the same LAN using WiFi or Ethernet.
    4. Login to your WiFi router's management web page and figure out the IP address that your router has assigned to your Raspberry Pi. In my case the Raspberry Pi's address was
      Looking up RPi's IP address from the router's admin webpage
    5. Use PuTTY (download putty.exe from here) to connect to Raspberry Pi using the IP address that you got by looking up the WiFi Router's DHCP client's list in the previous step. If PuTTY complains about the host key not being cached, then just press Yes. You will now be able to access Raspberry Pi's console over the network.
    6. login as: root
      password: root
  2. [OPTIONAL] Create extra partition:
    1. If you your SD card was more than 2GB, then you can use the extra empty space (ArchLinux image uses up only the first 2GB) to create an extra partition for your own use
      1. Issue the command fdisk /dev/mmcblk0 over the console (PuTTY)
      2. Create a new partition by pressing "n"
      3. Select primary partition and leave the starting and ending size to default, fdisk will automatically create a new partition to use up the remaining empty space.
      4. Press "w" to update the partition table and reboot the Raspberry Pi by issuing the command reboot
      5. This will cause PuTTY to disconnect from Raspberry Pi. Wait  30 seconds and connect to RPi again.
      6. Look up "the mmcblk0**" entries under /dev, you will note a new partition there: /dev/mmcblk0p3.
      7. Format this partition: mkfs.ext4 /dev/mmcblk0p3
      8. Label the partition: e2label /dev/mmcblk0p3 data
      9. Edit fstab to automatically mount the partition, issue the command nano /etc/fstab 
        and add the following line to the end of the file and save the file before exiting:
        /dev/mmcblk0p3  /data       ext4    defaults        1       2
        editing /etc/fstab
      10. Issue the following commands in succession to mount the partition for now:
        • mkdir /data
        • mount /data
      11. Your partition is now accessible at /data
  3. Installing basic tools and mihini on RPi
    All commands must be issued via the console accessed using PuTTY. Make sure that Raspberry Pi has access to the internet via the LAN to which it and the computer you are using are connected.
    1. Update pacman's package lists:
      pacman -Syy
    2. Update pacman itself:
      pacman -S pacman
    3. Install essential development tools (compiler etc.):
      pacman -Syu base-devel
    4. Change directory to /data or to your home directory - where ever you want to download the mihini package from AUR
    5. Get the mihini tarball from ArchLinux User Repository:
    6. Extract it:
      tar -xvzf mihini-git.tar.gz
    7. Enter the extracted folder: cd mihini
    8. Edit PKGBUILD to include the gpio module to be built along with the rest of the modules included in the mihini package: nano PKGBUILD
    9. Change the line make lua luac modbus_serial
      make lua luac modbus_serial gpio 
      and save the file.
    10. Build mihini (dependencies will also be downloaded):
      makepkg -s --asroot

      Completion of mihini build
  4. Finalizing mihini installation:
    Mihini is now installed at /opt/mihini. There are a few more steps required to complete the installation.
    1. Edit the file ./etc/systemd/system/mihini.service by using the nano editor:
      nano /etc/systemd/system/mihini.service
    2. Change the line ExecStart=/opt/mihini/bin/appmon_daemon -a /opt/mihini/bin/agent -w /opt/mihini -u mihini -g mihini -n 5
      ExecStart=/opt/mihini/bin/appmon_daemon -a /opt/mihini/bin/agent -w /opt/mihini -u root -g root -n 5 
      and save the file.
    3. Enable mihini to start at boot up: systemctl enable mihini
    4. Create symbolic link to lua binary: ln -s /opt/mihini/bin/lua /usr/bin/lua
    5. Link the libraries, execute the following three commands in succession:
      • cd /etc/
      • /bin/sh -c 'echo "/opt/mihini/lib/" > 01-mihini.conf'
      • ldconfig
    6. Reboot Raspberry Pi by issuing the command: reboot
  5. Install Eclipse & Lua Development Tools on your Windows PC:
    1. Download the 32-bit Windows version ( from here
    2. Extract it to a folder on your Windows PC and execute LuaDevelopmentTools.exe
    3. Select an appropriate workspace location and click OK
    4. From the Help menu, select "Install New Software"
    5. Select "" from the "Work with" dropdown menu
    6. Tick the checkbox against the last of the four entries: "Lua Development Tools - Koneki" and click Next
    7. This will install remote development support. You will be asked to restart Eclipse.
  6. Create connection to RPi using Eclipse:
    1. From the Window Menu select Open Perspective > Remote System Explorer
    2. Right click anywhere in the empty white region of "Remote Systems" pane on the left side and select "New Connection"
    3. Select "Lua SSH System" and click next.
    4. Enter your RPi's IP address in your Hostname field.
    5. Enter some name (eg "Bosco's RPi") in the name field and click Next
    6. Click Finish
    7. A new entry will be create in the "Systems Explorer" pane. Right click on it and select "connect"
    8. You will be asked for User ID and password. Enter "root" for both and tick the checkboxes to save them and press OK.
    9. For the 3 dialog boxes that pop up:
      1. Click Yes to add the new host to the hosts list
      2. Click Yes when it objects about not being able to verify the authenticity of the host.
      3. You can click No when it asks you to enter the master
    10. Switch back to Lua perspective (button on top right corner "Lua" or select from Windows>Open Perspective> Other)
      Remote System Explorer View
  7. Hello World:
    1. Create a new Lua Project in Eclipse: File>New>Lua Project
    2. Enter the project name as "MyHelloWorld" and click finish
    3. A new entry called "MyHelloWorld" will be created under Scripts Explorer. Expand it and open main.lua inside the src folder
    4. Delete the contents of main.lua and type the following in it:
      package.path = '/opt/mihini/lua/?.lua;/opt/mihini/lua/?/init.lua;' .. package.path
      package.cpath = '/opt/mihini/lua/?.so;' .. package.cpath
      local log   = require "log"
      local sched = require "sched"
      local function main()
          log("GENERAL", "INFO", "My first Mihini app is alive :)")
    5. Save the file (Ctrl+S)
    6. Now we need to run the file on RPi. To do this, bring down the Run menu and select "Run As > Lua Remote Application"
    7. The lua script will be uploaded to Raspberry Pi and run remotely. The results will be displayed in the console.
    8. Press the stop button to stop the script.
      Running HelloWorld remotely on RPi