Archive for the 'Arduino' Category

Arduino + Ethernet shield, A better Webserver

The Arduino Ethernet Shield allows an Arduino board to connect to the internet. It is based on the Wiznet W5100 ethernet chip (datasheet). The Wiznet W5100 provides a network (IP) stack capable of both TCP and UDP. It supports up to four simultaneous socket connections.

The Webserver example sketch that comes with the Arduino IDE is is a great starting tutorial and should be at lest reviewed before reading this tutorial. The web server example sketch is  missing a few key features such as, the ability to respond to different GET requests with different response.

This tutorial assumes that you are using Eclipse as your editor for Arduino and you are familiar with both the Arduino and the Ethernet shield. This tutorial was created in response to VHS Hack challenge. You can download the Eclipse project for this tutorial from our website. Respond to different GET requests with different response source code.

When you request a page from the Arduino in your web browser, your web browser create a HTTP GET request. This GET request is sent to the Arduino’s where it gets processed and a response is formed and sent back to your web browser and is displayed to the user.

The GET request will look something like this

Hypertext Transfer Protocol
GET /helloworld.html HTTP/1.1\r\n
Host: 192.168.1.177\r\n
Connection: keep-alive\r\n
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1025 Safari/532.5\r\n
Cache-Control: max-age=0\r\n
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n
Accept-Encoding: gzip,deflate,sdch\r\n
Accept-Language: en-US,en;q=0.8\r\n
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n
\r\n

The first line in a HTTP GET request contains the requested file, in this example it is /helloworld.html. We can use this line to determine what page the user is requesting. The following function takes the request buffer and extracts the request string and stores it in a CWebserverRequest structure for reference.

bool CWebserver::ProcessHeader( char * requestBuffer, CWebserverRequest * request )
{
if( request == NULL || request == NULL ) {
return false;
}
sprintf( request->request_string, "/" );
// Get the request path.
char * get = strstr( requestBuffer, "GET " );
if( get != NULL ) {
get += 4 ;
char * eol = strstr( get, " " );
if( eol != NULL ) {
eol[ 0 ] = 0 ;
}
unsigned short length = strlen( get );
if( length > HTTP_MAX_REQUEST_LENGTH ) {
length = HTTP_MAX_REQUEST_LENGTH-1;
}
strncpy( request->request_string, get, length );
request->request_string[ length ] = 0 ; // NULL termanate the string.
}
return true;
}

We can user the request string later in our code to determine what page the user was requesting and respond accordingly.

More information

Did you like this post?

Subscribe To The RSS Feed!
To catch many more articles like this in the future, make it easy on yourself and subscribe to me via RSS. You will not regret it!

Do you have a question?
We will do our best to try and solve any building automation, protocol, integration problem you may have

Arduino, Where to get help

Arduino is an open-source, open-hardware,  electronics prototyping platform based on flexible, easy-to-use hardware and software. It’s intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments.

This links/help page was created in response to VHS Hack challenge.

Arduino IDE

You can download a free and open source Arduino IDE from Arduino.cc. It works pretty well and includes lots of example sketches to learn about the Arduino. Once you are comfortable with the basics you can upgrade to using Eclipse as your main IDE for Arduino.

AVRDude

AvrDude is the application that is used to download code to the Arduino and is included with the Arduino IDE and WinAVR. If you are having problems downloading code to the Arduino read this article from LadyAda.net on AVRDude.

Ethernet shield RAW packet

A Ethernet shield was created for the Arduino that supports TCP connections. I can be used to serve webpages of the current status of the Arduino among other things.

By default the Ethernet shield only supports TCP connections, but you can download Ethernet RAW library to support UDP and RAW packets. This Ethernet RAW library also fixes some of the bugs discovered in the default Ethernet libary shipped with Arduino IDE 015.

Help with your projects.

There are several different places to get help with your Arduino projects.

  • Arduino.cc Playground – Arduino code, circuit diagrams, tutorials, DIY instructions, tips and tricks
  • Arduino.cc Forms – A form to talk to other developers about all things Arduino
  • ladyada.net Learn Arduino – Ladyada website has more great tutorials.
  • Chiphacker – A Question and Answer website for chip developers that is useful for getting specifics information about a certain features in the Arduino chip (
  • StackOverflow –  A Question and Answer website similar to chiphacker that is useful for getting answers to programing related questions.
  • Atmega 168/328p data sheet – Can find an answer to your question anywhere else, check the chips data sheet.
  • Getting Started with Arduino, Making Things Talk – Are two great books filled with different Arduino projects and examples.

Project ideas

  • Make: Arduino – Make magazine is one of the forerunners for everything DYI. There website has lots of information and examples projects.
  • Instructables.com – Tones of example projects and tutorials for the Arduino.
Did you like this post?

Subscribe To The RSS Feed!
To catch many more articles like this in the future, make it easy on yourself and subscribe to me via RSS. You will not regret it!

Do you have a question?
We will do our best to try and solve any building automation, protocol, integration problem you may have

Using Eclipse with Arduino Duemilanove

The Arduino IDE is good for simple projects, but as soon as you start building more complex multi file projects it just not up to the job. Eclipse on the other hand is free, powerful, and full-featured development environment that can be set up to work with AVR and Arduino. Arduino’s website has some decent instructions on how to compile with Eclipse for Arduino but its has some missing/confusing steps and is meant for C development not C++. It still has some good reference material that you should review.

In this tutorial we will be showing you how to set up the eclipse environment, Build the Arduino library, Create a Arduino project, Compiling C/C++ code, Uploading code to the Arduino, FAQ. This tutorial was created in response to VHS Hack Challenge.

Setting up the Eclipse environment

Download
You may not be able to find the exact versions that are used in this tutorial, just you the most updated versions.

Installation
  • Install Java runtime. Most computers will already have Java installed as it is used for many other applications.
  • Install Arduino software. The Arduino software comes with the Arduino library and source code examples that will be useful when developing your own projects.
  • Install Eclipse. This should be pretty straight forward. You can install Eclipse in any directory you want. For this tutorial we installed it in C:\Dev\eclipse-cpp
  • Install WinAVR, You can install WinAVR in any directory you want. For this tutorial we installed it in C:\Dev\WinAVR-20100110
  • Install Eclipse AVR plugin. You will have to manually copy this “plugin” into the the Eclipse installation. Move the folder which begins with the name “de.innot.avreclipse” to the “dropins” folder located within the eclipse folder. Restart Eclipse if you have it already running. There should now be a new “AVR” toolbar button in your workspace.

Build the Arduino library

Before we can create our first Arduino project we have to link in the Arduino library for Eclipse. There are several different ways of doing this described on Eclipse for Arduino tutorial but for this tutorial we will be creating a static library.

You can download the project and the library directly and skip this step.

  1. Open Eclipse and start a new C++ project.
  2. C++ Project
    • Project Name: ArduinoCore
    • Project Type: AVR Cross Target Static Libary
    • Toolchains: AVR-GCC Toolchain
  3. Select Configurations, Uncheck debug we will not be using the debug version of this library in this tutorial.
  4. Set the MCU type and frequency for your Arduino board. For example, for the Diecimila, use ATmega168 running at 16000000 Hz. For new versions of the Duemilanove, use ATmega328P running at 16000000 Hz. If you look closely at the Arduino board, you can see the target platform written on the main processor. Click Finish.
  5. In Eclipse, click Project->Properties.
  6. Select C/C++ Build and expand the category (e.g., click the diamond to the left of “C/C++ Build”).
  7. Select Settings under C/C++ Build.
  8. In the right pane, Click AVR Compiler
    • Debugging. Set “Generate Debugging Info” to No debugging info.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  9. In the right pane, Click AVR C++ Compiler
    • Debugging. Set “Generate Debugging Info” to No debugging info.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  10. Right click on the ArduinoCore project in the project explorer on the right and select Import.
  11. On the Import dialog, select General => File System and click next.
  12. On the File system dialog click browse and select the Arduino IDE’s hardware folder  “Arduino-0018\hardware\arduino\cores\arduino”
  13. Select all the files except main.cpp. Click finish
  14. Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.

Congratulations you have created the Arduino C/C++ library for Eclipse.  The library ( libArduinoCore.a ) can be found in the projects release folder. It should be around 26k

Path: \workspace\ArduinoCore\Release\libArduinoCore.a

Creating a Arduino project

Now that we have the Arduino library we can build our first Arduino project. We are going to start with the Blink tutorial from Arduino.cc its one of the simplest tutorials and should be a good building block for future projects.

You can download the project for this tutorial from our website.

  1. Create a new Eclipse C++ project, File => New => C++ project
  2. C++ Project
    • Project Name: Blinky
    • Project Type: AVR Cross Target Application => Empty project
    • Toolchains: AVR-GCC Toolchain
  3. Select Configurations, Uncheck debug we will not be using the debug version in this tutorial.
  4. Set the MCU type and frequency for your Arduino board. For example, for the Diecimila, use ATmega168 running at 16000000 Hz. For new versions of the Duemilanove, use ATmega328P running at 16000000 Hz. If you look closely at the Arduino board, you can see the target platform written on the main processor. Click Finish.
  5. In Eclipse, click Project =Properties.
  6. Select C/C++ Build and expand the category (e.g., click the diamond to the left of “C/C++ Build”).
  7. Select Settings under C/C++ Build.
  8. In the right pane, Click Additional Tools in Toolchain
    • Check Generate HEX file for flash memory
    • Check Print size
  9. In the right pane, Click AVR Compiler
    • Debugging. Set Generate Debugging Info to “No debugging info”.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  10. In the right pane, Click AVR C++ Compiler
    • Debugging. Set Generate Debugging Info to “No debugging info”.
    • Optimization. Set the Optimization Level to “Size Optimizations”.
    • Directories, Add the path to the Arduino IDE’s Hardware folder “Arduino-0018\hardware\arduino\cores\arduino”
  11. In the right pane, Click AVR C++ Linker
    • Command: avr-gcc
    • Command line pattern: ${COMMAND} –cref -s -Os ${OUTPUT_FLAG}${OUTPUT_PREFIX}${OUTPUT} ${INPUTS} -lm ${FLAGS} *NOTE — is two short dashes (- – with out the space) not a single long dash, WordPress auto converts two short dashes to a single dash
  12. In the right pane, Click AVR C++ Linker => Libraries
    • Add your Arduino library created above “ArduinoCore“, The name of the library does not include the extension or the pretext of ‘lib’.  Note: In this project I copied the library in to the base folder of the Blinky project for simplicity.
  13. Create a new source file,  File => New => Source file, Call it main.cpp
  14. Copy the contents of this file main.cpp in to main.cpp
  15. Build the project, On the mail dialog click the hammer in the too bar or by selecting Project =>Build All.

Uploading code to the Arduino

Lucky there is already a tool for this built in to AVR plugin for Eclipse called AVRDude. It should already be included with the AVR plugin for Eclipse so you shouldn’t have to download or install anything else.

  1. In Eclipse, click Project =>Properties.
  2. On the left hand side AVR => AVRDude
  3. You will need to create a new programmer configuration for your board but you only have to do this once.  Click New
  4. New programmer configuration
    • Configuration Name: Arduino Duemilanove
    • Programmer Hardware: Atmel STK500 Version 1.x Firmware
    • Override default port: //./COM14
      Note:  you will have to change this to your comport.)
    • Override default baudrate: 57600
  5. Click OK,
  6. On the AVRDude dialog select Advanced
  7. Check “Disable Device signature check” and click OK
  8. Highlight Blinky in the project explorer and click AVR => Upload to target device or the AVR icon with the green downward arrow.
  9. The results will be printed in the console.

Launching C:\Dev\WinAVR-20100110\bin\avrdude -pm16 -cstk500v1 -P//./COM14 -b57600 -F -Uflash:w:Blinky.hex:a
Output:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.05s

avrdude: Device signature = 0x1e950f
avrdude: Expected signature for ATMEGA16 is 1E 94 03
avrdude: NOTE: FLASH memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file “Blinky.hex”
avrdude: input file Blinky.hex auto detected as Intel Hex
avrdude: writing flash (1158 bytes):

Writing | ################################################## | 100% 0.61s

avrdude: 1158 bytes of flash written
avrdude: verifying flash memory against Blinky.hex:
avrdude: load data flash data from input file Blinky.hex:
avrdude: input file Blinky.hex auto detected as Intel Hex
avrdude: input file Blinky.hex contains 1158 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 0.52s

avrdude: verifying …
avrdude: 1158 bytes of flash verified

avrdude done. Thank you.

Congratulation you have uploaded your source code to the Arduino.

FAQ

Q: I keep getting this error message when I upload code with AVRDude to the Arduino.

avrdude: stk500_getsync(): not in sync: resp=0×00
avrdude done.  Thank you.

A: Disconnect and reconnect your Arduino and try again

More help

For more help with setting up Eclipse to work with Arduino see these two posts on the subject

If you have any other questions or suggestion on how to improve this article, please leave a comment.

Update:

DubiousTech:
First error I got was with the linker command line pattern. I got a compiler error “avr-gcc.exe: –cref: No such file or directory”. It looks like the “–cref” should be “–cref”. After changing that I got a different error saying “ld.exe: cannot find -lArduinoCore”. Eventually, after trying just about everything I could think of I discovered that I needed both the name of the library in the “-l” box and the file location in the “-L” box. This might very well be a simple mistake, but it’s not clear to a novice.

Steve:
You’ve got to update this post so the dash in front of “cref” is actually two dashes

Did you like this post?

Subscribe To The RSS Feed!
To catch many more articles like this in the future, make it easy on yourself and subscribe to me via RSS. You will not regret it!

Do you have a question?
We will do our best to try and solve any building automation, protocol, integration problem you may have



Pages