Scratch2Pi (part 2)

Previous blurb here

So having got technoboy10 twitter extension.js file up and running – I went about playing with it to alter it to  try and make it talk to a python program running on the local Pi.

And eventually I ended up altering the twitter extension.js file so instead of making GET  requests to obtain tweets, I made it make POST requests passing a pin number and value from a Scratch custom block.

ext.digitalWrite = function(pin, val) {
 var myKeyVals = { "writepin" : pin, "val" : val };

 var saveData = $.ajax({
 type: 'POST',
 url: "",
 data: myKeyVals,
 dataType: "json",
 success: function(resultData) { console.log("OK") }
 saveData.error(function() { console.log("who knows"); });

(As usual – this code worked – I’m sure in future I’ll be getting rid of half of it or doing it a completely different way 🙂

So “all” I needed now was a python prog to listen out for the POSTs and then decode and control the pins based on the key/value pairs sent.

As I was already running a lighttpd webserver on port 80 – I wanted to run another one on a different port so I just picked 8181.

I found this little bit of python and with then just added little bit of code in the do_post function to parse the data, and then use that to switch pins off and on via RPi.GPIO 🙂

(more to come – how do I get to read pins and pass back to Scratch?)

You may also like...

4 Responses

  1. To answer your question on how you can pass back to Scratch, that’s not possible using a standard web server like you’ve got above; you’ll need to use something else to read the data and sent it to the webpage.

    One of the best ways to do this is to use websockets – which like the name suggests are just like normal sockets (like the ones you use to communicate Scratch1.4 gpiohandler) except they work in webpages as well.

    You’d implement this having a Python websocket server running on the Raspberry Pi which can send and receive data to the websocket client on the webpage. You’d have it monitoring the pins, and when one of them changes you could send a message to the webpage which updates a variable for that pin, which can be detected in Scratch.

    Websockets would also allow you to get rid of the webserver code you’re using at the moment, by replacing the GET requests with much smaller and quicker websocket messages from the webpage to the python server on the Pi.

    The websocket server that I always use is because it’s fast, works well, and it only a single file. The guides there should also get you going – they’re all the documentation I’ve ever needed when I’ve worked with websockets.

    One downside with websocket is that when you’re not running them over TLS (which I presume you’re not) they can’t go through proxies. I’m not sure if the schools that you’re at have a proxy in place, but mine does and it’s a pain to get round.

    An alternative to websockets is Firebase which actually use websockets, but do it over TLS so proxies aren’t an issue, host the server for you, and provide a nice API to use. I’ve used this before in the exact scenario that you’re trying to do (ScratchX Raspberry Pi GPIO) but I seem to have misplaced the code for it, and the version I put on GitHub is an older, non-complete one 🙁

  2. cleoqc says:

    Thanks for documenting your process. It will be my turn soon enough.

Leave a Reply

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