Scratch Extensies

Scratch is een programmeertaal voor kinderen, of anderen die willen kennismaken met het programmeren van een computer. Zoals ik eerder schreef wordt het pas echt leuk op het moment dat je vanuit je Scratch programma iets buiten je computer kan besturen. In deze post heb ik beschreven hoe dit kan met de offline versie van Scratch. Extensies (JavaScript extensie) blijken echter ook prima te werken in de online versie van scratch.

Hoe dat moet? Dat beschrijf ik hier in het onderstaande stapppenplan.

    1. Ga naar de scratch website: http://scratch.mit.edu
    2. Klik op ‘Create’.
    3. Als de ‘Scratch Project Editor’ is gestart druk je op de F12 toets. Nu verschijnt er onderin je webbrowser een nieuw scherm (zie screenshot).
      Screenshot from 2014-10-10 09:31:35
    4. Type het onderstaande commando op de onderste regel van van het scherm (zoals is te zien in de screenshot hieronder), en druk op enter. Op deze manier wordt een JavaScript extensie geladen.
      ScratchExtensions.loadExternalJS("https://raw.githubusercontent.com/LLK/scratch-extension-docs/master/weather_extension.js")

      Screenshot from 2014-10-10 09:35:05

    5. Druk wederom op de F12 toets om de ‘developer tools’ te sluiten. En klik op ‘more blocks’. Er is nu een extra blokje zichtbaar geworden wat zojuist is geladen (zie screenshot).Screenshot from 2014-10-10 09:38:17
    6. Het nieuwe blokje kan worden gebruikt in een Scratch programma. In dit geval wordt het weer (de temperatuur in Fahrenheit) bepaald. Wederom, zie onderstaande screenshot voor een voorbeeld programmaatje (wanneer op het vlaggetje geklikt wordt roept het katje de huidige temperatuur in Boston).Screenshot from 2014-10-10 09:42:06
    7. Een scratch programma waarin extensie worden gebruikt kan normaal worden opgeslagen op de Scratch website. Omdat er gebruik wordt gemaakt van extensies, die nu nog experimenteel zijn, is het niet mogelijk om deze Scratch programma’s via de Scratch website te delen.
    8. Bij het opnieuw laden van het Scratch programma moeten stap 3, 4, 5 opnieuw worden uitgevoerd. Dit kan worden opgelost door gebruik te maken van GreaseMonkey, een plugin voor de firefox webbrowser (er is vast ook wel een vergelijkbare tool voor andere browsers) waarmee extra JavaScript (het laden van een paar extensies bij het openen van Scratch) kan worden uitgevoerd.

Op dit moment zijn de Scratch extensies nog experimenteel. Voor zover ik kan beoordelen is het Scratch team zeer zorgvuldig met het toevoegen van nieuwe functionaliteit, wat bij dit soort extensies helemaal belangrijk is. De laatste berichten zijn dat extensies in 2015 worden toegevoegd aan de officiele versie van Scratch.

 

Scratch

Scratch (link) is een omgeving voor kinderen om kennis te maken met programmeren.Het is een visuele programmeertaal waar je doormiddel van het slepen, en aan elkaar koppelen van allerlei blokjes een programma kan maken. Bijvoorbeeld om een animatie te besturen, of een eenvoudig spelletje te maken. Scratch draait in een webbrowser (geen extra software nodig), maar kan ook als een offline programma draaien.

Scratch is leuk, maar het wordt pas helemaal leuk als je vanuit je scratch programma kan samenwerken met dingen buiten je computer:  een website, een robot, een thermometer, noem maar op. De offline versie van Scratch heeft voor dit doel de HTTP extensie waardoor het mogelijk wordt om speciale blokjes te maken die informatie uitwisselen met websites.

Omdat de HTTP extensie experimenteel is in de huidige versie van Scratch, en deze daarom nogal verborgen zit in de software beschrijf ik hierbij hoe een HTTP extensie kan worden gemaakt.

De HTTP extensie bestaat uit een JSON document waarin de nieuwe blokjes worden gedefinieerd, en de locatie van de webserver wordt opgegeven. Bijvoorbeeld:

{ 
  "extensionName": "Mijn module",
  "useHTTP":true,
  "host": "192.168.1.44",
  "extensionPort": 1880,
  "blockSpecs": [
    [" ", "Sonos %s", "scratchSonos", "Hallo Scratch"]
  ]
}

Een meer gedetailleerde specificatie is ook beschikbaar (link). In het kort is de betekenis dat het gaat om een extensie met de naam ‘Mijn module’. Het is een HTTP extensie (useHTTP is true), die beschikbaar is op ip adres 192.168.1.44, en poort 1880. In blockSpecs staat een lijst van blokjes, met per blokje een lijstje parameters per blokje. Achtereenvolgens zijn dat type, format, functie, gevolgd door 0 of meer default waarden.

Op het moment dat Scratch dit blokje gaat uitvoeren wordt een HTTP GET call gedaan naar adres:

http://192.168.1.44:1880/scratchSonos/Hallo%20Scratch

De JSON extensie kan in de offline versie van Scratch worden geladen door het menu ‘Bestand’ te klikken terwijl de linker shift-toets is ingedrukt. Door het indrukken van de linker shift-toets verschijnt de extra menu optie ‘Import experimental HTTP extension’ die dan ook moet worden gekozen.  Selecteer nu het JSON bestand, en klik op OK. Onder ‘Meer blokken’ is nu het nieuwe blok te vinden. Zie bijgevoegde screenshots.

scratch1
Klikken op Bestand met de linker shift toets ingedrukt geeft de extra optie ‘import external scratch extension’ waarmee het mogelijk wordt om een HTTP extensie te laden.
Na het succesvol laden van de HTTP extensie zijn nieuwe blokjes zichtbaar in de categorie 'Meer blokken'
Na het succesvol laden van de HTTP extensie zijn nieuwe blokjes zichtbaar in de categorie ‘Meer blokken’

Een snelle voorbeeld scratch applicatie is die waar door klikken op de ‘kat’ wordt gevraagd om je naam in te vullen. De ingevulde naam wordt vervolgens via de HTTP extensie naar een text-to-speech engine gestuurd die via Sonos (een draadloos speakersysteem) de tekst weer uitspreekt. Dit laatste is niet beschreven in deze post, dat komt vast een andere keer.

scratch3
Een scratch programma waarin een blokje uit de HTTP extensie wordt gebruikt.

 

 

 

Home Automation Spaghetti

Lightweight message brokers like mosquitto, that makes use of the MQTT are of great use in the world of home automation and the Internet-of-Things. With MQTT, functionality like interfacing with specific hardware, interfacing with webservices, reading/writing databases can be dived into separate programs/scripts instead of putting everything together in a complex monolithic application. This provides flexibility, and a means to extend existing functionality by listening in on existing MQTT topics, that already being published.

What I like about a lightweight message bus is illustrated by the following example which I’m actually using at the moment. One program (a perl script) is connected to the OVMS webservice, where it listens to updates on the status of my electric vehicle, a Twizy, and publishes this data   (location, state of charge, speed, …) in the form of a JSON encoded object via MQTT. Another program, which is driving a LED matrix display (located in the kitchen), is subscribed to these MQTT messages, and uses them to display the actual charging state on the display.

 

UntitledWith this in place, It is possible to add a third program that writes all data to file, a fourth program that switches a wall outlet when the state-of-charge has reached 100%, a fifth program that plays an audible message when the Twizy is arriving home, a sixth .. etc. etc. While this setup is great, and is very, very flexible, the relation and dependencies between publishers and subscribers if written in program code, and therefore difficult to understand.

And that’s where Node-RED come into the picture. Node-RED is visual tool that displays the process flow between (the spaghetti) between a number of building blocks. With Node-RED it is possible to visually design a flow that, in the context of the previous example, trigger on messages from the Twizy, transform these in to readable text, and publish them to a LED matrix output. It is still spaghetti, but now it’s visible. A sample flow that I built recently is that where updates from the Twizy (received through MQTT, which is the block on the left), are routed based on changes in distance or charging state (blocks DISTANCE and STATE), transformed into a set of different messages, and then publised to a Text-to-speed engine (SONOS), or mobile phone (with Notify My Android).

twizyfbp

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Connecting the Twizy

A post on the twizyowners forum about timed charging the Twizy triggered me to document me recent progress in Twizy and home automation hacking and write this post. In this post i’m trying to explain how I connected the Twizy to my home automation system, and controlled charging of the Twizy. Until recently we just plugged in the AC cable of the Twizy when arriving at home (in fact we still do). But what annoyed me was that a ventilator kept on running for hours and hours while Twizy charging was completed. What I wanted to do was disconnect the plug from the socket when charging was finished to avoid (from my perspective) unneeded energy consumption. The most simple and robust method for achieving this is walk to the garage and unplug the Twizy. I decided to do it automagically via home automation. A complete schematic of the whole setup is depicted below.

Connecting the Twizy to a home automation system can be approached from two perspectives: 1) receiving status updates from the Twizy and 2) controlling the Twizy

Receiving status updates from the Twizy

Our Twizy is extended with a so called OVMS Module. The OVMS module is a small device (with GPS and GPRS) that talks to the CAN-bus of the Twizy. OVMS stands for the openvehicle open source hardware/software project. Details on this project and how to get the OVMS module are available on the open vehicles website (http://www.openvehicles.com).

By default the OVMS module is configured to log status updates (GPS location, State of charge, Temperature, Speed, Odometer, and much more) over the Internet to a central server hosted by the open vehicles project. When communicating with the Twizy, you connect to the OVMS server instead of the car. A sample client application in Perl is provided on the github site of the project (client_app.pl). In order to get this client running you probably need a Linux compatible system, install the required perl modules, and create a small configuration file (ovms_client.conf) where you must enter the location of the OVMS server and your OVMS module credentials. The client_app program does not really ‘do’ something. When started, it connects to the OVMS server, decodes and prints out status update messages from the Twizy.

In order to connect the Twizy to my home automation system I modified the client_app.pl script so that the content of messages is decoded into understandable data (GPS location, charge status, speed, odometer, etc) and the the Twizy status is made available to other applications via an MQTT message broker (mosquitto). My modifications to the script can be downloaded here (client_app_mqtt.zip).

Control the Twizy

For now the only Twizy control I need is switching the AC socket for charging. For this purpose I make use of a Conrad/ELV FS20 wireless switching socket (link). This switch is triggered by an 868Mhz OOK encoded protocol. Luckily, the JeeLink is capable of transmitting this protocol.

The OVMS client application contains a rule that when the state of charge (SOC) reaches 100%, an MQTT message with the topic ‘FS20’ is being sent. The contents of the message is a JSON encoded structure with the details of the FS20 message (address of the socket and command 0 for off and 17 for on)

if ( $status->{soc} == 100 ) {
  mqttpublish('FS20',"{"hc":4865,"addr":0,"cmd":0}","FS20");
} else {
  mqttpublish('FS20',"{"hc":4865,"addr":0,"cmd":17}","FS20");
}

The JeeLink – MQTT bridge script (see this post)  subscribes to the ‘FS20’ MQTT messages and forwards them to the JeeLink. A modified version of the JeeLink-MQTT bridge which encodes FS20 messages is available here. The JeeLink then transmits a FS20 protocol message and the socket is switched.

Cosm Popularity

How popular is Cosm? I consider myself as a heavy user of the free Cosm webservice for storing all sorts of sensor data (home, energy, washing machine, sports). Not that I am a huge fan of the service for this moment its ok for me. Features like triggers, debugging and privacy groups are really helpful.

But somehow I always get the feeling that Cosm is not really taking of, and that the number of  subscribers (usually very important for a free service) is not enough to create a profitable business.

To get insight in the growth of Cosm, I used the feedid, identifier of a stream of sensor data and the fact that most feeds are public.  This feedid is a positive integer, newly created feeds always have a greater value of the feedid than older feeds. To me, the feedid looks like a simple counter, possible proportional to the number of feeds.

What I did was the following: I loaded ‘http://www.cosm.com/feeds/<feedid>’  in my webbrowser, and wrote down the creation date of the particular feed.  Then I requested the same url, but now for another feedid. So I queried Cosm from 0 to 85000 in steps of 5000. What I got was the following chart.

In contrast to what I thought, the number of feeds (and probably users) is still increasing. Also the amount of increase (now approx. 160 feeds per day) is growing.

Coming back to the initial question about the popularity of Cosm. The trend for Cosm seems to be ok, but will it be enough for them?

 

Wachten op de Pebble

In mei 2012 heb ik meegedaan met de crowdfunding van de Pebble. De Pebble is een slim horloge dat in verbinding staat met je mobiele telefoon. Met de Pebble kan je door middel van apps berichten lezen, gemiste gesprekken zien, de muziek bedienen of als sportcomputer gebruiken. Die apps kunnen naar wens worden geinstalleerd of zelf worden gemaakt met de Pebble SDK.

Zelf ben ik van plan de Pebble te gebruiken tijdens het fietsen, skeeleren en schaatsen. Kan je zonder je telefoon uit je broek/jas/achterzak te halen zien hoe hard je gaat, of hoeveel kilometer je hebt gedaan. Dan is zo’n horloge een stuk makkelijker. Op dit moment gebruik ik daar nog de LiveView (van SonyEricsson) voor. Met de Pebble moet dat als het goed is een enorm stuk verbeteren. Met name het display van de LiveView is met een beetje zonlicht niet meer te zien. Het e-paper display van de Pebble is juist bedoel om buiten met direct zonlicht goed te kunnen aflezen. Ik ben benieuwd.

De crowdfunding van de Pebble was (in ieder geval voor de makers van Pebble) een magesucces. In totaal is meer dan 10 miljoen dollar opgehaald! De makers zijn aan de slag gegaan, en via updates om de drie weken worden de ‘backers’ (iemand die geld heeft gestoken in de Pebble) op de hoogte gebracht van de voortgang van het project. Er wordt nog geen definitieve opleverdatum gegeven vanuit de gedachte dat het missen van een deadline erg nagatief is. Door de inhoud van de updates krijg je goed inzicht in de voortgang, en kan je zelf zien hoe het staat met de Pebble: De eerste pre-production van de Pebble is gedaan. Nu volgen een aantal cycli van testen en produceren alvorens de uiteindelijke massaproductie wordt gestart.

Inmiddels is het Oktober 2012, en is de Pebble nog niet af. Tijdens de initiele crowdfunding is een geschatte oplever datum van september afgegeven. In het forum en ook op Twitter is goed te merken dat er Backers zijn die het wachten zat zijn, of moeilijk kunnen omgaan met de onbekende opleverdatum. Ik ben benieuwd hoe dit uitpakt voor Pebble. Volgens Pebble wordt de frequentie van de updates opgevoerd naar om de twee weken.

The hidden features of Cosm, or a Java API for Cosm

Recently, Pachube changed its name to Cosm (http://cosm.com). The new website appears to be a redesign of the pachube website. The core functionality of Pachube/Cosm is still the same. A powerfull feature of Pachube/Cosm is Groups (https://cosm.com/docs/beta/group/). This feature makes it possible to create a group of Cosm users, and make private feeds available to the group. To me, this functionality resembles the functionality available in popular social networks like Twitter, Facebook and LinkedIn. Unfortunately the group feature is only available with the API, and not through the website of Cosm.

The Pachube makeover was also noted by ReadWriteWeb, which reports that the makeover is disappointing. I hope that this type of news will not impact the popularity and availabillity of Pachube/Cosm, because I think that the core functionality is unique.

For me, this was the motivation to create a Java library for Cosm, that is intended to support all Cosm API functionality. This API should make life easier when programming against the ‘hidden’ features of Cosm. The library is JCosmAPI, and is hosted at github. The library is based on JPachube, but with several important features which are different from JPachube.

At the time of writing this post, I just finished an almost complete implementation of the API. Testing and documenting is lacking.

JCosmAPI is available for download at: https://github.com/pieterjm/JCosmAPI

 

 

The JeeNode and the Dragino

At this moment I have installed a number of JeeNodes in my house for energy monitoring and home automation purposes. At this time of writing I am using JeeNodes for:

  • Monitoring home electricity consumption based on a current (clamp) and voltage sensor (AC-AC transformer) using the recipe of the openenergymonitor project described here.
  • Monitoring gas consumption based on a hall-effect sensor, which is described here
  • Sending/Receiving OOK encoded signals for:
    • S555TH temperature/humity sensors
    • FHT80b wireless room thermostats
    • FS20 wireless relays
  • Controlling my central heating system (see a previous post on that topic)
  • Reading the status and controlling a washing machine (see this post)
  • Displaying status on a scrolling 8X128 led matrix (not yet documented)

For communication with the outside world (the Internet) I used a JeeNode USB attached to the USB port of a freetz’ed FritzBox 7170, and a dedicated serial to Internet proxy program on the FritzBox. While this setup worked fine, it was difficult to make changes (recompile new firmware etc.). This was the reason to switch over to the Dragino system.

After doing some experiments with a JeeNode attached to a Dragino, I installed a JeeNode SMD (very flat form factor) into the Dragino case.

The new JeeNode SMD

The JeeNode is connected to the dragino cable via a simple cable (details described here). The JeeNode itself attached with Velcro to the cover of the Dragino. The picture below shows the Dragino board and my JeeNode connector cable.

And the end result, a nice clean box

The JeeNode is programmed with a dumb tranceiver program. The only function of this program is to convert messages received over 868Mhz to a hex encoded message on the serial port, and vice versa: hex encoded messages arriving through the serial port are transmitted over 868Mhz.  The arduino sketch for this tranceiver program is here.

The dragino runs a lua script that talks to the serial port, decodes incoming messages, and encodes messages to be sent in my 868Mhz home network.

Internet connected washing machine

Before describing how I connected my washing machine to the Internet. Let’s start with the background.

The smart grid is expected to be the future intelligent energy network. In the smart grid, enabled with digital technology, it should become possible to control appliances at home depending on energy cost and the availabillity of renewable energy. A popular smart grid use case is doing the laundry. Within limits the washing machine can run at arbitrary hours, so that electricity demand is moved to a more ideal situation (low energy price for instance).

The term smart grid has been used since 2005, and since then a lot of information aboutthe grid and appliances have become available. While six years is relatively short for energy networks, it is very long for the Internet. From this perspective I find it rather strange that still no Internet ‘connected’ washing machines are available on the market today. Miele, one of the leading washing machine vendors in Europe, showcased a ‘smart grid’ capable washing machine in fall 2010. Still general availabillity seems to be far away.

And what to do with legacy washing machines? In the Netherlands, washing machines are expected to live longer than ten years. For me, this was the motivation to see if it is possible to turn my washing machine into a ‘smart’ appliance.

And now the details of my modification. From the EAI perspective it falls in the category ‘User Interface Integration’. Our washing machine, a Bosch 64800 is equiped with a control panel made up out of a rotary switch for selecting the wash program, a number of leds for reporting the state of the machine, and six push buttons for starting/stopping and additional functions (eco-mode, extra water, faster wash cycle, and spin dryer speed). A picture of the control panel is shown below.

And the bare control panel inside the machine looks like this

Interestingly the control panel on the inside of the machine is equiped with a lot more push buttons (9) than are exposed on the outside of the machine (6). Operating voltage of the board was measured to be 5VDC.

For my modification I used a JeeNode (a wireless Arduino compatible microcontroller board). The first step of was to solder two wires on the electronics board parrellel to the Start/Pause button. This button provides functionality to start/stop the washing process at any time. The other end of the cable is connected to a JeeNode with a Relay plug.

Next step is to glue three LDR’s on the perspex backside of the controlpanel. The LDR’s were glueed to the start/pause led, the ‘looptijd’ led (washing in progress), and the ‘einde’ led (washing done). The wires are connected to the JeeNode (with an additional 1M resistor). For more functionality, more LDR’s (and more buttons) can be connected to the JeeNode for more detailed state information (which part of the washing cycle, etc) and more control.

The funny thing is that after fitting the control board into the washing machine, no changes are visible from the outside of the washing machine. Also, normal usage is still the same as before.

As can be seen in the picture below, the state of the machine can be retrieved succesfully. In the picture the state is plotted as a function of time. The states of the washing machine are: 0 for off, 1 for ‘program set and ready to start’, 2 for ‘washing’ and 3 for ‘ready, washing done’.

The graph also show an improvement I wanted to make. Since the washing machine lacks a ‘delay start’ function, the freshly washed laundry remains lying in the machine for a couple of hours… Fortunately, It is now possible to add this functionality through the JeeNode. I started by adding a rule that starts the washing machine if the washing machine is in state ‘1’ (ready to start a wash), and the time is 6 o’clock in the morning (during off-peak hours).

Here the washing machine was filled with Laundry, waiting until six o’clock, when the machine was started. The spikes at the beginning of the graph are a number of testing cycles to test the relay.

Now my washing machine is ‘connected’, implementing of a number of features is trivial:

  • Reporting state on home automation display (done)
  • Reporting state via Twitter/SMS
  • Couple washing cycle to the energy price or the availabillity, renewable energy or reduce peak demand. This is not really necessary, but because all components are availble (solar panels, energy measurements) it is interesting enough to give it a try.

The arduino sketch is available here.

Dragino experiments

Last week Bart van der Meerssche sent me the newest version of the flukso. This new system consists of a motherboard (dragino). Most important feature for me are the connectors (2×8 and 2×7) for microcontrollers such as Arduino, or the JeeNode. For me, the dragino is an ideal host for my JeeNode home automation projects.

First of all I wondered if the enclosure of the dragino (MS12) is large enough to house a JeeNode. I did some testing with an experiment board and a JeeNode (v5). My conclusion is that fitting is possible within the MS12 enclosure. Today JeeLabs announced the availabillity of a JeeNode SMD. This ‘flat’ JeeNode would make inclusion in the dragino even better.

The other activity I started with is communication between the the dragino and a JeeNode. The following table lists the connections between the pins of the dragino 2×8 connector and the pins on the JeeNode.

Draguino pin JeeNode pin
5V PWR
GND GND
SIN RXD
SOUT TXD

Here is a picture of my setup. The JeeNode is inserted into a breadbord, and wirejumpers connect the breadboard to the dragino. Communication was tested with the following  ‘Hello World!’ sketch on the arduino:

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("Hello World!");
delay(1000);
}

On the dragino, the flukso deamon was stopped (/etc/init.d/flusko). Also the crontab entry with a periodic reboot was removed. Serial communication (readonly) with the JeeNode was tested with a small, my first, Lua script.


serial=io.open("/dev/ttyS0","r")
while line==nil do
line=serial:read()
serial:flush()
end
print(line)

And, it worked! The ‘Hello World!’ was coming in line by line on the dragino.