Communication between server and python script help

54 views
Skip to first unread message

Esteve Martín

unread,
Apr 30, 2017, 10:19:53 AM4/30/17
to web2py-users
Hi,
I was recently introduced to web development so i'm stil quite a newbie and i'm having difficulties to find the correct way the implement what i want.
At the moment i'm working in a project and I seem to have hit a wall. I would appreciate some advice to get me on the right path.

I have some arduinos that constantly send sensor data trough serial ports to a python script and i would like this data to be uploaded to the server. The server should then store it into the database and also plot it in real-time. I would like the server to be capable to actuate on the arduino as well, for example to change the value of some of its pins. 

To implement this i thought of making a token based protocol over a two-way communication channel between my python program and the web2py application. The thing is that i have no idea how to do this. I have done some research but i've been overflown with information. What would you recomend? I've read websockets would do the trick but i would like some council before getting into it.

Thanks and sorry for my mediocre english,

Esteve


Marlysson Silva

unread,
Apr 30, 2017, 12:43:20 PM4/30/17
to web2py-users
1. In your python script you are achiving handle the data's arduino?

2. To "real time" changing exists the rethink dB .. To this propose..

Carlos Kitu

unread,
May 1, 2017, 12:52:11 PM5/1/17
to web...@googlegroups.com
Hi Esteve,
I use web2py, and also as a side project, I develop firmware for microcontrollers (with mbed OS, not arduino, but quite similiar). I didn't have the need of connecting both worlds, so take this as hints, not real tested experience.

First, let's check a couple of assumptions:
- I guess that you have a PC connected to your arduino though a serial port.
- That PC has a python script receiving data from the arduino

I would try with a REST API client for python, to connect the PC python's script with the server. After a quick search, I found this one: Requests: HTTP for Humans (http://docs.python-requests.org/en/latest/), that might be fine four your needs. Maybe someone in this group could suggest a better option
Just to be sure, I would implement some kind of buffering in the PC python script, i.e. some queue to store the received data from the arduino, until it is transmitted to the server. You could get some hints from this link: https://docs.python.org/2/library/io.html

EDIT:
I forgot to mention that you might need two threads at the PC side: one to receive arduino's data through serial port, and another to send that data to the server using REST API, so that you can keep both tasks running simultaneously, as far as the python threads allow concurrency.

At the server side, web2py, I would implement a REST API (see http://web2py.com/books/default/chapter/29/10/services#Restful-Web-Services) to receive the PC script's data and store it in a table.

Good luck and please report in this group your findings, just in case I need to do something like this sometime later, so that I can take advantage of your experience.
Best regards.

Donald McClymont

unread,
May 1, 2017, 5:50:44 PM5/1/17
to web2py-users
I did this a while back https://github.com/DonaldMcC/pronto which was a small and very simple way to send commands direct from web2py server to an arduino which was controlling a cubetto prototype. I appreciate this is not your current use case but thought I'd share in any case.

Cubtetto became a kickstart project at the following site https://www.kickstarter.com/projects/primotoys/cubetto-hands-on-coding-for-girls-and-boys-aged-3 but there were previous links on how to build the prototype.

This side of things worked fine and I could send commands from a tablet or whatever to the arduino.  The web2py side was simple but there were a few issues with getting the jointjs library working and I haven't tested it with latest versions of this so there might be issues.

Regards
Donald
Reply all
Reply to author
Forward
0 new messages