Creating an installable lua only Hello World Package for OpenWrt

Introduction 

In a previous post (Adding new elements to OpenWrt's LuCI on GL-MiFi) we saw how to add new entries to the web interface of a device running OpenWrt by adding/editing files directly on the device itself. In this post we will see how to create an installable .ipkg package to do the same. We will be using the OpenWrt SDK on an Ubuntu host to build the  .ipkg package.

I will be using Linksys's MR8300v1 WiFi router which runs on Qualcomm SoCs based on the ipq40xx platform (Quad core ARMv7)

Setup the development device (aka router)

  1. Provide internet to the development router by connecting a LAN cable from your home WiFi router's LAN port to development routers WAN port.
  2. Connect a straight LAN cable from your laptop to router’s LAN port
  3. Install OpenWrt from https://openwrt.org/toh/linksys/mr8300
  4. [Optional] Install again if you want to ensure that same version of OpenWrt is in both flash partitions
  5. Login to the device using web browser https://192.168.1.1/ (Login: root, leave password blank)
  6. Go to https://192.168.1.1/cgi-bin/luci/admin/system/admin and set a password for root, otherwise the router won’t be accessible via SSH
  7. [Optional] In the next step, we will be installing development tools on a Ubuntu host. If you - like me - are running Ubuntu inside a VM on Windows, you will need to add network adapter to the VM in the bridged, bridging to the physical Ethernet NIC of your laptop to which you have connected a LAN cable to the development device (i.e. router running OpenWrt)
     
    [Optional] Create bridge adapter for your Ubuntu running inside a VM so that your development router assigns an IP to your VM and you are able to access the router from the VM over SSH

  8. Access your router over SSH: root@192.168.1.1
    Install luci-lua-runtime, while logged into the router over SSH:
    1. opkg update
    2. opkg install luci-lua-runtime
     

Setup SDK for OpenWrt on Ubuntu 24.04.1 x86_64 and build the Hello World App

  1. While in ~
  2. sudo apt update
  3. sudo apt upgrade
  4. sudo apt install -y build-essential git subversion libncurses-dev zlib1g-dev gawk flex gettext wget unzip python3 python3-setuptools rsync zstd tree
  5. Locate SDK URL on OpenWrt’s website
    1. Go to https://downloads.openwrt.org/releases/
    2. Click on 24.10.2 (Corresponds to OpenWrt installed on development device)
    3. Click on targets
    4. Click on ipq40xx (Development device’s platform)
    5. Click on generic
    6. Scroll down and under the section titled “Supplementary Files” locate link to file named something like: openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64.tar.zst
    7. Copy link to that file and use it in the next step.
  6. wget https://downloads.openwrt.org/releases/24.10.2/targets/ipq40xx/generic/openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64.tar.zst
  7. unzstd openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64.tar.zst
  8. tar -xf openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64.tar
  9. rm openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64.tar
  10. cd openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64
  11. mkdir -p package/luci-app-helloworld/luasrc/{controller,view}
  12. cd ~/openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64/package/luci-app-helloworld
  13. nano Makefile
    1
    2
    3
    4
    5
    6
    7
    8
    include $(TOPDIR)/rules.mk
    
    LUCI_TITLE:=LuCI Hello World
    LUCI_PKGARCH:=all
    
    include $(TOPDIR)/feeds/luci/luci.mk
    
    $(eval $(call BuildPackage,luci-app-helloworld))
    
  14. nano luasrc/controller/helloworld.lua
    1
    2
    3
    4
    5
    6
    7
    8
    9
    module("luci.controller.helloworld", package.seeall)
    
    function index()
        entry({"admin", "services", "helloworld"}, call("action_index"), _("Hello World"), 100)
    end
    
    function action_index()
        luci.template.render("helloworld")
    end
    
  15. nano luasrc/view/helloworld.htm
    1
    2
    3
    4
    <%+header%>
    <h2>Hello World App</h2>
    <button onclick="alert('Hello World!')">Click Me</button>
    <%+footer%>
    
  16. The layout should look like:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    vboxuser@ubuntu-24:~/openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64/package/luci-app-helloworld$ tree
    .
    ├── luasrc
    │   ├── controller
    │   │   └── helloworld.lua
    │   └── view
    │       └── helloworld.htm
    └── Makefile
    
    4 directories, 3 files
    
  17. cd ~/openwrt-sdk-24.10.2-ipq40xx-generic_gcc-13.3.0_musl_eabi.Linux-x86_64 
  18. ./scripts/feeds update -a
    (Reference: https://openwrt.org/docs/guide-developer/toolchain/using_the_sdk)
    (PS: I had to switch to Vi for internet because the command kept timing out in Airtel)
  19. ./scripts/feeds install -a
  20. make menuconfig
    1. Go to LuCI → 3. Applications
    2. You will see 2 entries for luci-app-helloworld
    3. Select first one and press the “Y” key, to turn the checkbox next to luci-app-helloworld from [M] to [*]
       
      Enable the luci-app-helloworld from the Configuration
    4. Highlight and press Save to save the .config
    5. Exit
  21. make package/luci-app-helloworld/compile V=s
    (Clean Command if needed: make package/luci-app-helloworld/clean)
  22. scp -O bin/packages/arm_cortex-a7_neon-vfpv4/base/luci-app-helloworld_*.ipk root@192.168.1.1:/tmp
  23. ssh root@192.168.1.1
  24. opkg install /tmp/luci-app-helloworld_*.ipk
  25. Open the router's web interface in your browser: http://192.168.1.1/cgi-bin/luci/ 
    annnd....
This is what the entry in OpenWrt Device's Web UI would look like

Comments

Popular posts from this blog

Replacing the current sense resistor in Portable Chargers/Power Banks for powering low power DIY projects.

Interface USB Mouse to your Arduino using CH375B

Simple TCP client server sockets application using IPv6 and IPv6