TinyBasic for Nanode

As one of those that has taken up the TinyBasic Christmas Challenge issued by Ken Boak, the creator of Nanode, a range of Arduino compatible microcontroller boards, I am posting my updates. The basic code is able to run simple Basic commands and run small programs. What it lacked is a place to store programs for later retrieval and also to increase the available memory for program storage. Unlike the Arduino, the Nanode-RF comes with an external 32Kbyte static ram chip fitted and a uSD card slot.

The codebase used is at https://github.com/thiseldo/NanodeBasic and already had a number of I/O commands added by Dave CJ.

My updates are:

  • Move static strings to program memory
  • Convert to use SRAM for TinyBasic program, variables and stack giving 32768 bytes instead of the original 1400bytes!
  • Implemented LOAD/SAVE/DIR/LRUN commands for uSD to Load, Save, directory list and load then run basic programs. All stored in plain text and can be changed by editing the files on the uSD card.
  • Include simple SRAM checker, if it fails then pulse digital pins 5 and 6 alternately
  • Added simple MEM command to display memory used
  • Added EPEEK and EPOKE to access EEPROM memory. Thanks Dave-CJ
  • Added RND for simple random number generation. Thanks Dave-CJ
  • Added TSECS for incrementing tenths of a second counter
  • Auto running of program from uSD card. Just name the program autoexec.bas and it will load and run on reset.

Filenames on the uSD card are 8.3 format and there are no checks on file extensions that you can use. Files are stored in the root directory only.

Still to do:

  • Filename handling is basic, only coded to prevent buffer overflows, format isnt checked.
  • No protection on setting I/O pins used by SRAM and uSD card. DOUT 4=0 will cause Syntax Error to be displayed for every subsequent command.

The sketch is available to download from https://gist.github.com/1519025, please note this required a Nanode with SRAM and uSD socket to fully work.

It requires a number of libraries to function:

One thing I have found is that when using Nanode with SRAM fitted, as this is a 3.3V board, the fuses set on the Atmega328P must be set correctly in order to prevent the board locking up. By default this is set to 2.7V but for a 3.3V board this can be too high, resetting it to 1.8V seems to cure this.

For those with suitable programmer, an alternative bootloader is available at https://github.com/thiseldo/NanodeBootLoader. This includes options for both 5V and 3.3V boards.

Migrating from EtherShield to EtherCard library

Important Note: I have now stopped development work on the EtherShield library
However, this is not the end for Arduino and ENC28J60 libraries. The excellent EtherCard library from Jeelabs. I have decided this is the library I am to use in my examples and future ENC28J60 developments.

As the two librariers are based on the same original code they share a lot of similarities. One being that I wrote the DHCP code that is used in both the EtherShield and EtherCard libraries. There are a number of things that need to be changed in your sketches in order to use the EtherCard library. These are documented below:

Header file

Replace

#include <EtherShield.h>

with

#include <EtherCard .h>

Library use

Replace

#define BUFFER_SIZE 650
static uint8_t buf[BUFFER_SIZE+1];
EtherShield es=EtherShield();

With

#define BUFFER_SIZE 650
byte Ethernet::buffer[BUFFER_SIZE];

ENC28J60 initialisation

Replace

// Initialise SPI interface
es.ES_enc28j60SpiInit();
// initialize enc28j60
es.ES_enc28j60Init(mymac,8); // To specify CS pin
// es.ES_enc28j60Init(mymac); // To use default CS

es.ES_client_set_wwwip(websrvip); // target web server

With

if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) {
Serial.println( "Failed to access Ethernet controller");
while(1);
}

or, if using a Nuelectronics ENC28J60 shield to use correct select pin

if (ether.begin(sizeof Ethernet::buffer, mymac,10) == 0) {
Serial.println( "Failed to access Ethernet controller");
while(1);
}

Allocate IP Address using DHCP

Replace

es.allocateIPAddress(buf, BUFFER_SIZE, mymac, 80, myip, mynetmask, gwip, dnsip, dhcpsvrip );

With

if (!ether.dhcpSetup()) {
Serial.println( "DHCP failed");
while(1);
}

Allocate IP Addresses using static addressing

Define Addresses with

static uint8_t myip[4] = { 192,168,1,101 };
static uint8_t gwip[4] = { 192,168,1,1 };

Setup library with

ether.staticSetup(myip, gwip);

Resolve DNS Hostname

Replace

if(!es.resolveHostname(buf, BUFFER_SIZE,(uint8_t*)HOSTNAME ) > 0 ) {
Serial.println("Hostname not resolved");
}

With

if (!ether.dnsLookup(PSTR( HOSTNAME ) )) {
Serial.println("DNS failed");
while(1);
}

Other Bits

You might want this bet next just to make sure ARP has been done for Router address:


while (ether.clientWaitingGw())
ether.packetLoop(ether.packetReceive());

Main Processing

Replace

dat_p=es.ES_packetloop_icmp_tcp(buf,es.ES_enc28j60PacketReceive(BUFFER_SIZE, buf));

With

dat_p = ether.packetLoop(ether.packetReceive());

Sending HTTP GET Request

Replace

es.ES_client_browse_url(PSTR("/xyz.php"), "?search=Arduino", PSTR(HOSTNAME), &browserresult_callback);

With

ether.browseUrl(PSTR("/xyz.php"), "?search=Arduino", PSTR(HOSTNAME), &browserresult_callback);

General Packet Buffer Access

Replace

buf[55]

With

Ethernet::buffer[55]

DHCP Lease Expiry

Replace:

With

if (ether.dhcpExpired())
ether.dhcpSetup();

Setting alternative port for http server

Use:

ether.hisport = 1234;

There are more changes but these should cover most cases for now.

Still to address

The EtherCard library still needs a few updates such as:

  • Sleep mode
  • dodgy TCP implementation allowing only one packet before closing connection

Remote sketch upload to Nanode

Recently I made a presentation to the Nanode application development weekend to demonstrate how to send a new sketch to a Nanode. The idea is that the nanode is a network connected device and with a small addition to the ENC28J60 library it would be possible for it to act as a simple TFTP server and accept new code uploads or poll a remote server to check for updated sketches that can be downloaded.

The demonstration on the day conisisted of a Nanode with additional SRAM installed on the empty pads on the underside of the board. The nanode was also converted to run at 3.3V only as this is a requirement for the SRAM chip. The demonstration nanode had a modified bootloader that checked the SRAM for a magic number at the start and a code length. This then triggered the bootloader to reprogram the main flash. The compiled sketch in .hex format was transfered from a laptop to the nanode using a tftp client.

As detailed in the presentation, there are a number of options for delivering the updated code to the nanode, this was a simple proof-of-concept to demonstrate that once a sketch has been transfered to SRAM it is then possible to reprogram the flash with this new image. The actual delivery mechanism is likely to change in the future but the code in the bootloader should not need to change much.

There are still a number of issues to resolve, such as using an optimised bootloader to increase the available code space, add extra checks to make sure malicious code is not sent or recieved by the nanode.

The bootloader is still available from GitHub

More details and code to follow.иконииконописikoniсвети георги

Nanode gets new boots

I’ve been playing around with a couple of Nanode boards for a while now after having an early beta version. Check out the link for full details and specs. The latest version has a few options and I am using one in standard form to connect my CurrentCost electricity monitor to my local lan to broadcast updates. The second Nanode has been modified to use 3.3V instead of 5V and to include the 32K SRAM chip on the underside of the board. This then enables the Nanode to have a lot more storage space than is currently available on the Atmega328 and to also be able to work with a MicroSD card without having to include level shifting resistors.

One frustration I have found with the stock bootloader included with the Nanode kits is that there is no indication that anything is happening when you are uploading a new sketch. The first thing I wanted to do was fix this. I’ve based my updates on the Arduino Optiboot boot loader as this has been cut down and optimised for the Arduino by shrinking its code size and increasing the upload speed, amongst other things.

The Optiboot code allows a LED to be flashed upon restart and to be flashed when data is being transfered. As the existing code was configured to use the Arduino LED on digital I/O pin 13, it was a simple matter of changing the defines for the LED pin.


// nanode LED connected to PD6 - I/O 6
#define LED_DDR DDRD
#define LED_PORT PORTD
#define LED_PIN PIND
#define LED PIND6

Make sure that the line #define LED_DATA_FLASH is not commented out too.

Using a modified makefile to compile the code using WinAVR produces a .hex file that can then be burnt into the Atmega328.

To integrate the bootloader into the Arduino IDE its a matter of creating the nanode directory in the sketchbook/hardware/ directory. The directory tree should be like this:


sketchbook
+--hardware
   +--nanode
      +--bootloaders
      |  +--nanodev5
      +--cores
      |  +--Arduino
      +--boards.txt

The bootloader .hex file is in the nanodev5 directory, the cores/Arduino contains a copy of all the files from arduino-0022hardwarearduinocoresarduino.

The boards.txt would look like this:

# Optiboot Nanode support
# based on Optiboot from http://optiboot.googlecode.com by Peter Knight, 2010
#
# Nanode support by Andrew Lindsay
##############################################################

nanodeatmega328o.name=[Nanode] Nanode V5 w/ ATmega328
nanodeatmega328o.upload.protocol=stk500
nanodeatmega328o.upload.maximum_size=32256
nanodeatmega328o.upload.speed=115200
nanodeatmega328o.bootloader.low_fuses=0xff
nanodeatmega328o.bootloader.high_fuses=0xde
nanodeatmega328o.bootloader.extended_fuses=0x05
nanodeatmega328o.bootloader.path=nanodev5
nanodeatmega328o.bootloader.file=optiboot_nanodev5.hex
nanodeatmega328o.bootloader.unlock_bits=0x3F
nanodeatmega328o.bootloader.lock_bits=0x0F
nanodeatmega328o.build.mcu=atmega328p
nanodeatmega328o.build.f_cpu=16000000L
nanodeatmega328o.build.core=arduino

############################################################

If all is well then you should see a new board, [Nanode] Nanode V5 w/ ATmega328 on the IDE Boards menu.

You should now be able to burn the bootloader from the Arduino IDE using your preferred programmer.

I’ll get round to packaging this up for the benefit of those that dont have te AVR toolchain installed and are not able to compile their own boot loaders. However, you’ll still need a programmer such as the USBTinyISP, in order to burn it into your Atmega328.

Did I mention the extra RAM? What can it be used for? With a modified bootloader it should be possible to upload code to the SRAM and trigger a reset that then causes the bootloader to check for a valid code image and program the sketch as if it had been received through the serial interface. I already have a simple upload sketch that uses TFTP to transfer a .hex file and writes it into the SRAM. More about this later…

икониПравославни икониикони на светци

Spam of the Day

I had to laugh at this spam that dropped into my junk folder:


I am The Rt Hon David Cameron MP,Prime Minister, First Lord of the Treasury
and Minister for the Civil Service British Government. This letter is to officially inform you that (ATM Card Number 7302 7168 0041 0640) has been
accredited with your favor. Your Personal Identification Number is 1090.The VISA Card Value is £2,000,000.00(Two Million, Great British Pounds
Sterling).Contact Rt Hon William Hague MP.First Secretary of State for Foreign and Commonwealth Affairs
Email; primeminster@live.co.uk
Tel: ;+447045727044

If anyone falls for this then they are obviously dumber than the originator of the email!

RESTduino for Nanodes

икониAfter seeing the work done by Jason Gullicksonism on RESTduino – Arduino for the rest of us and code at https://github.com/jjg/RESTduino I decided to port the sketch to use my ENC28J60 EtherShield library. This is now available at https://github.com/thiseldo/EtherShield_RESTduino and includes an updated demo.

A perfect platform for this code is the Nanode, it provides Arduino functionality combined with an integrated ENC28J60 ethernet all on a single board.

The demo uses a common cathode RGB LED (can be easily modified for common anode) with blue to digital 3, red to digital 5 and green to digital 6. The demo is based on the slider UI demo from the jQuery site. This shows 3 sliders and a square of colour, the sliders change the colour of the square. This also sends an request to the Arduino to update the appriopriate colour value.

To use, just point your web browser at the Nanode, for example http://192.168.1.177/3/HIGH will set pin 3 high. Replacing HIGH with a value in the range 0-255 on a PWM enabled pin will set the PWM value.

http://192.168.1.177/3 will return the status of pin 3 as JSON {“3″:”HIGH”}

Using http://192.168.1.177/a0 will return the analog value of A0 as JSON {“a0″:”123″}

With these calls you should then be able to interface your Nanode or arduino with ENC28J60 based ethernet board with web pages and other apps that can send http requests and receive simple JSON responses.

Full credit to Jason Gullicksonism for the original code, ideas and documentation.

Creating a Pachube controled RGB LED

This article describes a simple application to demonstrate the ENC28J60 EtherShield library. It can be run on a plain Arduino or clone and an ethernet shield based on the ENC28J60 ethernet chip (Note: This is not the same as the official ethernet shield). An alternative is to use the Nanode, wiki site at Hackspace which combines the arduino and the EtherShield into a single compatible board. It also demonstrates the network capability of the board and the control functionality included in the Pachube dashboard application.

For this project you will need:

  • Arduino or clone and ENC28J60 based ethernet shield or a Nanode
  • Pachube account and master api-key
  • RGB LED and 3 current limiting resistors, value depends on specification of LED
  • Internet connection with router running DHCP server

RGB LEDs

RGB LEDs are available in the UK from:

Oomlout. I received one of these LEDs as a sample in an order and can say it is a good bright LED with a cloudy lens which helps blend the colours.

Earthshine Electronics. I’ve not tried these, but for a pack of 10 they are a bargain!

There are two types of RGB LED, one with a common cathode (negative) and the other with a common anode (positive). Both will work with the example provided the correct option is selected at the top of the example code. If this is set incorrectly then when you expect the LED to be on, it will be off and vice-versa.

Similar RGB LEDs should be readily available in whatever country you reside in.

Setting up Pachube feed

For the demo we are using the Pachube dashboard to create a set of three knobs that control the datastreams in a feed. The datastreams and feed are created in your account when the dashboard is created. The steps are described in detail below and shown in the screen captures.

  • Login to www.pachube.com
  • Click on the link my api keys. Take a copy of your master API key as you need it in the app.
  • Visit the Pachube dashboard app page at http://apps.pachube.com/dashboard/create.php
  • Enter 0 for number of switches and 3 for number of dials then paste your master API key in to the box.
  • Open the link that says Then click here when you are ready! in another browser window or tab
  • you should then see the results with 2 links, one to the dashboard and another for the stream.
  • To make things look nice, we should edit the stream first. As per the screenshot below, set the stream names in the Tags fields to Red, Green and Blue then click the Save Feed button.
  • Go back to the results tab and open the dashboard URL in another window or tab. It should now have the dials labelled Red, Green and Blue

Get your master API key Create the app, select 3 dials All created

 
Update the feed names Access the dashboard  

Running the example code

After installing the updated ENC28J60 library (you may need to check this out again as the example has recently been updated), launch the Arduino IDE, select the Examples menu option and select the EtherShield library then the EtherShield_PachubeRGB example. The parts to change are:

  • Set the type of RGB LED, use #define COMMON_ANODE for common anode LEDs and #undef COMMON_ANODE for common cathode LEDS
  • Set BLUEPIN, REDPIN and GREENPIN to the correct PWM ports used for your LED, defaults are 3, 5 and 6
  • Define your Pachube master API key in the HOSTNAME variable. Replace the XXXXXXXXXX part with your API key
  • Define the feed number to use in the HTTPPATH. Replace the NNNNN part with your feed number

Upload the sketch to your Arduino or Nanode, plug in the ethernet and away it should go. Initially the LED is Red, then amber while it is allocating an IP address, then green when it is ready to request Pachube data.

Select your browser window with the Pachube dashboard, point your mouse at the end of one of the knobs and drag it round, the value should change on the dashboard. After a few seconds you should also start to see the RGB LED change colour.

Nanode working RGB LED Close up view of RGB LED and current limiting resistors

This is only an example, however, with suitable code it could be reading a Pachube feed that monitors your electricity usage and changes colour according to how much is being used, i.e. green for low and red for high power usage.

икони

New EtherShield Library available

The long awaited update to the EtherShield library is now available, the changes in this release include:

  • DHCP support – Now your project can find its own IP address, dns server and gateway addresses
  • Fixed length problems in main ip code where packet lengths were being passed as bytes not words so as a result incorrect packet lengths were being seen
  • New examples added for DHCP test and Pachube RGB LED demo using DHCP

Find it on Github at https://github.com/thiseldo/EtherShield

We are mobile.

This site is now available in mobile format. Just access it from your phone browser. It comes in as text with minimal graphics.

Give it a try, just scan the code to access the site from your mobile phone!

qr code

New Ethernet Packet Dump library

I’ve created a small library to dump received ethernet packets to the serial port. Pull the code from Github at https://github.com/thiseldo/EthPacketDump for more.

See the README file for how to use it.

ИкониikoniИдея за подаръкикони