1. A Scratch Networking Sensor Client Library for Python
From the Scratch Wiki:
The experimental extension feature is enabled using the right-button menu on one of the two sensor blocks. When remote sensors are enabled, Scratch listens for connections on port 42001. Once a connection is established, messages are sent in both directions over the socket connection.
Note: For a more detailed description of how to enable/use remote sensors see the Scratch Wiki.
In other words, this means, that one could share global Scratch variables among all remote sensor clients connected to Scratch. The same is true for Scratch messages: they are broadcasted to all clients. For example:
- A remote sensor client instance connects to scratch.
- The user defines a global variable X in Scratch and assigns the value 1 to it.
- This results in a sensor update message send to all clients notifying them, that there is a variable X with a value of 1 assigned to it.
Note: it is also possible that the client introduces a new variable and assigns a value to it. The variable will be introduced to Scratch by an update message.
An update message is sent every time the value of the variable changed by the client or by the server (Scratch).
Also messages could be sent from Scratch to remote sensors and vice versa.
Beside providing a simple API to manage messages and variables, a wrapper framework is provided. The wrapper framework helps with implementing sensor clients that run in the background as daemons. Currently there is a full featured wrapper included for wrapping the GPIOs of an Raspberry Pi into an Scratch remote sensor client. This allows one to access the GPIOs of the Pi from Scratch by simply setting/getting some global variables (There is also an other project providing GPIO access to Scratch through Python)
Raspberry Pi GPIOs as Remote Sensors Part 1
Raspberry Pi GPIOs as Remote Sensors Part 2
2. Further Readings
- Use the Raspberry Pi GPIO Remote Sensor Client
- Write a Sensor Client by Using the API
- Using the Tools that come with this API
3. Project Directory Layout
binShell-Script wrappers to start dummy server
srcSources of this library
setenv.shSet PYTHONPATH for testing
setup.pySetup script to install/distribute
To use this Scratch remote sensor protocol library, Scratch in a version >= 1.3 is needed. If you are going to run Scratch and the remote sensors on different machines, make sure the machines are accessible through networking.
The Python API needs Python 2.7.
5. Get the Sources
The sources are available at github:
mkdir $HOME/src cd $HOME/src git clone https://github.com/wendlers/scratch-pynetsense.git
6. Install the Library
To install the library issue the following command in the top-level project directory:
cd $HOME/src/scratch-pynetsense python setup.py install
Alternatively you could place the “scratch” folder from “src” to a directory of your choice and make your PYTHONDIR point to it. E.g. if you copied “src/scratch” to “~/python/scratch”:
Some examples are located under “example-src/“. On the command line, you could run them after you installed the library like this:
If you prefer running the examples from the project directory (without installing the library), you could do the following:
. setenv.sh python example-src/<ExampleToRun>