]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_12_02_server/sketch_12_02_server.ino
first commit
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_12_02_server / sketch_12_02_server.ino
1 // sketch_12_02_server
2
3 #include <SPI.h>
4 #include <Ethernet.h>
5
6 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
7 EthernetServer server(80);
8 EthernetClient client;
9
10 const int numPins = 5;
11 int pins[] = {3, 4, 5, 6, 7};
12 int pinState[] = {0, 0, 0, 0, 0};
13 char line1[100];
14 char buffer[100];
15
16 void setup()
17 {
18   Serial.begin(9600);
19   while (!Serial){};  // Leonardo needs this
20   for (int i = 0; i < numPins; i++)
21   {
22      pinMode(pins[i], OUTPUT);
23   }
24   if (Ethernet.begin(mac)) 
25   {
26     Serial.print(F("Point your Browser at: http://"));
27     Serial.println(Ethernet.localIP());
28   }
29   else
30   {
31     Serial.println(F("Could not connect to network"));
32   }
33   server.begin();
34 }
35
36 void loop()
37 {
38   client = server.available();
39   if (client) 
40   {
41     if (client.connected()) 
42     {
43       readHeader();
44       if (! pageNameIs("/"))
45       {
46         client.stop();  
47         return;
48       }
49       client.println(F("HTTP/1.1 200 OK"));
50       client.println(F("Content-Type: text/html"));
51       client.println();
52
53       sendBody();
54       client.stop();            
55     }
56   }
57 }
58
59 void sendBody()
60 {
61   client.println(F("<html><body>"));
62   sendAnalogReadings();
63   client.println(F("<h1>Output Pins</h1>"));
64   client.println(F("<form method='GET'>"));  
65   setValuesFromParams();
66   setPinStates();
67   sendHTMLforPins();
68   client.println(F("<input type='submit' value='Update'/>"));
69   client.println(F("</form>"));
70   client.println(F("</body></html>"));
71 }
72
73 void sendAnalogReadings()
74 {
75   client.println(F("<h1>Analog Inputs</h1>"));
76   client.println(F("<table border='1'>"));
77   for (int i = 0; i < 5; i++)
78   {
79     int reading = analogRead(i);
80     client.print(F("<tr><td>A")); client.print(i); 
81     client.print(F("</td><td>")); client.print((float) reading / 205.0);
82     client.println(F(" V</td></tr>"));
83   }
84   client.println("</table>");
85 }
86
87 void sendHTMLforPins()
88 {
89   for (int i = 0; i < numPins; i++)
90   {
91     client.print(F("<p>Pin "));
92     client.print(pins[i]);  
93     client.print(F("<select name='"));
94     client.print(i);
95     client.println(F("'>"));
96     client.print(F("<option value='0'"));
97     if (pinState[i] == 0)
98     {
99       client.print(F(" selected"));
100     }
101     client.println(F(">Off</option>"));
102     client.print(F("<option value='1'"));
103     if (pinState[i] == 1)
104     {
105       client.print(F(" selected"));
106     }
107     client.println(F(">On</option>"));
108     client.println(F("</select></p>"));  
109   }
110 }
111
112 void setPinStates()
113 {
114   for (int i = 0; i < numPins; i++)
115   {
116      digitalWrite(pins[i], pinState[i]);
117   }
118 }
119
120 void setValuesFromParams()
121 {
122   for (int i = 0; i < numPins; i++)
123   {
124     pinState[i] = valueOfParam(i + '0');
125   }
126 }
127
128 void readHeader()
129 {
130   readRequestLine(line1);
131   while (readRequestLine(buffer) > 1 && buffer[0] != '\n') {}
132 }
133
134 int readRequestLine(char *line)
135 {
136   char ch;
137   int i = 0;
138   while (ch != '\n' && i < 100 && client.available())
139   {
140     if (client.available())
141     {
142       ch = client.read();
143       line[i] = ch;
144       i ++;
145     }
146   }
147   line[i] = '\0'; 
148   return i;
149 }
150
151 boolean pageNameIs(char* name)
152 {
153    // page name starts at char pos 4
154    // ends with space
155    int i = 4;
156    char ch = line1[i];
157    while (ch != ' ' && ch != '\n' && ch != '?')
158    {
159      if (name[i-4] != line1[i])
160      {
161        return false;
162      }
163      i++;
164      ch = line1[i];
165    }
166    return true;
167 }
168
169 int valueOfParam(char param)
170 {
171   for (int i = 0; i < strlen(line1); i++)
172   {
173     if (line1[i] == param && line1[i+1] == '=')
174     {
175       return (line1[i+2] - '0');
176     }
177   }
178   return 0;
179 }
180
181