]> git.piffa.net Git - arduino/blob - books/ArduinoNextSteps-master/ArduinoNextSteps/sketch_12_04_server_wifi_experimental/sketch_12_04_server_wifi_experimental.ino
first commit
[arduino] / books / ArduinoNextSteps-master / ArduinoNextSteps / sketch_12_04_server_wifi_experimental / sketch_12_04_server_wifi_experimental.ino
1 // sketch_12_04_server_wifi
2
3 #include <SPI.h>
4 #include <WiFi.h>
5
6 char ssid[] = "Linda-and-Simon";      // your network SSID (name) 
7 char pass[] = "myroomistidy";   // your network password
8
9 WiFiServer server(80);
10 WiFiClient client;
11
12 const int numPins = 5;
13 int pins[] = {3, 4, 5, 6, 7};
14 int pinState[] = {0, 0, 0, 0, 0};
15 char line1[100];
16
17 void setup()
18 {
19   Serial.begin(9600);
20   while (!Serial){};  // Leonardo needs this
21   for (int i = 0; i < numPins; i++)
22   {
23      pinMode(pins[i], OUTPUT);
24   }
25   if (WiFi.begin(ssid, pass)) 
26   {
27     Serial.print(F("Point your Browser at: http://"));
28     Serial.println(WiFi.localIP());
29     delay(3000);
30   }
31   else
32   {
33     Serial.println(F("Could not connect to network"));
34   }
35   server.begin();
36   Serial.println("started the server");
37 }
38
39 void loop()
40 {
41   client = server.available();
42   if (client && client.connected()) 
43   {
44     readRequest();
45     sendResponse();
46   }
47 }
48
49 void readRequest()
50 {
51   waitForClient();
52   readHeader();
53   Serial.print("Line1=");
54   Serial.println(line1);
55   
56   if (! pageNameIs("/"))
57   {
58     Serial.println("Wrong Page");
59     client.stop();  
60     return;
61   }
62   Serial.println("Right Page");
63 }
64
65 void sendResponse()
66 {
67   client = server.available();
68   if (client && client.connected()) 
69   {
70     sendHeader();
71     Serial.println("Sent Header");
72     sendBody();
73     Serial.println("Sent Body");
74     delay(1);
75     client.stop();        
76   }    
77 }
78
79 void waitForClient()
80 {
81   while (! client.available()) {};
82 }
83
84
85 void sendHeader()
86 {
87   client.println(F("HTTP/1.1 200 OK"));
88   client.println(F("Content-Type: text/html"));
89   client.println(); 
90 }
91
92 void sendBody()
93 {
94   client.println(F("<html><body>"));
95   sendAnalogReadings();
96   client.println(F("<h1>Output Pins</h1>"));
97   client.println(F("<form method='GET'>"));  
98 //  setValuesFromParams();
99 //  setPinStates();
100 //  sendHTMLforPins();
101   client.println(F("<input type='submit' value='Update'/>"));
102   client.println(F("</form>"));
103   client.println(F("</body></html>"));
104 }
105
106 void sendAnalogReadings()
107 {
108   client.println(F("<h1>Analog Inputs</h1>"));
109   client.println(F("<table border='1'>"));
110   for (int i = 0; i < 5; i++)
111   {
112     int reading = analogRead(i);
113     client.print(F("<tr><td>A")); client.print(i); 
114     client.print(F("</td><td>")); client.print((float) reading / 205.0);
115     client.println(F(" V</td></tr>"));
116   }
117   client.println("</table>");
118 }
119
120 void sendHTMLforPins()
121 {
122   for (int i = 0; i < numPins; i++)
123   {
124     client.print(F("<p>Pin "));
125     client.print(pins[i]);  
126     client.print(F("<select name='"));
127     client.print(i);
128     client.println(F("'>"));
129     client.print(F("<option value='0'"));
130     if (pinState[i] == 0)
131     {
132       client.print(F(" selected"));
133     }
134     client.println(F(">Off</option>"));
135     client.print(F("<option value='1'"));
136     if (pinState[i] == 1)
137     {
138       client.print(F(" selected"));
139     }
140     client.println(F(">On</option>"));
141     client.println(F("</select></p>"));  
142   }
143 }
144
145 void setPinStates()
146 {
147   for (int i = 0; i < numPins; i++)
148   {
149      digitalWrite(pins[i], pinState[i]);
150   }
151 }
152
153 void setValuesFromParams()
154 {
155   for (int i = 0; i < numPins; i++)
156   {
157     pinState[i] = valueOfParam(i + '0');
158   }
159 }
160
161 void readHeader()
162 {
163   readRequestLine(line1);
164   char buffer[100];
165   while (readRequestLine(buffer) > 1 && buffer[0] != '\n')
166   {
167     Serial.println("read another line");
168   }
169   Serial.println("Finished Reading Request");
170 }
171
172 int readRequestLine(char *line)
173 {
174   char ch;
175   int i = 0;
176   while (ch != '\n' && i < 100 && client.available())
177   {
178     if (client.available())
179     {
180       ch = client.read();
181       line[i] = ch;
182       i ++;
183     }
184   }
185   line[i] = '\0'; 
186   Serial.print("Header Line:");
187   Serial.print(line);
188   Serial.print(" len=");
189   Serial.print(i);
190   Serial.print(" ch0=");
191   Serial.println((int)line[0]);
192   return i;
193 }
194
195
196 boolean pageNameIs(char* name)
197 {
198    // page name starts at char pos 4
199    // ends with space
200    int i = 4;
201    char ch = line1[i];
202    while (ch != ' ' && ch != '\n' && ch != '?')
203    {
204      if (name[i-4] != line1[i])
205      {
206        return false;
207      }
208      i++;
209      ch = line1[i];
210    }
211    return true;
212 }
213
214 int valueOfParam(char param)
215 {
216   for (int i = 0; i < strlen(line1); i++)
217   {
218     if (line1[i] == param && line1[i+1] == '=')
219     {
220       return (line1[i+2] - '0');
221     }
222   }
223   return 0;
224 }
225
226