You are on page 1of 28

Webduino

Putting a web server in your embedded widget a NYC Resistor Workshop November 22nd, 2009 Ben Combee http://webduino.googlecode.com

Class Materials
Files on the Flash Drive: Copy of presentation in PDF format Webduino source and example files Wiznet 5100 data sheet HTTP 1.1 specification Arduino Ethernet Library Reference Arduino IDE 0017 for Win/Mac/Linux

What We're Covering


Understanding Your Home Network What's a Web Server? Why do web addresses start with HTTP? How do I modify the sample code?

The Layers of the Network


Hello, World (your application) HTTP (the web's protocol) TCP (Transmission Control Protocol) IP (Internet Protocol) Ethernet (what goes on the wire) Cables and Connectors

Cables and Connectors


Cables are usually Cat5 or Cat6 depending on the network speed Connectors have 8-pins and are called RJ-45 Transmit and Receive occur over matched pairs of wires

Ethernet
Hardware connection using a shared media Each device has an unique 48-bit MAC number Data is formed into frames that contain the source and destination MAC twisted-pair networks have each device directly connected to another device or to a hub/switch supports broadcast mode where every computer on network gets your frame Frames have a simple CRC check to detect corruption

IP (Internet Protocol)
Layer on top of Ethernet Computers are identified with a 4-byte IP address Example: 192.168.42.103 ARP is used to map Ethernet MACs to IP addresses Local network specified using a subnet mask Packets for foreign nets are sent to a gateway TCP is a protocol for making a series of raw packets look like a serial connection with error correction A computer supports multiple connections by mapping each one to a port number 127.0.0.1 is reserved as loopback

IP Addresses
Computers are identified with a 4-byte IP address Example: 192.168.42.103 127.0.0.1 is reserved as loopback 10.x.x.x and 192.168.x.x are private address ranges NAT (Network Address Translation) is used to make private networks talk to the public Internet Home routers usually provide a 192.168.x.x network to your computers all bound into one public Internet IP address

TCP (Transmission Control Protocol)


A way to make a series of raw packets look like an error-free serial connection Supports multiple applications by adding the idea of "ports" Traditionally, port 80 is used for web servers Other useful ports 22 - SSH 23 - telnet 25 - SMTP (sending mail) more at http://www.iana.org/assignments/port-numbers

Special TCP/IP Addresses


127.0.0.1 is loopback -- use it to connect to yourself 10.x.x.x, 174.16.x.x, 192.168.x.x are for private networks 169.254.x.x is for local assignment 255.255.255.255 is for local broadcast

Assigning a Network Address


Static Assignment Pick an address on the local network Manually configure gateway, DNS settings DHCP let a server assign you an address this lease could change when you reconnect

Domain Name Service


Turns "www.example.com" into 192.168.72.32 Names are in a hierarchy Roots are .com, .edu, .org. .net, .us, etc Secondary servers handle subdomains like mit.edu, nycresistor.com, nasa.gov A records map a name to an IP address Multiple names can map to one IP

How do web servers work?


Client sends a request and a set of headers GET <shorturl> HTTP/1.1 Host: example.com <other-headers> <blankline> or POST <shorturl> HTTP/1.1 Host: example.com <other-headers> <blankline> <url-encoded-content>

How do web servers work?


Server sends back data with status and header at start 200 OK Content-Type: text/html <other-headers> <blank-line> <HTML web page> Can also send a redirect to another page or an error message

Arduino Ethernet Shield


http://arduino.cc/en/Main/ArduinoEthernetShield $40 to $45 for preassembled board Based on the Wiznet 5100 Ethernet chip Uses SPI as the communications channel Support library ships with Arduino IDE Our class will be using this hardware

Adafruit Ethernet Shield


http://ladyada. net/make/eshield/index.html $15 for shield kit, $20-$50 for module Supports connecting to etiher a $20 Wiznet module or a $30-$50 XPort module, depending on what sockets are installed If used with Wiznet module, code compatible with Arduino Ethernet board If used with XPort Direct, communication uses soft serial library.

NKC Ethernet Shield


http://www.nkcelectronics.com/nkc-ethernet-shield-forarduino-mega--duemilanove--diecimila-diy-kit.html $32 for shield including Wiznet module Comes as a solder-it-yourself kit Code compatible with Arduino Ethernet board Easily modified for Arduino Mega compatibility

Seeed Studio Ethernet Adapter


http://tinyurl.com/seeedethernet Cost: $22 to $28, preassembled Older design, uses the Microchip ENC28J60 chip Programming interface is SPI, but you have to implement your own TCP/IP stack. Useful for some local network applications, but needs a lot of extra code to be able to handle incoming TCP/IP connections

Wiznet: Specifying the MAC address


MAC is a unique 48-bit identifier for every Ethernet device. Blocks are assigned by the IEEE to network device manufacturers. In theory, you need to register for a private address block to give MACs to your Arduino. In practice, just grab MAC address for your laptop's ethernet port and add one to the last byte. In Arduino code, you'd write
uint8_t mac[6] = { 0x00, 0x22, 0x15, 0xFC, 0x38, 0x81 };

Don't reuse the MAC of another device on your local network!

Wiznet: Specifying the IP Address


IP address is a four-byte number that identifies a host on the internet. It's usually written as a "dotted quad", like 127.0.0.1. Home networks usually have a router that makes a private network at 192.168.0.x or 192.168.1.x. Pick a number that's in your home network range, but outside the range that gets automatically assigned to computers via DHCP. Example Arduino code:
uint8_t addr[4] = { 192, 168, 42, 51 };

Wiznet: DHCP
DHCP library available now for the Wiznet chip. http://blog.jordanterrell.com/post/Arduino-DHCP-LibraryVersion-04.aspx Requires a bit of RAM, so best used with Arduino boards using the ATMega328 or ATMega1280 chips Better for clients, since you need to know the IP address of your server from other computers

Saying "Hello, World"


Goal: have the device send a web page that looks like <html> <head><title>Hello, World</title></head> <body><h1>Hello, World!</h1></body> </html> to a GET query for "/" or "/index.html"

Outputting HTML from Code


server.print("<p>Hello!</p>"); simplest way, text lives in RAM (1K on 168!) P(message) = "<h1>Webduino</h1>"; server.printP(message); Great for longer text items, text lives in Flash (16K on 168) server << "You have " << n << " bananas"; Use code from http://arduiniana.org/libraries/streaming Simpler way to write a bunch of server.print statements Great for complex messages

Adding sensor data to the webpage


Read a light sensor or temp sensor connected to an input pin before you start outputting the page, save in a variable. Add the value to the HTML output server.print("<p>The temperature is "); server.print(temp * 100); server.print("F.</p>"); or server << "<p>The temperature is " << (temp * 100) << "F.</p>";

Making an Interactive Form


Start by making form in your editor, then move into code Output form controls Accept the HTTP POST with the form control data Version 1.2 of library also allows GET requests

Submitting a Form from Command Line


Get data curl http://myarduino/raw curl -d "post data" http://myarduino/raw curl is a command-line tool available from http://curl.haxx.se/ Runs on Linux, Mac OS X, Windows, acts like a little web browser and returns the output to your console or script

Using AJAX
Embed Javascript code in your page to POST data back to your form Example: AJAX Buzzer You can load scripts and images from well-known locations on net, rather than from Flash memory on device

Security and Multiple Users


Server on the Arduino can only talk to one client at a time If multiple people try to connect, one will wait for the connection to be created until the first dies. No CPU power to run encrypted HTTP with SSL Can embed password in URL or in POST data Alas, very snoopable by someone on network Use VPN to control access to your network Limit accessibility to computers outside your local LAN

You might also like