├── README.md ├── autostart ├── check-wifi.sh ├── config.txt ├── crontab └── node-server.service /README.md: -------------------------------------------------------------------------------- 1 | # Raspberry Pi - Kiosk Mode 2 | 3 | A collection of scripts that can be used to quickly enable a raspberry pi to boot up as a stand alone kiosk. If you're wanting to run a local server, you will need to follow all of these steps. If you want to boot up and hit a public url, you can skip the Node7 install. The benefit of running your app locally is you can have a custom error screen if the internet connection drops out. 4 | 5 | To make this work I was using the January 2017 version of Raspian Jessie with Pixel and a Rasberry Pi 2. 6 | 7 | ## SD Formatter for Mac 8 | To format an SD card using the FAT format you will need SDFormatter, as Disk Utility cannot do this by default. 9 | https://www.sdcard.org/downloads/formatter_4/eula_mac/index.html 10 | 11 | ## Installing Rasbian 12 | To install rasbian on your SD card, download the disk image from https://www.raspberrypi.org/downloads/raspbian/ and unzip it. It will contain a .img file, make a note of the path. If you don't know the path, you can find it by dragging the downloaded .img file into the terminal. This will show as something similar to ~/users/username/Downloads/rasbian.img. Replace [PATH_TO_.img] with your actual path in the next command. 13 | 14 | You will also need to know the identifier for your SD card slot in your Mac. To find this, click the Apple icon in the top left, and choose 'About This Mac'. Then choose 'System Report'. Then choose 'Card Reader' from the sidebar. This will give you the identifier, such as 'disk2'. Replace [DSK] with your disk identifier in the next command. 15 | 16 | Now, from your terminal type: 17 | ``` 18 | sudo dd bs=1m if=[PATH_TO_.img] of=/dev/[DSK] 19 | ``` 20 | 21 | This will install Rasbian to the SD card. This may take some time. 22 | 23 | ## Installing Node 7 24 | 25 | By default, Rasbian ships with Node 0.10, this can be verified by typing `node -v` into the terminal. The next steps will remove this and install Node7. 26 | 27 | First, remove Node and NPM. 28 | ``` 29 | sudo apt-get remove nodejs 30 | ``` 31 | ``` 32 | sudo apt-get remove npm 33 | ``` 34 | 35 | Next, update the package manager. 36 | ``` 37 | sudo apt update 38 | ``` 39 | The apt update command doesn't actually update anything, it just downloads the most up to date packages. So to install them, run: 40 | ``` 41 | sudo apt full-upgrade 42 | ``` 43 | 44 | Once the package manager is up to date, you can go ahead and download Node7 (woop), followed by the install command. 45 | ``` 46 | curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash - 47 | ``` 48 | ``` 49 | sudo apt-get install nodejs 50 | ``` 51 | To verify your node version type: 52 | ``` 53 | node -v 54 | ``` 55 | 56 | ## Running a node server on startup 57 | 58 | Before we can run the Node server you will need to have the application installed on your Pi. Presuming you have your project on github already, you can get its https address and clone it into your Pi's home folder using the following command, note that your_username and some_awesome_project will be your actual username and project name. 59 | ``` 60 | git clone https://github.com/your_username/some_awesome_project.git:/home/pi 61 | ``` 62 | 63 | Next, change directory into that folder using the following commands to install all of your projects dependencies, again replacing some_awesome_project with your projects actual name. 64 | ``` 65 | cd /home/pi/some_awesome_project 66 | ``` 67 | ``` 68 | npm install 69 | ``` 70 | 71 | Now, make sure everything is working by starting up your Node server. Usually this is the command `npm start` but it may be `node server`. 72 | ``` 73 | npm start 74 | ``` 75 | 76 | Once your server is running, you can go ahead and create a server file. Create this in the systemd directory using the following command: 77 | ``` 78 | sudo nano /etc/systemd/system/node-server.service 79 | ``` 80 | Then add the following code replacing the path to some_awesome_project in the WorkingDirectory and ExecStart lines with the path to your Node application. The Exec start line in the example is the equivalent to running the command `node server.js` so you may need to change this to `ExecStart=/usr/bin/npm start` if you start your project with the `npm start` command. 81 | ``` 82 | [Service] 83 | WorkingDirectory=/home/pi/some_awesome_project 84 | ExecStart=/usr/local/bin/node --expose-gc /home/pi/some_awesome_project/server.js 85 | Restart=always 86 | StandardOutput=syslog 87 | StandardError=syslog 88 | SyslogIdentifier=nodeServer 89 | User=root 90 | Group=root 91 | Environment=NODE_ENV=production 92 | 93 | [Install] 94 | WantedBy=multi-user.target 95 | ``` 96 | Exit nano with `ctrl + x` and press `Y` to save the file. You can then activate the system file with the following command: 97 | ``` 98 | sudo systemctl enable node-server 99 | ``` 100 | 101 | To check that it worked, reboot your Pi using `sudo reboot` and once it has loaded back up, use the browser navigate to the port that your server usually runs on, eg: `http://localhost:3000` and you should see your Node application running. 102 | 103 | ## Booting Chromium into kiosk mode on start up 104 | 105 | The last step in the process is to boot the chromium-browser into kiosk mode to show your Node application full screen. To do this you need to add one line of code to the autostart file. To edit your autostart file, use the following command: 106 | ``` 107 | nano /home/pi/.config/lxsession/LXDE-pi/autostart 108 | ``` 109 | Add the following line to the bottom of the file. The `--kiosk` flag removes the frame and makes it full screen. The `--incognito` means that it doesn't remember sessions, so if you pull the power chord out of your Pi, you won't get a warning next time you boot up Chromium. Remember to change the port to whatever your node server is running on. If you're not running a node server, you can change the http address to any website address. 110 | ``` 111 | @chromium-browser --kiosk --incognito http://localhost:3000 112 | ``` 113 | If you want to remove the mouse pointer you can install unclutter and again add that to the autostart file. Install unclutter using the following command: 114 | ``` 115 | sudo apt-get install unclutter 116 | ``` 117 | Again, open your autostart file: 118 | ``` 119 | nano /home/pi/.config/lxsession/LXDE-pi/autostart 120 | ``` 121 | and add the following line to the bottom: 122 | ``` 123 | @unclutter -idle 0.1 -root 124 | ``` 125 | ## Auto reconnect 126 | 127 | In testing I found that the Wifi can be a bit flakey on the Raspberry Pi. To combat this you can set up a cronjob that periodically checks the wifi and either forces an attemp to reconnect, or reboots the whole Pi itself. After hours of testing, the force attempt works 9 times out of 10, and is a much softer approach, however it does still break and not come back online. The reboot method works all the time, but is obviously more extreme. Setting up a cronjob for each at different time intervals is probably the best way to guarantee uptime when you're not there to keep an eye on it. 128 | 129 | Set up a new bash file with the following command: 130 | ``` 131 | sudo nano /usr/local/bin/check-wifi.sh 132 | ``` 133 | Add the following code. The `ping -c4 8.8.8.8 > /dev/null` line will ping google.com 4 times and then feed into the if statement. If the ping process exit's cleanly then $ should be 0, if it cant access google, it will fail. If it fails, it attempts to reconnect wlan0. 134 | ```bash 135 | #!/bin/bash 136 | 137 | ping -c4 8.8.8.8 > /dev/null 138 | 139 | if [ $? != 0 ] 140 | then 141 | echo "No network connection, restarting wlan0" 142 | /sbin/ifdown 'wlan0' 143 | sleep 5 144 | /sbin/ifup --force 'wlan0' 145 | fi 146 | ``` 147 | 148 | If you want to use the reboot command, then replace the if statement with the following one: 149 | ```bash 150 | if [ $? != 0 ] 151 | then 152 | sudo /sbin/shutdown -r now 153 | fi 154 | ``` 155 | To make sure the right permissions are set to run the cronjob, set them using the following command: 156 | ``` 157 | sudo chmod 775 /usr/local/bin/check-wifi.sh 158 | ``` 159 | Next, open crontab using: 160 | ``` 161 | crontab -e 162 | ``` 163 | Add the following line. The `*/1` means the cronjob will run every 1 minute. It then runs `sudo` and gives the root access to the home directory using `-H` so that it can run the bash script. `> /dev/null 2>&1` is a redirect to dump syslogs and syserrors into a void. 164 | ``` 165 | */1 * * * * /usr/bin/sudo -H /usr/local/bin/check-wifi.sh > /dev/null 2>&1 166 | ``` 167 | 168 | ## Setting timezone 169 | 170 | Sometimes the raspberry pi time can end up out of sync. To fix this, install nptdate with the following command: 171 | 172 | ``` 173 | sudo apt-get install ntpdate 174 | ``` 175 | 176 | Once installed, run the configuration using: 177 | 178 | ``` 179 | sudo raspi-config 180 | ``` 181 | 182 | Choose localisation settings and set yout timezone. Once you reboot the Pi, the time should sync back up. 183 | -------------------------------------------------------------------------------- /autostart: -------------------------------------------------------------------------------- 1 | # sudo apt-get install unclutter 2 | # nano /home/pi/.config/lxsession/LXDE-pi/autostart 3 | 4 | @chromium-browser —kiosk —incognito http://localhost:3000 5 | @unclutter -idle 0.1 -root -------------------------------------------------------------------------------- /check-wifi.sh: -------------------------------------------------------------------------------- 1 | # sudo nano /usr/local/bin/check-wifi.sh 2 | # sudo chmod 775 /usr/local/bin/check-wifi.sh 3 | 4 | ping -c4 8.8.8.8 > /dev/null 5 | 6 | if [ $? != 0 ] 7 | then 8 | echo "No network connection, restarting wlan0" 9 | /sbin/ifdown 'wlan0' 10 | sleep 5 11 | /sbin/ifup --force 'wlan0' 12 | fi 13 | -------------------------------------------------------------------------------- /config.txt: -------------------------------------------------------------------------------- 1 | # sudo nano /boot/config.txt 2 | 3 | hdmi_force_hotplug=1 4 | hdmi_group=2 5 | hdmi_mode=1 6 | hdmi_mode=87 7 | hdmi_cvt 800 480 60 6 0 0 0 8 | ? 9 | start_file=start_x.elf 10 | fixup_file=fixup_x.elf 11 | #gpu_mem=128 12 | -------------------------------------------------------------------------------- /crontab: -------------------------------------------------------------------------------- 1 | # crontab -e 2 | 3 | */1 * * * * /usr/bin/sudo -H /usr/local/bin/check-wifi.sh > /dev/null 2>&1 4 | -------------------------------------------------------------------------------- /node-server.service: -------------------------------------------------------------------------------- 1 | # sudo nano /etc/systemd/system/node-server.service 2 | 3 | [Service] 4 | WorkingDirectory=/path/to/your/repo 5 | ExecStart=/usr/local/bin/node --expose-gc /home/pi/path/to/your/repo/server.js 6 | Restart=always 7 | StandardOutput=syslog 8 | StandardError=syslog 9 | SyslogIdentifier=nodeServer 10 | User=root 11 | Group=root 12 | Environment=NODE_ENV=production 13 | 14 | [Install] 15 | WantedBy=multi-user.target --------------------------------------------------------------------------------