webduino class
DESCRIPTION
Webduino Class, ArdunioTRANSCRIPT
![Page 1: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/1.jpg)
WebduinoPutting a web server in your
embedded widget
a NYC Resistor Workshop November 22nd, 2009
Ben Combee
http://webduino.googlecode.com
![Page 2: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/2.jpg)
Class Materials
Files on the Flash Drive: Copy of presentation in PDF formatWebduino source and example filesWiznet 5100 data sheetHTTP 1.1 specificationArduino Ethernet Library ReferenceArduino IDE 0017 for Win/Mac/Linux
![Page 3: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/3.jpg)
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?
![Page 4: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/4.jpg)
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
![Page 5: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/5.jpg)
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
![Page 6: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/6.jpg)
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
![Page 7: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/7.jpg)
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
![Page 8: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/8.jpg)
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
![Page 9: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/9.jpg)
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
![Page 10: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/10.jpg)
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
![Page 11: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/11.jpg)
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
![Page 12: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/12.jpg)
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
![Page 13: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/13.jpg)
How do web servers work?
Client sends a request and a set of headers GET <shorturl> HTTP/1.1Host: example.com<other-headers> <blankline>
or
POST <shorturl> HTTP/1.1Host: example.com<other-headers><blankline><url-encoded-content>
![Page 14: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/14.jpg)
How do web servers work?
Server sends back data with status and header at start 200 OKContent-Type: text/html<other-headers><blank-line><HTML web page>
Can also send a redirect to another page or an error message
![Page 15: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/15.jpg)
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
![Page 16: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/16.jpg)
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.
![Page 17: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/17.jpg)
NKC Ethernet Shield
● http://www.nkcelectronics.com/nkc-ethernet-shield-for-arduino-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
![Page 18: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/18.jpg)
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
![Page 19: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/19.jpg)
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!
![Page 20: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/20.jpg)
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 };
![Page 21: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/21.jpg)
Wiznet: DHCP
● DHCP library available now for the Wiznet chip.● http://blog.jordanterrell.com/post/Arduino-DHCP-Library-
Version-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
![Page 22: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/22.jpg)
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"
![Page 23: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/23.jpg)
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
![Page 24: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/24.jpg)
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>";
![Page 25: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/25.jpg)
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
![Page 26: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/26.jpg)
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
![Page 27: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/27.jpg)
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
![Page 28: Webduino Class](https://reader034.vdocuments.us/reader034/viewer/2022051418/55cf9913550346d0339b6c68/html5/thumbnails/28.jpg)
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 dataAlas, very snoopable by someone on network
Use VPN to control access to your networkLimit accessibility to computers outside your local LAN