How I got Scratch talking to my Raspberry_Pi

This post now superceeded by this one – just follow the link and you’ll soon be flashing some LEDs 🙂

You may also like...

29 Responses

  1. cymplecy says:

    Updated to use Remote Sensor Connections instead of Mesh and Scratch variable called gpio-output11 now

  2. oaktech says:

    Well, that was easier than I expected…

    Ps… “You can failry easily blow your Pi up” (is failry deliberate?)

  3. cymplecy says:

    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.

  4. cymplecy says:

    Slight change to the instructions to get the order right and changed the python filename to scratch_gpio_handler.py

  5. cymplecy says:

    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.

  6. Andy Larkin says:

    Fantastic work! Thanks for pulling this together.

  7. Claire says:

    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?

  8. Michael Attenborough says:

    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.

    • cymplecy says:

      Sorry – last paste went a bit wrong
      Should be OK now and added downloadable link

      • Michael Attenborough says:

        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!

        • cymplecy says:

          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 🙂

          • Michael Attenborough says:

            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.

          • cymplecy says:

            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

            Simon

          • cymplecy says:

            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 🙂

          • cymplecy says:

            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 !

  9. Phil says:

    hi
    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.

    Connecting… Trying
    Connected!
    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
    self.run()
    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

  10. Phil says:

    sorry the version of Python I have is 2.7

  11. BPK says:

    Hi,
    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.

    Any ideas?

    Many thanks again

    • cymplecy says:

      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

  12. BPK says:

    Thanks Simon – which link!?

  1. 21st July 2012

    […] How I did it How I got Scratch talking to my Raspberry_Pi | cymplecy Reply With […]

Leave a Reply to Phil Cancel reply

Your email address will not be published. Required fields are marked *