HTTP Get Request with response

Hello!

I have this code:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0x90,0xA2,0xDA,0x0D,0xDE,0xBF};
char server[] = "www.domain.hu";
IPAddress ip(xxx,xxx,x,xxx);

int z=posts('email',2);

char posts(char typ2){
  EthernetClient client;
  String data;
  data="";
  switch (typ2){
    case 'email':
      data+="emailIt=2";
    break;
  }

  if (client.connect(server,80)) {
    client.println("GET ........./ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
  }
  resp=client.read();
  delay(250);
  client.stop();
  return resp;
}

But I always get resp=-1
Any idea?

Thank you!

int z=posts('email',2);

Which ONE key did you press to get email in the single quotes? The sequence of letters e, m, a, i, and l are a string, needing double quotes, not a character needing single quotes.

Of course, that's going to require major changes to the rest of the code.

Not the least of which is that code won't even compile.

This is not the full code!

int z=posts('email',2);

char posts(char typ2){
  EthernetClient client;
  String data;
  data="";
  switch (typ2){
    case 'email':
      data+="emailIt=2";
    break;
  }

  if (client.connect(server,80)) {
    client.println("GET ........./ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    Serial.println(data)
  }
  resp=client.read();
  delay(250);
  client.stop();
  return resp;
}

Serial.println(data) print a right values Serial.println(data)->emailIt=2

sketch_sep25c:2: error: too many arguments to function 'char posts(char)'
sketch_sep25c:1: error: at this point in file
sketch_sep25c.ino: In function 'char posts(char)':
sketch_sep25c:4: error: 'EthernetClient' was not declared in this scope
sketch_sep25c:4: error: expected `;' before 'client'
sketch_sep25c:13: error: 'client' was not declared in this scope
sketch_sep25c:13: error: 'server' was not declared in this scope
sketch_sep25c:20: error: expected `;' before '}' token
sketch_sep25c:21: error: 'resp' was not declared in this scope
sketch_sep25c:21: error: 'client' was not declared in this scope

Look, we aren't here to guess what your code is. If you want help, post code that compiles and produces the output you claim it produces.

I see you decided to ignore PaulS about your 'email' constant.

How to use this forum

Read this before posting a programming question

int z=posts('email',2);

char posts(char typ2){

The function “posts” does not take two arguments, so it is hard to believe you got it to compile either.

Ok here is the complete, working code just no get request response. The post method is work correctly

I think it should be a little more flexible…

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90,0xA2,0xDA,0x0D,0xDE,0xBF};
char server[] = "www.domain.hu";
IPAddress ip(xxx,xxx,x,xxx);

int an;
boolean _intArray[6];
int _timeArray[6];
String radars;
boolean emailIt=true;
int anLow=200;
int resp;
int isOnlineTime=0;

void setup() {
  SPI.begin();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  for (an = 1; an < 6; an++) {
    _timeArray[an]=0;
    _intArray[an]=true;
  }
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
}

void loop() {
  if (analogRead(0)>1000 && emailIt==true){
    for (an = 1; an < 7; an++){
      switch (an){
        case 1:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm('email',myString);
            }
          }
        break;
        case 2:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm('email',myString);
            }
          }
        break;
        case 3:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm('email',myString);
            }
          }
        break;
        case 5:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm('email',myString);
            }
          }          
        break;
        case 6:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm('email',myString);
            }
          }
        break;
      }
      if (10<_timeArray[an]){ 
        _intArray[an]=true;
        _timeArray[an]=0;
      }else{
        _intArray[an]=false;
        _timeArray[an]++;
      }
    }
  }
  
  radars="";
  for (an = 0; an < 7; an++){
    switch (an){
      case 0:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 1:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 2:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 3:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 5:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 6:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
    }
  }

  int x=alarm('radars',radars);
  int z=posts('sentinelalarm');
  if (z==1){
    analogWrite(3, HIGH);
  }else{
    analogWrite(3, LOW);
  }
  int v=posts('switch');
  if (v==1){
    analogWrite(4, HIGH);
    delay(700);
    analogWrite(4, LOW);
  }else{
    analogWrite(5, HIGH);
    delay(700);
    analogWrite(5, LOW);
  }
  int u=posts('checkviewers');
  if (u==1){
    isOnlineTime+=1;
    int t=posts('setonline');
  }
  int y=posts('email');
  if (y==1){
    if (isOnlineTime>5){
      emailIt=true;
    }
  }else if (y==0 || y==3){
    emailIt=false;
  }
}

char alarm(char typ1,String zone){
  EthernetClient client;
  String data;
  data="";
  switch (typ1){
    case 'email':
      data+="zone=";
      data+=zone;
    break;
    case 'radars':
      data+="radar=";
      data+=zone;
    break;
  }
  
  if (client.connect(server,80)) {
    client.println("POST .../alarm.php HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
  }
  delay(250);
  client.stop();
}

char posts(char typ2){
  EthernetClient client;

  String data;
  data="";
  switch (typ2){
    case 'email':
      data+="emailIt=2";
    break;
    case 'sentinelalarm':
      data+="securityAlarm=2";
    break;
    case 'switch':
      data+="switchReq=2";
    break;
    case 'setoffLine':
      data+="isOnline=0";
    break;
    case 'checkviewers':
      data+="isOnline=2";
    break;
  }

  if (client.connect(server,80)) {
    client.println("GET .../ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
  }
  Serial.println(data);
  resp=client.read();
  Serial.println(resp);
  delay(250);
  client.stop();
  return resp;
}

Run this sketch.

void setup ()
  {
  Serial.begin (115200);
  Serial.println ();

  char foo = 'email';
  Serial.println (foo);
  }  // end of setup

void loop () { }

Explain the output.

Ok but how that it can operate on my first function???

And the Serial.println(data) is what I want...

Ok but how that it can operate on my first function???

How can what operate on what function?

'email' is wrong. Get over it.

OK my code now looks like this:

#include <SPI.h>
#include <Ethernet.h>

byte mac[] = {0x90,0xA2,0xDA,0x0D,0xDE,0xBF};
char server[] = "www.domain.hu";
IPAddress ip(xxx,xxx,x,xxx);

int an;
boolean _intArray[6];
int _timeArray[6];
String radars;
boolean emailIt=true;
int anLow=200;
int resp;
int isOnlineTime=0;

void setup() {
  SPI.begin();
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
  for (an = 1; an < 6; an++) {
    _timeArray[an]=0;
    _intArray[an]=true;
  }
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(A3, OUTPUT);
  pinMode(A4, OUTPUT);
  pinMode(A5, OUTPUT);

  pinMode(3, INPUT);
  pinMode(5, INPUT);
  pinMode(6, INPUT);
}

void loop() {
  if (analogRead(0)>1000 && emailIt==true){
    for (an = 1; an < 7; an++){
      switch (an){
        case 1:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 2:
          if (analogRead(an)>anLow){
            if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 3:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
        case 5:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }          
        break;
        case 6:
        if (digitalRead(an)==HIGH){
          if (_intArray[an]==true){
              String myString = String(an);
              int z=alarm(1,myString);
            }
          }
        break;
      }
      if (10<_timeArray[an]){ 
        _intArray[an]=true;
        _timeArray[an]=0;
      }else{
        _intArray[an]=false;
        _timeArray[an]++;
      }
    }
  }
  
  radars="";
  for (an = 0; an < 7; an++){
    switch (an){
      case 0:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 1:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 2:
        if (analogRead(an)>anLow){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 3:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 5:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
      case 6:
        if (digitalRead(an)==HIGH){
          radars+="1|";
        }else{
          radars+="0|";
        }
      break;
    }
  }

  int x=alarm(2,radars);
  int z=posts(1);
  if (z==1){
    analogWrite(3, HIGH);
  }else{
    analogWrite(3, LOW);
  }
  int v=posts(2);
  if (v==1){
    analogWrite(4, HIGH);
    delay(700);
    analogWrite(4, LOW);
  }else{
    analogWrite(5, HIGH);
    delay(700);
    analogWrite(5, LOW);
  }
  int u=posts(3);
  if (u==1){
    isOnlineTime+=1;
    int t=posts(4);
  }
  int y=posts(5);
  if (y==1){
    if (isOnlineTime>5){
      emailIt=true;
    }
  }else if (y==0 || y==3){
    emailIt=false;
  }
}

int alarm(int typ1,String zone){
  EthernetClient client;
  String data;
  data="";
  switch (typ1){
    case 1:
      data+="zone=";
      data+=zone;
    break;
    case 2:
      data+="radar=";
      data+=zone;
    break;
  }
  
  if (client.connect(server,80)) {
    client.println("POST .../ajaxes/alarm.php HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(data.length());
    client.println();
    client.print(data);
  }
  Serial.println(data);
  delay(250);
  client.stop();
}

int posts(int typ2){
  EthernetClient client;
  TextFinder finder(client);
  String data;
  data="";
  switch (typ2){
    case 5:
      data+="emailIt=2";
    break;
    case 1:
      data+="securityAlarm=2";
    break;
    case 2:
      data+="switchReq=2";
    break;
    case 4:
      data+="isOnline=0";
    break;
    case 3:
      data+="isOnline=2";
    break;
  }

  if (client.connect(server,80)) {
    client.println("GET .../ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
  }
  Serial.println(data);
  resp=client.read();
  Serial.println(resp);
  delay(250);
  client.stop();
  return resp;
}

But the response is the same

              int z=alarm(1,myString);

Creating a local variable that immediately goes out of scope is a waste of time. You might as well delete this line. Unless you want to store the variable somewhere else, or you don't care about the return code. If that is the case, though, why does the function return a value?

    client.println("POST .../ajaxes/alarm.php HTTP/1.1");

Does your code actually look like this? Or, did you remove stuff that you don't want us to see?

int alarm(int typ1,String zone){

This function is missing a return statement. Therefore, it returns garbage.

Why do you not read what the POST returns?

  resp=client.read();

Read one character, even if the client has yet to send anything. Not good.

But the response is the same

And you're still tap dancing. Tell us what function is not returning what you expect. Tell us what you expect, and what it actually returns.

After you fix all the above errors, first.

In addition to PaulS: That is the correct response. client.read() will equal -1 (no character available) until the server sends a response, and that is not immediate. The server will close the connection when finished sending packets, then you close your end.

  if (client.connect(server,80)) {
    client.println("GET ........./ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
  }
  resp=client.read();
  delay(250);
  client.stop();
  return resp;

Here is the way to get the server response, but it is a bunch of characters, not just one.

  if (client.connect(server,80)) {
    client.print("GET ........./ajaxes/posts.php?");
    client.print(data);
    client.println(" HTTP/1.1");
    client.print("Host: ");
    client.println(server);
    client.println("Connection: close\r\n");

    while(client.connected()) {
      while(client.available()) {
        resp=client.read();
        Serial.print(resp);
      }
    }
    client.stop();
  }

There is no timeout in that response read. It could lock up in the “while(client.connected())” loop if the connection breaks or the server stalls. Here is a link to code that has the timeout.
http://playground.arduino.cc/Code/WebClient

edit: And the first GET send line is incorrect. no println() there, just print().

603368796784898069327284777632808566767367323445474773698470474768846832728477763250464847476978346210601041161091086260104101971006210601161051161081016251485032701111171101006047116105116108101621060471041019710062609811110012162106010449627011111711010060471044962106011262841041013210011199117109101110116321049711532109111118101100326097321041141011026134104116116112584747119119119461151051081059910511710910710211646104117471041059897955248524610411610910834621041011141016047976246604711262106047981111001216260471041161091086210603368796784898069327284777632808566767367323445474773698470474768846832728477763250464847476978346210601041161091086260104101971006210601161051161081016251485032701111171101006047116105116108101621060471041019710062609811110012162106010449627011111711010060471044962106011262841041013210011199117109101110116321049711532109111118101100326097321041141011026134104116116112584747119119119461151051081059910511710910710211646104117471041059897955248524610411610910834621041011141016047976246604711262106047981111001216260471041161091086210603368796784898069327284777632808566767367323445474773698470474768846832728477763250464847476978346210601041161091086260104101971006210601161051161081016251485032701111171101006047116105116108101621060471041019710062609811110012162106010449627011111711010060471044962106011262841041013210011199117109101110116321049711532109111118101100326097321041141011026134104116116112584747119119119461151051081059910511710910710211646104117471041059897955248524610411610910834621041011141016047976246604711262106047981111001216260471041161091086210603368796784898069327284777632808566767367323445474773698470474768846832728477763250464847476978346210601041161091086260104101971006210601161051161081016251485032701111171101006047116105116108101621060471041019710062609811110012162106010449627011111711010060471044962106011262841041013210011199117109101110116321049711532109111118101100326097321041141011026134104116116112584747119119119461151051081059910511710910710211646104117471041059897955248524610411610910834621041011141016047976246604711262106047981111001216260471041161091086210

This is the resp! My php code look like this:

  if ($_GET['emailIt']=='1'){
    $sql_data_array = array('variable_value' => '1');
    tep_db_perform('variables', $sql_data_array, 'update', 'variable_name = "emailIt"');
    echo 1;
  }else if ($_GET['emailIt']=='0'){
    $sql_data_array = array('variable_value' => '0');
    tep_db_perform('variables', $sql_data_array, 'update', 'variable_name = "emailIt"');
    echo 0;
  }else if ($_GET['emailIt']=='3'){
    $sql_data_array = array('variable_value' => '3');
    tep_db_perform('variables', $sql_data_array, 'update', 'variable_name = "emailIt"');
    echo 3;
  }else if ($_GET['emailIt']=='2'){
    $query = tep_db_query("SELECT variable_value FROM variables WHERE variable_name='emailIt'");
    while ($data = tep_db_fetch_array($query)) {
      $email=$data['variable_value'];
    }
    echo $email;
  }

Try this instead. You are printing the decimal values of the return string.

    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        Serial.write(ch);
      }
    }

myDuino:
6033687967848980693272847776328085667673673234454747736984704747688468327284

If you look at it the right way, that turns out to be:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">

You might want to consider printing things as characters, not integers. Printing out all your debugging, without a space or anything in-between each one, just leads to a jumbled mess.

Also, PHP and C treat string constants like ‘emailIt’ differently.

I don't have any header on my php file. Now I get the real value but get some other unwanted infos too.

0HTTP/1.1 200 OK Date: Wed, 25 Sep 2013 12:59:43 GMT Server: Apache Vary: Accept-Encoding Content-Length: 1 Connection: close Content-Type: text/html; charset=UTF-8

Parse that at the blank line. Everything after the blank line is the important stuff to you. A blank line is a double cr/lf (\r\n\r\n). That denotes "end of header".

SurferTim:
Parse that at the blank line. Everything after the blank line is the important stuff to you. A blank line is a double cr/lf (\r\n\r\n). That denotes “end of header”.

I don’t understand this exactly??

OR please write me how can I modified the code:

...
client.println(server);
    client.println("Connection: close\r\n");
    while(client.connected()) {
      while(client.available()) {
        char ch=client.read();
        resp=ch;
        Serial.print(ch);
      }
    }...