DEV Community

Saifur Rahman Mohsin
Saifur Rahman Mohsin

Posted on

Enabling my IoT devices on Apple Watch

I recently purchased an Apple Watch Ultra, which is an excellent watch if you love swimming, scuba diving, hiking, adventure and other sports. Besides those, it's also a regular watch with the entire Apple eco-system. In my house, I use a bunch of IoT devices to make my life easy i.e. geysers, lights, roomba, etc. which has some automation and control from wherever. So far, I have been using them thro' Alexa and Google Home, which are supported by these devices. However, none of them support Apple HomeKit which means I cannot ask Siri to do this... do that. This is unfortunate because I want to be able to do that thro' the Apple eco-system. I have 2 options:

  1. Run Wireshark and collect traffic while using these devices, document the protocol/API calls and then re-create them as a native Watch app.

  2. Use HomeBridge and install it on my Raspberry Pi Zero W and that acts as a bridge to allow my devices to be seen on Apple Home App, which currently is configured to shout "Cat detected" through Alexa using a webhook when it's camera detects a cat walking by (long story!).

I went with 2, as it's easier and simpler to implement and I already have everything I need. Here are the instructions for anyone who would like to re-create it.

First, go through the usual installation of RaspianOS on your Pi device (I use lite because it's faster on my Pi Zero W which has limited resources):

  • Find the device identifier of the connected SD card using:
diskutil list
Enter fullscreen mode Exit fullscreen mode

Mine was /dev/disk4 and then burn the image into the device using:

sudo gdd if=2022-09-22-raspios-bullseye-armhf-lite.img of=/dev/disk4 bs=64M status=progress
Enter fullscreen mode Exit fullscreen mode

As you can see, the file ends with .img so I had already extracted the .xz file using macOS native Archive Utility. If not, you would have to do that.

Possible errors: If the gdd command throws an error:

  1. gdd: failed to open '/dev/disk4': Resource busy Then you need to unmount the SD card first using diskutil:
diskutil unmountDisk /dev/disk4
Enter fullscreen mode Exit fullscreen mode
  1. gdd: failed to open '/dev/disk4': Operation not permitted The other alternative is a locked SD card in which case you need to confirm that it's not locked.

And then it takes a while to burn the OS into the card. I usually run caffeinate -dismut 65535 and set my brightness to 0 and take a break.

Finally, I create a wpa_supplicant.conf in the root of the pen drive where the image was written with my WiFi details, like:

country=IN
update_config=1
ctrl_interface=/var/run/wpa_supplicant

network={
 scan_ssid=1
 ssid="MyWiFiSSID"
 psk="MyWifiPassword"
}
Enter fullscreen mode Exit fullscreen mode

I also drop an empty file with filename as ssh, that enables SSH on the Pi during the first boot. One of the annoying things about RaspianOS is that if you mess up this last part and boot it up once without a wpa_supplicant.conf or ssh, you have to burn the image again from scratch as these files are taken in during first-boot only!

Of course, in the past, I've gotten annoyed with this to the point where I used a mini-HDMI to HDMI cable along with a USB keyboard to connect to it, then use raspi-config to change the config from the inside but that's a lot of work for me esp. moving monitors, using USB converters, etc.

Anyway, once it's completed. You can stick the SD into the PI and then boot it. It will connect to the Wifi with it's default hostname raspberrypi.local. This is useful to connect to it later without looking up the IP address.

Then I open a terminal on my mac and connect like:

ssh pi@raspberrypi.local
Enter fullscreen mode Exit fullscreen mode

I believe the default password is either raspberry or alpine. I usually setup a custom username which you can by specifying the username and hashed password during the initial setup.

Installing Homebridge

Now for the fun part. Well, there's already good instructions here and now that you are ssh-ed into the pi, you just have to run those commands and follow the instructions. Finally, open your browser and paste in the URL in the format shown in the wiki or just put http://raspberrypi.local:8581 as it will resolve the hostname and open it too. It should open HomeBridge's Web UI.

Now open the Home app on your iPhone and click Add Accessory. It will open a scanner and you can click on the HomeBridge status page to find a big QR code for "pairing" on the left sidebar. Scan that code and it will set up the Homebridge Hub on your phone (make sure you're on the same Wi-Fi network). Once it's setup, it will appear not on the accessory list but inside Home Settings -> Home Hubs & Bridges of the Settings section.

Now you have to manually add the plugins that target the IoT devices at your home. So the way this works is that home bridge acts as an interface to connect those IoT devices to Apple's Home app. Those plugins are generally written targeting each IoT manufacturer and their protocols/APIs to interact with the device in it's format and homebridge converts it so it uses HomeKit APIs.

For example, in my case I have a Xiaomi Vaacuum Roomba at home so I installed the homebridge-miot plugin to enable it to be discovered. Ensure you see a verified badge next to the plugin listing for "official" plugins, that are more trustable. Once I set it up with my Mi Cloud Account ID and password, it detected and added my Roomba to the Accessories. Now I can control it via the app, and in extension, my Watch.

Bonus -- Setting up custom username/password

I'm sure some users would want to change the username password to a custom one. If you saw the documentation section on this it would suggest running openssl passwd -6 on the pi system, type in the passwd and then get the credential and use that in the initial text file. This is annoying because the headless pi might not be easy to access initially. I use a microUSB to female-USB cord to connect a keyboard to my pi so I could type keys. Here's what I did to send back the key to my machine.

  1. Generate the key and save it.
openssl passwd -6 > key.txt
Enter fullscreen mode Exit fullscreen mode
  1. Send it online to file.io
curl -F "file=@key.txt" https://file.io
Enter fullscreen mode Exit fullscreen mode

Their service lets you upload stuff from the command line. The returned JSON will have a short URL which you can easily type into your phone and get the key.

Top comments (0)