Skip to content

Instantly share code, notes, and snippets.

@berikv
Last active May 1, 2019 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berikv/582f5a7e87010d7cf4069c9fd167f6af to your computer and use it in GitHub Desktop.
Save berikv/582f5a7e87010d7cf4069c9fd167f6af to your computer and use it in GitHub Desktop.
Setup headless raspberry pi from scratch
1) `sudo apt-get install git-core`
Setup git remote
2) `scp -r <your project> <your username>@<ip of raspberry pi>:`
3) `ssh <your username>@<ip of raspberry pi>:`
4) `cd <your project>`
5) `git config --bool core.bare true`
6) Go back to your local machine (Ctrl-D)
7) In you project dir, run `git remote add rp <ip of raspberry pi>:<your project>`
8) Make a change in your project
9) `git add .`
10) `git commit -m"some commit message"`
11) `git push`
  1. Download date-raspbian-stretch-lite.img https://www.raspberrypi.org/downloads/raspbian/
  2. Extract the zip, double click the .img to mount
  3. Open terminal, run cd /Volumes/boot
  4. Run touch ssh
  5. Run
cat << EOF > wpa_supplicant.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=<NL>

network={
	scan_ssid=1
	ssid="<NETWORK NAME>"
	psk="<NETWROK PASSWORD>"
}
EOF
  1. Find a free ip address on your network

  2. Unmount boot

  3. Plug in your sd card

  4. Use diskutil list to find the disk number

  5. sudo dd if=/path/to/date-raspbian-stretch-lite.img of=/dev/disk2 bs=1m conv=sync (replace /dev/disk2 with the right one from diskutil list)

  6. Wait (~10 minutes) until the process finished (use Ctrl-T to see progress)

  7. Unmount boot

  8. Plug sd card into raspberry (make sure the power is off)

  9. Turn on raspberry and wait for it to come online

  10. Find its IP (eg. By pinging the broadcast address ping 192.168.1.255 then use arp -a to see potential devices)

  11. ssh pi@<ip.address.of.raspberry> (password is 'raspberry')

  12. sudo adduser <yourusername>

  13. sudo usermod -a -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,input,netdev,gpio,i2c,spi <yourusername>

  14. logout (Ctrl-D)

  15. ssh <yourusername>@<ip.address.of.raspberry>

  16. sudo userdel pi

  17. [ -f ~/.ssh/id_rsa.pub ] && ssh-keygen -t dsa -f ~/.ssh/id_rsa.pub

  18. cat ~/.ssh/id_rsa.pub | ssh <yourusername>@<ip.address.of.raspberry> "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"

  19. sudo apt-get update

  20. sudo apt-get dist-upgrade

  1. sudo apt-get install vim npm
  2. sudo npm install -g homebridge homebridge-nest
  3. sudo su -
  4. These steps are based on https://gist.github.com/johannrichard/0ad0de1feb6adb9eb61a/
cat << EOF > /etc/default/homebridge 
# The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
HOMEBRIDGE_OPTS=-U /var/lib/homebridge

# If you uncomment the following line, homebridge will log more 
# You can display this via systemd's journalctl: journalctl -f -u homebridge
# DEBUG=*
EOF
cat << EOF > /etc/systemd/system/homebridge.service
[Unit]
Description=Node.js HomeKit Server 
After=syslog.target network-online.target

[Service]
Type=simple
User=homebridge
EnvironmentFile=/etc/default/homebridge
# Adapt this to your specific setup (could be /usr/bin/homebridge, check with `which homebridge`)
# See comments below for more information
ExecStart=/usr/local/bin/homebridge $HOMEBRIDGE_OPTS
Restart=on-failure
RestartSec=10
KillMode=process

[Install]
WantedBy=multi-user.target
EOF
  1. useradd -M --system homebridge
  2. mkdir /var/lib/homebridge
  3. touch /var/lib/homebridge/config.json
  4. chown -R homebridge:homebridge /var/lib/homebridge
  5. systemctl daemon-reload
  6. systemctl enable homebridge
  7. systemctl start homebridge
  8. Log out of root (Ctrl-D)
  9. journalctl -u homebridge

Allow node to access bluetooth (needs to be done for every reboot)

  1. sudo setcap cap_net_raw+eip $(eval readlink -f `which node`)
  2. sudo apt-get install bluetooth bluez libbluetooth-dev libudev-dev
  3. sudo npm install -g bluetooth-hci-socket

Connect from the raspberry pi to a HM-10. In my case the HM-10 is connected to an arduino. The arduino runs a program that reads and writes on the serial port of the HM-10. These commands test the connection.

raspberrypi:~ $ sudo bluetoothctl
[bluetooth]# scan on
[NEW] Device 1C:B2:B0:72:2B:E1 1C-B2-B0-72-2B-E1
[CHG] Device 34:15:13:87:E1:91 RSSI: -56
[CHG] Device 34:15:13:87:E1:91 TxPower: 0
[NEW] Device C8:69:CD:2A:5F:6D C8-69-CD-2A-5F-6D
[NEW] Device 51:B4:41:02:72:99 51-B4-41-02-72-99
...
[bluetooth]# scan off
[bluetooth]# connect 34:15:13:87:E1:91 
[HMSoft]# info 34:15:13:87:E1:91 
[HMSoft]# list-attributes 34:15:13:87:E1:91 
Primary Service
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service0010
	0000ffe0-0000-1000-8000-00805f9b34fb
	Unknown
Characteristic
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011
	0000ffe1-0000-1000-8000-00805f9b34fb
	Unknown
Descriptor
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011/desc0014
	00002901-0000-1000-8000-00805f9b34fb
	Characteristic User Description
Descriptor
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011/desc0013
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
Primary Service
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service000c
	00001801-0000-1000-8000-00805f9b34fb
	Generic Attribute Profile
Characteristic
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service000c/char000d
	00002a05-0000-1000-8000-00805f9b34fb
	Service Changed
Descriptor
	/org/bluez/hci0/dev_34_15_13_87_E1_91/service000c/char000d/desc000f
	00002902-0000-1000-8000-00805f9b34fb
	Client Characteristic Configuration
[HMSoft]# select-attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011
[HMSoft:/service0010/char0011]# notify on
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Notifying: yes
Notify started
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Value: 0x3e
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Value: 0x43
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Value: 0x30
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Value: 0x30
[CHG] Attribute /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011 Value: 0x54
...
[HMSoft:/service0010/char0011]# notify off
[HMSoft:/service0010/char0011]# write 0x3E 0x30 0x30
Attempting to write /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011
[HMSoft:/service0010/char0011]# write 0x3E 0x46 0x46
Attempting to write /org/bluez/hci0/dev_34_15_13_87_E1_91/service0010/char0011
[HMSoft:/service0010/char0011]# disconnect 34:15:13:87:E1:91
[bluetooth]# exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment