# Tutorial of adding a remote start to Rancilio Silvia espresso ### _By Carl Larsson (cl223wi)_ ## Overview This tutorial is the final result of a project in the Applied IOT course at Linneaus University. It goes through a process of setting up a remote monitoring, and startup sequence for the fairly common Rancilio Silvia espresso machine (hereafter referred to as only Silvia). It will mainly combine the Silvia, the microcontroller Lopy4, a relay board, and the website Adafruit.io to do so. Visual Studio Code is used to upload python code to the controller and the estimated time for completing the tutorial is presented in Table 1 below. _**Table 1: Estimated time requirement.**_ | Setting up the hardware | Setting up the code | Setting up Adafruit | Total | | -------- | -------- | -------- | -------- | | 2 hours | 1 hour | 0.5 hours | **3.5 hours** | **Disclaimer:** This project involves dealing with high voltage applications and is done for educational purposes only. It should never be performed without the proper knowledge and understanding of what is being done. Any action you take upon the information presented here is strictly at your own risk, I will not be liable for any consequences for the use of this material. Also consider this my warning to you to think your own projects through before starting them. ## Objective I chose to take on this project to learn more about IOT and to achieve less of a hurdle when I have my coffee in the mornings. It combined my interest for coffee and systems control. Furthermore, it was chosen to be a challenge where I did not have any specific solutions in mind prior to starting, as this bias to often is the present in my projects. Not only that, it served to teach me about IOT and dip my toes in the topic to hopefully further fuel my interest. It did give me an appreciation of how simple it could be and that a lot is actually possible to accomplish with small means. IOT is applicable everywhere. Hopefully you, the reader, will find something to take away from reading/building this, which could be anything from discovering your future hobbies, to make better coffee. ## Materials Below in Table 2 the Bill of materials can be read. Some can be bought together, some ordered separately with great delivery times, and some is probably easiest to get a hold of in local stores. The Lopy4 and expansion board can for example be bought together from [Electrokit](https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-lopy4-basic-bundle/ "Electrokit basic Lopy4 bundle"), the sensor can be bought from [Amazon.se](https://www.amazon.se/gp/product/B09463BW76/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&language=en_GB&psc=1 "Amazon.se Non-Contact fluid level sensor"), and the 1mm^2 wire is probably easiest to find in a local hardware store. An example of how to get a hold of all the stuff can be found in Appendix A. Total cost for the shopping example is 1428.29 SEK. Other than common handtools, a crimping plier and a heatgun is used for the cable lugs and heat shrink tubing. _**Table 2: Bill of materials**_ | Component | Amount | Description | Image | |--------------------|--------|----------------------------------------------------------------------------------------------------------------------------------------------------------|-------| | Lopy4 | 1 | Microcontroller, made by Pycom | ![](https://i.imgur.com/jXM9rL0.png) | | Expansion Board 3.1 | 1 | Board to expand the Lopy4's capability, made by Pycom | ![](https://i.imgur.com/qcvdXaW.jpg) | | Fluid level sensor | 1 | XKC-Y25-V, [Like this one](https://www.amazon.se/gp/product/B09463BW76/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&language=en_GB&psc=1 "Amazon.se link") I got. Here's an [explanation and pictures of how it works](https://www.electrodragon.com/product/non-contact-liquid-level-sensor/) | ![](https://i.imgur.com/d6pBfI5.jpg) | | Bread board | 1 | Board to connect Dupont wires together | ![](https://i.imgur.com/nBPbFGR.jpg) | | Relay board | 1 | Board that contains relays for controlling higher voltages | ![](https://i.imgur.com/aAymIjn.jpg) | | USB charger | 1 | Normal 5V USB charger brick with adequate capacity to drive the relay board | ![](https://i.imgur.com/nKJ6aH0.png) | | USB cable | 1 | A regular USB cable between the charger and Lopy4 | ![](https://i.imgur.com/TtiAGLA.jpg) | | Dupont wire M-F | 5 | Wires to connect circuits together | ![](https://i.imgur.com/fggpiNC.jpg) | | Dupont wire M-M | 12 | Wires to connect circuits together | ![](https://i.imgur.com/euiUTHo.jpg) | | Wooden board | 1 | Any small wooden board fit to attach the components to | ![](https://i.imgur.com/Bv5F4w6.jpg) | | Screws | 4-8 | Small wood screws to attach boards to the wooden board | ![](https://i.imgur.com/vdJ5qsl.jpg) | | 1 mm^2 cable | 1.5-8m | High voltage cable for directing Silvia currents | ![](https://i.imgur.com/Pthj5q8.jpg) | | Cable lugs | 8 | To connect high voltage wires in the Silvia | ![](https://i.imgur.com/w2vMKWk.jpg)| | Heat shrink tubing | | Isolating tubing, enough to cover the cable lugs | ![](https://i.imgur.com/APZ1pMU.jpg) | | Cable gland |1-2 | Anywhere high voltage cables pass metal surfaces, these are mounted | ![](https://i.imgur.com/1UUUniA.jpg) | ## Computer set up The Lopy4 is running Micropython and can be programmed using an IDE. In this tutorial Visual Studio Code (VSC) on Windows 10 will be used. When the IDE is installed, Node.js and Pymakr VSCode Extension also needs to be installed. After that the Lopy4 needs to be flashed with the latest firmware. This is done Using Pycom's Firmware updater. ### VSC, Node.js, and Pymakr First the IDE VSC needs to be installed and adapted to using it with the Lopy4, the procedure can be followed at [Pycoms documentations website - vscode](https://docs.pycom.io/gettingstarted/software/vscode/ "Pycoms documentations website - vscode"). Now connect the Lopy4 to a USB port on the computer, and it should automatically be detected and connected. If that is not the case, try the procedure found below the instructions for installing the IDE. ### Flashing the Lopy4 Flashing (Setting up the flash memory of the device) on the Lopy4 is done using Pycoms update tool. Instructions on how to do so can be found at [Pycoms documentations website - device](https://docs.pycom.io/updatefirmware/device/ "Pycoms documentations website - device"). Note that it is not necessary to "Force update Pybytes registration", as this is only needed if we were to use Pybytes, which we will not. The procedure of flashing leaves the device in a "clean" state without any former programming done by the user, but only the pycom firmware. ### Uploading code to the Lopy4 Now the microcontroller is ready to be programmed. In VSC, open a new, empty, directory and here the files for the project will be put. Name the directory accordingly. The [code files on GitHub](https://github.com/Te-ex/Applied-IOT-Linneaus-cl223wi/blob/master/main.py "Applied IOT Linneaus cl223wi") are to be put in this directory, one simply presses the "upload" button. When the files are successfully transferred, the Lopy4 will automatically first run the boot.py file and then the main.py file. ## Putting everything together Now it's time to start putting everything together. **Note:** this project results in a temporary setup to test the functions of the system, and should not be left as a permanent solution. Start by mounting the Lopy4 on the expansion board (with the USB port facing you) and screw it in to the wooden board to keep everything tidy and secure. Then mount (usually comes with adhesive on the underside) the breadboard next to it on the right side (If it got numbering it is convenient to mount it, so the row numbers increase towards you). The last thing to mount on the wooden board is the relay board. This is mounted to the right of the bread board with the big terminals facing right. An image of the setup can be seen in Figure 1. The left + rail of the bread board is at 3.3V and supplies the relay board's control circuit. The right + rail of the bread board is at 5V and supplies both the sensor and the relay board's relay circuit. The control circuit of the relay board already contains adequate voltage regulation, so no extra resistors are needed. The sensor could probably be powered using the 3.3V rail instead, but it is not rated for it and the current draw needs to be measured and checked against the maximum draw from the 3.3V output of the Lopy4. The relay board's relay circuit draws a maximum of 300mA and together with the 12mA of the Wi-Fi consumption is comfortably less than the maximum 500mA output of the USB connection. The sensor and auxiliary power draw of the Lopy4 is expected to not exceed the leftover headroom of the USB power supply. The sensor's output wire is connected through the bread board as an input on pin 16 on the Lopy4. The relay board is controlled with pins 20,21,22, and 23 on the Lopy4 using the setting OPEN_DRAIN. Both - rails on the bread board act as common ground and is connected together. The mode, **M**, wire from the sensor is merely connected to the bread board to keep it tidy. It is not connected to anything. It could be used to flip the output value of the sensor. It is however easier to flip in the programming and to not use an extra wire. The wiring is done as in Figure 2 below. Please refer to the diagram when connecting the wiring. **NOTE:** Remove the VCC-JDVCC jumper on the relay board! The fluid level sensor can be mounted in your own preferred height and is easiest to adhere to the inside of the back panel using tape. ![Image of wooden board setup](https://i.imgur.com/5aIZuHF.jpg) _**Figure 1: Image of physical wooden board setup. Note that this image does not contain the high voltage cables, or the USB power cable at this point.**_ ![Image of wiring setup](https://i.imgur.com/DQR8d8b.jpg) _**Figure 2: Circuit diagram of wiring and board setup.**_ ### Setting up the high voltage wiring and connecting it to the Silvia **NOTE:** This is dealing with high voltage applications and should not be done without the proper knowledge and understanding of what is being done. See former disclaimer! To direct the high voltage between the relay board and the Silvia wires with isolated cable lugs are used. [Here's a link](https://www.biltema.se/en-se/car---mc/car-electrical-system/cable-connectors/double-spade-connector-10-pack-2000041160 "Biltema cable lug") for an example of cable lug. Be sure to use one compatible with the size of your wire. When the cable lug is mounted, heat shrink tubing is put over it to isolate it from the environment in the Silvia. The other end is also exposed to fit in the relay board. A completed cable is shown in Figure 3. **NOTE:** if any high voltage cable is run through the case of the Silvia (passing metal framing) there needs to be a cable gland installed to protect from chafing and electrical shorts as a result. The wires are installed on the inside of the Silvia like Figure 5 behind the front panel shown in Figure 4 (Image sourced from [pidsilvia.com](http://www.pidsilvia.com/)). Here can also be seen which connection corresponds to what cable. Opening the top cover only requires removing the water bin and four philips screws on top of the machine. Pair them up corresponding to the circuit diagram. Here is a [Link to the Rancilio Silvia wiring diagram](http://www.pidsilvia.com/Images/Silvia%20Electrical%20Diagram.pdf "Rancilio Silvia wiring diagram") for the interested. ![High voltage cable, cable lug, and heat shrink](https://i.imgur.com/UABjpjM.jpg) _**Figure 3: Image of prepared high voltage cable. Note the heat shrink in the other end that corresponds to the colour of the first end. All 8 wires use different combinations of black and red heat shrink for easy identification.**_ ![Image of Rancilio Silvia front panel and what connections on the back corresponds to](https://i.imgur.com/JqRpkCw.png) _**Figure 4: Image of Rancilio Silvia front panel and what connections on the back corresponds to.**_ ![](https://i.imgur.com/6zqG1zt.jpg) _**Figure 5: Image of installed high voltage cable. Note that this cable is placed on the yellow one connector on the lamp only for convenience of showing a connection.**_ ## Platform To present the status of the water level of the Silvia and send a signal to start it up, [Adafruit.io](https://io.adafruit.com/) was used. This cloud platform was selected for its intuitive setup procedure, ability to display data online, send data back to the Lopy4, and that it's free. There are limits of how much data can be fed every minute, and it is only stored for 30 days. However, this is more than enough for this application as we send less data than that and only need it for 5 minutes at most. The easiest cloud platform to use with pycom devices is however Pybytes, though it only allows for collection of data and not send our startup signal back. Therefore, Adafruit with slightly more capability was chosen. Since this project could be viewed as home automation, it would probably be a neater solution to set up a local server. This would scale better if one in the future would connect more microcontrollers at home. The local server could then act as a gateway if any cloud service would be used in addition to connecting remotely to the local server. ### Setting up Adafruit and a dashboard for a data interface First an account needs to be set up at Adafruit. Once that is done, two feeds are set up under the "IO" tab, "Feeds", "View all", and finally "+ New feed". Name the first one, used for sending water level status, "water" and the second, used for starting the Silvia, "poweron". Fill in the descriptions accordingly. Then a dashboard needs to be set up. Go to the "Dashboard" tab and make press "+ New dashboard". Name it and put in a fitting description. Next click your created dashboard and an empty black space should present itself. This is the dashboard, only yet empty. Now press the cogwheel to the right and create a new block. Click the "toggle" button and choose the "poweron" feed. Next put in a fitting name, like "Power" and create the block. It should appear on the dashboard. Next create another block, this time using the "indicator" and put in a fitting title, the condition "< 0.5" and create it. An image of a complete dashboard can be seen in Figure 6. There is a limit for the free user at Adafruit of 30 data points every minute, however, the programming is set up to only send a data point every 5 minutes, when the water level goes past the threshold, or when the machine is started/turned off. This will not exceed the maximum data limit during normal use of the Silvia. The data is stored for only 30 days, however, this is way more than the 5 minutes it needs to be stored for this application. Next thing is to retrieve your key and additional information to couple Adafruit with the Lopy4. Go to the tab "My Keys" and save the information somewhere safe. It will be used in the next section. ![Image of Adafruit dashboard containing a power switch and water level indicator](https://i.imgur.com/mxqRPSE.png) _**Figure 6: Adafruit dashboard**_ ## The code and connectivity This section will briefly describe the code used for programming the Lopy4, and the details can be found in the [files on GitHub](https://github.com/Te-ex/Applied-IOT-Linneaus-cl223wi/blob/master/main.py "Applied IOT Linneaus cl223wi"). The files are to be downloaded and put in your working directory (where your boot.py and main.py file is located) and edited for your adaption. The subsections below describe different parts of the code and why they are used. For structure and usage of uMQTT a [Core Electronics article by Chris](https://core-electronics.com.au/tutorials/internet-of-things-with-pycom-and-adafruit-io.html "") was referenced. ### Wi-Fi For the Lopy4 to be able to send and recieve information, Wi-Fi is used. [Pycoms documentation for setting up Wi-Fi](https://docs.pycom.io/tutorials/networks/wlan/ "Pycom documentations - Wi-Fi") is used to setup the connection to a router. Note that this is already included in the [GitHub files](https://github.com/Te-ex/Applied-IOT-Linneaus-cl223wi/blob/master/main.py "Applied IOT Linneaus cl223wi"). The SSID and password information is separeted in the files for convenience. Added is also status indications by the on board LED. Your task is to put in your Wi-Fi SSID and password at: ```python= WIFI_SSID = "KaffeKlockan15?" WIFI_PASS = "ConsiderYourselfInvited1500" ``` respectively. Note that provided SSID and password only is dummy values and not the authors personal Wi-Fi details. Wi-Fi is used as a wireless connection between the Lopy4 and the home router. As the Silvia is a home user style machine it is fitting to use Wi-Fi as it is already set up in many homes. ### uMQTT The messaging protocol uMQTT will be used talk between Adafruit and the Lopy4. To set this up a library file is needed and the Adafruit setting retrieved before is put in the code as well. First download [the file from micropython's GitHub](https://github.com/micropython/micropython-lib/blob/master/micropython/umqtt.simple/umqtt/simple.py "umqtt") and save the code in your working directory and name it 'umqtt.py'. Now the settings for connecting to Adafruit is left. In the code files your task is to put in the uMQTT details needed to publish and subscribe to Adafruit. Note that a random GUID is needed and can be generated at [guidgenerator.com](https://www.guidgenerator.com/) and that the filled in information below is just dummy examples and should not be used. Fill in the information in the code files at: ```python= AIO_USER = "KaptenFilling" #Username AIO_KEY = "aio_f4c3r01l" #Active Key AIO_CLIENT_ID = "4a312cac-fead-4b97-b94c-f7d38d7491a9" #guidgenerator.com AIO_CONTROL_FEED = "KaptenFilling/feeds/poweron" AIO_WATER_FEED = "KaptenFilling/feeds/water" ``` The choice of messaging protocol is a proven solution for IOT data transfers. It is very lightweight and fully capable of taking on this application. Adafruit supports it and I did not see any reason to use something else when Adafruit already was chosen. ### Control and water status functionality The main functionality of the code is in publishing water level status and listening to changes in the signal for turning on and turning off. This is done using a main loop that does those two things each after the other, over and over again. For listening to startup, or shutdown commands, the subroutine callback function: ```python= sub_cb(topic, msg) ``` is subscribed to the Adafruit MQTT. Whenever it receives the "ON" command, it runs the startup function: ```python= startup() ``` , and whenever it receives the "OFF" command, it runs the power off function: ```python= poweroff() ``` Details for both functions can be found in the [python code on GitHub](https://github.com/Te-ex/Applied-IOT-Linneaus-cl223wi/blob/master/main.py "Applied IOT Linneaus cl223wi"). For sending the water level status, the function: ```python= send_water() ``` which checks if it's time to check the water level and then checks if it is time to publish. It uses the function: ```python= check_water() ``` to read the fluid level sensor. The check_water() function is also used in the startup() function to provide a crucial check of water level. If there is no water the boiler might burn when started. ## Finalizing the design A final assembly can be seen in Figure 7 and contains the whole setup. Although this project isn't data driven in the sense of collecting greater amounts of data and drawing conclusions from it, the project is fairly easy to expand and monitor more functions of the Silvia using more sensors. The project went far smoother than expected and was very fulfilling in what I wanted to learn and achieve. The result can be seen in Figure 7 below and is only a temporary set up to do final testing of the functionality. As previously mentioned this project was only done for testing. ![Layed out parts](https://i.imgur.com/W20jh8x.jpg) _**Figure 7: Complete setup with controller and Silvia.**_ ![Mounted sensor](https://i.imgur.com/UDHRhnG.jpg) _**Figure 8: Close up image of mounted fluid level sensor using tape with adhesive on both sides.**_ ![High voltage cables mounted to the relay board](https://i.imgur.com/yQuDgGV.jpg) _**Figure 9: Image of mounted high voltage cables.**_ ### Further work 1. Make it permanent This is only a temporary setup to prove the concept. A permanent solution with adequate housing of the controller and relay board with proper cables (not Dupont) and soldered connections is needed if it is to be used for brewing coffee. 3. Find a more robust power source to drive the relay board. The relay board is quite demanding of its power source. The source needs to deliver enough current immediately when the controller circuit switches. As it is now, it barely flips even though the charger is rated for 4A. The instantaneous power delivery is barely enough. Flipping the relays sequentially (rather than at the same time) helps a great deal. 4. Implement a feedback controller to improve espresso shot quality This type of espresso machine (single boiler) is notorious for inadequate temperature control. Implementing a feedback controller could deliver greater shots and is fairly easy to implement in this existing setup with only one more relay and a thermocouple (and an implemented feedback control algorithm of course ;). 5. Tweet machine status Want to tell your spouse if the machine is ready to go tomorrow? Make it post on Twitter for status updates. # Appendix A Below in Figure 1, 2 and 3, an example of how to get a hold of items to construct a remote start of the Rancilio Silvia is seen. One order from [Electrokit](https://www.electrokit.com/), one from [Biltema](https://www.biltema.se/), and the sensor from [Amazon.se](https://www.amazon.se/-/en/gp/product/B09463BW76/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1). ![](https://i.imgur.com/cNdr5Zb.png) _**Figure 1: Electrokit shopping cart.**_ ![](https://i.imgur.com/fIS9H3L.png) _**Figure 2: Biltema shopping cart.**_ ![](https://i.imgur.com/aD0jZfD.png) _**Figure 3: Amazon.se shopping cart.**_