IoT.js landed in Raspbian

Following previous efforts to deploy iotjs on Raspberry Pi 0, I am happy to announce that IoT.js 1.0 landed in Debian, and was sync’d to Raspbian for ArmHF and Ubuntu as well.

While the package is targeting the next distro release, it can be easily installed on current versions by adding a couple of config files for “APT pinning”.

If you haven’t set up Raspbian 9, just dump the current Raspbian image to SDcard (for the record I used version 2018-03-13-raspbian-stretch-lite)

Boot your Pi.  To keep track of changes in /etc/, let’s install etckeeper:

sudo apt-get update
sudo apt-get install etckeeper

Upgrade current packages:

sudo apt-get upgrade
sudo apt-get dist-upgrade

Declare the current release as default source:

cat<<EOT | sudo tee /etc/apt/apt.conf.d/50raspi
APT::Default-Release "stretch";
EOT

Then add a repo file for the next release:

cat /etc/apt/sources.list | sed 's/stretch/buster/g' | sudo tee /etc/apt/sources.list.d/raspi-buster.list

Unless you want to test the upcoming release, it maybe be safer to avoid upgrading all packages yet.  In other words, we prefer that only iotjs should be available from this “not yet supported” repo.

cat<<EOT | sudo tee /etc/apt/preferences.d/raspi-buster.pref
Package: *
Pin: release n=buster
Pin-Priority: -10
EOT

cat<<EOT | sudo tee /etc/apt/preferences.d/iotjs.pref
Package: iotjs
Pin: release n=buster
Pin-Priority: 1
EOT

Now iotjs 1.0-1 should appear as available for installation:

sudo apt-get update ; apt-cache search iotjs
iotjs - Javascript Framework for Internet of Things

apt-cache policy iotjs
iotjs:
  Installed: (none)
  Candidate: 1.0-1
  Version table:
     1.0-1 1
        -10 http://raspbian.raspberrypi.org/raspbian buster/main armhf Packages

Let’s install it:

sudo apt-get install iotjs
man iotjs

Even if version 1.0 is limited in compared to the development branch, you can start by using the http module which is enabled by default (not https).

To illustrate this, when I investigated “air quality monitoring” for a TizenRT+IoT.js demo I found out that OpenWeatherMap is collecting and publishing “Carbon Monoxide” Data, so let’s try their REST API.

Create a file, example.js for example, that contains:

var http = require('http');

var location = '48,-1';
var datetime = 'current';

//TODO: replace with your openweathermap.org personal key
var api_key = 'fb3924bbb699b17137ab177df77c220c';

var options = {
  hostname: 'api.openweathermap.org',
  port: 80,
  path: '/pollution/v1/co/' + location + '/' + datetime + '.json?appid=' + api_key,
};

// workaround bug
options.headers = {
  host: options.hostname
}

http.request(options, function (res) {
  receive(res, function (data) {
    console.log(data);
  });
}).end();

function receive(incoming, callback) {
  var data = '';

  incoming.on('data', function (chunk) {
    data += chunk;
  });

  incoming.on('end', function () {
    callback ? callback(data) : '';
  });
}

And just run it:

iotjs example.js
{"time":"2018-03-27T02:24:33Z","location":{"latitude":47.3509,"longitude":-0.9081},"data":[{"precision":-4.999999987376214e-07,"pressure":1000,"value":1.5543508880000445e-07}
(...)

You can then use this to do things such as update a map or raise an alert on anything useful, or try to rebuild master branch.

Setup WiFi on Your Raspberry Pi 2 with Tizen

This article is part of a series on Tizen on the Raspberry Pi 2.

Once you have your RPi2 up and running with Tizen, it is helpful to get network connectivity setup. This guide will describe how to enable WiFi on your RPi2. Doing this will make it much easier to work on the device because it will allow you to connect using ssh through your wireless network and access remote resources on the Internet.

Get Familiar With the Hardware

The procedure to enable WiFi on Tizen for Raspberry PI2 (Rpi2) is simple.

You will first want to make sure Tizen detects your WiFi adapter. You can find it by running lsusb which will display all connected usb devices, including . My own RPi2  is using a Realtek device based on the RTL 8188CUS chipset:

Bus 001 Device 006: ID 0bda:8176 Realtek Semiconductor Corp. RTL8188CUS 802.11n WLAN Adapter

Once connected, it produces the following messages:

[    2.787810] usb 1-1.4: new high-speed USB device number 6 using dwc_otg
[    2.889252] usb 1-1.4: New USB device found, idVendor=0bda, idProduct=8176
[    2.889265] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
[    2.889274] usb 1-1.4: Product: 802.11n WLAN Adapter
[    2.889283] usb 1-1.4: Manufacturer: Realtek
[    2.889292] usb 1-1.4: SerialNumber: 00e04c000001
[   16.520739] usbcore: registered new interface driver rtl8192cu

You can use view this on your own device by running dmesg -wH before plugging in the device. This command will show you Kernel messages as the device is connected.

Getting WiFi Running Wih Connman

The Tizen distribution uses connman to setup the Network interfaces. In order to use, it is required to enable WiFi and to configure connman to create a service. This is done via connmanctl command:

root@raspberrypi2:~# connmanctl
connmanctl> enable wifi
Enabled wifi
connmanctl> agent on
Agent registered
connmanctl> services
*AO Wired      ethernet_b8dead85dead_cable
*A  mynetwork  wifi_deaddeaddead_00dead00dead_managed_psk
               wifi_deaddeaddead_hidden_managed_psk
    othernet   wifi_deaddeaddead_4756542d42323dead_managed_psk
connmanctl>connect wifi_deaddeaddead_00dead00dead_managed_psk
Connected wifi_deaddeaddead_00dead00dead_managed_psk
connmanctl> exit

If mynetwork requires a passphrase, it will ask when connect is issued:

root@raspberrypi2:~# connmanctl
connmanctl> enable wifi
Enabled wifi
connmanctl> agent on
Agent registered
connmanctl> services
*AO Wired      ethernet_b8dead85dead_cable
*A  mynetwork  wifi_deaddeaddead_00dead00dead_managed_psk
               wifi_deaddeaddead_hidden_managed_psk
    othernet   wifi_deaddeaddead_4756542d4232dead_managed_psk
connmanctl> connect wifi_deaddeaddead_00dead00dead_managed_psk
Connected wifi_deaddeaddead_00dead00dead_managed_psk
Phassphrase? [type your password here]
connmanctl> exit

The configuration associated with the above, including the password, will be saved at /var/lib/connman.

Please notice that connman won’t start wifi network after reboot. So, if you need to enable wifi again, after reboot, you’ll need to call:

connmanctl connect wifi_deaddeaddead_00dead00dead00dead_managed_psk 

Have fun!

Raspberry Pi is a trademark of the Raspberry Pi Foundation

RPi2: Getting Smart on Tizen

This article is part of a series on Tizen on the Raspberry Pi 2.

Once you’ve got Tizen running on your Raspberry PI 2 (RPi2) using the procedures described in Tizen on RPi2: Now Supporting 3D Acceleration, you will probably want to be able to dynamically add/remove packages. It’s a pain to be required to rebuild and flash your OS every time you want to change the included software, so following this tutorial will help make this much easier. This small tutorial shows how to install the smart tool that is shipped on Tizen, but is not installed by default.

1) Add Smart to the RPi2 Image

The first step is to add the Smart Package Manager (python-smartpm) to the RPi2 Tizen image. This is the same package manager used on Yocto, and Tizen has its own patches for it.

In order to install it, you should apply the following simple patch on your Tizen repository:

--- a/meta-tizen/meta-tizen-common-base/recipes-image/images/tizen-core-image-minimal.bb
+++ b/meta-tizen/meta-tizen-common-base/recipes-image/images/tizen-core-image-minimal.bb
@@ -66,0 +67,2 @@ CORE_IMAGE_BASE_INSTALL += "gstreamer1.0-libav"
+# Package management
+CORE_IMAGE_BASE_INSTALL += "python-smartpm"

If you are unfamiliar with the patching process in linux, create a file that contains the above information, and use patch to apply the patch to: meta-tizen/meta-tizen-common-base/recipes-image/images/tizen-core-image-minimal.bb

Once patched, re-build the image as described on the previous tutorials and copy it to an SD card.

NOTE: You should replace /dev/sdz on the commands below and on the next sections of this article, in order to point to your specific disk that it is mapped. Otherwise you may damage your system!

As shown at Tizen on RPi2: Now Supporting 3D Acceleration, the build and install procedure is:

source ./tizen-common-init-build-env build
bitbake tizen-common-core-image-crosswalk-dev
dd if=tmp-glibc/deploy/images/raspberrypi2/tizen-common-core-image-crosswalk-dev.rpi-sdimg of=/dev/sdz bs=256M && sync

2) Resize the RPi2 Root Image to the Disk Size

By default, the RPi image size is minimal and does not fill the entire SD disk. We likely need to extend it to use all your SD card space, in order to have space to install new rpm packages on it.

Check the first sector number of the second partition of the disk, remember it, and use it to re-create the partition using the maximum sector number as the end of the partition.

In the example below, the first sector is 90112, and the last sector is 15564799. Delete partition 2 and make a new one from 90112 to 15564799. Note this is a slightly dangerous operation – you are deleting the sector with the important data on it. As long as you don’t do anything else in between it should be fine. The usual cautions about making sure which device’s partition table you are changing of course apply.

# fdisk /dev/sdz
Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): p
Disk /dev/sdz: 7.4 GiB, 7969177600 bytes, 15564800 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x000ceb8d

Device      Boot Start     End    Sectors   Size  Id Type
/dev/sdz1   *     8192   90111      81920    40M   c FAT32 W95 (LBA)
/dev/sdz2        90112 1810431    1720320   840M  83 Linux

Command (m for help): d
Partition number (1,2, default 2):

Partition 2 has been deleted.

Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-15564799, default 2048): 90112
Last sector, +sectors or +size{K,M,G,T,P} (90112-15564799, default 15564799):

Created a new partition 2 of type 'Linux' and of size 7.4 GiB.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Now, we’ll use the resize2fs command to extend the ext4 filesystem to fill the entire partition. Before that, the partition should be checked with e2fsck. Resize2fs may complain that you need to check again using “e2fsck -f”; if so, follow those instructions.

Those steps are shown below:

# e2fsck /dev/sdz2
e2fsck 1.42.12 (29-Aug-2014)
/dev/sdz2: clean, 16253/215040 files, 174308/215040 blocks
# resize2fs /dev/sdz2
resize2fs 1.42.12 (29-Aug-2014)
Resizing the filesystem on /dev/sdz2 to 1934336 (4k) blocks.
The filesystem on /dev/sdz2 is now 1933312 (4k) blocks long.

3) Put the rpm Files on a Web Server

After the bitbake build completes, you will find RPM packages in the directory build/tmp-glibc/deploy/rpm/. Copy the contents to your apache (or similar) web server root into a suitable directory (if you intend to use the same machine to do the builds and serve the packages, you can also make a symbolic link instead of copying).

The next step assumes that the server is at address 192.168.1.1 and that the files were copied under the rpm directory.

4) Add Smart Repositories to the RPi2 rootfs

Boot the RPi2 Tizen image, and log in as root (the default password is root). Then run the following command to create the package distribution channels on smart:

for i in all cortexa7t2hf_vfp_vfpv4_neon raspberrypi2; do smart channel --add $i type=rpm-md baseurl=http://192.168.1.1/rpm/$i; done

And update the channels with smart update:

# smart update
Loading cache...
Updating cache...        ############################### [100%]

Fetching information for 'all'...
-> http://192.168.1.1/rpm/all/repodata/repomd.xml                
repomd.xml               ############################### [ 16%]
-> http://192.168.1.1/rpm/all/repodata/primary.xml.gz
-> http://192.168.1.1/rpm/all/repodata/filelists.xml.gz
filelists.xml.gz         ############################### [ 25%]
primary.xml.gz           ############################### [ 33%]

Fetching information for 'cortexa7t2hf_vfp_vfpv4_neon'...
-> http://192.168.1.1/rpm/cortexa7t2hf_vfp_vfpv4_neon/repodata/repomd.xml
repomd.xml               ############################## [ 50%]
-> http://192.168.1.1/rpm/cortexa7t2hf_vfp_vfpv4_neon/repodata/primary.xml.gz
primary.xml.gz           ############################## [ 58%]
-> http://192.168.1.1/rpm/cortexa7t2hf_vfp_vfpv4_neon/repodata/filelists.xml.gz
filelists.xml.gz         ############################## [ 66%]

Fetching information for 'raspberrypi2'...
-> http://192.168.1.1/rpm/raspberrypi2/repodata/repomd.xml
repomd.xml               ############################## [ 83%]
-> http://192.168.1.1/rpm/raspberrypi2/repodata/primary.xml.gz
primary.xml.gz           ############################## [ 91%]
-> http://192.168.1.1/rpm/raspberrypi2/repodata/filelists.xml.gz
filelists.xml.gz         ############################## [100%]

Updating cache...        ############################## [100%]

Channels have 5310 new packages.
Saving cache...

5) Install Packages on RPi2!

Now, installing a new package is no more complicated than this simple command:

# smart install crosswalk-thirdparty
Loading cache...
Updating cache...  #################################### [100%]
Computing transaction...
Installing packages (1):
 crosswalk-thirdparty-11.40.277.0-r0@cortexa7t2hf_vfp_vfpv4_neon
917.7MB of package files are needed. 932.1MB will be used.
Confirm changes? (Y/n): y

Fetching packages...
-> http://192.168.1.1/rpm/cortexa7t2hf_vfp_vfpv4_neon/crosswalk-thirdparty-11.40.277.0-r0.cortexa7t2hf_vfp_vfpv4_neon.rpm
crosswalk-thirdparty-11.40.277.0-r0.cortexa7t2hf_vfp_vfpv4_neon.rpm           ########## [100%]

Committing transaction...
Preparing...                                 ########## [  0%]
   1:Installing crosswalk-thirdparty         ########## [100%]

Once you’ve successfully completed these steps, you should be able to install new versions of packages as they are developed without being required to flash your device every time. This seemingly minor change should help save time for anyone trying to build things for Tizen on the RPi2.

Get Involved

If you want to learn more or get involved, you can join us in the Tizen IRC chatroom, or subscribe to the Tizen Community mailing list. Lastly, you can also stay tuned to this blog to catch more articles about building things on Tizen and Raspberry Pi.

Have fun!

Raspberry Pi is a trademark of the Raspberry Pi Foundation