## Tutorial on how to connect an IKEA sit/stand desk to the internet using pycom and pybytes
### Overview
Gabriel Borglund - gb222jt
The name of the desk I have used is RODULF-GF-470 and it's one of the cheapest sit/stand desks that you can buy. The motorcontroller is integrated into the legs. To make the table go up, you have to hold a button for the entire duration of raising the dask. The same is true for when you want to lower the desk. I therefore thougth that it would be a good idea to connect the desk to the internet so that I can type a command instead of holding the button. If you follow this tutorial, the actuall assembly time should be pretty quick. I would aproximate it at a couple of hours.
### objective
The fact that you had do hold down the button for raising the table during the entire raising time, was to me stupid. I actually assumed that you would be able to set levels by yourself, but this was not the case.
Something else that entrigued me about this project was the ability to track the position of the desk. I mean what is the point of having a raisable desk if you never rais it. I think we all have heard the scary claims about how bad it is for you to sit down for extended periods of time, so if I can track how much i sit vs stand at my desk, I hopefully stand a better chance making myself stand more, and therefore stand out amongst the horrible statistics that tell us how much we are sitting down.
### Material
To complete this project you will need:
* one lopy4
* one sit/stand desk from IKEA (I used the RODULF-GF-470)
* one breadboard(soldering would probably be better for a permanent solution)
* two transistors(I used two BC549, but I also found a slightly hacky way, without using transistors discussed below)
The lopy4 is the interface between the desk controller and the internet.
The breadboard is optional. I found it very usefull for wiring up a prototype quickly, but I will probably solder the connections in the future instead.
I used the transistors to short the controller cables on the sitstand desk.
I bought the lopy the breadboard and cables for 90€ at electrokit.se. The transistors I got from a local makerspace, but they should be pretty easy to find on the internet. I paide about 5Skr per transistor.
### Hacking the table.
To rais the table you have to hold down a button(se picture 1). It turns out that the way the table works is, you just short two cables and the actuall motor controll does all of the smoothing out of motion. This is both a good and a bad thing for me. It means that to rais the table, all I have to do is short the cables. But it also means that I can't controll the motor on a fine level or send the exact position of the desk to the pycom server, it does however mean, that to get the intended functionality(although sligtly limited) all we have to do is short two cables.

Picture 1: showing the buttons to go up and down.
### Computer setup
The first step is downloading the flashing software for the pycom borad. I downloaded it from their official website.
To flash the board, I followed these steps.
* connect the lopy via the USB cable to my MacBook.
* start the updater
* press continue
* press continue
* check the Force update Pybytes registration
* check the Enable Pybytes / SmartConfig support
Now go to the website pybytes.pycom.io and add a new device and follow these steps:
* press the add device via usb button
* choose the lopy4
* choose wifi (if you have not yet configured your wifi this should be done first. Make sure not to use a 5Ghz connection)
* Choose your wifi and press the save button
* now copy the code under the title: "AVTIVATE YOUR DEVICE WITH FIRMWARE UPDATER"
Go back to the Pycom firmware updater and follow these steps:
* Press the continue button
* paste the code that you copied earlier from the pybytes website and press continue.
* press continue and wait while the software is being flashed to your board.
Now that your Lopy4 is updated it is time to setup the code. I used the text editor ATOM, and the pybytes plugin.
Just open up the ATOM editor in some directory of your choice. If the pybytes plugin is installed it should be opened automatically.
You now make a new directory called act. This is the directory where the source code will live.
Now make a new file called pymakr.conf. This file should reside in the directory containing the directory called act. That is not in the act dir. Paste the following code into the .conf file:
```
{
"address": "/dev/tty.usbmodemPy3f59031",
"username": "micro",
"password": "python",
"sync_folder": "act"
}
```
But you obviousl have to change the adress string to be the path to the usb connected lopy4. The rest should be okay.
Now hop back into the act directory and create two files. main.py and table.py. Paste the following into main.py:
```
import table
t = table.Table(pybytes)
```
and the following into the table.py file:
```
from machine import Pin
import time
class Table:
def __init__(self, pybytes):
self.pybytes = pybytes
self.pin23 = Pin('P23', mode=Pin.OUT)
self.pin22 = Pin('P22', mode=Pin.OUT)
self.pin23.value(False)
self.pin22.value(False)
self.standing = False
self.currentPos = 0
def move_up(self, sec):
self.currentPos = self.currentPos + sec
self.pybytes.send_signal(0, self.currentPos)
self.pin23.value(True)
time.sleep(sec)
self.pin23.value(False)
def move_down(self, sec):
self.currentPos = self.currentPos - sec
self.pybytes.send_signal(0, self.currentPos)
self.pin22.value(True)
time.sleep(sec)
self.pin22.value(False)
def stand(self):
self.standing = True
#I found that 11 is the perfect time for getting to my prefered height.
self.move_up(11)
def sit(self):
self.standing = False
#For me the best sitting position is at the very bottom.
self.move_down(self.currentPos)
self.pin22.value(True)
time.sleep(2)
self.pin22.value(False)
def reset(self):
self.standing = False
self.pin22.value(True)
time.sleep(20)
self.pin22.value(False)
self.currentPos = 0
```
Once you have saved both of the files, you can proceed to press the upload project to device button in the pymakr plugin.
Congrats your lowpy4 should now be configured to recieve commands and to to controll the table.
### Putting everything together
Alright, here comes the fun part. You are now ready to wire up the breadboard(or solder stuff permanently you crazy person). The wiring should follow that of picture 2. But the jist of it is, you connect the two different positive wires for lowering and raising the table to the collector pins of the two bc549 transistor, and you connect the ground wire from the table and from the lopy board to the emitter pin on the two bc549s. The only thing left to do is connect the data pins on your lopy board to the base pin on the transistors, so that is what you do. Pin 22 should be connected to the base of the transistor controlling the lowering wire, and pin 23 should be connected to the raising transistor. This is just because that is the way I wrote the code, but you can of course rearange that if you please.

Picture 2: showing how the project is wired.
### Platform
I used the pybytes platform and the online pymakr platform, to get the wanted functionality. This solution is not very elegant. To raise the table I call ```t.stand()``` method via the pymakr repl. This is an uggly solution. I am sure there are better ways of doing things, but this way I get the flexibility of raising the table a specific amount, or just switching between standing and sitting mode. I also made the lopy send a signal to the pybytes platform. This signal is sent every time the position of the desk is changed. This means that I can track my sitting/standing habits while working, which is pretty neat.
### The code
The code is pasted above, but the jist of it is that the values of the two pins 22 and 23 defines a 3.3V voltage, that in turn controlls the transistors. When calling the move up or move down functions data is also sent to the pycom servers telling them what the current position of the table is. The reset method doesn't send this data, and is needed because the lopy sets the two pins high when it is booting. This means that if the lopy4 is reset when it is connected to the table the table will raise itself, and if we call sit after the table has raised itself, the wrong position will be sent to the pybytes server...
### Transmitting the data / connectivity
The data is sent every time the position of the desk is changed. This means we are not sending more data then we need to. The data is sent over wifi since I couldn't get LoRa to work, but working with LoRa, shouldn't really be a problem for this project specifically. The datarate is very low, and that means that LoRa would have been a good choice.
I couldn't find any good information regarding what transport protocol pybytes use for sending the repl data to the lopy4. I tried sniffing the packets with wireshark but, that didn't really help either...
### Presenting the data
I used the pybytes built in dashboard to display the data. The data is saved every time I change the position of tha desk. The data is presented in a linechart(se picture 3).

Picture 3: showing the line chart on the pybytes website.
### Finalizing the design
There is a lot of potential for improvement with this project. This is more of a very crude prototype than a finilized product. If I would wan't to put in more time into the project, I would probably connect the current API to some database, so that I could query that database and have the table move that way. This would mean I could do all kinds of cool stuff. The table could be voice controlled via Alexa, siri or google assistant pretty easily. That way I could probably also make an app for controlling the desk very easily.
The prototype works suprisingly well.