How I got Scratch talking to my Raspberry_Pi by cymplecy · 20th July 2012 This post now superceeded by this one – just follow the link and you’ll soon be flashing some LEDs 🙂
Updated to use Remote Sensor Connections instead of Mesh and Scratch variable called gpio-output11 now
Well, that was easier than I expected…
Ps… “You can failry easily blow your Pi up” (is failry deliberate?)
OK, the latest version of the Python handler maps four outputs from Pins 11,12,13 and 15 and also sets-up Pins 16,18,22 and 7 as inputs with pull-up resistors. The code is a bit cleaner as well.
Slight change to the instructions to get the order right and changed the python filename to scratch_gpio_handler.py
Latest version introduces gpio-ouputall.
gpio-outputall which should be set to binary patterns such as 1000 (same as gpio-output3 = 1) and 0101 (same as setting gpio-output2 and gpio-output0 to 1). This allows for simultaneous changing of pin outputs without having wait statements.
Fantastic work! Thanks for pulling this together.
When I get to here Use nano and copy and paste this code and save it (Ctrl-O) as Blink11.py I can the following error message… File “blink11.py”, line 3, in
GPIO.setmode(GPIO.BOARD) any ideas?
Did you run it using sudo e.g
sudo python Blink11.py
as to use the GPIO you need to be running as the admin (root) 🙂
The scratch_gpio_handler.py script in this post seems to have lost most of its indentation, which is pretty terminal for a python program. Any chance you can fix this, or make the script downloadable somehow? I’m very keen to try this out.
Sorry – last paste went a bit wrong
Should be OK now and added downloadable link
Thanks for that, I now have it working!
But I had to un-comment line 160, ‘data = data[4:].split(” “)’ to make it work with Scratch version “1.4 (Linux) of 2-Dec-11”. Perhaps for the benefit of other users you could say whether this is a bug in the script or something which depends on the version of Scratch.
Thanks for writing this page, anyway – it seems that nobody else is providing documentation on this interesting subject!
Hi Michael – sorry code was in transition – should be OK now – check out additional changes re using broadcasts at the end of the article and let me know what you think please 🙂
Yes, that works fine now, and with the broadcast method too!
In the spirit of helpful criticism, I’ll point out this though: line 150, ‘print ‘Data %s :’ % dataraw’ includes the 4-byte binary length count in the printed data, which sends binary data to the terminal window and makes a bit of a mess. I’d comment out this line, and if you want some diagnostic info then uncomment line 161, adjusted slightly to read “print ‘Length: %d, Data: %s’ % (length, data[4:])”.
And on the practicalities of downloading the code on a RasPi: I couldn’t get the download from DropBox to work from Midori, but if you hover over the top lines of the code listing above, some icons appear at the top right. The “copy to clipboard” doesn’t seem to work, but if you click “view source” you get the listing in a new tab and you can then Ctrl-A Ctrl-C to copy the code and then paste it into LeafPad.
I’ll change it in my next version – Ta – the code is just a complete bodge jsut to get things going, I’m still experimenting 🙂
I’ve added your helpful downloading hint – we’ll need to find a file repositry that’s compatible with Midori
You might be interested in the variable display issue BTW
I did the bodge of printing all of data as the commented method doesn’t give the full broadcast message (as I think sensor-update msgs and broadcast msgs are slightly different but I haven’t got into that yet as I’m a JDI bodger and not a careful person – if you fancy taking a look at what’s going on it would be handy 🙂
I’ve made some major changes to scratch_gpio_handler, much better handling of the Scratch messages, added in concept of a gpio-pattern variable that does what gpio-outputall used to do (e.g bit pattern control) and made gpio-outputall now do the same as the allon/alloff broadcast and just switch all the outputs on or off.
PS its now set to use pins 11,12,13,15,16,18 as outputs and 22 and 7 and two inputs
Note this issue with using variables which explains why my Scratch scripts sometimes don’t seem to intialise properly !
I’m very interested in this project, the scratch_gpio_handler.py script above seems to be giving my system some issues, I’m the latest release of rasbian – this has python 3.2.3 and I’m wondering if that’s the problem, the scratch script seems to running I see the status of the output changing within Scratch, but this is not being reflected at the gpio – led stays off.
the simple blink11.py works with no problem.
here is the error I see.
Data sensor-update “gpio_output0” 1 :
Length: 31, Data: s
Exception in thread Thread-2:
Traceback (most recent call last):
File “/usr/lib/python2.7/threading.py”, line 551, in __bootstrap_inner
File “scratch_gpio_handler.py”, line 101, in run
pin_value = (pin_value_map >> i) & 0b1
NameError: global name ‘pin_value_map’ is not defined
Data sensor-update “gpio_output0” 0 :
Length: 31, Data: s
hope you or someone can help
1. the variable should be gpio-outputx e.g – not _ 🙂
2. But it shouldn’t error anyway! – I update script all the time so check if you are using V0.1 (its in the comments at the beginning) I’ve just tried it here and no error even if using gpio_output0 instead of gpio-output0
OK changing it to gpio-output – resolved my issue
Thanks for your prompt reply and for this blog
so now I’m having a bit of trouble reading in the inputs, I do not see either listed in the sensor value selection box, all I see are (slider, light, sound, resistance A..D, tilt and distance) with or without the check box selected – any ideas?
If the scratch_gpio_hander.py isn’t running then this can happen
If you haven’t enabled Remote Sensor Connections in Scratch then the same effect
You need both together 🙂
the remote connection is deffinately made, the outputs are updating as expected.
I’ll certainly give the beta a go
Maybe its time to become an alpha tester of my easy peasy way of doing it that creates desktop icons that do all the work for you 🙂
sorry the version of Python I have is 2.7
Thanks for your script and sharing it. I do have it working but with the following issue:
If I create a scratch script that turns gpio-output1 variable to 1 sleeps for a bit and then turns the variable to 0 all works. However, it blocks after I set the variable to 1 and before I set it to 0. Scratch and my RPI freezes and I cannot do anything (even move the mouse).
I want to be able to say set gpio-output1 to 1 on down arrow key press and only set it back to zero on up arrow key press. Due to the problem above if I create this script it sets gpio-output1 to 1 correctly but then hangs and does not listen to the up arrow key press.
Many thanks again
Hi BPK – there may be a mistake in the latest python handler script on that post -I’ll check it out – I’d recommend switching to using the scripts via this link as that is where I’ve moved onto now regards Simon
Thanks Simon – which link!?
The most recent post on this blog