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