Go Down

Topic: Arduino WiFi server / client (Read 693 times) previous topic - next topic

chriissis

This section seems to be a better place for my problem..

I want to be able to access the arduino from ip/get_variables
and based on those variables to send a response to another page.

So it must be a client and a server.

Here's my code.
The problem seems to be the sendGET function, because the program halts (it won't respond when i access it from the browser) after it executes.

Code: [Select]

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

char ssid[] = "PinkLink";      //  your network SSID (name)
char pass[] = "unu1doi2trei3";   // your network password

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

char serverName[] = "puiucristian.com";

void setup() {
  Serial.begin(115200);      // initialize serial communication

  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    while(true);        // don't continue
  }

  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:   
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
    delay(5000);
  }
  server.begin();                           // start the web server on port 80
  //printWifiStatus();                        // you're connected now, so print out the status
  Serial.println("Ready");
}

int x = 0; // if it's 1, it will trigger the sendget function
void loop() {
  x = 0; // is 1 only if there's a get variable named .....
  client = server.available();   // listen for incoming clients
 
  if (client) {                             // if you get a client,
    Serial.println("new client");           // print a message out the serial port
    String currentLine = "";                // make a String to hold incoming data from the client
    while (client.connected()) {            // loop while the client's connected
      if (client.available()) {             // if there's bytes to read from the client,
        char c = client.read();             // read a byte, then
        Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // 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) { 
            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:   
            client.println("HTTP/1.1 200 OK");
            client.println("Content-type:text/html");
            client.println();
           
            client.println("..");

            // 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 carriage return character,
          currentLine += c;      // add it to the end of the currentLine
        }

        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /doSomething")) {
          x = 1;
        }
      }
    }
    // close the connection:
    client.stop();
    Serial.println("client disonnected");
  }
 
  //check if x is 1
  if (x == 1) sendGET();
}

void sendGET() {
  if (client.connect(serverName, 80)) {
    Serial.println("connected");
    client.println("GET /bu.php HTTP/1.0");
    client.println();
  }
  else {
    Serial.println("connection failed");
    Serial.println();
  }

  while(client.connected() && !client.available()) delay(1); //waits for data
  while (client.connected() || client.available()) { //connected or data available
    char c = client.read();
    Serial.print(c);
  }

  Serial.println();
  Serial.println("disconnecting.");
  Serial.println("==================");
  Serial.println();
  client.stop();
}

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);
}




Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy