creating an arduino web server from scratch hardware and software

19
Creating an Arduino Web Server From scratch hardware and software

Upload: justin-mclean

Post on 28-Jan-2015

109 views

Category:

Technology


1 download

DESCRIPTION

A short presentation of how to turn an Arduino (open source physical computing platform) into a web server in around 20 lines of code.

TRANSCRIPT

Page 1: Creating an Arduino Web Server from scratch hardware and software

Creating an Arduino Web Server

From scratch hardware and software

Page 2: Creating an Arduino Web Server from scratch hardware and software

Web Servers

Simpler than you think

A web server

Listens for connections

Parses requests

Send back status messages and resource requested

Page 3: Creating an Arduino Web Server from scratch hardware and software

Arduino Overview

Open source physical computing platform

Multiple form factors

Easy to code (C/C++ like) and use (USB)

Low cost

Great for prototypes, wearable computers, sensor networks etc

Page 4: Creating an Arduino Web Server from scratch hardware and software

Arduino Hardware

ATMega 8 bit microcontroller

32K program memory, 2K SRAM, 1K EEPROM

Dual purpose digital inputs and outputs

Analog inputs

High performance (16 MIPS)

Low power (3.3V or 5V)

Page 5: Creating an Arduino Web Server from scratch hardware and software

Arduino IDE/Software

Open source

Cross platform (Mac, PC and Linux)

Alpha but stable

Simple to use

Includes documentation, help, samples and library for common tasks

Page 6: Creating an Arduino Web Server from scratch hardware and software

Arduino Shields

Hardware can be easily extended

Shields plug in on top of standard boards

Build your own or buy off the shelf (lots available)

Stackable, can use more than one

Page 7: Creating an Arduino Web Server from scratch hardware and software

Ethernet Shields

Several shields available

Two main types DHCP and non DHCP

Some configuration required

Non DHCP you set IP and MAC address in code

Page 8: Creating an Arduino Web Server from scratch hardware and software

IP and MAC address

Set IP address and MAC address in your code

Need to be careful with duplicates

Set up like so:Ethernet.begin(mac, ip);

Page 9: Creating an Arduino Web Server from scratch hardware and software

Ethernet Library

Standard ethernet library

Includes Client and Server classes

Create server like so:Server server(80);server.begin();

Bare bones web server about 20 lines of code and 5K compiled

Page 10: Creating an Arduino Web Server from scratch hardware and software

HTTP Protocol

HyperText Transfer Protocol

Used by web servers to transfer web pages to be displayed in your web browser

Connection (usually) on port 80

Page 11: Creating an Arduino Web Server from scratch hardware and software

TCP Connections

TCP three way connection handshake

Client sends SYN with random number (A)

Server replies with SYN-ACK containing A+1 and random number (B)

Client replies with ACK containing B+1

Luckily ethernet library does this for you!

Page 12: Creating an Arduino Web Server from scratch hardware and software

Connection Code

Create a clientClient client = server.available();

Connectedwhile (client.connected(){....}

Page 13: Creating an Arduino Web Server from scratch hardware and software

HTTP Requests

Start with request “GET index.html HTTP/1.1”

Optional headers “Accept-Language: en”

Empty line

Optional message body (POST and other requests)

Page 14: Creating an Arduino Web Server from scratch hardware and software

HTTP Request Hack

Standard GET request have request followed by blank line

So just ignore what is requested and check for blank line

Page 15: Creating an Arduino Web Server from scratch hardware and software

Request Code

Read characterschar c = client.read();

If newline and last character was newlineif (c == '\n' && blankline) { ...}if (c == '\n') { blankline = true;}

Page 16: Creating an Arduino Web Server from scratch hardware and software

HTTP Response

Send back status line "HTTP/1.1 200 OK"

Send back content type "Content-Type: text/html"

Send blank line

Send (HTML) content

Page 17: Creating an Arduino Web Server from scratch hardware and software

Response Code

Send status and content typeclient.println("HTTP/1.1 200 OK"); client.println("Content-Type: text/html"); client.println();

Send contentfor (int i =0; i < nolines; i++) { client.println(content[i]);}

Page 18: Creating an Arduino Web Server from scratch hardware and software

Close Connections

Clear up after youclient.flush();client.stop();

Page 19: Creating an Arduino Web Server from scratch hardware and software

Arduiono Web Server Demo

Aruino IDE

Web browser