// This sets up a Wi-Fi access point on your Arduino. // Connect to it at // - turn the LED on // - turn the LED off // - show uptime in milliseconds // Uncomment one of the following sections: // This is for Arduino Uno: Connect the Wi-Fi module on serial port 1 // #define WifiSerial Serial1 // Makes WifiSerial a synonym for Serial1 // #define DebugSerial Serial // Makes DebugSerial a synonym for Serial // This is for Galileo: Connect the Wi-Fi module on the standard serial port #define WifiSerial Serial #define DebugSerial 0 // cannot use on Galileo, so trigger an error // The LED we turn on and off int LED = 13; // Send a command to the Wi-Fi modem int issueCommand(char *command) { WifiSerial.println(command); delay(10); if (!WifiSerial.find("OK")) return 0; // Error return 1; // OK } // Read data from a client. Returns the data read and sets *id to the client id. char *read_data(int *id) { // If a client connects, the modem sends a string // +IPD,,[,,]: // Wait for connection from a client if (!WifiSerial.findUntil("+IPD,", "\r")) return NULL; // read ID *id = WifiSerial.parseInt(); if (!WifiSerial.findUntil(",", "\r")) return NULL; // read length int len = WifiSerial.parseInt(); // ignore until colon if (!WifiSerial.findUntil(":", "\r")) return NULL; // allocate data char *data = (char *)malloc(len + 1); if (data == NULL) return NULL; // Fill it WifiSerial.readBytes(data, len); // And we're done data[len] = '\0'; return data; } // Send data to the client id void send_data(char *data, int id) { // To send data, use "AT+CIPSEND=,\r\n", // followed by data int len = strlen(data); WifiSerial.print("AT+CIPSEND="); WifiSerial.print(id); WifiSerial.print(","); WifiSerial.println(len); delay(20); WifiSerial.write(data, len); delay(100); // DebugSerial.write(data, len); } // Send a HTML document to client ID void send_html(char *data, int id) { // We always send the same page char output[2048]; sprintf(output, "HTTP/1.1 200 OK\r\n\ Content-Type: text/html\r\n\ Content-Length: %d\r\n\r\n%s", strlen(data), data); send_data(output, id); } void led_on() { digitalWrite(LED, HIGH); } void led_off() { digitalWrite(LED, LOW); } // Check whether S starts with the substring T int starts_with(char *s, char *t) { return strncmp(s, t, strlen(t)) == 0; } // Handle a request from client ID void process_data(char *data, int id) { // We ignore all requests except for GET if (!starts_with(data, "GET")) return; // This is where extra processing of data // may take place if (starts_with(data, "GET /on")) { led_on(); send_html("

The LED is ON

Turn LED off", id); return; } if (starts_with(data, "GET /off")) { led_off(); send_html("

The LED is OFF

Turn LED on", id); return; } if (starts_with(data, "GET /time")) { char out[2048]; sprintf(out, "

Current time: %ld

", millis()); send_html(out, id); } // All other requests send_html("

Hello, world

Turn LED on", id); } void setup() { // put your setup code here, to run once: // Set up Wi-Fi serial connection WifiSerial.begin(115200); WifiSerial.setTimeout(5000); // Set up Debugging serial connection // DebugSerial.begin(9600); // DebugSerial.println("Starting"); // Set up LED pinMode(LED, OUTPUT); // Initialize Wi-Fi modem. // Access Point mode if (!issueCommand("AT+CWMODE=2")) return; // SSID, password, channel, and authentication if (!issueCommand("AT+CWSAP=\"PFE\",\"\",1,0")) return; // Allow multiple users to connect if (!issueCommand("AT+CIPMUX=1")) return; // Run a server on port 80 if (!issueCommand("AT+CIPSERVER=1,80")) return; // DebugSerial.println("Success"); } void loop() { // put your main code here, to run repeatedly: int id; char *data = read_data(&id); if (data == NULL) return; // DebugSerial.print(data); process_data(data, id); free(data); }