raspi as a wireless feedback module

Helge
Posts: 114
Joined: 31.05.2010, 18:32

raspi as a wireless feedback module

Post by Helge » 19.05.2013, 20:56

Hello,

Some weeks ago Halorw inspired me with his idea of a train equipped with a raspi and RFID.
My garden railroad is still cab controlled and an automatic controlled train schedule is due to the lack of sensors and feedback modules not possible.

As owner of a garden railroad, I know wiring can be hard job. My track and the wiring is already laid and I am reluctant to dig new trenches. So, if I want to run my train automatically I have to find a wireless solution.

So what's the solution ? It is a wireless feedback module based on a raspberry pi.

A raspberry has 8 GPIO input pins to connect simple (on/off) sensors and a wlan with TCP/IP connection. So what are the requirements for wireless feedback module:

Automatic connect/reconnect to wlan accesspoint
Automatic connect to rocrail server
Reading the sensor inputs and sending the results to the rocrail server
Power supply via track voltage to avoid 230V AC wiring in the garden

A little bit challenging was the reliable connect and reconnect of the communication link together with a usb/wlan bug for rt5370 wlan chipsets.

For my tests I choose a raspberry pi B rev 2 and raspberry pi A. The aim was to reduce costs and power consumption. The target hardware should be a raspberry pi A.
What I found out was that a raspberry pi B rev 2 works fine even with Logitech WL0145 or WL0084B wlan stick. The A version of a raspi works only well with an Edimax EW-7811Un stick. There are possible workarounds to address this problem (e.g. cron jobs to reconnect the link), but for the time being it is fine for me to know the working combinations.

I attached a picture of my raspi “sensor shield”
It is very much a prototype board and hardened (robust terminals) for the “outdoor” use. It is the B version with a Logitech WL0145 wlan stick.

The reading of a raspi input is quite easy. It requires only some pullup resistors. Be aware that the logic level of a raspi is 3V and not the more common 5V. As long as you connect simple reed sensors to the GPIO there is nothing more to care about.

The more sophisticated electronic part is the step down voltage controller to generate via a rectifier the necessary 5V supply voltage out of the DCC track voltage. I made the easy approach and I used a ready to run module for approx. 3,-€.

That was the “hard” side of the feedback module. For the soft side you need the following files or modifications.

Add your wlan0 to the network config file interfaces
Add your access point data to wpa_supplicant.conf
Copy the python script feedback.py into your pi folder
Copy the new daemon file start_feedbackd into the init.d folder
Add the symbolic links to run the daemon on start (update-rc.d start_feedbackd default)

So what is still to be done.
Up to now I did workbench testing only. The next step will be an outdoor test in order to see how reliable this module is for garden railroading. I will soon come up with the necessary files and hopefully with a rough drawing of the “feedback shield”.

For thoose of you who need more input ports should consider the i2c-bus to expand the ports. But this will require more software and much more hardware work.

Greetings

Helge
Attachments
feedback_shield2.jpeg
feedback_shield2.jpeg (649.71 KiB) Viewed 1086 times

Kereta Api

Post by Kereta Api » 20.05.2013, 10:50

Hello Helge,

I am dealing with the same problems and the last two mounths I am surching on the web for a solution. I think you are the man who is the nearest by this target.

For some reasons I have to ride the trains on Battery so with the Rfid-tags under the tracks and the Reader in the train I want to sent this information to the computer. I also want to try Reader under the tracks and the tags in the train and then send the info "wireless" to the computer, but this is still a great problem.

I will follow your thread.
Succes,
Cor.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 20.05.2013, 14:41

I just finished the controlling and programming software for my Rpi 16 channel servo controller. All that is left is the feedback and it is done. I figure another hour for the feedback portion, and the programming of whole package is complete. Next will be the documentation and install scripts.

Writing the core feedback program was the easiest part of all since I was using srcp as a communications protocol between Rocrail and the Rpi. I spent more time playing with feedback and watching Rocrail objects change color than I did writing it.

This was my first time working with i2c. I can say that with the tools available, it was easy to install and program the I2c based device on the Rpi.

Since the Adafruit 16 channel card is just a nice PC board with a PCA9685, it was easy to kook up to the Rpi. 4 wires and the i2c bus connections were complete.

My biggest time suck was the python programming. I have a love-hate relationship with python due to python's ambiguous typecasting of variables.

The reason I mention all this is that I wanted to encourage you to dive into I2c. The Adafruit I2C libraries are easy to work with if you are using the 16 servo controller. If this is your first time getting into I2C I recommend purchasing the 16 channel controller and using that as a learning device.

If you get the Adafruit 16 servo controller and get stuck, I have a few servo testing programs written that will show how to control speed, direction, and servo travel endpoints.

If you need I/O ports, the MCP23017 would be my first choice. With a capacity of 8 MCP23017 devices on a I2C bus, the total number of I/O ports on a single Rpi is 128!


Richard

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 20.05.2013, 15:22

One more thing. You might consider looking at Rocrail/srcp/Rpi for your feedback solution.

Here is a program that will toggle srcp feedback #7 and back again. Un-comment the print statements to see the srcpd responses. All that is needed is to wrap some logic controls around this. I think that the srcpd loopback device is all that is needed to be configured in srcpd to implement feedback from the Rpi to Rocrail. I have not tested this idea on a stock srcpd server. I know it works with my modified version of srcpd.

-----------------snip here-------------------
#!/usr/bin/python
import socket
import time
s = socket.socket()
s.connect(("127.0.0.1",4303))
#print s.recv(256)
s.send("go\n")
#print s.recv(256)
s.send("set 1 fb 7 0\n")
#print s.recv(256)
time.sleep (3)
s.send("set 1 fb 7 1\n")
#print s.recv(256)
s.close()
------------------snip-here-------------

Helge
Posts: 114
Joined: 31.05.2010, 18:32

Post by Helge » 20.05.2013, 16:17

Hello,

Thank you Cor and Richard for your responses and encouragement to go a step further.

My Garden railroad is just small layout and I need only 12 sensors, at least for the beginning. So two raspis at different places on my layoutr will do it. There are lots of offerings for raspi i2c bus shields on the web. I will consider the i2c bus for my next project.

As promised, I have attached the necessary files for the raspi. In alle places where you will find XX you have to replace it with your network data.

My accesspoint is a raspi too, so you will find the ssid “raspberry AP” in the wpa-supplicant.conf. You can replace it with to your ssid. Within the files interfaces and wpa-supplicant.conf you will find the name raspberry. It is a label and it links the wlan0 configuration to the corresponding part in the wpa-supplicant.conf, so don't change it.
The current network configuration requires an accesspoint with dhcp server.

In order to adapt the feedback module to your layout and rocrail server, you have to customize a small part of the python script feedback.py. The sensor ids are the rocrail ids.

#user specific data: ip address rocrail server and rocrail sensor ids
rr_server_ip = 'XXX.XXX.XXX.XXX' #your rocrail server address
sensor_1_rr_id = 'XXXX' #your sensor ids
sensor_2_rr_id = 'XXXX' #your sensor ids
sensor_3_rr_id = 'XXXX' #your sensor ids
sensor_4_rr_id = 'XXXX' #your sensor ids
sensor_5_rr_id = 'XXXX' #your sensor ids
sensor_6_rr_id = 'XXXX' #your sensor ids
sensor_7_rr_id = 'XXXX' #your sensor ids
sensor_8_rr_id = 'XXXX' #your sensor ids

Replace the XXX with your ids and save the modified script to the pi folder.
To get the whole thing running at start up, you have to copy a daemon file start_feedbackd into the init.d folder and to create the symbolic links via sudo update-rc.d start_feedbackd defaults for the runlevels

I think that's it.
I removed some debugging and testing information from feedback.py, so I hope it will still run. From my point of view, python is all little bit tricky regarding the code structure. But I am a python novice.

Greetings

Helge
Attachments
feedback.zip
(3.11 KiB) Downloaded 78 times

Helge
Posts: 114
Joined: 31.05.2010, 18:32

Post by Helge » 20.05.2013, 16:36

Hello Richard,

Thank you very much for you code example :) . I am using the native rocrail protocol. Rob provided me with a short xml statement and it works perfectly.
Years ago, I was dealing with srcp. It was my first approach to control my railroad via TCP/IP protocol. I developed my own small "App" for a Sharp Zaurus PDA. It worked very well with srcp but then I found rocrail, which offered much more functionality to me.

Thanks and greetings

Helge

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 20.05.2013, 18:39

The nice part about Rocrail is it's ability to support multiple controllers.

That built in design spec and the srcp enhancements that Rob so graciously provided make SRCP a robust and attractive solution.

Richard

PS I just finished testing the feedback code for my servo controller! Now to write the documentation, the install script, and the documentation. I discovered that TCP/IP (socket) connections from python require that anything received from the connection be printed so that the input buffer gets flushed. To do that, redirecting the print statements to /dev/null works just fine.
Last edited by Richard-TX on 21.05.2013, 01:58, edited 1 time in total.

LDG
Site Admin
Posts: 2672
Joined: 18.10.2010, 00:03
Location: near Karlsruhe/Germany

Post by LDG » 20.05.2013, 19:18

Hello Richard,
Richard-TX wrote:I discovered that TCP/IP (socket) connections from python require that anything received from the connection be printed so that the input buffer gets flushed. To do that, redirecting the print statements to /dev/null works just fine.
You must fetch the server's response from the socket. Assigning the response to a variable

Code: Select all

answer = s.recv(256)
is sufficient. Usually you should always check/parse the response from the server ("200 OK"/"4<x><y> <error text>") :roll:

Regards,
Lothar

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 21.05.2013, 02:06

Thanks for the tip Lothar.

Not sure how I missed that.

works perfectly.

Now I have to clean up the mess from all the stuff I tried.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 24.05.2013, 15:36

I completed the Rpi servo controller with feedback. The feedback portion was the easiest part of that whole project.

Based on srcpd, all that is needed is some I/O to some external device on the Rpi and then a little bit of python code and Rocrail sees the feedback.

Feedback and switch states are saved. When the system is booted, all of that info gets reentered into srcpd.

Helge
Posts: 114
Joined: 31.05.2010, 18:32

Post by Helge » 08.08.2013, 14:52

Hello,

After several weeks of testing and struggling with rough environment on my garden layout the feedback module has reached a final status and works very reliable.

I had a lot of problems with false alarms. I connected my reed sensors via shielded cables to my feedback module, but this proofed not to be sufficient enough. Therefore I had to modify the python script and to add a filter function to suppress false alarms.

You'll find the new script and an eagle schematic of the board layout in the attached zip file.
I haven't test the eagle files yet, so I can not guarantee that its error-free.

My next project/idea is an simple accessory module being able to set 8 outputs for leds or something else. In this context my question to Rob or all of you is:

Can I use the rocrail tcp/ip xml protocol for an accessory module like the feedback module and what would be an example xml command string to set an output.

Best regards

Helge
Attachments
feedback2.zip
(62.95 KiB) Downloaded 80 times

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 08.08.2013, 15:28

I have found that the easiest way to tell Rocrail that a sensor has been triggered is to use the srcp service built into Rocrail.

Attached is an example program that can be run on the Rpi that will set a Rocrail FB device using srcp.



Change the ip address to the machine where rocrail is running.

It is invoked with "setfb -a <fb num> -p <0 or 1>"

To do things manually, telnet to rocrail over port 4303. Assuming that a sensor is defined correctly, here is what is sent. First the #2 sensor is turned off and then it is turned on.

# telnet 10.232.1.4 4303
Trying 10.232.1.4...
Connected to 10.232.1.4.
Escape character is '^]'.
Rocrail 2.0r5671; SRCP 0.8.4; SRCPOTHER 0.8.3
go
1375971392.507 200 OK GO 1
set 1 fb 2 0
1375971417.257 200 OK
set 1 fb 2 1
1375971554.914 200 OK

There is no quit for a srcp session so a cntl] q is needed to quit the telnet session.

Here is how I have FB2 defined in my POC (Proof of Concept) Rocrail installation.

When it comes to Outputs on the Rpi, then that is a different issue. I will address that in another posting. I chose SRCP as a communications protocol between Rocrail and the Rpi but the program could be easily modified for your needs.
Attachments
setfb.txt
(1.33 KiB) Downloaded 19 times
fb2.JPG
fb2.JPG (27.91 KiB) Viewed 920 times
Last edited by Richard-TX on 08.08.2013, 16:34, edited 1 time in total.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 08.08.2013, 16:26

Outputs on a Rpi controlled by Rocrail.


There are a few different things that need to be addressed when considering using a RPI as a accessory output decoder.

1 - The output (LED) interface.
2 - The RPI limitations
3 - communications protocol

In the case where an all singing, all dancing LED controller is desired, then leveraging the 16 channel LED/Servo controller from Adafruit is about as good as it gets. Up to 1,984 LEDs can be directly driven per RPI. If more than one LED is being driven per controller output, then additional transistors are needed due to the increased current demands.

The reason that the Adafruit controller is such a huge win is that the LED brightness is adjustable in 4096 steps from full on to full off. Another reason is that the controller provide level shifters from the 3 volt RPI bus and the +5 volt LED supply.

Relays are always an option but none are exactly cheap especially when it comes to interfacing with the RPI. The Sainsmart relay boards is about all that is out there but there are some things that may need some tweaking. The Sainsmart relays boards aren't a robust design but it doesn't take much to make them so. On the plus side the Rpi control software is simpler for relays and the interfacing per output a little simpler than the LED controller. I use a MCP23017 chip to control relays. It costs a couple of bucks and controls 16 outputs. A max of 384 outputs (8 chips per I2C bus and 8 chips on the SPI bus) can be realized per Rpi.


Software and communications - This is a tough one. No matter what communications protocol is used between Rocrail and the Rpi, it still has to be translated. As a result, a few python scripts are needed. The python scripts will provide the translation from commands that Rocrail sends to the commands and logic for device control.

I have a script called "dorelay". What that program does is translate from an integer received from Rocrail to bit manipulation of a MCP23017 register on the Rpi's I2C bus. It is not difficult to write; just time consuming.

Protocol
I chose srcp because I believe that srcp is the only way that true system interoperability is going to be realized. There are too many proprietary protocols out there and if all manufacturers supported srcp then Rob's job would be a lot easier. It also can remove some of the more ridiculous DCC limitations. As far as srcp and Rocrail goes, it is mostly plug-and-play. Enough about that.

I have no idea what is really required to interface the Rocrail protocol to the Rpi. I have never tried. As a result I can offer no suggestions or support at this time.

If you want more info regarding interfacing and control of LEDs or relays on the Rpi, let me know. I have done a fair amount of it and have some basic software tools that should help you if you should need it.

Your experience with false triggers is one that is very common. Even more common is switch bounce. There are a few ways to add the needed hysteresis to solve switch bounce issues. I have found that a combination of software and hardware design solves any of those issues.


Richard

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 09.08.2013, 06:04

I also completed a RPI track sensor project. It was more work than I originally anticipated due to the features I wanted. The trick was to let the sensors interrupt the RPI so that constant polling was not needed. It works. It is based on the MCP23S17 or MCP23017.
Last edited by Richard-TX on 09.08.2013, 14:30, edited 1 time in total.

Richard-TX
Posts: 1320
Joined: 10.07.2012, 04:00
Location: Texas, USA
Contact:

Post by Richard-TX » 09.08.2013, 14:18

The I2C bus chips are dirt simple to add and program. Much easier than bit banging the individual GPIO ports.

Post Reply

Return to “DIY Hardware”