Go to content Go to menu

This post describes the Cosm smalltalk package which allows it to send sensor measurements to a cosm.com stream using either Pharo or Squeak Smalltalk.

It’s also possible to use this package with squeak on your Raspberry PI so you can create a data logger with Smalltalk instead of Python or shell scripts as described here.

Use your smalltalks monticello browser to add the following repository and open the repository.

MCHttpRepository
	location: 'http://www.min.at/prinz/repo/cosm'
	user: ''
	password: ''

Inside you will find two packages. One for Squeak (4.x) and one for Pharo (1.4; 2.x).

First you should already have a cosm.com registration. If not register it’s easy and free. Cosm.com allows you to create so called feeds. A feed represents a collection of one or more streams. Streams are used to store values which can be charted on your cosm.com dashboard. So for example you can register a feed MyFeed and inside this feed a stream called Temperature. Now using the Cosm Smalltalk package you are able to send temperature measurements to your Temperature stream.

| client stream result ]
" Create a client instance with your cosm.com API key "
" and feed id "
client := CosmClient 
     newApiKey: 'this should be your API key' 
     feedID: 12345.
" Specify to which stream you want to send data and "
" in which format. Available formats are #JSON, #CSV "
" and #XML "
stream := client 
     openStream: 'my stream name' 
     withFormat: #JSON.
" Send a value (42) to the stream once "
result := stream put: 42.
" Take a block, execute it every x seconds (10) and send "
" the result of it to a cosm.com stream. This represents "
" an automatic stream "
stream put: [ 255 atRandom ] every: 10 
	align: false.
 
" Check if a stream is running automatically "
stream isRunning.
" Stop an automatic running stream "
stream stop.

When using automatic stream mode the time window can be controlled by every: which controls how often data is sent in seconds and align: which specifies if the time window should be time aligned. If set to false the time window starts after the block is evaluated. If set to true the time window is aligned based on how long the block took to evaluate. For example:

Aligned StartAt TimeWindow Block evaluation Next run
true 21:10:10 10 sec. 5 sec. 21:10:25
false 21:10:10 10 sec. 5 sec. 21:10:20
true 21:10:10 10 sec. 13 sec. 21:10:30
false 21:10:10 10 sec. 13 sec. 21:10:33

 
Running the following code

stream put: [ 255 atRandom ] every: 10 
	align: false.

produces random results between 0 and 255 sent to a stream resulting in a simmilar graphs like this:


cosm-stream.png

So now you can send your measurements into the cloud.