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 10.10.10.23
      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:
      wget http://aur.archlinux.org/packages/mi/mihini-git/mihini-git.tar.gz
    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
      to
      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
      to
      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/ld.so.conf.d/
      • /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 (org.eclipse.koneki.ldt.product-win32.win32.x86.zip) 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 "http://download.eclipse.org/koneki/releases/stable" 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 :)")
      end
       
      sched.run(main)
      sched.loop()
      
    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

13 comments:

Guilhem Saurel said...

Instead of whole paragraph 3 & 4, you can:

$ pacman -Syu yaourt
$ yaourt -S mihini-git

and the systemd daemon should be running ;)
(http://wiki.eclipse.org/Mihini/Install_Mihini )

There is also a way to improve the connection from LDT, but it stills a nightly build:

http://wiki.eclipse.org/Mihini/Run_Mihini_on_an_Open_Hardware_platform#Step_4_-_Setup_Koneki_environment



Thanks for your work !

Anurag Chugh said...

I tried using yaourt, but it didnt work for me so I had to switch to AUR. Also I referred to the eclipse wiki link that you have mentioned to write up this post. I was thinking of editing the wiki to simplify, edit out and add in some parts, but being a beginner, I decided against it and wrote up my blog. I mentioned the wiki link upfront to avoid confusion.

Guilhem Saurel said...

it's sad that yaourt didnt work for you, I will try it a little more, but I understand your point :)

Thanks for your feedback !

Ramaprasad Nakka said...

Thanks a lot for the detailed instructions. I spent 4 days with Raspbian & Mihini to make GPIO work, finally installed Arch Linux on another card and followed your instructions. Able to work with GPIO in one hr.
Thanks a lot.

Avinash said...

hi ,
when i am trying to perform step 10.Build mihini. i am getting
makepkg: command not found

can anyone please help me out to solve this...............

Thanks for any kind of help

Anurag Chugh said...

@Avinash These steps are for ArchLinux. Are you using ArchLinux or Raspbian? Provide some more info regarding your setup...

Avinash said...

thanks Anurag for your quick response, Actually i was using UBUNTU 13.04 version............................does mihini server has support for this ?

Anurag Chugh said...

These steps are only for ArchLinux... will not work for other distros. I like ArchLinux so I made it run on only that distro..

Avinash said...

ok thank you will install ARCHLINUX and follow your steps..............

Avinash said...

hi
i have installed arch linux on sd card by following steps 1,2,3. when i am trying to check ip address in router i could not find it. can you please suggest some solution ?

Anurag Chugh said...

Avinash, what router do you have? you can also connect Raspberry Pi to a TV or HDMI monitor and plugin a keyboard in it and use ifconfig command to check its ip address

ganju said...

Hello Anurag, i did all the steps and things installed fine.. till, while running the Lua on the RPI, i get the error "Launching MyHelloWorld_main_RPI" has encountered a problem. On details , i get
"An internal error occurred during: "Launching MyHelloWorld_main_RPI".
org/eclipse/koneki/ldt/core/IProjectSourceVisitor2"

Can u help pl ?
ganeshs3681@gmail.com

ganju said...

I'm running version 1.1 downloaded from http://www.eclipse.org/koneki/ldt/

Post a Comment