mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

cant find python mesh scratch simple examples

Wed Apr 30, 2014 7:43 am

hi

I am looking for a simple example to get me started connecting python to scratch using mesh.

I used the examples from http://wiki.scratch.mit.edu/wiki/Commun ... Python_3.x
I have python 2.7 and 3.3
The example initially didn't work due to

Code: Select all

    b  += a[i]
TypeError: Can't convert 'int' object to str implicitly
So I had to cast this

Code: Select all

b  += str(a[i])
Which seems a bit rubbish being on an official scratch wiki

Anyway after changing this so it runs I get more errors in Scratch which I cant really understand yet 'message too big bad size field'

I am really after a simple example of interfacing the too programs that will give me a kick start.

It seems most of the examples are pretty poor in respect to the fact that they don't really explain how to utilise the connection (if I get it to work). I don't know how to use the broadcasts.

I am looking online but cant really find any clear simple examples

Does anyone know of any?

Thanks

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: cant find python mesh scratch simple examples

Wed Apr 30, 2014 1:40 pm

This is a good starter in that the lower level scratch-python is handled for you.
https://github.com/pilliq/scratchpy

I've not used it myself but I've seen it in use in other progs

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Wed Apr 30, 2014 1:52 pm

Thanks

I am actually after getting an understanding of the mesh python handling. Not many tutorials. Am having to root through code and try and figure it out. But I suppose that's the best way to learn.

Its mainly the scratch side that's seems harder. The passing of the strings etc.

I am trying to get the least obfuscated view of the process so am trying to avoid other peoples libraries, but on the other hand learn from examples.

The examples I see are also often way past beta's and then become too complicated. ha ha

I want the Hello World versions!!

ghp
Posts: 1244
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: cant find python mesh scratch simple examples

Wed Apr 30, 2014 5:25 pm

Hello,

the first 4 bytes are the length of the record in binary. A working sample is, out from my scratchClient software:

Code: Select all

    def send_scratch(self, cmd):
        n = len(cmd)
        a = array('c')
        a.append(chr((n >> 24) & 0xFF))
        a.append(chr((n >> 16) & 0xFF))
        a.append(chr((n >>  8) & 0xFF))
        a.append(chr(n & 0xFF))
        try:
            self.scratch_socket.send(a.tostring() + cmd)
        except Exception as e:
            if logger.isEnabledFor(logging.INFO):
                logging.info(e)
            pass
A description is found in http://wiki.scratch.mit.edu/wiki/Remote ... s_Protocol

Good luck
Gerhard

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 12:38 pm

Thanks

It looks like your code is for use with python 2.7

I am still wondering why the version 3 code doesn't actually work.

I am going to keep looking.

Just trying to understand this 4 byte section yet!

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 12:53 pm

Ok the 4 bytes I get now.

I appends this to the beginning of the string, they are hex values of bytes when displayed, but cast to strings I think

the version in python 3 doesn't perform this well.

Anyway I will keep looking

User avatar
croston
Posts: 686
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 2:03 pm

You could also take a look at https://pypi.python.org/pypi/scratch - this works in both Python 2 and 3.

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 2:24 pm

Thanks

I will look at this code.

will look how python 3 is handles its 4 byte buffer. It seems that the encoding on 2.7 is much more straight forward. Especially how it concatenates it all.

ghp
Posts: 1244
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 3:30 pm

Hello,

tried some things in python3, and think that something like the following snippet will solve the problem:

Code: Select all

cmd = 'broadcast hello'
#
# the length is the number of bytes in the message, not the number of characters.
#
n = len(bytes( cmd, 'UTF-8'))

a = array('b')
# convert len to the first four bytes
a.append(((n >> 24) & 0xFF))
a.append(((n >> 16) & 0xFF))
a.append(((n >>  8) & 0xFF))
a.append((n & 0xFF))
a.frombytes( bytes( cmd, 'UTF-8'))
remark: occasionally, I dislike python.

regards,
Gerhard

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 3:38 pm

Ok thanks

Closest I got for my output was:
b'\x00\x00\x00\x10'broadcast"hello"
from using

Code: Select all

def sendCMD(cmd):
    n = len(cmd)
    a = []
    a.append((n >> 24) & 0xFF)
    a.append((n >> 16) & 0xFF)
    a.append((n >> 8) & 0xFF)
    a.append(n & 0xFF)
    b = ''
    for i in list(range(len(a))):
        b  += str(a[i])

    print(str(bytes(a)) + cmd)

sendCMD('broadcast"hello"')
Not very good with python

What is array('b') for?


I

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 3:42 pm

Ok got it.

Typecode.

Char

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 3:43 pm

Well signed char should I say, for b

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 3:51 pm

ghp wrote:Hello,

tried some things in python3, and think that something like the following snippet will solve the problem:

Code: Select all

cmd = 'broadcast hello'
#
# the length is the number of bytes in the message, not the number of characters.
#
n = len(bytes( cmd, 'UTF-8'))

a = array('b')
# convert len to the first four bytes
a.append(((n >> 24) & 0xFF))
a.append(((n >> 16) & 0xFF))
a.append(((n >>  8) & 0xFF))
a.append((n & 0xFF))
a.frombytes( bytes( cmd, 'UTF-8'))
remark: occasionally, I dislike python.

regards,
Gerhard
Why did you use array('b')
signed char

Rather than array('c')
char

Just trying to get an understanding

Cheers

ghp
Posts: 1244
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 4:33 pm

Hello,

have a look at https://docs.python.org/3/library/array ... dule-array
The 'c'-type is no longer supported. And thinking about the problem a while, I found that sockets do not handle characters, but bytes. In single-byte-charactersets, chars and bytes are same, but in utf-8, chars can be multibyte. So it is more precise to have byte arrays. In most cases this will be not a problem, when you keep texts in ascii range.
The 'u'-type will be removed in later versions, and does not help long term.

Regards,
Gerhard

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Thu May 01, 2014 5:07 pm

Great that Gerhard

Many thanks for your help

mirojr
Posts: 50
Joined: Fri Oct 11, 2013 10:17 am

Re: cant find python mesh scratch simple examples

Fri May 02, 2014 10:00 am

Finally got the thing to work in its absolute basic form. Its a Kernighan type HelloWorld GPIO at best.

I might post it with the scratch interface showing as well, because for novice like myself was a bit tricky finding out how to do very basic broadcast.

Thanks Gerhard and Thomas Preston https://github.com/piface/pifacedigital-scratch-handler
and http://wiki.scratch.mit.edu/wiki/Commun ... with_a_GUI

Code: Select all

from array import array
import socket
import time
import sys
import RPi.GPIO as GPIO

GPIO.cleanup()

GPIO.setmode(GPIO.BOARD)


PORT = 42001

HOST = '127.0.0.1'
if not HOST:
    sys.exit()

print("Connecting...")
scratchSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
scratchSock.connect((HOST, PORT))
print("Connected!")



while True:

    data = scratchSock.recv(1024).decode('utf-8')
    data = data[4:]
    data = data.split(' ')

    GPIO.setup(11,GPIO.OUT)
    
    if data:
        print(data[1])
        if data[1].strip('"') == "pin11-high":
            GPIO.output(11,GPIO.HIGH)
        elif data[1].strip('"') == "pin11-low":
            GPIO.output(11,GPIO.LOW)

Return to “Scratch”