send HTTP command

So I can send an HTTP command to affect my home automation controller (enter in my browser) in this form:

http://192.168.1.59:3480/data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0

or this:

http://192.168.1.59:3480/data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=1

to turn off and on a device respectively. These HTTP commands work...

I am trying to do that with Arduino, and looking for a bit of help. I have looked at various client code to get it to work...

void phoneyTV(boolean status)
{
  if (client.connect(myserver, 80)) {  //starts client connection, checks for connection
    Serial.println(F("connected"));
    client.println(F("http://192.168.1.59:3480/data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue="));
    client.println(status? "1" : "0");
    //client.println("Host: web.comporium.net");
    client.println("Connection: close");  //close 1.1 persistent connection  
    client.println(); //end of get request
  } 
  else {
    Serial.println("connection failed"); //error message if no client connect
    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(); //gets byte from ethernet buffer
    Serial.print(c); //prints byte to serial monitor 
  }

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

entire code attached...

NPTWorkingWebpageWithDHTexpanded.ino (10.9 KB)

I would think your client GET request should look more like below. not sure what the "status" part is doing.

  if (client.connect(myserver, 3480)) {  //starts client connection, checks for connection
    Serial.println(F("connected"));
    client.print(F("GET /data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue="));
    client.print(status? "1" : "0");
    client.println(F(" HTTP/1.1"));
    client.println(F("Connection: close"));  //close 1.1 persistent connection  
    client.println(); //end of get request
  }

thanks @zoomcat,

the

 client.print(status? "1" : "0");

is intended to append the command with the instruction one or zero as in the first post.

Would

 client.println(status? "1" : "0");

make more sense?

As you can tell, I have no knowledge on how to structure this GET

like this:

if (client.connect(myserver, 3480)) {  //starts client connection, checks for connection
    Serial.println(F("connected"));
    client.print(F("GET /data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue="));
    client.println(status? "1" : "0");
    client.println(F(" HTTP/1.1"));
    client.println(F("Connection: close"));  //close 1.1 persistent connection  
    client.println(); //end of get request
  }

Would

Code: [Select]

client.println(status? "1" : "0");

make more sense?

No. Print the same information to the serial port to see why.

Thanks Paul, I got it... we with to print " HTTP/1.1" on the same line.

I appreciate the help and will test it later!

client.println(status? "1" : "0");

Th "ln" part appends a carriage return/line feed on the end, which you don't want.

yup... got it, big thanks.

I didn't know that the " HTTP/1.1" has to go on the same line as the rest of the GET expression.

I appreciate your assistance.

Ok, so it works... much appreciation for the assistance. karma to both of you.

I did however have to add a bit for it to "work" that is, the client.stop(). It "works" now but.... I must have a memory leak somewhere. after multiple successful attempts of changing the state of the light... the program stops in a manner that causes me to have to reset the arduino.

This is the function that I used including the added client.stop().

void phoneyTV(boolean status)
{
  if (client.connect(myserver, 3480)) //starts client connection, checks for connection
  {  
    Serial.println(F("connected"));
    client.print(F("GET /data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue="));
    client.print(status ? F("1") : F("0"));
    client.println(F(" HTTP/1.1"));
    client.println(F("Connection: close"));  //close 1.1 persistent connection  
    client.println(); //end of get request
    delay(1);
    client.stop();
  }
}

entire sketch attached. I have to hunt down the culprit.

Thanks!

NPTWorkingWebpageWithDHTexpanded.ino (11.4 KB)

Have you included a way to check the socket status? Here is mine. Call it when you are going to connect and send a request to see if there is a socket available.

#include <utility/w5100.h>

byte socketStat[MAX_SOCK_NUM];

void ShowSockStatus()
{
  for (int i = 0; i < MAX_SOCK_NUM; i++) {
    Serial.print(F("Socket#"));
    Serial.print(i);
    uint8_t s = W5100.readSnSR(i);
    socketStat[i] = s;
    Serial.print(F(":0x"));
    Serial.print(s,16);
    Serial.print(F(" "));
    Serial.print(W5100.readSnPORT(i));
    Serial.print(F(" D:"));
    uint8_t dip[4];
    W5100.readSnDIPR(i, dip);
    for (int j=0; j<4; j++) {
      Serial.print(dip[j],10);
      if (j<3) Serial.print(".");
    }
    Serial.print(F("("));
    Serial.print(W5100.readSnDPORT(i));
    Serial.println(F(")"));
  }
}

A socket status list:
0X0 = available.
0x14 = socket waiting for a connection
0x17 = socket connected to a server.
0x1C = socket connected waiting for close.
0x22 = udp socket.

Tim,

Are you advising to ShowSockStatus() before I transmit the HTTP command?

something like this?

void phoneyTV(boolean status)
{
  if (ShowSockStatus() == 0x00)
  {
    if (client.connect(myserver, 3480)) //starts client connection, checks for connection
    {  
      Serial.println(F("connected"));
      client.print(F("GET /data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue="));
      client.print(status ? F("1") : F("0"));
      client.println(F(" HTTP/1.1"));
      client.println(F("Connection: close"));  //close 1.1 persistent connection  
      client.println(); //end of get request
      delay(1);
      client.stop();
    }
  }
}

Thanks,

Jim

ShowSockStatus doesn't return anything. It displays the socket status for all sockets. If none of the sockets show show a status of 0x0, then the client.connect call will fail.

void phoneyTV(boolean status)
{
  ShowSockStatus();

  if (client.connect(myserver, 3480)) //starts client connection, checks for connection
  {

SurferTim:
ShowSockStatus doesn't return anything. It displays the socket status for all sockets. If none of the sockets show show a status of 0x0, then the client.connect call will fail.

void phoneyTV(boolean status)

{
  ShowSockStatus();

if (client.connect(myserver, 3480)) //starts client connection, checks for connection
  {

thanks Tim,

In addition to adding your function I broke up this:

http://192.168.1.59:3480/data_request?id=action&output_format=xml&DeviceNum=88&serviceId=urn:upnp-org:serviceId:SwitchPower1&action=SetTarget&newTargetValue=0

into smaller bits as in the function that I am now using (below). I noticed that the demos used multiple print() functions versus sending one long (flash-stored) string. It occurred to me that those large strings had two work their way through the stack and perhaps that was what was stalling my program. Well, so far, much better.

Thanks!!

Jim

void phoneyTV(boolean status)
{
  ShowSockStatus();
  if (client.connect(myserver, 3480)) //starts client connection, checks for connection
  {  
    DEBUG_PRINTLN(F("connected"));
    client.print(F("GET /data_request?"));
    client.print(F("id=action&output_format=xml"));
    client.print(F("&DeviceNum=88"));
    client.print(F("&serviceId=urn:upnp-org:"));
    client.print(F("serviceId:SwitchPower1"));
    client.print(F("&action=SetTarget"));
    client.print(F("&newTargetValue="));
    client.print(status ? F("1") : F("0"));
    client.println(F(" HTTP/1.1"));
    client.println(F("Connection: close"));  //close 1.1 persistent connection  
    client.println(); //end of get request
    delay(1);
    client.stop();
  }
}