Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't mix Client / Server #36

Open
RobinWoo opened this issue Sep 20, 2013 · 1 comment
Open

Can't mix Client / Server #36

RobinWoo opened this issue Sep 20, 2013 · 1 comment

Comments

@RobinWoo
Copy link

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 printWifiStatus() {
    // print the SSID of the network you're attached to:
    Serial.print("SSID: ");
    Serial.println(WiFi.SSID());
    // print your WiFi shield's IP address:
    IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
    Serial.println(ip);
    // print the received signal strength:
    long rssi = WiFi.RSSI();
    Serial.print("signal strength (RSSI):");
    Serial.print(rssi);
    Serial.println(" dBm");
}

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.

@salanki
Copy link

salanki commented Sep 23, 2013

Seems the same as (unresolved) #12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants