9 02 2019
When should I take the train ?
Let me give you a little bit of context. I’m doing remote working since quite a long time now. That’s mean I don’t take the train as often as I used to. I really enjoy it but, as a side effect, I lost the habit of taking a crowded train, which make it even more pain the few times I have to take it today.
My home is located around 7 to 10 min walk to the train station. Depending if you walk fast or slow. We also have to take consideration that the train schedule could vary from every 20 min to 1 h based on the hour of the day.
As I’m not quite lucky, usually when I’m ready to go to catch my train, it’s usually the worst timing to be able to get a train. So I have to wait the next one, grab my laptop, start working on something and miss the optimal timing to not wait at the station again :p
I decided to find a geeky solution. The first thing to look at is how can I access to train schedule data.
The most common and easy way to do geeky things is to use API. So I start to look if there is one API available for RATP (Autonomous Operator of Parisian Transports). And … what a really good news, they decided to make their API public (https://data.ratp.fr/explore/dataset/horaires-temps-reel/).
I followed docs to know how access to work with this public API and learned that I need to request access to it. Fine it’s quite usual to create an account on the software and ask for a token to access to it.
Unfortunately, I had some little surprise to work with this API. After I read the procedure I was confused, I felt like doing my paperwork. To request an access to the API I had to print paper form and filled it with my personal information, a static IP to access to the API and sign it. Then scan it and send it back.
Then, as all good old software, don’t expect a nice and clean REST API, no no no, enjoy the SOAP API and the huge wsdl file and burn your eyes on big blocks of xml. To be honest it was nearly as painful to use then wait the train.
But those things apart, that’s really nice to see those companies moving forward, allowing people play their API.
At this point I wrote a little dumb script to get from the API the remaining time for my next train.
What can I do with those datas ? I firstly thought create a simple web page with this information and use it on my phone. But that was not convenient enough.
Searching for idea on internet, I found a real USB traffic light (http://www.cleware-shop.de/USB-MiniTrafficLight-EN), it looked really fun so I goes for it …
Official sample of code :
Official example are mainly in cpp, python examples are kind of binding on those cpp library. Having to compile some cpp code to interact with an USB device is kind of boring. I was looking for a python script to directly communicate with my USB device using the python standard USB library. I decided to search what people did on internet.
I saw lots of peoples playing with it in different languages (ruby, python, node, …). I tried several Python codes but I struggle a little bit with it. Some were not working or had strange behavior with my traffic light. The best sample of code I found is https://github.com/rbaier/python-clewareampel
So I decided to debug it and tried to make it work with my traffic light. The code was very straightforward, I found only two potential line which could fail in my case.
Which I simplify verified using lsusb command
And the following one :
To be sure of the hexa code, I ended reading USB docs and adding debug on the cpp code. It was really fun to dig into the cpp code and convert decimal to hexa and compare results with my Python code.
The hexa code of all my sample was similar. As I’m not an USB expect, those hexa values sound legit as it’s described a SET_CONFIGURATION request from my host to the USB device. I assumed it was ok :
Following the doc, we have quite a lot of information in the Setup Packet for bmRequestType
In our case which give :
And about bRequest = 0x09 If we read the Standard Device Requests section it’s SET_CONFIGURATION (0x09)
From this part I started to look one layer above and read some docs about pyusb.ctrl_transfer. Sadly the first sentence was :
I don’t intend to explain the purpose of each transfer and the differences among them. Therefore, I assume you know at least the basics of the USB transfers.
Which is not my case. I decided to give a random try at the way described in the starting guide https://github.com/pyusb/pyusb/blob/master/docs/tutorial.rst#lets-get-it-started
The example in the starting guide does not use ctrl_transfert. It gives me the following code : https://github.com/gaelL/python-clewareampel
Magically it worked. I felt quite lucky, but anyway it’s only a hacky project, I don’t really have big plan for it.
Some implementation later I had this nice fully working traffic light following this color code.
- Blinking, Unexpected error. I’m broken, help me 🙂
- No train for the next one hour.
- Don’t go now
- Be prepared to leave soon
- Go, optimal time to catch the train
- Hurry up, if you walk quite fast, you will be able to catch the train.
My last work was to setup it on a raspberry and that’s it, I had a nice traffic light at home to tell me when I to take the train.