arduino-libraries / WiFi

WiFi Library for Arduino
http://arduino.cc/
115 stars 58 forks source link

Can't mix Wifi Client / Server #28

Open agdl opened 8 years ago

agdl commented 8 years ago

From @RobinWoo on September 20, 2013 8:33

Hello all,

It seems not possible to mix client and server with the WifiShield library / Firmware. Please look at the following code. The Wifi is used as server and check for "GET /alm" request. In this case, the Wifi is used as client to send a Push alert if the time between 2 notifications was passes.

#include <SPI.h>
#include <WiFi.h>
#include <FlexiTimer2.h>
#include <WString.h>

#define NETWORK_SSID "****"     //  network SSID (name) 
#define NETWORK_PASSWD "***"  // network password
#define KEY_INDEX 0;        // network key Index number (needed only for WEP)
#define LED 13
#define CYCLE_NB_BETWEEN_NOTIFY 12000;

int status = WL_IDLE_STATUS;
WiFiServer server(80);
WiFiClient alertClient;

void setup() {
    Serial.begin(9600);
    pinMode(LED, OUTPUT);  

    // check for the presence of the shield:
    if (WiFi.status() == WL_NO_SHIELD) {
        Serial.println("WiFi shield not present");
        while(true) {
            digitalWrite(LED, HIGH);
            delay(125);
            digitalWrite(LED, LOW);
            delay(125);
        }
    }

    // Set the IP address for the WIFI
    //WiFi.config(serverIp);
    // attempt to connect to Wifi network:
    do {
        Serial.print("Attempting to connect to SSID: ");
        Serial.println(NETWORK_SSID);
        status = WiFi.begin(NETWORK_SSID, NETWORK_PASSWD);
    } while ( status != WL_CONNECTED);

    // Start server
    server.begin();

    // you're connected now, so print out the status:
    printWifiStatus();

    // Set Timer 2 interrupt
    FlexiTimer2::set(5, interruptTimer2);
    FlexiTimer2::start(); // active
}

void loop() {
    // Client connected
    WiFiClient client = server.available();
    if (client) {
        String response = "";
        String currentLine = ""; // make a String to hold incoming data from the client
        Serial.println("new client");
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                if (c == '\n') { // if the byte is a newline character
                    // Check GET request 
                    int ind1 = currentLine.indexOf("GET /");
                    if(ind1 >= 0) {
                        String cmd = currentLine.substring(ind1+5, ind1+8);
                        if (cmd == "alm") {
                            alertAvailable=true;
                        }
                    }
                    // if the current line is blank, you got two newline characters in a row.
                    // that's the end of the client HTTP request, so send a response:
                    if (currentLine.length() == 0) {
                        Serial.println("Send response to client.");
                        client.println("HTTP/1.1 200 OK");
                        client.println("Content-type:text/html");
                        client.println();
                        // the content of the HTTP response follows the header:
                        if (response != "") {
                            client.println(response);
                        }
                        // The HTTP response ends with another blank line:
                        client.println();
                        // break out of the while loop:
                        break;
                    } else { // if you got a newline, then clear currentLine:
                        currentLine = "";
                    }
                } else if (c != '\r') { // if you got anything else but a cr character,
                    currentLine += c; // add it to the end of the currentLine
                }
            }
        }
        //stopping client
        client.stop();
        Serial.println("Client disonnected");
    }

    // Send alert ?
    if ((alertAvailable==true) && (timerAlert == 0)) {
        sendAlert("ALERT+TEST");
    }
}

void interruptTimer2() {
    if (timerAlert > 0) {
        timerAlert --;
    }
}

void sendAlert(String message) {
    if (alertClient.connect(alertServerIp, 80)) {
        Serial.println("connected to ALERT server");
        Serial.println("Send Alert for "+message);
        alertClient.println("GET /push/push.php?call=*******&message=+"+message+" HTTP/1.1");
        alertClient.println("Host:pautex.fr");
        alertClient.println("Connection: close");
        alertClient.println();
        alertClient.flush();
        alertClient.stop();
        Serial.println("End Alert for "+message);
        alertClient.flush();
    }
    alertAvailable = false;
    timerAlert = CYCLE_NB_BETWEEN_NOTIFY;
}

The server work correctly and send response for any request from my browser until I send the request ip-adress/alm. In this case, the alert is sent (client work correctly). But the server seems to be stopped: The "server.available()" return 0.

Copied from original issue: arduino/Arduino#1582