]> git.piffa.net Git - sketchbook_andrea/blob - hardware/ethernet/ethernet_analog_read/ethernet_analog_read.ino
Initial Commit
[sketchbook_andrea] / hardware / ethernet / ethernet_analog_read / ethernet_analog_read.ino
1 /*
2   Analog read
3   
4   Read a value from a Arduino sensor and publish
5   it in a web page.
6   
7   Note: you can set up the NIC with DHCP or static address
8   
9   */
10
11 #include <EtherCard.h>
12
13 // ethernet interface mac address, must be unique on the LAN
14 static byte mymac[] = { 
15   0x74,0x69,0x69,0x2D,0x30,0x31 };
16 //static byte myip[] = { 192,168,0,100 };
17
18 byte Ethernet::buffer[500];
19 BufferFiller bfill;
20
21 // Analog vars:
22 int analogSensor = A0;
23 int sensorRead = 0;
24
25 void setup () {
26   /* Static IP code
27   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
28    Serial.println( "Failed to access Ethernet controller");
29    ether.staticSetup(myip);
30    
31    
32    
33    */
34    // DHC code 
35   Serial.begin(57600);
36   Serial.println("\n[getDHCPandDNS]");
37   if (ether.begin(sizeof Ethernet::buffer, mymac) == 0) 
38     Serial.println( "Failed to access Ethernet controller");
39
40   if (!ether.dhcpSetup())
41     Serial.println("DHCP failed");
42 }
43
44 static word homePage() {
45   bfill = ether.tcpOffset();
46   bfill.emit_p(PSTR(
47   "HTTP/1.0 200 OK\r\n"
48     "Content-Type: text/html\r\n"
49     "Pragma: no-cache\r\n"
50     "\r\n"
51     "<meta http-equiv='refresh' content='1'/>"
52     "<title>Arduino WebServer</title>"
53     "<h1>Arduino analog Read</h1>"
54     "<p>Analog Value from Arduino: $D</p>"),
55     sensorRead );
56   return bfill.position();
57 }
58
59 void loop () {
60   sensorRead = analogRead(analogSensor);
61
62   word len = ether.packetReceive();
63   word pos = ether.packetLoop(len);
64
65   if (pos)  // check if valid tcp data is received
66     ether.httpServerReply(homePage()); // send web page data
67 }
68
69