Pages: [1]   Go Down
Author Topic: XML string  (Read 238 times)
0 Members and 1 Guest are viewing this topic.
Dallas, TX
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I posted in an earlier forum about an issue with null, I figured that out. Now I am getting an error of -3 when I send the post request (towards the end of the code). It appears to connect using the "post" function. My theory is that the XML is not being transmitted correctly. Any help would be appreciated. Thank you. Code is below:

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

//MAC address of ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xB2, 0xAE };

int readlight(int lt){

  float tmpflt = (float)lt*1.1/1024;
    int percent = 100*(0.99-tmpflt);
   
    if(percent > 100)
       percent = 100;
    if(percent < 0 )
       percent = 0; 
       
  return percent;
}

int readtemp(int tm){

  float tmpflt = (float)tm*1.1/1024;
 
    int Rth = 0;
    boolean done = false;
 
    while(done == false){
   
      float res1 = tmpflt*((float)Rth+1000);
      float res2 = (float)Rth*3.3;

                if(res2 > res1)
         done = true;
      else Rth+=1;
    }
     Rth-=1;
     
     //temperature
     float tmptemp = 25.0+(float)(Rth-500)*(-0.05);
     float fahrenheight = 32+tmptemp*(float)9/(float)5;
     
     int tmpout = (int)fahrenheight;
 
  return tmpout;

}

int readfromserial(){

  while(true){
 
    if(Serial.available() > 0)
      return Serial.read();
  }
}

int readvalue(int sensorid, int mode){

  const double ref = 1.1;
 
  int length;
  int address;
  int signalstrength;
  byte tmp;
 
  double voltage0, voltage1;
 
  boolean done = false;
 
  while(done == false){
  if(Serial.read() == 0x7E){
   
    int lengthh = readfromserial();
    int lengthl = readfromserial();
    length = 256*lengthh+lengthl;
   
    if(readfromserial() == 0x83){
     
      int addressh = readfromserial();
      int addressl = readfromserial();
      address = 256*addressh+addressl;
     
      if(address == (sensorid-1)){
        done =true;
       
      signalstrength = readfromserial();
     
      for(int i = 1;i <= 6; i++){
        tmp = readfromserial();
      }
     
      int ad0h = readfromserial();
      int ad0l = readfromserial();
      int ad0tmp = 256*ad0h+ad0l;
      readlight(ad0tmp);
   
      int ad1h = readfromserial();
      int ad1l = readfromserial();
      int ad1tmp = 256*ad1h+ad1l;
     
      if(mode == 0)
        return readlight(ad0tmp);
     
      if(mode == 1)
        return readtemp(ad1tmp);
       
      }
    }
  }
  }
 
  return -1;
}

EthernetClient c;
   HttpClient http(c);



void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(500);
 
  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(15000);
  }
 
}

void loop() {
  // main program loop
   
    // read values from sensors
  //  int sensorValue1 = readvalue(1,0);
  //  int sensorValue2 = readvalue(1,1);
    int sensorValue3 = 5;//readvalue(2,0);
    int sensorValue4 = 5;//readvalue(2,1);
    int sensorValue5 = 6;//readvalue(3,0);
    int sensorValue6 = 6;//readvalue(3,1);
    int sensorValue7 = 7;//readvalue(4,0);
    int sensorValue8 = 7;//readvalue(4,1);
   
    Serial.println("sensor readings");
    Serial.println(sensorValue3);
    Serial.println(sensorValue4);
    Serial.println(sensorValue5);
    Serial.println(sensorValue6);
    Serial.println(sensorValue7);
    Serial.println(sensorValue8);
 
   int err = 0;
   
   http.beginRequest();
   //err=http.put(kHostname, kPath);
   
   //generate url
   char* url = "http://ds.sidnow.com/SidAppService.svc/putDeviceData/?&apikey=PIRH2OOl@AlvtFxuLEJ4MA==&userId=111&status=1&type=mobile";
   
   
   
   
   
    err=http.post("ds.sidnow.com/", 80, url);   
   
   if(err == 0){
   
     Serial.println("startedRequest ok");
     //Serial.print(xmlStr);
   
     //apikey/token key
     http.sendHeader("apikey", "uQ9i/ROEOc5Z1AltY3uSUnbMgQtiktP7U4tXXxB1Ep4=");
     
     //char* xmlStrChars;
     
     //xmlStr.toCharArray(xmlStrChars, 1000);
     //http.sendHeader("deviceData", xmlStrChars);
     
     //write xml directly to ethernet out
     c.println("deviceData: <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><root><device><devicedata><data><Temp1><![CDATA[1]]></Temp1><Temp2><![CDATA[");
     c.print(sensorValue4);
     c.print("]]></Temp2><Temp3><![CDATA[");
     c.print(sensorValue6);
     c.print("]]></Temp3><Temp4><![CDATA[");
     c.print(sensorValue8);
     c.print("]]></Temp4><Light1><![CDATA[1]]></Light1><Light2><![CDATA[");
     c.print(sensorValue3);
     c.print("]]></Light2><Light3><![CDATA[");
     c.print(sensorValue5);
     c.print("]]></Light3><Light4><![CDATA[");
     c.print(sensorValue7);
     c.print("]]></Light4><UpdatedDate><![CDATA[05/04/2013 09:33:35 PM]]></UpdatedDate></data></devicedata><DC><MACADDRESS><![CDATA[3345674]]></MACADDRESS><IPADDRESS><![CDATA[1]]></IPADDRESS><IMEI><![CDATA[1]]></IMEI><SERIALNUMBER><![CDATA[]]></SERIALNUMBER></DC></device></root>");
   
     http.endRequest();
     
      err = http.responseStatusCode();
     
if (err >= 0)
{
Serial.print("Got status code: ");
Serial.println(err);

// Usually you'd check that the response code is 200 or a
// similar "success" code (200-299) before carrying on,
// but we'll print out whatever response we get
}
else
{
Serial.print("Getting response failed: ");
Serial.println(err);
}
}
else
{
Serial.print("Connect failed: ");
Serial.println(err);
}

http.stop();

// Wait for a bit before updating things again
delay(2000);   
  }
Logged

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your request should probably be:
Code:
err=http.post("ds.sidnow.com", 80, url);
ds.sidnow.com/ is not a valid host name so may be the problem.
Can't be sure though as I don't know what library you're using! smiley
Also next time please put all your code in code tags: [code][/code]
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12568
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am getting an error of -3 when I send the post request (towards the end of the code). It appears to connect using the "post" function.

I can see the call to http.post() followed by code that writes some request headers and a body, but I don't know what else is going on or where the error occurs - there's quite a lot of code there.

Can you reproduce the problem with a minimal sketch that does the least work necessary to initialise the network, perform the HTTP Post and demonstrate the problem? Include print statements to show what's working and what's failing, and include the output you get from them.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: