Sunday, July 23, 2017

Scan For Bluetooth Enabled iBeacons Via A Raspberry Pi IoT Device

Scan For Bluetooth Enabled iBeacons Via A Raspberry Pi IoT Device



Lately I’ve been working a lot with iBeacons and Raspberry Pi Internet of Things (IoT) devices.  I’ve been building an office monitor for logging foot traffic around the office.  I planted a few Raspberry Pi devices around the office and distributed some Gimbal iBeacons to a few of my coworkers so I could determine what part of the office experiences the most heat.
While Raspberry Pi 3 devices come with integrated bluetooth, Raspbian Linux is not configured to support BLE scanning.  There are a few packages that must be installed to make BLE scanning possible.
We’re going to see what is necessary to scan for BLE iBeacons using a Raspberry Pi with Raspbian Linux installed.
To be clear, you can actually scan for bluetooth devices with the latest version of Raspbian, but it isn’t enough out of the box.  Instead, we need to install a few things.
We’re going to be using Raspbian Linux.  If you don’t already have it set up, check out a tutorial I wrote previously on the subject.  Assuming your Raspberry Pi is already up and running with Raspbian, execute the following:
BlueZ is a very popular Linux bluetooth stack and it includes tools like hcitool and hcidump.  The goal with these tools is to scan for LE data in the background and dump the data into a script.
With the appropriate tools installed, iBeacons can be scanned and dumped like the following:
The above commands are only a part of the puzzle.  The data dumped by hcidump isn’t anything nice to work with.  Instead, we can take a look at a very popular parsing tool released by Radius Networks.
If you downloaded the tool, execute it like the following:
The script will output the UUID, major, minor and power for any iBeacon that was found.  I’m personally using the Radius Networks script to pipe the iBeacon information into another application that will record it.

Conclusion

With the price of iBeacons and Raspberry Pi devices being so low, it makes developing with the two more enjoyable.  To scan for iBeacon devices with a Raspberry Pi, you need the BlueZ tool set and the convenient Radius Networks command line script.  There are many other ways to scan for proximity beacons on a Raspberry Pi, for example Python, but that is a story best saved for another day.

Creating ISO images from a folder in OSX – Matt Berther

Creating ISO images from a folder in OSX – Matt Berther



Yet one other thing I love about OSX is that .iso is a first-class citizen. I'm able to mount/burn iso files without installing any other software (yes, I'm looking at you Windows).
Recently, I had a group of files and folders that I have been transporting around. I had been looking for a way to group these into an ISO so 1) they'd be easier to transport and 2) I could burn them to a CD easier. What I did not know was that with the hdiutil terminal app, I am also able to create an ISO of a folder in OSX without any additional tools.
The syntax is pretty simple:
hdiutil makehybrid -o ~/Desktop/image.iso ~/path/to/folder/to/be/converted -iso -joliet
Mission accomplished.

Tuesday, July 11, 2017

Ionic Camera Example, Tutorial

Ionic - Cordova Camera


10 Minutes with Ionic 2: Using the Camera with Ionic Native
Cordova InAppBrowser and Camera

In Android, inside InAppBrowser will only open file chooser without option of take photo from camera. This has been discussed at HTML File Input should support accept/source attributes

The reason camera option not available is "This issue is low priority because it's an extremely complicated issue to implement and test, and we already have plugins like Camera that work better than this method."


But I didn't get the patch work and I didn't see anyone get it work.

The following ideas/methods have been tried and failed.

1,  Use InAppBrowser executeScript to interact with web page






2,  Use 3rd party plugins


this is an old plugin, the fixes in the plugin already in latest inappbrowser, and the fix doesn't include having camera option in Android


Here's working example outside of InAppBrowser


Unfortunately this only works outside of InAppBrowser. I guess to use this, the default behaviour of trigger 'file chooser' need to be intercepted.








BTW
to customize a plugin, you need modify the plugin file then build again.

ionic cordova build android

same as modify the cordova core src files

Saturday, July 01, 2017

SSH to your Raspberry Pi via Bluetooth

If you like me, just want to be able to setup the Raspberry Pi's WiFi without connecting to any monitor, keyboard etc in a new environment. This tutorial is for you.

All credits go to  Patrick Hundal

Reference: Headless Raspberry Pi configuration over Bluetooth
Reference backup: Headless Raspberry Pi configuration over Bluetooth

All you need to do, is download PiBakery then make your recipe (image below is the recipe), write to your SD card, power on your Raspberry Pi, wait for a while, pair the Raspberry Pi in nearby bluetooth device list, then run command "$ screen /dev/cu.raspberrypi-SerialPort 115200"

Tips: apt-get update and apt-get upgrade will fail in bluetooth ssh.

(the wget command in the recipe is: wget "http://termbin.com/kibz" -O /home/pi/btserial.sh )


Headless Raspberry Pi configuration over Bluetooth ★ Mozilla Hacks – the Web developer blog

Headless Raspberry Pi configuration over Bluetooth ★ Mozilla Hacks – the Web developer blog



With the advent of the newer Raspberry Pi 3 (RPi) with built-in Bluetooth, there are now new options for getting connected to the console of the RPi, without the headache of having to dig up a monitor and keyboard (much less a serial cable with pinouts).
This is especially advantageous when running workshops and hackathons, where connectivity can become problematic. In fact, this specific hack came about from a recent Mozilla hackathon in Berlin, where we realized that we had 10-15 RPi for participants, but not a single monitor, keyboard, serial cable, or other means to connect to the RPis. So it raised the question – why couldn’t we use Bluetooth (BT) as a serial connection to get to the console?
Unlike other tutorials on the web, which assume you already have some console access established, I’ve provided steps which let you do this without any previous connection! To accomplish this, we’ll modify a base image of Raspian-Jesse to allow the RPi to boot into a mode where you can connect via Bluetooth and establish a “virtual” serial connection on startup.

Why is this awesome?

Lot of reasons:
  • No cables, monitors, or keyboards required.
  • It lets you work directly on the RPi when resources like wireless access points or wired switches are not available.
  • It gives you an easy way to get a second screen for viewing logs when testing the primary networking interfaces.
  • Or how about this common problem — you set up wifi for DHCP, but can’t find the machine on your network? Rather than using a tool like Pi Finder after the fact, you can just use your Bluetooth terminal session to enable wifi and then right after, run an ifconfig to get the IP.
  • Last but not least, it is great for multi-RPi hackathons — you’ll come across a number of problems in such circumstances, like too many devices with the same hostname, or ssh ports blocked on event space wifi.
It’s brilliant, fast and simple.  It is a little insecure, but we’ll also provide some instructions on how to solve this problem near the end of the article.
There are several useful internet resources that helped us in putting this together, but in particular, kudos to Dr. Rowland for this helpful script.

Preparing an RPi image for editing

Here’s how to get started.
  1. Download the latest Raspian Jessie image.
  2. Mount the image for editing.
Note: We are after the EXT4 partition, which contains the files we want to edit for startup.

Mounting on Mac

This proved challenging, as OSX doesn’t support EXT4 natively.  This limitation left two options; we felt more compelled to do the second, but the first is probably quicker.

1. Install native Ext4 drivers

This option involves installing EXT4 drivers into OSX (see https://osxfuse.github.io/) and read the .img file natively. These are not recommended for writing to EXT4 partitions, so use with caution.

2. Use a virtual machine

Here we will install Virtual Box, and then create a virtual machine of Linux.  We then operate in the VM to natively mount the .img of Raspian-Jessie through a shared folder.
  1. First, download and install VirtualBox.
  2. Next, download and install Mint Linux in a virtual machine under Virtual Box:
  3. In the Linux virtual machine, put the image in a shared folder (visible to both the VM and your desktop) and then follow the instructions in the link in the “Mounting on Linux” section below to mount the .img file under Linux and edit the image.

Mounting on Linux

Mounting EXT4 on Linux is a simple, well-documented process: see How can I mount a Raspberry Pi Linux distro image?

Mounting on Windows

This is a fairly simple process, as EXT4 is supported natively by Windows. For a simple guide on how to do this, see Can I view/copy the contents of an img file from Windows?

Editing the image

Once you have the image mounted, and you are at the command prompt, you can start editing the configuration files necessary to get us going.
Let’s start by creating the main script that will set up and establish the default Bluetooth services and serial port you will connect to on startup.
You’ll create this file in the /home/pi directory, like so:
$ sudo nano /home/pi/btserial.sh
Add the following lines to the script:
#!/bin/bash -e

#Edit the display name of the RaspberryPi so you can distinguish
#your unit from others in the Bluetooth console
#(very useful in a class setting)

echo PRETTY_HOSTNAME=raspberrypi > /etc/machine-info

# Edit /lib/systemd/system/bluetooth.service to enable BT services
sudo sed -i: 's|^Exec.*toothd$| \
ExecStart=/usr/lib/bluetooth/bluetoothd -C \
ExecStartPost=/usr/bin/sdptool add SP \
ExecStartPost=/bin/hciconfig hci0 piscan \
|g' /lib/systemd/system/bluetooth.service

# create /etc/systemd/system/rfcomm.service to enable 
# the Bluetooth serial port from systemctl
sudo cat <<EOF | sudo tee /etc/systemd/system/rfcomm.service > /dev/null
[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service

[Service]
ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 -a pi

[Install]
WantedBy=multi-user.target
EOF

# enable the new rfcomm service
sudo systemctl enable rfcomm

# start the rfcomm service
sudo systemctl restart rfcomm
Save the file, and then make it executable by updating its permissions like so:
$ chmod 755 /home/pi/btserial.sh
Now you have the basics of the script required to turn on the Bluetooth service and configure it.  But to do this 100% headless, you’ll need to run this new script on startup. Let’s edit /etc/rc.local to launch this script automatically.
$ sudo nano /etc/rc.local
Add the following lines after the initial comments:
#Launch bluetooth service startup script /home/pi/btserial.sh
sudo /home/pi/btserial.sh &
Save the rc.local script, unmount the image, and write it to an SD Card using your favorite tool (mine is ApplePiBaker).
Now you are ready to go.  Plug in power to the Rpi and give it 30 seconds or so to startup. Then unplug it, and plug it in again and let it boot up a second time.  Restarting the Bluetooth service doesn’t work correctly, so we need to reboot.
Now let’s connect.

Connecting to your RPi via Bluetooth

On your desktop/laptop, open up your Bluetooth preferences and ensure Bluetooth is enabled.
Select “raspberrypi” (or whatever you have used for PRETTY_HOSTNAME in the btserial.sh script) when it appears and pair with it. It should pair automatically (remember what we said earlier about security issues?)
Open a terminal window on your local machine, and start a screen session to connect via the new Bluetooth serial port created from the RPi connection. First let’s check the name of the serial connection:
$ ls /dev/cu.*
This should produce a list of available serial ports, one of which should now be named after your pi. Then we can connect.
$ screen /dev/cu.raspberrypi-SerialPort 115200
Give it a second,  and you should be at the prompt of the RPi console!  Congrats!

Further tips

Don’t stop reading yet — this section contains some useful tips about using the RPi terminal, including making it more secure.

Interactive terminal [optional]

After your initial amazement has subsided, you’ll realize your terminal window size isn’t reflected in the output from the serial session.  You can adjust this in one of two ways.

Manual resizing

Simply note the size of your current terminal window , and enter the command:
stty rows XX cols YY
replacing XX and YY with the size of your local terminal window

Automatic (dynamic) resizing

If you want your terminal session to be responsive when you change your local terminal window size, you’ll need to do the following:
Start up sshd, which is off by default.  From the command prompt, run:
$ sudo raspi-config
Now:
  1. Select “Advanced Config”
  2. Select “SSH”
  3. Select “Activate”
  4. Exit raspi config
With SSH active, you can now enter the following commands:
$ su pi -
$ ssh localhost
Your terminal window will now resize dynamically.

Security [optional, but not really…]

If you are on a deserted island, far away from humanity, you can stop now.  If not, you should be concerned that this connection is wide open for others to connect to and use.  To make this a little more secure, I would recommend turning off Bluetooth discoverability and pairing once you have connected to your RPi.
In the console on the RPi, run the following commands:
$sudo bluetoothctl
[bluetooth]#
Turn off discoverability:
[bluetooth]#discoverable no
Turn off pairing:
[bluetooth]#pairing no
And exit bluetoothctl:
[bluetooth]#quit
$
At this point you should be good to go with at least some small security measures in place.  If the machine restarts, you'll need to follow these security steps again when you reconnect.

Summary

So there you have it — you should now have set up your own working RPi console over Bluetooth, configured entirely headlessly. You should now be able to go forth and work on your own RPi projects, alongside others, with the minimum of connectivity issues.
If you run into problems getting this to work, or have any tips of your own to share, please do so in the comments.


Special thanks to Florian Merz for his technical review of this post.