// sketch_12_04_server_wifi #include #include char ssid[] = "Linda-and-Simon"; // your network SSID (name) char pass[] = "myroomistidy"; // your network password WiFiServer server(80); WiFiClient client; const int numPins = 5; int pins[] = {3, 4, 5, 6, 7}; int pinState[] = {0, 0, 0, 0, 0}; char line1[100]; void setup() { Serial.begin(9600); while (!Serial){}; // Leonardo needs this for (int i = 0; i < numPins; i++) { pinMode(pins[i], OUTPUT); } if (WiFi.begin(ssid, pass)) { Serial.print(F("Point your Browser at: http://")); Serial.println(WiFi.localIP()); delay(3000); } else { Serial.println(F("Could not connect to network")); } server.begin(); Serial.println("started the server"); } void loop() { client = server.available(); if (client && client.connected()) { readRequest(); sendResponse(); } } void readRequest() { waitForClient(); readHeader(); Serial.print("Line1="); Serial.println(line1); if (! pageNameIs("/")) { Serial.println("Wrong Page"); client.stop(); return; } Serial.println("Right Page"); } void sendResponse() { client = server.available(); if (client && client.connected()) { sendHeader(); Serial.println("Sent Header"); sendBody(); Serial.println("Sent Body"); delay(1); client.stop(); } } void waitForClient() { while (! client.available()) {}; } void sendHeader() { client.println(F("HTTP/1.1 200 OK")); client.println(F("Content-Type: text/html")); client.println(); } void sendBody() { client.println(F("")); sendAnalogReadings(); client.println(F("

Output Pins

")); client.println(F("
")); // setValuesFromParams(); // setPinStates(); // sendHTMLforPins(); client.println(F("")); client.println(F("
")); client.println(F("")); } void sendAnalogReadings() { client.println(F("

Analog Inputs

")); client.println(F("")); for (int i = 0; i < 5; i++) { int reading = analogRead(i); client.print(F("")); } client.println("
A")); client.print(i); client.print(F("")); client.print((float) reading / 205.0); client.println(F(" V
"); } void sendHTMLforPins() { for (int i = 0; i < numPins; i++) { client.print(F("

Pin ")); client.print(pins[i]); client.print(F("

")); } } void setPinStates() { for (int i = 0; i < numPins; i++) { digitalWrite(pins[i], pinState[i]); } } void setValuesFromParams() { for (int i = 0; i < numPins; i++) { pinState[i] = valueOfParam(i + '0'); } } void readHeader() { readRequestLine(line1); char buffer[100]; while (readRequestLine(buffer) > 1 && buffer[0] != '\n') { Serial.println("read another line"); } Serial.println("Finished Reading Request"); } int readRequestLine(char *line) { char ch; int i = 0; while (ch != '\n' && i < 100 && client.available()) { if (client.available()) { ch = client.read(); line[i] = ch; i ++; } } line[i] = '\0'; Serial.print("Header Line:"); Serial.print(line); Serial.print(" len="); Serial.print(i); Serial.print(" ch0="); Serial.println((int)line[0]); return i; } boolean pageNameIs(char* name) { // page name starts at char pos 4 // ends with space int i = 4; char ch = line1[i]; while (ch != ' ' && ch != '\n' && ch != '?') { if (name[i-4] != line1[i]) { return false; } i++; ch = line1[i]; } return true; } int valueOfParam(char param) { for (int i = 0; i < strlen(line1); i++) { if (line1[i] == param && line1[i+1] == '=') { return (line1[i+2] - '0'); } } return 0; }