|
| 1 | +<!--- Copyright (c) 2016 Gordon Williams, Pur3 Ltd. See the file LICENSE for copying permission. --> |
| 2 | +Home Automation with Raspberry Pi, MQTT, and Espruino |
| 3 | +===================================================== |
| 4 | + |
| 5 | +<span style="color:red">:warning: **Please view the correctly rendered version of this page at https://www.espruino.com/Home+Automation. Links, lists, videos, search, and other features will not work correctly when viewed on GitHub** :warning:</span> |
| 6 | + |
| 7 | +* KEYWORDS: Home Automation,Home,Node,Node-red,MQTT,Mosquitto,Pi Zero,Zero W,WiFi,Zero W WiFi,Pi 3 WiFi,Headless WiFi,Sonoff |
| 8 | +* USES: EspruinoWiFi,Relay Module,Breadboard |
| 9 | + |
| 10 | +Setting up your Pi |
| 11 | +------------------ |
| 12 | + |
| 13 | +[[http://youtu.be/yXvgVgxXjbM]] |
| 14 | + |
| 15 | +* First, you'll need a Raspberry Pi (ideally a [Zero W](https://www.raspberrypi.org/products/raspberry-pi-zero-w/) or a [Pi 3](https://www.raspberrypi.org/products/raspberry-pi-3-model-b/)) with at least an 8gb SD card. |
| 16 | + |
| 17 | +* Get Raspbian Lite from here: https://www.raspberrypi.org/downloads/raspbian/ |
| 18 | + |
| 19 | +* Follow the instructions for putting Raspbian on an SD card: https://www.raspberrypi.org/documentation/installation/installing-images/README.md |
| 20 | + |
| 21 | +* Once done, we need to set up 'headless' WiFi on the Pi |
| 22 | + |
| 23 | +* Unplug the SD card and plug it back in, and some new drives should appear. |
| 24 | + |
| 25 | +* Open the `boot` drive, and create a new file called `ssh` in it. It can be empty, but it's important that the file has **no** extension like `.txt`. This will enable the SSH Server so we can connect to the Pi wirelessly when it boots. |
| 26 | + |
| 27 | +* If your Pi has WiFi which you want to use, create a file in the same drive called `wpa_supplicant.conf` and put the following text in it, filling in the blanks with your WiFi key and password. |
| 28 | + |
| 29 | +``` |
| 30 | +network={ |
| 31 | + ssid="YourNetworkSSID" |
| 32 | + psk="Your Network's Passphrase" |
| 33 | + key_mgmt=WPA-PSK |
| 34 | + } |
| 35 | +``` |
| 36 | + |
| 37 | +* If you're on Windows, make sure you're using an editor like [Notepad++](https://notepad-plus-plus.org/) that allows you to set the line ending style to **Unix**, not Windows. |
| 38 | + |
| 39 | +* Eject the SD card in the OS, take it out of your PC and stick it in the Pi. Then plug the Pi into a USB phone charger (using the `PWR` USB socket if you're on a Pi Zero W). |
| 40 | + |
| 41 | +* Wait a few minutes until the activity LED stops flashing (sometimes you'll be able to check your router's status page to see if the Pi has connected). |
| 42 | + |
| 43 | +* Now it's time to install an SSH client. If you're on Linux of Mac OS you already have one, but if you're on Windows you'll want to [download and run PuTTY](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html). |
| 44 | + |
| 45 | +* Now connect via SSH to `raspberrypi` (or `raspberrypi.local`) with username `pi`. You can do this with a menu in PuTTY, or on other platforms just open a command prompt and type `ssh pi@raspberrypi`. When asked for a password, type `raspberry`. |
| 46 | + |
| 47 | +Now it's time to make sure the operating system is sized to your SD card. |
| 48 | + |
| 49 | +* Type `sudo raspi-config` |
| 50 | + |
| 51 | +* Use the arrow keys to go to `Advanced Options` and press Enter, then choose `A1 Expand Filesystem`. You can also change the password and default hostname (that's currently `raspberrypi`) here, but I won't cover that. |
| 52 | + |
| 53 | +* After that, press the right arrow twice to select `Finish`, then press `Enter` |
| 54 | + |
| 55 | +* Press `Yes` to reboot |
| 56 | + |
| 57 | +* Now wait a minute, then reconnect with `pi` at `raspberrypi` again. |
| 58 | + |
| 59 | +* Make sure all the package lists are up to date by typing `sudo apt-get update` |
| 60 | + |
| 61 | +* Type `sudo apt-get install mosquitto mosquitto-clients bluetooth bluez libbluetooth-dev libudev-dev` and press enter when prompted to install some packages we'll need - particularly the mosquitto MQTT server |
| 62 | + |
| 63 | +* Copy and paste this command to install the latest version of node-red and node, and agree when prompted: `bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)` |
| 64 | + |
| 65 | +It could take as long as half an hour to complete that step! |
| 66 | + |
| 67 | +* Now type `sudo systemctl start nodered.service` to start the node-red service |
| 68 | + |
| 69 | +* Type `cd ~/.node-red && npm install node-red-contrib-ui` to install the node-red UI, which will give us a neat dashboard and some graphs. This might take a while! |
| 70 | + |
| 71 | +* Type `sudo systemctl stop nodered.service` then `sudo systemctl start nodered.service` to restart the node-red service with the new `UI` module we just installed. It may take a minute or two. |
| 72 | + |
| 73 | +* Type `sudo systemctl enable nodered.service` to set node-red to start on every boot |
| 74 | + |
| 75 | +* Now that's done you should be able to go to http://raspberrypi:1880/ |
| 76 | + |
| 77 | +* Drag an `inject` node from the left on to the page |
| 78 | + |
| 79 | +* Drag an `mqtt` node from the **output** area on to the page |
| 80 | + |
| 81 | +* Connect the two nodes together by clicking and dragging between the squares on the end of them |
| 82 | + |
| 83 | +* Double-click on the MQTT node |
| 84 | + |
| 85 | +* In the menu that appears, type `/hello` into `Topic` |
| 86 | + |
| 87 | +* Under `Server` choose `Add new mqtt-broker` and click `edit` |
| 88 | + |
| 89 | +* fill in `localhost` for server, now click `Add`, then `Done` to finish the node |
| 90 | + |
| 91 | +* Now click 'Deploy' in the top right |
| 92 | + |
| 93 | +* Now type `mosquitto_sub -v -h raspberrypi -t "/#"` in the SSH window - this will set the Pi listening for all MQTT events (`#` is a wildcard) |
| 94 | + |
| 95 | +* If you click the button to the left of the `inject` block that says `timestamp` then you should now see a message appearing. |
| 96 | + |
| 97 | +* In the same way you can create MQTT block to listen for a specific message, and can use `mosquitto_pub -h raspberrypi -t "/hello" -m "message text"` to send a message to it. |
| 98 | + |
| 99 | +And that's it! |
| 100 | + |
| 101 | + |
| 102 | +Interfacing with an Espruino WiFi |
| 103 | +--------------------------------- |
| 104 | + |
| 105 | +**Video coming soon** |
| 106 | + |
| 107 | +But for those eager, the code is: |
| 108 | + |
| 109 | +``` |
| 110 | +var WIFI_NAME = "Espruino"; |
| 111 | +var WIFI_OPTIONS = { password : "helloworld" }; |
| 112 | +var MQTT_HOST = "raspberrypi"; |
| 113 | +var PATH = "/mydevice/"; |
| 114 | +var mqtt; |
| 115 | +var wifi; |
| 116 | +
|
| 117 | +function mqttMessage(pub) { |
| 118 | + console.log( |
| 119 | + "MQTT=> ",pub.topic,pub.message); |
| 120 | + if (pub.topic==PATH+"1/set") { |
| 121 | + var v = pub.message!=0; |
| 122 | + digitalWrite(B3, !v); |
| 123 | + mqtt.publish(PATH+"1/status", v?1:0); |
| 124 | + } |
| 125 | + if (pub.topic==PATH+"2/set") { |
| 126 | + var v = pub.message!=0; |
| 127 | + digitalWrite(B4, !v); |
| 128 | + mqtt.publish(PATH+"2/status", v?1:0); |
| 129 | + } |
| 130 | +} |
| 131 | +
|
| 132 | +function mqttConnect() { |
| 133 | + mqtt = require("MQTT").connect({ |
| 134 | + host: MQTT_HOST, |
| 135 | + }); |
| 136 | + mqtt.on('connected', function() { |
| 137 | + console.log("MQTT connected"); |
| 138 | + // subscribe to wildcard for our name |
| 139 | + mqtt.subscribe(PATH+"#"); |
| 140 | + }); |
| 141 | + mqtt.on('publish', mqttMessage); |
| 142 | + mqtt.on('disconnected', function() { |
| 143 | + console.log("MQTT disconnected... reconnecting."); |
| 144 | + setTimeout(function() { |
| 145 | + mqtt.connect(); |
| 146 | + }, 1000); |
| 147 | + }); |
| 148 | +} |
| 149 | +
|
| 150 | +setInterval(function() { |
| 151 | + if (!mqtt) return; |
| 152 | + mqtt.publish( |
| 153 | + PATH+"cputemp", |
| 154 | + E.getTemperature()); |
| 155 | +}, 2*60*1000); |
| 156 | +
|
| 157 | +setWatch(function() { |
| 158 | + if (!mqtt) return; |
| 159 | + mqtt.publish( |
| 160 | + PATH+"buttonpress", |
| 161 | + 1); |
| 162 | +}, BTN, {edge:"rising",repeat:true,debounce:50}); |
| 163 | +
|
| 164 | +
|
| 165 | +function onInit() { |
| 166 | + console.log("Connecting to WiFi"); |
| 167 | + wifi = require("EspruinoWiFi"); |
| 168 | + wifi.connect(WIFI_NAME, WIFI_OPTIONS, |
| 169 | + function(e) { |
| 170 | + if (e) { |
| 171 | + console.log("Connection Error: "+e); |
| 172 | + return; |
| 173 | + } |
| 174 | + console.log("WiFi Connected"); |
| 175 | + wifi.getIP(function(f,ip) { |
| 176 | + console.log("IP: ",ip); |
| 177 | + mqttConnect(); |
| 178 | + }); |
| 179 | + }); |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | + |
| 184 | +Interfacing with a SonOff |
| 185 | +-------------------------- |
| 186 | + |
| 187 | +**Video coming soon** |
| 188 | + |
| 189 | +But for those eager, the code is: |
| 190 | + |
| 191 | +``` |
| 192 | +var WIFI_NAME = "Espruino"; |
| 193 | +var WIFI_OPTIONS = { password : "helloworld" }; |
| 194 | +var MQTT_HOST = "raspberrypi"; |
| 195 | +var PATH = "/mydevice/"; |
| 196 | +var LED = D13; |
| 197 | +var RELAY = D12; |
| 198 | +var BTN = D0; |
| 199 | +var mqtt; |
| 200 | +var wifi; |
| 201 | +
|
| 202 | +function setState(v) { |
| 203 | + RELAY.write(v); |
| 204 | + LED.write(!v); |
| 205 | + mqtt.publish(PATH+"status", v?1:0); |
| 206 | +} |
| 207 | +
|
| 208 | +function mqttMessage(pub) { |
| 209 | + console.log("MQTT=> ",pub.topic,pub.message); |
| 210 | +
|
| 211 | + if (pub.topic == PATH+"set") { |
| 212 | + setState(pub.message!=0); |
| 213 | + } |
| 214 | + if (pub.topic == PATH+"eval") { |
| 215 | + try { |
| 216 | + mqtt.publish(PATH+"response", eval(pub.message)); |
| 217 | + } catch(e) { |
| 218 | + mqtt.publish(PATH+"exception", e.toString()); |
| 219 | + } |
| 220 | + } |
| 221 | +} |
| 222 | +
|
| 223 | +function mqttConnect() { |
| 224 | + mqtt = require("MQTT").connect({ |
| 225 | + host: MQTT_HOST, |
| 226 | + }); |
| 227 | + mqtt.on('connected', function() { |
| 228 | + console.log("MQTT connected"); |
| 229 | + setTimeout(function() { |
| 230 | + mqtt.subscribe(PATH+"#"); |
| 231 | + }, 1000); |
| 232 | + }); |
| 233 | + mqtt.on('publish', mqttMessage); |
| 234 | +} |
| 235 | +
|
| 236 | +
|
| 237 | +function onInit() { |
| 238 | + console.log("Connecting WiFi"); |
| 239 | + setInterval(function() { |
| 240 | + if (!mqtt) return; |
| 241 | + if (!mqtt.connected) { |
| 242 | + console.log("MQTT disconnected... reconnecting."); |
| 243 | + mqtt.connect(); |
| 244 | + } |
| 245 | + }, 60*1000); |
| 246 | + |
| 247 | + wifi = require("Wifi"); |
| 248 | + wifi.on('connected',function() { |
| 249 | + console.log("Connected to WiFi"); |
| 250 | + }); |
| 251 | + wifi.on('disconnected',function() { |
| 252 | + console.log("Disconnected from WiFi"); |
| 253 | + }); |
| 254 | + wifi.setHostname("MYDEVICE"); |
| 255 | + wifi.stopAP(); |
| 256 | + wifi.connect(WIFI_NAME, WIFI_OPTIONS, |
| 257 | + function(ap){ |
| 258 | + console.log("Successful connect."); |
| 259 | + }); |
| 260 | + // wait, and connect MQTT |
| 261 | + setTimeout(function() { |
| 262 | + console.log("MQTT connecting"); |
| 263 | + mqttConnect(); |
| 264 | + }, 10000); |
| 265 | +} |
| 266 | +
|
| 267 | +``` |
0 commit comments