Sunday, June 02, 2013

Extensions to SIMPL

In the last week I have been thinking about the user interfaces used on minicomputers of the 1960s, in particular the PDP-8, and wondering if a similar front LED and switch panel could be created cheaply as a nostalgic retro accessory shield for the Arduino.

I have added some LEDs to the 12 available digital I/O pins of the Arduino, so that I can recreate LED chaser and "vintage computer" style displays.

I've extended the SIMPL command set so that I can more easily address each LED in turn, or alternatively treat all twelve as a binary numerical display, to display address or data numbers in the range 0 to 4096.

The latest version of SIMPL can be found here on Github Gist:

https://gist.github.com/anonymous/facc9a262c0778aec7b6

Just download the Gist and compile it as an Arduino sketch.  You can then start typing SIMPL commands using the serial monitor window at 115200 baud. Make sure that you have selected the "Both NL and CR" option for the serial monitor.

Output Commands

In order to gain more immediate access to the I/O, I have added some new commands:

a   - sends an analog.Write value to a pin previously named with d
b   - prints out the current value of the milliseconds counter, allowing loop timing to be calculated
c   - prints out the current value of the microseconds counter

h   - sets a digital pin high    eg  6h
l    - sets a digital pin low     eg 13l
n   - decodes a number up to 4095 and displays it on digital pins 2 to 13     eg  1234n

n can be thought of as a very simple binary display.  It has a fast response time and allows the user a simple visible means of checking that a program is executing.

The n command can be useful to examine the characters stored in RAM.  Here it is used to output 1000 characters to the LEDs, starting at RAM address 256.  The characters are sent every 100mS and also printed to the terminal.

255!1000{y@rn100m}

255!    Store 255in y as the starting RAM address
1000{  Perform the loop 1000 times
y         Increment y each time around the loop
@        Move y into x
r          read the contents of RAM into x
n         Send the value to the LED display
100m   100mS delay to slow things down to a sensible speed
}         End of the loop

Decision Making

SIMPL currently lacks the if, then, else decision making, or the switch case structure. This would be a useful addition and I am currently working on some ways of implementing it.

It might be possible to test the x value against certain constants and then execute one of the upper case routines if the test was equal.

Other SIMPL Commands

SIMPL instructions are a mix of lower case ASCII characters, symbols and punctuation marks. These are the primitives, which are interpreted in sequential order by a simple interpreter.  The interpreter is an extension of Ward Cunningham's Txtzyme interpreter, which recognises the following:

a          Send an analog write (PWM) value to a pin
b          print out the current milliseconds count
c          print out the current microseconds count
d          define a digital port pin
e
f
g
h          set pin high
i           read an input
j          skip next instruction
k          access the loop counter
l           set pin low
m         define a delay in milliseconds
n          display a number on a line of LEDs
o          set an output
p          print the x variable to the terminal
q         query a block of RAM locations ( as character)
r          read from RAM
s          read an analogue input
t
u          define a delay in microseconds
v
w         write to RAM
x          increment x
y          increment y (as used in incrementing loop structures)



{}       loop the code contained within the braces

@       copy y to x
!         store x in y
?        Dump the contents of RAM to show the existing words

+
-
*
/
<
>


Numbers can be typed in the range  0 to 65535 and are stored in the x variable and used to control the above instructions.

eg.   6d            define digital port pin 6
       100m       define a delay of 100mS
       10{....}    go around a loop 10 times
       20p          Print 20 to the terminal

To these basic instructions I added a y variable, so that I could do simple maths.  You can store a number in y by using the store symbol !

      20!          Stores the value of 20 into y (by transferring it from x)

To get the contents of y back, we use the fetch symbol  @

     20!@p      Prints the value of 20 from y

We can do simple addition, subtraction, multiplication and division on x and y

     20!5+p      Adds 20 to 5 and prints result       (x=x+y)

     5!30-p      Subtracts 5 from 30 and prints result  (x=x-y)

     16!8*p     Multiplies 8 by 16 and prints result     (x = x*y)
    
     6!30/p       Divides 30 by 6 and prints result      (x=x/y)

Downloading the SIMPL sketch from Github Gist and programming it into a standard Arduino is the first step to discovering a new way to control physical computing devices.

Saturday, June 01, 2013

Extending SIMPL and flashing a few LEDs

I have been experimenting with SIMPL, and have added a few new commands to give it greater functionality.

I've wired Digital outputs 2 to 13 to a line of 12 LEDs, to give a very simple means of displaying numerical values.  It also allows LED chaser displays to be tinkered with.

I have added a command to allow the analogWrite function to control an output pin.

Name the pin first, e.g.  6d and then  the PWM value  such as 127a  This sets the PWM to about 50%  duty cycle.

I've also been investigating how fast the commands run, so added a couple of timing commands  b and c.   b prints the current value of the millisecond counter millis(), so typing b before and after a list of instructions will allow you to calculate how many milliseconds it took to execute.  c does the same, but uses the microseconds counter micros().

By timing 10000 cycles of a loop, I can show that SIMPL will add two 16 bit integers in about 6 microseconds.

I have added some commands for easier control of the I/O.    h and l are now used to set a port pin high or low  eg. typing 6h and 5l will immediately set those pins high and low.

After my experiments with LED chaser displays, I also wanted a way to immediately write an 8 bit number to 8 consecutive LEDS.  So the "n" command  displays a number on a row of LEDs.

Typing 255n will turn all the LEDs on Digital 2 to Digital 9.

To make incrementing loops easier, the command "x" now increments x and "y" increments y.

0!10{y@p}

This prints out the numbers from 1 to 10.    0! sets y to zero,    y increments the y variable.

The q,  r and w commands have been used to access the RAM.

300r will print the character stored at address 300.




SIMPL instructions are a mix of lower case ASCII characters, symbols and punctuation marks. These are the primitives, which are interpreted in sequential order by a simple interpreter.  The interpreter is an extension of Ward Cunningham's Txtzyme interpreter.  SIMPL now recognises the following lower case instructions:

a          Send an analog write (PWM) value to a pin
b          print out the current milliseconds count
c          print out the current microseconds count
d          define a digital port pin
e
f
g
h          set pin high
i           read an input
j          skip next instruction
k          access the loop counter
l           set pin low
m         define a delay in milliseconds
n          display a number on a line of LEDs
o          set an output
p          print the x variable to the terminal
q         query a block of RAM locations ( as character)
r          read from RAM
s          read an analogue input
t
u          define a delay in microseconds
v
w         write to RAM
x          increment x
y          increment y (as used in loop structures)



{}       loop the code contained within the braces

@       copy y to x
!         store x in y
?        Dump the contents of RAM to show the existing words

+       Add x and y
-        Subtract x and y
*       Multiply x and y
/        Divide x and y
<      Is y < x
>       Is y > x

A Little Nostalgia

In the decade that spanned the 1960s, the cost of computers fell by about two orders of magnitude, and with volume manufacture possible, they found their way into general scientific engineering and industrial use.

One area of science that particularly benefited from more widely available computing power, was that of biomedical research.  Two computer designs, in particular, spurred on this advancement,  the LINC of 1962, and the PDP-8 of 1965.

About 50 LINC machines were built, and a few survive today. This video shows a restored LINC in use.

http://www.youtube.com/watch?v=atJUnQN65dE

These were the first "benchtop" designs, and set the trend in design for the machines that were to be known as minicomputers.  In 1965 the PDP-8 cost $18,000, still a huge amount of money, but cheap enough to be built in volume and satisfy the needs of university laboratories.

Both the LINC and the PDP-8, owed their design to the general purpose digital modules, that were manufactured by DEC from the late 1950s onwards. Each machine was a pared down 12 bit design, cost engineered for efficiency at every level, offering a machine capable of real work for a modest price.

Over the next 20 years, the PDP-8 architecture was to be revisited many times, as advancements in   logic ICs and memory allowed significant cost savings to be made.  The original PDP-8 was the size of a small fridge, within 10 years it was housed in a single 19" rack.  The very last PDP-8 machines used a microprocessor (Intersil/Harris 6120) to implement the CPU.

By the mid 1970s, microprocessors were starting to appear, which led to the era of 8-bit microcomputers.  Some of these, such as the MITS Altair 8800 paid homage to the style of the PDP-8 with a front panel of many LEDs and programming switches.  The simple LED indicators allowed the user to see precisely what the registers were holding, and the switches allowed simple programs to be "toggled" directly into memory.

These machines are now approaching 50 year old , yet there are many enthusiasts who started their scientific or engineering careers programming such computers.  The National Museum of Computing at Bletchley Park owns an early PDP-8, which they have restored to working order.  They have made a series of short videos depicting its operation.

Here's how a short program is loaded into the machine from the front panel switches.

http://www.youtube.com/watch?v=DPioENtAHuY

Notice how much noise was present in a typical computer room.  Fans, printers and teletypes produces a near constant din.

The machines were relatively slow and simple by today's comparison,  capable of some 300,000 instructions per second. Nevertheless, they were capable of real work, despite their limitations in speed and memory.

In the mid 1960s, the standard user interface was a teletype printer with paper tape punch and reader. Programs would be loaded into the memory from paper tape, but often only after a simple tape loader had been manually toggled into the machine from the front panel switches.

The second of the NMOC videos shows how program on the the paper tape was loaded into memory.

http://www.youtube.com/watch?v=SOOFHFnB0D8

Application programs had to fit within the 4K memory limitation.  This video shows a Chess program, and the user interaction using the ASR33 Teletype, that was supplied with every PDP-8.

http://www.youtube.com/watch?v=OyDufWHsNVE

Contemporary to the LINC and the PDP-8 was a low cost magnetic tape drive, commercialised by DEC as the DECtape.  The last of the NMOC videos, shows the DECtape in use

http://www.youtube.com/watch?v=TWTpYUTbW8s








Monday, May 27, 2013

A LED Chaser Display using SIMPL

My last post described how a LED chaser display was one of my first forays into physical computing about 30 years ago.

My recent experiments with SIMPL, have shown that it is a perfect programming tool for simple interactions with physical computing devices - so I decided to recreate a simple LED chaser display, using SIMPL.

LED chaser displays require a bunch of LEDs which you can address individually. You also need to write code that provides basic loop structures and the means to control timing.  SIMPL, which uses the Txtzyme interpreter is ideal for this task.

Txtzyme uses 1o to set a port pin high and 0o to set it low.  The port pin has to be pre-specified using the d command.  This all becomes a little unwieldy, so lets define a couple of new words to make it easier to type.

We will use H and L to set the port pins high and low respectively.  LEDs are currently fitted to digital pins 2 to 9.

Now the definitions:

:Hd1o     - sets the defined pin high

:Ld0o     - sets the defined pin low

So typing 2H will turn on the LED on pin 2, and 2L will turn it off again. This simplifies the control of outputs to two very memorable commands H and L.

Now we need to generate a loop which turns all the LEDs on in sequence. Because of the way the loop counter works in SIMPL, descending loops are easier as the loop counter k can be accessed to control the loop.

This performs a down count

10{kd1o100mkd0o}

10 - perform the loop 10 times
kd1o  - move the loop count k into d to select the pin and set it high
100m - wait 100mS
kd0o  - move the loop count k into d to select the pin and set it low

We could now substitute H for d1o and L for d0o giving the construct

10{kH100mkL}

To make an upward counting loop is a little harder and it uses the l instruction (l as in little L).

l increments the y variable each time it is executed, and the word @ allows us to move y into x so that we can print it out.

So to make a loop that counts up from 1 to 10 we first have to set y to zero using 0! (store 0 in y)

0!10{l@p}  will print out the numbers from 1 to 10.

The construct l@p increments y, moves y into x and prints it out.  To turn this into a LED chaser we need to add the H and L words to control the LEDs and a short delay to control the speed:

We start with the loop - load y with a count of 1.  The loop will start at 2, and run to 9

1!9{l@pd1o100m@d0o}

We can now replace d1o and d0o with H and L

1!9{l@H100m@L}

Remember that @ gets the current value of the increasing loop count y and puts it into x.




Sunday, May 26, 2013

Not Enough Flashing Lights or Switches

Growing up in the 1970s, a common depiction of computers, at least by Hollywood film-makers, was a room full of wardrobe sized cabinets, with innumerable flashing lights and switches and spinning tape drives.  Frequently, the plot of the movie was centred on the computer malfunctioning in some malevolent way, and chaos breaking out.  The Italian Job, Westworld and 2001 A Space Odyssey all relied on computer failures to spice up the plot.

The reality was that throughout the 1960s, computer hardware was large and cumbersome, covered in lights and switches, and you only had to point a camera at any mainframe of that era, and you had the perfect cheap shot to enhance any sci-fi movie.

By the early 1980s, computers had entered homes and schools, and teenagers, such as myself at that time, were spending countless hours typing BASIC program listings from computer magazines, into low spec home computers, with dubious keyboards and scungy TV displays. In just 5 years, the first micros of the mid 1970s had evolved from homebrew rack systems with lights and switches, to mass produced machines on sale in every high street.  In homage to the early micros, the classic example of that era, was the MITS Altair 8800, which had a role in the 1983 film "War Games".

At about the same time that Matthew Broderick was pretending to program the Altair 8800, I had invested in an 8 bit input output card for my ZX81.  At about £20, and solder it yourself, it had cost me several weeks pocket money. Here for the first time, for me, was the means to connect to the real world and make the computer do something under program control.  The first thing was to connect 8 LEDs to the pins of the output port and with some simple BASIC programs, create a whole load of different "Knight Rider"  LED chaser display effects.

Fast forward 30 years, and technology has developed by several orders of magnitude. The average Smartphone now has 1 million times as much RAM as my first ZX81 and is clocked at 1000 times the speed.  Not to mention that it has four 32 bit ARM processors acting as a quad array, plus a specialist graphics processor unit. This type of processing power and memory storage capacity in your pocket was inconceivable just a few years ago.

However, just because millions carry around this sort of processing capability in their pockets or bags, does not mean that we have become a population of computer scientists.  In fact, the average citizen has no more inkling today of how a computer works, than 30 or 40 years ago, watching computers malfunction on Hollywood movies.

Fortunately, in recent years there has been an active and vocal campaign to help educate a tiny percentage of the population into the workings of computer technology.  The $20 Arduino and it's spin offs, has done more for teaching computing science, hardware, firmware and programming skills than any other device in the last 30 years.

So, in homage to those early homebrew computers with their flashing LED displays, I have connected a dozen LEDs to the pins of an Arduino, and recreated some of my light chaser memories from 30 years ago.

I have a new toy to play with, and a programming language SIMPL that lends itself to simple interactive physical computing. A few lines of SIMPL and I have a LED chaser display that demonstrates the benefits of having an interactive programming environment.

Wednesday, May 22, 2013

SIMPL - A simple programming language based on Txtzyme

SIMPL - A simple programming language based on Txtzyme

Last weekend I played around with Ward Cunningham's Txtzyme - a minimalist programming language, with an interpreter written in C so that it can be easily ported to many microcontrollers.

Txtzyme contains all the elements necessary to enable a microcontroller to interpret and execute a series of typed commands, and is an ideal example to learn the techniques employed by more sophisticated interpretive languages.

During the past week, I have written some extensions to Txtzyme and tried out a few ideas to  make Txtzyme more versatile and easier to use.

This blog post is a tutorial in Txtzyme, and it's new extensions to create more of a useful language - which I am calling SIMPL - A Serial Interpreted Minimal Programming Language.

SIMPL runs in under 6K on the standard Arduino.

A Brief Description of Txtzyme.

Txtzyme consists of an interpreter contained within a loop.  The interpreter, intended to be very simple, decodes individual ASCII characters, and executes a block of C code associated with that character. This is similar, in principle to how Forth scans through a series of Forth words and executes the code associated with them, but Txtzyme treats each ASCII character as a word, greatly simplifying the scanning process.

The interpreter steps through a string of ASCII characters, executing the associated code blocks in turn. This may be slow in speed in comparison to assembly language execution, but the user does not need to know about machine code, assembly language or even C to make the microcontroller perform simple tasks.

Numbers and Maths

Txyzyme enumerates numbers and assigns them to an integer variable x. Typing 123p into the serial terminal will set x to 123, and then print that value out when the p command is executed.

I extended Txtzyme with the use of another integer variable y.  A number can be stored in y by using the ! character. This again being borrowed from Forth.

456! will initially assign 456 to x, and then copy it into y.

The use of the second variable allows simple maths operations to be performed. Taking the simple interpreter, I added some maths operations + - * and / .

456!123+p  will set y to 456, then set x to 123, add x and y, leaving the result in x and then print out the answer 579.

I/O Commands

Txtzyme was designed to perform simple I/O operations on the pins of the microcontroller, with each operation being initiated by a serial command.  This allows ports to be set, inputs to be read and analogue inputs to be read and printed to the serial terminal.  The keywords that perform these operations are generally only single ascii characters, chosen to make the commands surprisingly human readable.

First you have to state which I/O pin you wish to use.  This is done with the d command.

For example  6d  will select digital pin 6

You may then set this selected pin to high using 1o   (where o = output)  or to low using 0o.

To read an input pin, first you have to define it eg.  8d will define digital 8. Then you use i, for input to read it's state into x. p will then print out the value.

To read the value on one of the ADC pins, you use the s command, which means analogue sample.

0sp will read the value on ADC 0 into x and print it out.

Loops

Txtzyme uses a simple loop structure, allowing commands to be executed repeatedly. It also uses the native delay functions present on the Arduino to provide simple timing functions - ideal for flashing LEDs and generating musical tones from output pins. Txtzyme can toggle output pins at up to 47kHz, a speed that is only limited by the Arduino digitalWrite function.

The loop function will execute any command contained within braces {}.  It uses x to initialise the loop counter, k to a starting value, from whence k will be decremented to zero and terminate the loop.

For example to print out ten readings from analogue pin 0

10{0sp}

To see the loop count variable k decrementing

10{kp}  will print out the numbers from 9 decrementing to 0

Txtzyme does not yet have the ability to do complete FOR/NEXT loops. This would be a very useful addition, the means to perform a loop such as

for i = x to y step z

I have added primitives @ l y and  z

y allows direct access to the variable y so typing 10y   is equivalent to y=10

@ was intended to be the equivalent of Forth's fetch, but in SIMPL it copies the value stored in y to x.

l is a loop counter.  It increments y by 1 every time

The construct l@p  increments y by 1, copies it to x and prints it out.

To print an ascending series of numbers from 1 to 10 uses this construct

0y10{l@p}

When combined with the read primitive r (see below) this can be used to read and print consecutive RAM addresses.

I/O with Loops and Timing

Loops can be used to flash LEDs or generate tones, and can be combined with the millisecond and microsecond delay functions to generate appropriate timed behaviour.

To flash a LED on pin 13 ten times, on for 500mS and off for 500mS

13d10{1o500m0o500m}

To turn this into an audible note to sound a small speaker on pin 6, we shorten the delay to say 500uS, and generate say 1000 cycles (1000 times around the loop).

6d1000{1o500u0o500u}


Creating New Definitions

The simple Txtzyme interpreter readily executes a program contained in the characters in the input buffer, but wouldn't it be great if you could store these mini programs in RAM so you could use them time over?

The mechanism to execute a program from RAM, is to point the interpreter at the location of the starting character and let it execute the characters in turn until it finds a return '\r' or newline '\n' character that marks the end of the string.

Here we have to start borrowing ideas from Forth, the concept of the "word" that signifies the start address of a block of code to execute, and the "colon definition" - a mechanism to write new words into memory.

To keep things simple, our new words will be assigned only the capital letters, allowing up to 26 new words to be defined, namely A to Z.

Additionally, to simplify the addressing of these words, and to keep the RAM usage within the limitations of the ATmega328 ( as used on the Arduino), we will allocate each word just 48 bytes of memory, with each consecutive word starting on the 48 byte address boundary.  This allows us to easily decode the ASCII value of the character, find the starting address of the associated code, and execute the word.

To create a new word, and to tell the interpreter to copy the input character string into the correct position of RAM we use the "colon definition".

We type a colon followed by the capital letter of the word we wish to create.  Suppose we liked the tone example from above and want to assign it to the letter T, we type

:T6d1000{1o500u0o500u}

The colon definition code will detect the leading : and then decode the T to 84 in decimal.  It then creates an address by multiplying 84 by the allocated permissible word length of 48 bytes, and adds this to the start address of the array assigned to hold the definitions.  This process happens automatically and we don't have to concern ourselves about the exact address that holds T.

Once stored in RAM, the interpreter prints out T, to say it has been defined, and then executes the new word twice. This is a quirk of the interpreter.  So we hear the tone twice as long.

Now whenever we type T, we will get the tone.

In order to keep track of what we are doing, I have added a ? command.   This prints out the entire definition RAM, showing the commands A to Z and the code that is associated with them.  This allows a definition to be edited, by cut and paste operations from the ? listing into the input buffer, and changing what ever parameter is to be edited.  The edited definition is then automatically stored back into RAM, when you press return.

Some Prestored Definitions

Just for fun, I decided to hard code some "musical notes" into the definitions. Characters A to G play a short musical note, roughly tuned to A = 440Hz , so it's possible to play tunes just by typing the notes.  Having "musical debug" is also a great way of determining whether the program is doing what was intended.

The SIMPL strings for properly tuned notes are as follows, and are pasted into the RAM array when the sketch is first compiled.  They can be written over at anytime.


//  40{1o1106u0o1106u}     // A 440 Hz
//  45{1o986u0o986u}         // B 493.88 Hz
//  51{1o929u0o929u}        // C 523.25 Hz
//  57{1o825u0o825u}        // D 587.33 Hz
//  64{1o733u0o733u}        // E 659.26 Hz
//  72{1o690u0o691u}        // F 698.46 Hz
//  81{1o613u0o613u}        // G 783.99 HZ

Building Up Programs

The real power behind the colon definition is that new and more complex words can be assembled from existing definitions, and then executed as singe commands. For example, suppose we have defined three tone generating words A B and C.

We can type ABC - which will play the three tones in succession

Alternatively   5{ABC}  will play the 3 tone sequence 5 times over

We could then define a new word I as

:I5{ABC}

Whenever we type I, we get ABC played 5 times.

So from very simple definitions, quite complex operations can be performed.

This is an incredibly versatile technique. It allows you to write your own routines, assembled from other routines and then be able to use names that you can remember.

For example, you can define a word H to set the port pin high, and L to set it low.  If you have a LED connected to pin 6,  6H will turn it on, whilst 5L will set pin 5 to logic low. It becomes very easy to toggle any output pin - just with a couple of memorable keystrokes.

This ability to form new definitions is one of the fundamental and most powerful aspects of Forth - so very worthwhile to borrow it to make it adaptable to the minimal programming environment of SIMPL.

Some Other Additions

The Txtzyme interpreter is readily modified to include new functions.  This is a work in progress and I have added some simple extensions.

I now have comparison operators < and > to test whether x is less than y or greater than y.  These operations will set x to 1 if true or 0 if false.

10!5<   translates to "is 5 less than 10 ?". This is true so x is set to 1.

10!5>  translates to "is 5 greater than 10 ?". This is false so x is set to 0.

To make use of these comparisons I have introduced the jump command j.   If  x = 1 the next command is skipped, otherwise it is executed as normal.

Memory Operations.

I wanted a means to directly edit the contents of an address in RAM and read it.  These are equivalent to PEEK and POKE.

The y variable is used to hold the address  - so  723! will set the address up as 723.

The x variable is used to hold the data

To write to RAM we use w and to read we use r

So 723!65w   will write the character ASCII 65 into address 723.  You can then type ? to see that it's there. It pokes an "A" into the first location of the word defined by J.

To read a location,

723!r  will read the contents of location 723 and print it as an ASCII character.  You get your "A" back.

As this character has been poked into a definition, it has modified that definition. In this case it will cause definition J to play the tone associated with A.

Finally, I needed a means to look at a block of memory. The q command does this by printing out the desired number of characters starting at the address stored into y.

As an example, address 627 holds the start address of the H command

Typing 627!48q  will print out 48 consecutive characters, which in our case is the start-up message

_Hello World, and welcome to SIMPL_

With the loop primitive l we can also read and print successive memory locations

627y48{l@r}

This sets the address in y to 627 and reads and prints 48 consecutive locations using the r primitive.

Using the colon definition, the construct l@r could be assigned to R,   as in :Rl@r

So to read and print 33 characters from address 627 we use

627y33{L}

A Work in Progress

SIMPL and it's underlying Txtzyme interpreter is constantly evolving as new commands are added to try new ideas.

It is unlikely that it will ever be a serious language, but a novel experiment and a means to understand how an interpreter can be manipulated to execute a series of simple commands.

Many of the ideas have been borrowed from and inspired by Charles Moore's Forth as it evolved from a set of ideas into a proper language during the 1960s.

You can download a recent version of SIMPL from github gist - but be aware that it is a work in progress.

https://gist.github.com/anonymous/5648871

I hope others will get as much enjoyment as I have from tinkering with SIMPL and Txtzyme.


Sunday, May 19, 2013

More Thoughts on Txtzyme - and a musical interlude

In the last post I discussed the simple interpreted language Txtzyme and offered some ideas on how the language could be extended.

Ward Cunningham, the creator of txtzyme describes it as text that "catalyses action".  I think this is a fair description.

In summary, Txtzyme uses some very simple methods to produce an executable program and interface with the Arduino hardware. It uses the following Arduino functions to enable the interpreter to implement a simple serial interface and access the digital and analogue I/O:

Serial.read
Serial.print
digitalWrite
digitalRead
analogRead
delay
delayMicroseconds

Much of the simplicity is that a single ASCII character is interpreted to produce an action, and by stringing ASCII characters together the interpreter will handle them in turn and execute each action in order.

The interpreter allows the use of a simple loop structure, by repeating the actions enclosed within braces (curly brackets)  {.......}

Numerical characters are decoded into a decimal number and assigned to a single numerical variable   x which is used as a parameter to control some of the actions.  For example, time is handled using the two native Arduino functions delay (mS) and delayMicroseconds.

These are given the characters m and u respectively, such that

1000m  is a delay of 1000 milliseconds
500u   is a delay of 500 microseconds

These are useful to slow down program operation, such as when reading ports or ADCs and printing of for creating accurate timing loops for generating frequencies.

Extending the Ideas

My first foray into txtzyme was to enhance the simple interpreter such that it could execute a series of user routines invoked by typing their uppercase ASCII character name. The use of uppercase differentiates them from what I will call txtzyme primitive functions which are represented by lowercase characters.

I fitted a small loudspeaker to digital 6 and from it I was able to produce a series of beeps and tones, however it soon got tedious having to retype the txtzyme strings into the input buffer each time I wanted to try a new tone. This inspired me onto the next level of extension,  the means to code each txtzyme string into a named array in memory, and execute the string just by typing its name. This is an idea heavily borrowed from the idea of Forth words, but simplified to use just a single uppercase character (A-Z) as the name.

For the demonstration, I defined six user routines called A-F, each of which caused a short musical tone to be output from pin 6 of the Arduino.  The code to do this is on my github gist

https://gist.github.com/anonymous/5604829

Each uppercase character defines a txtzyme array of characters which can be interpreted as though it was typed into the serial terminal buffer.  For example, a musical note is defined by the following characters stored in the A array and will be invoked whenever the text interpreter encounters the character A.  6d selects digital port pin 6, and produces 75 cycles of tone.  The port pin is low for 708 microseconds and then high for 708 microseconds.

char A[64] = "6d75{1o708u0o708u}";

The A array has a total of 64 characters reserved for it although only 17 are used. This is not very efficient on RAM usage, but since there are only 26 user routines, less than 1700 bytes of RAM are    allocated for this storage.

I then proceeded to write similar strings for the "notes"  B through to F. Once compiled, these notes could be played just by typing ABC, or any other sequence into the serial terminal.

I then defined G by the array

char G[64] = "ABCDEFFEDCBA";

To my delight, and surprise it played the sequence of 12 notes perfectly.  Using musical tones to debug program flow is a neat trick, and a lot easier and quicker than counting flashes on a LED!

Another though was what happens if a word calls itself - using this example

char H[64] = "GH";

Typing H produced an infinite loop of tone sequence - not unsurprisingly.

Finally I tried an example that reads and prints the adc0 and plays a tune each time

char I[64] = "{0spABC}";

Notice how this is just a loop structure with no controlling parameter. This means that the number of times the loop is executed can be specified by typing that number before the I,

5I    - perform the I sequence 5 times.

Colon Definitions

The Colon Definition is a construct borrowed unashamedly from Forth.  It is the method by which new words (actions) can be defined and stored into memory. This has the advantage of not having to keep typing new stings repeatedly just to try them out, but also provides a simple means to write new code, test it and edit it until it works.

The above demonstration bypassed the need for the colon definition, by using a hard coded array of characters defined in the C code. Now it is time to bite the bullet and extend the interpreter to handle the colon definition.

The example above written as a colon definition would be written

:A6d75{1o708u0o708u};

As txtzyme ignores whitespace, this could be rewritten with spaces to make it more legible

: A 6d 75{1o 708u 0o 708u};

To handle the colon definition we need a routine that on seeing the colon, gets the next character (capital A) as the name and copies the next n characters to an array called by that name. This process ends when the interpreter encounters the semicolon.

Unfortunately, I'm not really a C programmer, so the clever use of pointers and the reference and dereference operators tends to fox me somewhat, but after an hour of some kludgy coding, I had a function that would perform the colon definition task, and assemble the definition into a named array.

In theory the name of the definition eg A, should just be an address in memory, and on typing A, the interpreter starts processing the characters that appear at that address until the newline character is encountered.

Viewing and Editing txtzyme definitions.

I also decided that the ? character would be a good way of examining the code contained within a definition, allowing cut and paste editing from the serial terminal.

So  ?A prints out the entire definition of A to the terminal

:A6d75{1o708u0o708u};


To be continued.





Saturday, May 18, 2013

Txtzyme - A minimal interpretive language and thoughts on simple extensions

Imagine a very simple programming language which could run on any microcontroller with the minimum of on chip resources. A language that could invoke complex instructions and one that could be used to exercise the basic I/O functions of the microcontroller with a simple serial command interface.

I have always been a proponent of minimalist programming languages and so it was nice to encounter something new this week. A language that consists of commands invoked by single ASCII characters which allows complex procedures to be assembled from simple arrays of characters.

It reminded me of the tiny Basics and tiny Forths that were written in the late 1970s for resource limited microcontrollers.

On Thursday evening, I attended the regular OSHUG meeting (Open Source Hardware Users Group) which is held each month at C4CC near Kings Cross.

The third speaker was Romilly Cocking, who presented a report on his quick2link project - a very simple, extensible protocol for controlling sensor/actuator networks.  quick2link provided the means to control an Arduino as the I/O subsystem of a Raspberry Pi project.

The Pi is limited in its I/O capabilities and so adding an Arduino to handle the I/O seems a sensible idea.  To allow the Pi to control the Arduino a very simple protocol is needed, and since the Arduino is essentially a microcontroller with a serial interface, a serial command interpreter seems appropriate.

Txtzyme

quick2link was inspired by the work by Ward Cunningham (creator of the first wiki) and his minimalist txtzyme command interpreter, for controlling the I/O of simple microcontrollers.

I was intrigued by txtzyme and its simplicity and having downloaded the txtzyme interpreter for the Arduino, decided to have a play. As a simple command interpreter written in C, it compiles to just 3.6k, a very low overhead for even the smallest of today's microcontrollers.

Txtzyme allows the control of the Arduino I/O, using a simple interpreted language.

The commands are reduced to single lower case ASCII characters such as i for input and o for output. Each command can be given a single numerical parameter, limited to 65535 by the unsigned integer of the C interpreter.

The interpreter, a mere 90 lines of C code, parses a serial string, evaluating numerical characters and executing the alpha character commands.

Only a few commands are implemented, leaving a lot of scope for language extensions.

0-9 enter number
p print number
a-f select pin
i input
o output
m msec delay
u usec delay
{} repeat
k loop count
_ _ print words
s analog sample
v print version
h print help
t pulse width


Whilst appearing limited, this simple command set is essentially all that is needed to get a microcontroller project up and running.

The interpreter makes use of the Arduino functions, including Serial.read, digitalWrite, digitalRead, Serial.Println, delay, delayMicroseconds and analogRead.  With these simple functions the interpreter can execute a string of commands, manipulate I/O and print results to the terminal emulator.

The hello world of the Arduino is to flash the LED. The following txtzyme string implements this efficiently:

10{1o1000m0o1000m}

This will flash the LED ten times for 1000mS on and 1000mS off.

Change the parameters a little and you can produce an audible tone to a speaker connected to an output pin.

1000{1o1m0o1m}

A quick play with the command set showed that as well as simple port manipulation, txtzyme could toggle port lines at up to 47kHz, read and display ADC channels, create tones on a piezo speaker and perform simple time delays.

txtzyme uses a compact syntax, but is nevertheless quite human-readable. The following command produces a short beep to a speaker connected to an output port

400{1o200u0o200u}

400 is the loop counter - so perform the instructions enclosed between the {..} 400 times

1o   -  set the designated port pin high
200u  wait 200 microseconds
0o   -  set the designated port pin low
200u  wait 200 microseconds

Extensibility

I then started thinking about how txtzyme could be extended, to include new functions, and soon had simple integer arithmetical operations working  using +  -  * and /.  There are roughly 32 printable punctuation characters in ASCII, all of which could be utilised by adding a new case statement to the interpreter and writing the code to handle each function.

The lower case characters i o m u p k s are already used in the core interpreter - so it might be sensible to reserve all lower case characters for future extensions to the core interpreter. This would leave the upper case characters to be used for User defined functions.

Txtzyme is still a rather limited language, capable of concatenating blocks of code together and performing multiple loops through code blocks.  There needs to be a simple mechanism to pass numerical parameters to the code blocks and also build the if...then construct. This will clearly need some further thought in creating useful language structures whilst keeping the syntax very simple.

What txtzyme lacks is the ability to store and retrieve these simple routines. Once you have typed return at the end of the terminal text buffer the input characters are lost forever - unless you  copy them to the clipboard first. For the language to be extensible, there needs to be an easy way to manage the storage and retrieval of these text strings.

One solution might be to borrow from the Forth community, and create a "colon definition" - giving each routine a single upper case alpha character name. That would allow for 26 unique code snippets and an easy way to manage/execute them - solely by typing their name.

Let's call the routine above "Beep"  and assign it capital B as it's name.  We could use the :   ;  structure to define the body of the routine in the same way a Forth word is defined.  So the beep word definition becomes:

:B400{1o200u0o200u};

txtzyme allocates 64 bytes of RAM to its input buffer.  A very simple addressing scheme could use the ASCII value of B, to assign a start address to the RAM segment holding the body of the code.

On encountering the first colon : the interpreter needs to switch to a colon definition compiler mode, which interprets the next character B as the start address of the buffer to hold the colon definition.  It then starts storing the characters into this buffer until it encounters the closing semi-colon ;

Once this colon definition has been stored in RAM, any occurrence of the letter B is interpreted as a pointer to the buffer, from where to start executing the commands.

Whilst wasteful of unused RAM locations, this scheme would be easy to implement and allow simple routines to be stored . Regular used routines could be stored in flash as "primitives".

After a little head-scratching, I realised that I could store the txtzyme characters that perform a function in an array, and give the array a name: For example this produces a low note on a speaker connected to digital pin 6.  (50 cycles of 2mS on, 2mS off)

char A[64] = "6d50{1o2m0o2m}";

In order to execute the function, I just had to pass the pointer of the array i.e. A to the txtEval function and include this case statement within the routine that evaluates the serial input buffer.


      case 'A':  
      txtEval(A);
      break;

I then wrote a couple of txts which produce a medium tone and a high tone, and assigned them to B and C.


char B[64] = "6d100{1om0o1m}";            // A medium tone beep
char C[64] = "6d100{1o500u0o500u}";     // A high tone beep

And then included their "names"  in the case statements of the interpreter


      case 'B':  
      txtEval(B);
      break;

      case 'C':  
      txtEval(C);
      break;  

Now it is possible to type any combination of A B and C into the serial input buffer and have the tones played in order.

These capital characters also can be inserted into loops - so to play the sequence A B C  ten times, all you need is

10{ABC}


I have created txt arrays to generate 6 musical notes A - F   - see my Github Gist for the code

https://gist.github.com/anonymous/5604829



In the same way that the native Arduino function calls are used to exercise the basic I/O, further library functions could be accessed and called by a single alpha character. For example  S for servo control and P for pwm could be invoked with a simple numerical parameter:

160S   moves the servo, connected to the nominated port pin, to 160 degrees.

128P   outputs a 50% duty cycle PWM waveform on the nominated port pin.



To be continued.







Sunday, October 28, 2012

A Brief History of Nanode

                            The first Nanode on a prototyping board controls an RGB lamp

Nanode was conceived back in the summer of 2010, when I returned from a trip to Shenzhen, China. I was between jobs, and had a bit of time on my hands for tinkering with some new product ideas.

Nanode arose out of the need to find a cheaper way of connecting simple open source hardware, such as Arduino to the internet, so that Arduino could be used for a range of sensing and control applications.  

The combination of an Arduino and a low cost ethernet shield came to approximately £35, and from a quick glance at the Arduino and the ethershield showed that not only could the circuit design be produced on one pcb, but produced at considerably lower cost. So with the breadboarded prototype working, I set about producing a pcb version, which using conventional through-hole components and DIL ICs, could be sold as a kit and assembled by anyone with basic soldering skills.

It was anticipated that the kits could be sold for about £20, and so ten prototypes were made to test out the pcb layout.  These first ten Nanode prototype pcbs were produced and sent out to friends and enthusiasts for beta testing and application development.

                                           A pair of Nanode Prototypes - April 2011
                                           
From the outset, Nanode was offered as a DIY kit consisting of the pcb, the ICs and some 50 other  components. Assembly was fairly straightforward - and an online pictorial build guide was produced to make the assembly process as simple as possible - even for those who had had little or no previous soldering experience.



During the summer months of 2011, 550 Nanode kits were produced in the UK on green pcbs and sold out by the end of August. In September, 500 new red pcbs arrived from China changing the appearance of Nanode somewhat.

                                 The popular Nanode 5 on a red pcb. September 2011

Following a review of the design in the Autumn of 2011, it was clear that the addition of a wireless transceiver link would considerably enhance the capabilities of the device. With a RF module onboard, Nanode could "talk" to other hardware devices such as smart sensors, extending the control and monitoring range to several tens of metres from the ethernet router.  RF range tests showed a useful range of better than 125m.

The pcb was updated at the end of October 2011, so that Nanode could benefit from an RFM12 RF transceiver, and thus be compatible with JeeNodes from Jeelabs. In addition to the RF module, an extra 3V3 regulator, a miniUSB socket for power and an extra green LED were added.  There were spare locations on the board to which a 32K SRAM, a real time clock and a microSD card could be added to the underside of the pcb.

                                        Nanode RF on an early prototype green pcb.

Following the success of the first prototype above 500 Nanode RF units were produced in China on a distinctive blue pcb.

The last of these Nanode RF kits sold out at the end of October 2012. So from the humble kit beginnings and just 18 months, almost 2000 Nanodes have been produced so far, with production and kitting happening in the UK, USA and China.


                                     500 Nanode RF kits were produced on blue pcb.

Clearly a replacement to the Nanode RF was needed, and this is where our friends at Wicked Device of Ithaca NY, stepped in.

Wicked Device had become Nanode distributors in the USA, and in 2011 had produced their own versions of the kit and pcb.

Following the requirement for a low cost ethernet gateway product for the Air Quality Egg project,  Dirk Swart and Vic Aprea at Wicked Device redesigned the Nanode RF so that it utilised surface mount components, and could be mass produced on a pick and place machine. So it's my pleasure to introduce the new Nanode Gateway, the first of the Nanode SMT units, which is being produced at Wicked Device's premises in upstate New York. These units will be available through the Nanode Shop from early November.




The new Nanode Gateway produced by Wicked Device provides a platform for the creative development of web connected devices, and with the use of low power RF, extends this connectivity well beyond the reach of normal WiFi devices.
Nanode Gateway uses low power 433MHz or 868MHz wireless to form a bidirectional link between the internet and remote sensors.  Within the home and garden environment the Gateway has a range exceeding 100m. It is available with or without a radio module, which can easily be added later.
The Nanode Gateway allows you to connect sensor networks – for example environmental or weather sensors, to the internet for web based data logging and visualisation. The bidirectional wireless link allows the remote control of devices using relays or actuators providing the means for web controlled home automation.
Now it is possible to connect sensors all around your home and garden back to the internet, and control external devices using a web browser interface.

  • Combines Ethernet and low power 433MHz or 868MHz wireless connectivity on an Arduino compatible platform.
  • Provides a gateway for low power wireless devices to connect to the internet – with a range exceeding 100m.
  • Wireless link allows monitoring and control of remote sensors and devices
  • Open Source Hardware and Open Wireless Protocol used throughout.
  • ATmega328 microcontroller programmed using Arduino IDE
  • Supports up to 30 slave sensors or devices
  • Low cost, and expandable using Arduino like shields
  • Used in the Air Quality Egg and Open Energy Monitor Projects
  • A building block of the Internet of Things
  • Manufactured in the USA by Wicked Device.

Friday, October 26, 2012

Nanode on a breadboard - an exercise in minimalism

                                                        Nanode on a breadboard

The original Nanode was conceived as an exercise in minimalism, starting with the question "What is the least hardware needed to allow an electronic device to interact with the internet".

In the summer of 2010, I experimented with a simple design built onto a prototyping breadboard, and a few months later, in the spring of 2011, this crude prototype matured into the first pcb version of Nanode.

Yesterday, I had a bit of a tidy up around my work room, and I came across the original breadboard Nanode, so for a bit of a challenge, and some hindsight, I wondered if I could make the design even simpler.

At a very basic level, the Nanode consists of an Atmel AVR microcontroller and a Microchip ethernet controller. This combination was originally selected because they are both available in DIL packages and are thus easy to prototype with on a breadboard. Added to these ICs are the ethernet connector or magjack, a couple of crystals and a handful of passive components.

The ENC28J60 ethernet controller is a 3V3 device, and by running the micro at 3V3, the design can be significantly simplified.  With the micro running at 16MHz, the design draws about 165mA at 3V3.

Four connections are needed between the ENC28J60, which make up the SPI bus.  These are the MOSI and MISO data lines, the serial clock SCK and a chip select line.  Additionally the ENC28J60 should be reset from a common reset line shared with the microcontroller.

With this connection between the microcontroller and the ethernet controller, the micro has 6 analogue input lines and 8 digital I/O lines available for the user's application.

The ATmega328, ENC28J60 and ethernet magjack breakout board are all available from CoolComponents if you wish to build something similar at minimum cost.


Software Functionality

The key to the functionality of the Nanode is it's ability to operate as a simple web client, connected via the internet and communicate with a cloud based server.

As a bare minimum, the Nanode should be able to post a packet of data to the server application,  and request a packet of data back.  The outward going packet would usually contain data from analogue sensors and the return packet often contains command and control data.

In this context the server application is likened to a mailbox, where mail between two parties can be transferred via a suitable secure box, maintained by a third party.

In the case of Nanode, we are using a server application provides by the likes of emonCMS or Cosm, where in addition to simple handling of data, the server also runs data visualisation, arithmetical processing and data manipulation applications.  Having all this processing grunt available in the server, means that the code running on the microcontroller can be minimal.

As a simple example, I have the Nanode measuring outside and room temperatures using low cost thermistors connected to the analogue input channels. These temperature readings are posted up to emonCMS where the data can be graphed and visualised.

The digital output lines are connected to six LEDs, four of which also have a simple R-2R resistor ladder network connected, which makes a simple digital to analogue converter. This could be used for driving an analogue output - such as varying the brightness of an LED lamp.



Saturday, October 06, 2012

Upgrading a Nanode to a ATmega1284 microcontroller



One of the problems encountered with Nanode, or Arduino is that sooner or later you come up against the physical limitations of the ATmega328.

With 32K of flash and 2K of RAM, the '328 is great for small projects - but when you start to do anything that uses a lot of RAM - such as lots of string handling, the 2K limit is quickly exhausted - and strange things start to happen in your code.

So this week, my challenge was to familiarise myself with the ATmega1284, and assess whether it might be a suitable upgrade for some of my projects.

Earlier in the week, I looked at using the real time clock and SD card on the Nanode RF to make a 4 channel real time datalogger, and it was whilst developing this code, that the RAM limitations of the '328 really started to cause problems.

Fortunately, there is a fairly simple upgrade to the '328, and this is the ATmega1284P.  In a 40 pin DIP package it makes prototyping on breadboard easy, and with 16K of RAM, and 128K of flash it gives plenty of space for developing larger applications.

In addition to the extra RAM and Flash, it also has two more analogue input lines, a second UART, an extra INT pin, an option for a 32KHz crystal and 8 more I/O lines.

The I/O ports are arranged almost symmetrically around the 40 pin DIP, making the circuit layout very straightforward.  For reference, the ATmega1284P summary datasheet is here.

An ATmega1284 on a breadboard

The first thing to do was to build a minimum system on a breadboard consisting of the microcontroller, the 16MHz crystal, the reset circuit and a FTDI programming connector.



Above is the simple breadboard layout, with the crystal, 22pF capacitors and reset circuit at the bottom centre of the picture and the FTDI compatible programming connector at the top right of the breadboard.  This is the simplest possible circuit configuration (using 16MHz external crystal and remote RESET), and it leaves all the other remaining pins as I/O.

To begin to use this microcontroller, it first has to be bootloaded with an Arduino IDE compatible bootloader, and the files which describe it's registers and physical pinout have to be incorporated into the Arduino IDE.  To do this, I followed Maniacbug's excellent tutorial, and it wasn't long before I was up and running simply Blinky code on the ATmega1284.

The almost symmetrical layout of the I/O ports makes of a logical arrangement of functions.  There are four 8-bit ports Port A, Port B, Port C, and Port D. Unlike the ATmega328, there is no need to devote port pins to the 16MHz crystal or reset pin - so all 32 lines are available - compared to just 20 on the '328.   Additionally, there are 8 analogue input pins on Port A, and these do not share the I2C functions - as on the '328, so if you need more analogue inputs and I2C, the '1284 offers an extra bonus.

To make the '1284 compatible with the Arduino IDE, it needs to have it's physical port pins mapped  to the Arduino pin numbering system.  There are many unique ways in which this could be done, but I personally would prefer to have a pin-mapping that is similar to the Arduino '328 - so the likes of RX, TX, INT0, INT1 are given their familiar Dig 0, Dig 1, Dig 2 Dig 3 numbering, and the SPI bus appears on the familiar Dig 10 - Dig 13 pins. This way you can remember what is where  - without having to tie your mind in mental knots trying to port sketches and libraries from the '328 to the '1284.

The Ports

Port A is the analogue input port.  It appears on pins 40 down to 33 on the IC, with the related (and  useful) AREF, GND and AVCC on the adjacent pins 32,31 and 30.  It makes sense to bring all of these analogue pins out to a common connector. The order of A0 - A7 may appear in reverse to the Arduino user.  This is not a problem as the pin mapping file can be used to reverse the order in which these pins are named. Unline the 10-bit ADC in the '328 - this ADC has an input stage with selectable gain of x1, x10 and x200, and can be used both single- ended or differentially. The high gain stage makes direct reading of thermocouples a possibility.

Pins 1 to 8 of the package are home to Port B, the upper half  B4 - B7  has the hardware SPI lines present.  As Nanode uses three SPI connected devices on board, it would be neat to cluster the chip select lines for these devices on the same port - so provisionally allocating Port B0 - Port B5 as CS lines seems sensible.

Port D is located on pins 14 to 21 of the package.  It has our familiar pin functions like RX, TX INT0 and INT1, as well as four consecutive PWM pins on Port D4 - Port D7. If six pwm lines are required - for example driving 3 phase bridge circuits, there are a further pair of pwm channels on Port B3 and B4. There is a second UART available on Port D2 and Port D3 - if the INT0 and INT1 functions are not needed.

Port C runs along pins 22 to 29.  First are the I2C pins SCL and SDA on package pins 22 and 23.  These are useful for reading devices such as the real time clock on the Nanode.  The ATmega1284P is also provided with a JTAG port on pins 24 to 27.  If JTAG programming/debug is not needed, then these form a useful additional 4 general purpose I/O lines - but if you need JTAG, be careful what hardware you connect to them, which may interfere with the JTAG operation.  You may have to disable the JTAG function in the fuse settings if you want these lines for general use.

The ATmega1284P has a second on chip oscillator - designed to work with a 32768Hz watch crystal - or other 32KHz input.  These crystal pins are on pins 28 and 29.  If you use a watch crystal to implement an RTC - it is possible to wake the microcontroller from sleep at regular intervals, whilst otherwise keeping it in low power mode.

In addition to the rich peripheral set accessed by the ports, the ATmega1284P also has an additional 16 bit timer, and 4K of E2 non-volatile memory.

Connecting it up to a Nanode.

Having made up the simple breadboard, it was time to connect it up to a Nanode.

Most of the Nanode ICs are connected to the microcontroller using the SPI bus, and so the MOSI, MISO, SCK and any chip select lines would be needed.  The RTC clock/calendar IC on the Nanode uses the I2C bus, so SCL and SDA would be needed too. Finally the Nanode board would need to be powered, and provide a reset so +5V, 0V and RESET were wired back to the breadboard.

During tests it was found that the ATmega1284 would run at 3V3, but not program successfully at 3V3. It was therefore decided to run the '1284 at 5V, and use potential divider resistors on all the output lines between the '1284 and the Nanode RF board where almost all of the hardware runs on a 3V3 supply i.e. the ENC28J60, the RFM12 and the SDcard.

Dividers were thus needed on MOSI, SCK, and the three chip selects ENC28J60_CS, RFM12_CS and SDcard_CS.  The RTC connected to the I2C bus is 5V tolerant and so no divider was needed.

In the top photograph, the lower breadboard made from clear plastic has these potential divider resistors  - and some diagnostic LEDs.  The Nanode Rf with the '328 microcontroller removed is on the right.

The next blog post in this series will have links to code developed to run the Nanode RF using the ATmega1284.









Friday, September 21, 2012

Casting the net further

About 3 years ago, whilst working for a central London energy monitor company, a co-worker asked me whether it would be possible to come up with a device which would allow her to switch off electrical equipment in her home remotely using the internet. I thought about the problem for a while and finally cobbled together a solution that used a Microchip  dev.Net board, a repurposed wireless remote socket and a couple of PIC microcontrollers.

It was a real lash up, but it proved the point that the internet could be usefully used to remotely control simple devices - provided that it could extend it's capabilities beyond the router and the CAT 5 cable, using a low power wireless link. With wireless, it is possible to set up a zone of connectivity around the ethernet to wireless gateway, and any wireless compatible device brought into this zone would benefit from 2 way connectivity to the net.

This proved to be a powerful technique, and the advent of affordable open source hardware based around the ATmega328 microcontroller and the RFM12 low power transceiver allowed users to experiment with wireless sensor networks for remote sensing and home automation. The great thing is that this hardware is now available from a variety of sources, and shares RF compatibility - allowing you to mix and match devices from various supplers.

Further experimentation in the Autumn of 2009, led to the desire to produce an affordable internet gateway, and some work was done using hacked about energy monitors and the ethernet equipped Microchip PIC 18F67J60. Our out of hours development work led to "once per second"  power readings being sent up to a web based visualisation system in December 2009. Some of these ideas ultimately went on to be incorporated into the Nanode project during its design phase in March 2011.

This summer, I have spent the last 6 weeks visiting a variety of weekend events, including OggCamp 2012 in Liverpool, EMFCamp in Milton Keynes, the Brighton Mini Makerfaire and just theis last weekend OSHCamp in Hebden Bridge, Yorkshire.  Seldom a weekend at home it has been a good opportunity to catch up with the open hardware scene - and realise that the whole world has gone Raspberry Pi mad.

It was in the open fields of EMFCamp that I decided to do a range check on the RFM12 wireless transceiver. Using a couple of devices fitted with 868MHz  modules, I was able to wander around the campsite with my laptop and prove that the open field range of the RFM12 was better that 250m.  In a built up suburban enviroonment this translates to about half this distance - a similar test done in my street got me 130m from home before packet reception became unreliable.

The emonCMS server has become a central part of the remote control and monitoring system. It allows data  from sensors to be datalogged and visualised, whilst at the same time permitting commands and data to be sent back to control wireless nodes.


If we think of a node as a microcontroller with a RFM12 attached, there could be a standard set-up page on the server  which allows you to define the pin functions and allocate those to various input devices - analogue and pulse sensors, and output devices, such as relays, pwm drives etc.



It is our intention to publish the command structure - we are still looking at the best way to send both commands and data back to individually addressed wireless nodes.

There is also the issue of privacy/security.  It's good to be able to see other people's public feeds, but if my gas boiler is connected to a relay on a wireless node, it would be foolish and unsafe to give everyone access to that device.

One way of handling this is with the use of apikeys that are generated when you first register an account on the server.  

The simplest node could just be an Arduino with RFM12 shield or JeeNode, or even a stripboard Arduino with an RFM12 module mounted. Nathan Chantrell cleverly shows how to do this in his blog post


Nathan has gone on to produce similar designs with the ATtiny - if you want minimalism, and the ATmega1284 - if you want more I/O and much more RAM.  For most people, the ATmega328 is a good compromise, offering 6 analogue inputs and 7 digital I/O, once the RFM12 has been connected to the SPI bus and interrupt line.
The introduction of the new surface mount, ready assembled Nanode RF, this Autumn, will allow easier access to the project for those who don't wish to solder a kit, and with the Sketchgarden remote bootloader and emonCMS server, it should be even easier for people to get up and running quickly.

There is now Nanode compatible hardware from JeeLabs, OpenEnergyMonitor and just recently harizanov.com - with a very small ATtiny based node http://harizanov.com/2012/09/super-funky/

With a lot of people purchasing Raspberry Pi's, it seems logical to use this device as a local server running a version of emonCMS, and already some experimental RFM12 boards have been made that plug into the PI's   I/O connector.


We believe that the combination of simple open source hardware based on 8 bit microcontrollers fitted with a low power wireless link is a powerful tool for extending the internet into the field of remote control and monitoring.  Nanode is committed to continue to produce low cost hardware to develop this further.

Ultimately we should like to see Nanode gateway products priced between £20 and £25, and simple ATtiny based wireless sensing nodes starting at around £10.  Continued use of DIL and through-hole designs suitable for the DIY/home assemby market, will be complemented by ready assembled SMT devices.




Wednesday, September 19, 2012

Using Nanode RF and emonCMS for Remote Control and Monitoring

For the last couple of years I have been looking at simple low cost methods of measuring analogue data - such as that from temperature or humidity sensors placed around the house and getting that data up to a cloud based server for datalogging and visualisation.

With the introduction of the Nanode RF last September, and working cooperatively with Trystan Lea and Glyn Hudson of openenergymonitor.org, we have managed to send data from a variety of wireless sensors to   their emonCMS server.  Trystan and Glyn developed this platform mainly for logging and visualising electricity data - both consumption and generation from PV,  but the versatile open source server can be applied to a wide variety of monitoring and control tasks.

A typical system consists of one or more wireless sensors, which collect analogue or pulse data and send this  wirelessly to the Nanode RF - which acts as a basestation.  Nanode RF runs a multinode sketch, which allows it to receive data from any wireless sensors that are in range.  These sensors have a node ID from 1 to 30 and can be arranged into different groups.  Provided that the sensors don't all try to transmit at once, and a certain time randomness should be included into their transmission slots, the Nanode RF receivers their individual data and posts it up to the emonCMS platform - where it can be manipulated, graphed and visualised.

As well as electricity data, emonCMS has been used for heat-pump monitoring and bee hive temperature monitoring.

Now a little documented feature of emonCMS is that the last posted value of a feed can be requested by the Nanode RF. This feed value can generally be a 16-bit integer - for easy interpretation by the Nanode, and with suitable decoding can be used to control the hardware either on the Nanode RF, or any of the wireless nodes connected to it.

It is a relatively simple matter to create a web dashboard which contains switches and rotary controls - allowing different parameters to be sent to the Nanode RF, or any of it's connected nodes.  A simple example would be a RGB LED lamp, connected to pwm pins on the Nanode controlled remotely from sliders or knobs on the dashboard.

Trystan's code for reading a feed value is here on GitHub. - example g in the Guide.

An extension to this is to get the Nanode RF to send the feed value as an RF packet out to the wireless nodes. A simple hack was done using one of Trystan's graphical LCD display units which feature a pair of bicolour LEDS.  Sending a feed value of 0,1,2 or 3 allows you to remotely control the colour of the LEDs from the server - and because we are using the RFM12 - the distance between the base station and the wireless node can be quite large - better than 200m in open field conditions or 100m or so in a built up street.

If you want to experiment with this you just need to put the following line of text into your browser's URL window

http://emoncms.org/api/post?apikey=8795903a6c0611123007be8abef16e60&json=power:1

By changing the value of power from 0 to 3, this causes the colour of the  LEDs on my emonGLCD to change.  You can send me feedback on Twitter @monsonite - I'm going to get a proper control dashboard set out, but for the moment it proves the point.

More complex controls, for individually accessing the I/O pins of remote RF nodes could readily be devised, allowing each such node to be accessed from it's Node ID.  The read apikey ensures that your feeds are safe and cannot be tampered with - without having the apikey, the correct password and username.

This system makes it very simple to incorporate the monitoring and logging of room temperatures - from RF nodes - including WiNode, JeeNode - and the new very small, ATTiny84A based Funky,  another RFM12B compatible node. At the same time, relay outputs connected to these nodes could be used to turn on central heating, open or close radiator valves, or switch on or off electrical appliances - all from the web accessible platform - using laptop or even an app running on a smart phone.

In the 18 months that Nanode has been around, it has gone from strength to strength, and is now appearing in various compatible versions from new suppliers. The latest is the Micro IoT Gateway from Harizanov.com. This will make a great RF gateway, but just needs a RFM12 fitted to make it compatible with Nanode, Openenergymonitor and JeeNodes.

Watch out for an important announcement regarding an exciting Nanode new product towards the end of next week.


Sunday, August 26, 2012

Building an end to end wireless sensor network

Building Wireless Sensing Networks - using Open Hardware.

Over the last couple of days, I have been busy setting up a wireless sensor network as part of the preparations in readiness for a web controlled central heating and energy monitor system.

The various hardware modules are all open source designs - and I chose to use a mix and match approach to the system which was put together using hardware from a variety of vendors.  Compatible devices were used from JeeLabs, Openenergymonitor and Nanode Ltd.

If you haven't looked recently, there are now new offerings of ethernet gateways, wireless sensor nodes and open data services from a number of providers - the smart wireless sensor arena is starting to get interesting.

Here's a brief description of what is needed. A fuller report will appear in a later post.

There are four main parts to the system:

1.   The Wireless Node or WiNode. Supplied by Nanode Ltd.


This is the system's analogue sensor, with 4 channels of 10 bit ADC.  WiNode can interface to a range of analogue inputs, such as thermistors for temperature sensing, current sensors or optoreflective sensors for detecting motion or occupancy  or even reading the dial on the gas meter. Additionally there are 4 channels of high current pwm driver which can be used to control relays, solenoids, dc motors etc. WiNode can be powered from a 5V USB type supply, or from a 9V-12V unregulated dc supply. Typically WiNode reads its analogue inputs every 5 to 10 seconds and sends that data wirelessly to the display and the ethernet gateway.

Whilst WiNodes can be bought from Nanode Ltd - for around £30, there are other compatible sensors such as the JeeNode available from JeeLabs and emonTX - an electricity monitor available from Openenergymonitor.  The JeeNode was the first device to combine the ATmega328 and the RFM12B - and  through Jean-Claude Wippler's RFM12 code library, wireless communication between devices has been made very simple to implement.  The JeeNode is the "barebones" wireless node - and this is reflected in the price - just 18.50 Euros - from the Jeelabs Shop.



Alternativley, if you are feeling keen, the basic microcontroller and RF module used on WiNode can be hand assembled on stripboard for about £10.  Here Nathan Chantrell shows  temperature sensors based on both the ATmega328 and a temperature sensing node based on the ATtiny. 

2. The second part of the system is the Ethernet Gateway. This is supplied by Nanode Ltd - and is known as the Nanode RF.



The gateway combines the microcontroller and wireless transceiver with an ethernet controller - in this case the ENC28J60.  The on board micro runs a simple client, which receives the data packets from the sensor nodes, and pushes them up to the web based server.

As well as pushing data up to the server, the Nanode RF makes the occasional request for server time. Once it receives the packet containing server time - it makes it available to the emonGLCD so that real time can be displayed. The time packet could be sent to all nodes in the system to synchronise their time - as WiNode has an on-board real time clock, which could be occasionally be reset to server time.

The basestation is supplied by openenergymonitor as emonBase, or you can buy it directly from Nanode Ltd as the Nanode RF for about £30.

3. The third part of the system is a local graphic LCD display from openenergymonitor.org.


This display allows you to monitor the wireless data from the sensors, and also can be used as a user interface. It has it's own temperature and light sensors, and two Red/Green LEDS which can be used to indicate physical conditions - such as too hot, too cold - or for energy monitoring applications high load and low load. In addition, the display has 3 user configurable push buttons - which can be used for screen and menu item selection.

Whilst primarily a display module, the emonGLCD also sends regular temperature readings up to the server, and also has it's on screen clock regularly updated by time requests to the server.

This display is obtainable from openenergymonitor.org as a DIY kit, emonGLCD for approximately £50.

4. The final piece of the "end to end" system is the cloud based application software running on a web server.  This program receives the raw data from the Ethernet Gateway, and allows it to be processed and visualised by the remote server. The very nice chaps at Openenergymonitor have written a very comprehensive server, data logger, visualiser, and analysis package called emonCMS.

Here is a screenshot from Nathan Chantrell - who has been using emonCMS for energy and temperature monitoring.

It just takes a few minutes to log in to emonCMS, obtain an api key and start to record and graph data from the sensor network. emonCMS was originally intended for energy monitoring, but it is versatile enough to manage a range of different data feed types and has a wide variety of data manipulation, graphing, visualisation and dashboard controls.