Raspberry Pi ZeroW: Easily build Apple’s HomeBridge with a data cable

Category: Tag:

This article mainly introduces: Only one data cable and one SD card can be easily completed homebridge installation. (Because I can’t afford accessories, so here I use homebridge to control the switch of the software on the remote php server, the principle is actually similar)

First download the raspbian mirror and balenaEtcher tool.

Of course, you can also choose win32diskimager, but after I have used balenaEtcher, I still think balenaEtcher is really simple and easy to use.

Raspbian download link: https://www.raspberrypi.org/downloads/raspbian/

balenaEtcher official download link: https://www.balena.io/etcher/

Raspbian system, because the desktop is not needed later, I use “Raspbian Stretch Lite” here, do not unzip it after downloading it.

1. Insert the SD card into the reader and insert it into the computer, and then open balenaEtcher (it is slow when it opens), click Selectimage, select the raspbian mirror you just downloaded, click Flash, wait for the flashing and verification to complete, and it displays “FlashComplete!” The flashing is complete.

2. Pull out the card reader and plug it into the computer again, a partition boot will appear.

Modify the config.txt file in the boot partition, and add a new line below the last line: dtoverlay=dwc2

Modify the cmdline.txt file in the boot partition, find the word rootwait, and add after it: modules-load=dwc2,g_ether

Note that each parameter is separated by a space, and they are all on the same line.

Note: More information about dtoverlay can be found in the README in the boot/overlay folder.

Create a text file in the root directory of the boot partition, and then rename it to ssh. Be careful to remove the .txt suffix, and then you can start SSH login (changes to the new version of Raspbian). SSH is disabled by default, so you need to create a file named ssh in the boot partition to enable the SSH service.

Note: This ssh file is easy to lose. If ssh cannot log in, first check whether ssh is lost.

3. Insert the TF card, and connect the Micro USB cable to the Zero W USB port. If it is normal, the computer will install the relevant driver and display a new network card.

Wait for the driver installation to complete, open “Device Manager -> Network Adapter” and check if there is a device similar to “USB Ethernet/RNDIS Gadget”,

If yes, skip the following steps, if not, you need to manually install the RNDIS/Ethernet Gadget driver.

Since I am here with WIN10, WIN10 x64 users can download the driver here, users of other systems please install “RNDIS/Ethernet Gadget Driver” on google.

Specifically how to install, after downloading and decompressing, there will be a driver installation method inside.

After successful installation, “RNDIS/Ethernet Gadget” will appear in “Device Manager -> Network Adapter”! ! !

4. Next, use putty to link the Raspberry Pi, enter raspberrypi.local in the Host Name and click Open:

 

In the pop-up window, enter the default user name pi of the Raspberry Pi, then press Enter and enter the default password raspberry:

If this interface appears, it means the login is successful.

5. Configure the WIFI link to connect the Raspberry Pi to WIFI to expand the SD card space.

enter:

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf    

Insert at the bottom (note: change to your own WIFI name and WIFI password below)

network={
         ssid="Your WIFI name"
          psk="WIFI password"
        }

When you are done, press Ctrl+x, then enter y, and then press Enter.

Then use raspi-config to expand the SD card space.

enter:

sudo raspi-config

Select Advanced Options->Expand Filesystem->Finsh->OK, then the Raspberry Pi will restart, wait a while (about 2 minutes), and then connect to the Raspberry Pi again. The specific steps are the same as above:

6. Modify the default source of Raspberry Pi, install and configure HomeBridge.

enter:

sudo nano /etc/apt/sources.list

Comment out the original source inside, add a # in front, and insert at the bottom:

deb http://mirrors.aliyun.com/raspbian/raspbian/ stretch main non-free contrib
deb-src http://mirrors.aliyun.com/raspbian/raspbian/ stretch main non-free contrib

When you are done, press Ctrl+x, then enter y, and then press Enter:

Then enter them one by one (Why do you need to perform two updates, I don’t know, just do it)

sudo apt-get update

sudo apt-get upgrade

sudo apt-get update

sudo apt-get install npm

sudo apt-get install git-core

sudo apt-get install libnss-mdns

sudo apt-get install libavahi-compat-libdnssd-dev

 

Note: It is recommended to install separately one by one, so that if something goes wrong, it is convenient to check the problem.

Then install Node. It is recommended not to use apt-get to install. Please go to the official website of Node to choose ARMv6 (many people make mistakes because of choosing ARMv7).

Node download address: https://nodejs.org/zh-cn/download/

Right-click to copy the download link of ARMv6, and then run wget in the Raspberry Pi to blank the link you copied, similar to the following:

wget https://nodejs.org/dist/v10.16.0/node-v10.16.0-linux-armv6l.tar.xz

Then unzip, install HomeBridge, configure HomeBridge:

xz -d node-v10.16.0-linux-armv6l.tar.xz

tar -xvf node-v10.16.0-linux-armv6l.tar

cd node-v10.16.0-linux-armv6l

sudo cp -R * /usr/local/

sudo npm install -g –unsafe-perm homebridge

 

Install a Homebridge plugin (homebridge-http-switch)

sudo npm install -g homebridge-http-switch

The homebridge-http-switch is a switch function, here is just for demonstration.

Note: For more plug-ins, you can search for “homebridge-plugin” on the npm official website. There are many plug-ins with various functions and usage methods, which are very detailed.

Configure HomeBridge

sudo nano /root/.homebridge/config.json    

Then enter:

{
    "bridge": {
        "name": "HomeBridge",
        "username": "B9:36:DA:93:30:20",
        "port": 51826,
        "pin": "031-45-166"
    },
    "description": "Bla Bla Bla",
    "accessories": [{
          "accessory": "HTTP-SWITCH",
          "name": "Switch",
          "switchType": "stateful",
          "onUrl": "http://xxx.com/On.php",
          "offUrl": "http://xxx.com/Off.php",
          "statusUrl": "http://xxx.com/Status.php"
    }],
    "platforms": []
}

When you are done, press Ctrl+x, then enter y, and then press Enter.

Note: The onUrl, offUrl, and statusUrl here need to be configured by yourself. Below I have made an example of switching the software on the PHP server (sorry, I am too poor to afford accessories).

Because I tried many methods before, but I couldn’t start the software in the foreground of the server. It can only be run as the www user. Finally, I found that it can be achieved with a scheduled task. First, create a scheduled task on your server (note: the following code is in your Run with administrator privileges on the cmd of the server):

schtasks /create /tn "homerun" /tr "c:\windows\system\calc.exe" /sc daily /st 00:01    

On.php

<?php
exec('schtasks /run /TN "homerun"');
echo '{"statusPattern":"true"}';
?>

Off.php

<?php
exec('taskkill /f /im "calc.exe"');
echo '{"statusPattern":"true"}'
?>

Status.php

<?php
$res=exec('tasklist | find /i "calc.exe"');
$res=(string)stripos($res,"calc.exe");
if($res!==""){
  $res="1";
}else{
  $res="0";
}
echo $res;
?>

Note (bridge): The username here can be set to the mac address of your Raspberry Pi (I tried to change it to something else and it’s okay), the port does not need to be changed, the pin is the pin code that will be used when linking later. modify

Note (accessories): The onUrl here means that the Raspberry Pi will visit this link when you click to open it, and offUrl means that you will visit this link when you click to close it, and the statusUrl is the status link (statusUrl returns 0 to close and 1 to open). Here: https://www.npmjs.com/package/homebridge-http-switch

 

7. Connect HomeBridge and set HomeBridge to start automatically after booting.

enter:

sudo homebridge

Then open the “Family” on the phone (if you deleted it before, go to the Appstore to install it), click on the + sign in the upper right corner -> add accessories -> scan code for connection:

 

8. Configure HomeBridge to start automatically after booting.

enter:

sudo nano /etc/rc.local

Add a line of code before exit 0:

sudo nohup homebridge > /home/pi/homebridge.log 2>&1 &

When you are done, press Ctrl+x, then enter y, and then press Enter.

After that, every time you turn off the Raspberry Pi and then turn it on again, Homebridge will automatically start! ! !

Note: Raspberry Pi and mobile phone must be under the same WIFI, otherwise the device will not be found in the “home”.

This is the basic Homebridge installation. In the next article, I plan to turn it into a smart color-changing Homekit lamp.

Here are some links you may need to use:

Raspbian download link: https://www.raspberrypi.org/downloads/raspbian/

balenaEtcher official download link: https://www.balena.io/etcher/

Node download address: https://nodejs.org/zh-cn/download/

NPM official homebridge plugin list: https://www.npmjs.com/search?q=homebridge-plugin

README of HomeBridge: https://github.com/nfarina/homebridge/blob/master/README.md

Reviews

There are no reviews yet.

Be the first to review “Raspberry Pi ZeroW: Easily build Apple’s HomeBridge with a data cable”

Your email address will not be published. Required fields are marked *