Friday, May 29, 2015

Yowsup server

This time I wish to share some thought with you about an evolution of my projects. No need to code something for now...

In all my previous posts about Youwsup and Whatsapp messaging I always assumed on thing: chatting involved just one Raspberry Pi.

Anyway I own more than one RPi. What if I wish to use Whatsapp with more than one device?

This is what I have now:

I wish to be able to use Whatsapp at least with RaPiRo and Octoprint, but also osmc could be useful.

Octoprint could inform me about reaching the end of a printing process, or about a problem during the print. Also I could ask for a photo during the printing. I know that I could check all of these things using the web interface, but with a message I could be informed almost instantly.

RaPiRo is used for some home automation. In the future I will use something different (see the post for more info), but keeping most of the features that are already implemented. Messaging with my home server is a must for me, so I really need Yowsup here.

Owncloud could be leaved alone, but actually it also records ambient data (pressure, humidity and temperature) as it's always on. Data can be read by the home server, but I could also ask for current status by Whatsapp.

I just cannot just install Yowsup on every device ad use them at the same time. One Whatsapp account cannot be used on more that one device and it could be blocked.

One solution could be using a different phone number for every device, but this means money and I do not want to spend for more cards.

The best way seems to be the creation of one server that would get the messages from the clients and sends them with Yowsup. When receiving a message it will check the destination somehow and redistributes the text to the right client.

I will use the RPi with Owncloud as a server. It's always connected, so it can send and receive at any time. The other devices could be on or off, so the server needs also to check for this.
Now I need to choose how to exchange messages between the clients and the server. For this task there are several different solutions and maybe I'm not using the best one, but it's the easiest for me.

Probably the best solution would be e real network message protocol, but this seemed to me a bit too complicated (but I could be wrong, of course!).

At the end I opted for a simple file transfer method: client will create a text message in the server and the server will read and send it. Let's see an example of a workflow...

A 3D printing just ends and Octoprint needs to notify me. It creates a text file named "octoprint.msg" containing the text "Your 3D object is ready". After the creation, the file is sent to the the Yowsup server using a simple scp shell command. This ends the client tasks.

In the server there is a Python script running (for now every 10 seconds). This script checks for the existance of the file named "octoprint.msg". When this file is found the script reads its content and deletes the file (this way at the next check the file will not be found and the message will not be sent again). The read text is sent to the recipient by adding "Octoprint: " at the beginning.

Now the recipient receives "Octoprint: Your 3D object is ready".

Every client uses a different filename, so there's no risk of overwriting a message not yet sent to the recipient (remember the 10 seconds delay between every check).

Let's see another example where I send something for a client.

I forgot to turn off osmc and I'm not at home, so I send this message: "Osmc: shutdown".

The server receives this message and (after a lower case conversion) it checks for a specific prefix (in this case "osmc: "). The right prefix is found, so it creates a text file names "whatsapp.msg" with the received message as content, than it sends the file to the right RPi, always using the scp command.

The client check at regular intervals for the existance of this file. When found it reads the content and looks for specific commands (exactly as in our parser) and executes the requested task.

This time the file can have the same name for all the clients as it can be sent just by the server, so no conflict would arise.

No comments:

Post a Comment