Making multiple POST requests

Hello!

I’m working on a project where I have an Arduino Uno, an Arduino GSM shield and a temperature sensor. I am supposed to read the temperature wherever I go, hence the GSM shield, and upload it to a server. And if the temperature changes more than x degrees up or down it should update the date to the server.

I’m sending a SOAP message to the server since that’s the standard the company I work for uses. I’m not looking for just updating the temperature, I want to send an entirely new SOAP message to the server each time the temperature changes.

So far I have managed to, at start up, read the temperature and successfully send it to the server. But in the loop, I can’t get it to work as I want.

// libraries
#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN       "" // replace your GPRS APN
#define GPRS_LOGIN     ""    // replace with your GPRS login
#define GPRS_PASSWORD  "" // replace with your GPRS password

// initialize the library instance
GSMClient client;
GPRS gprs;
GSM gsmAccess; // include a 'true' parameter for debug enabled

// This example downloads the URL "http://arduino.cc/"

// URL, path & port (for example: arduino.cc)
char server[] = "xxx.xxx.xx.xxx";
char path[] = "/Service";
int port = xx; // 80 for HTTP

int   analogValue;
float oldtemp;
float newtemp;

void setup()
{
  // initialize serial communications
  Serial.begin(9600);
  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;

  // Start GSM shield
  // If your SIM has PIN, pass it as a parameter of begin() in quotes
  while(notConnected)
  {
    if((gsmAccess.begin(PINNUMBER)==GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD)==GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }

  analogValue = analogRead(0);

  oldtemp = ((analogValue / 4) - 50);

  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  if (client.connect(server, port))
  {
    httprequest(oldtemp);
  } 
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
}

void httprequest(float temp)
{
  int temperature = (int)temp;
  String tmp = "" + temperature;
  Serial.println("connected");
  // Make a HTTP request:
  client.println( "POST /Service HTTP/1.1" );
  client.println( "Host: xxx.xxx.xx.xxx:xx" );
  client.println( "Content-Type: text/xml; charset=utf-8" );
  client.println( "SOAPAction: \"publishXmlEvent\"" );
  client.println("Content-Length: 1299");
  client.println( "Connection: Close" );
  client.println();
  client.println(F("<?xml version=\"1.0\" encoding=\"utf-8\"?>"));  //42
  client.println(F("<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\""));  //71
  client.println(F("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));  //55
  client.println(F("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"));  //47
  client.println(F("<soap:Body>"));  //11
  client.println(F("<publishXmlEvent xmlns=\"http://eventmanager.linksmart.eu\">"));  //60
  client.println(F("<eventXmlString>"));  //16
  client.println(F("&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;"));  //48
  client.println(F("&lt;Event xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""));  //65
  client.println(F("xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" about=\"f4605a3b-cad6-4198-a4d2-9760e799aa81\""));  //93
  client.println(F("prefix=\"inertia:http://ns.inertia.eu/ontologies"));  //48
  client.println(F("linksmart:http://linksmart.org/Ontologies/1.0#Draft\" eventStatus=\"Production\""));  //80
  client.println(F("schemaVersion=\"0.5.0\" systemsID=\"LinkSmart\" xmlns=\"http://linksmart.org/Event/1.0\"&gt;"));  //92
  client.println(F("&lt;EventMeta&gt;"));  //17
  client.println(F("&lt;EventType typeof=\"TempSensorDevice\" /&gt;"));  //47
  client.println(F("&lt;Topic typeof=\"OBSERVATION\" /&gt;"));  //38
  client.println(F("&lt;/EventMeta&gt;"));  //18
  client.println(F("&lt;Content&gt;"));  //15
  client.println(F("&lt;IoTEntity xmlns=\"http://linksmart.org/IoTEntity/1.0\"&gt;"));  //62
  client.println(F("&lt;Name&gt;TempSensorDevice&lt;/Name&gt;"));  //41
  client.println(F("&lt;IoTProperty about=\"01842FD8:TempSensorDevice\""));  //51
  client.println(F("typeof=\"inertia:TempSensorDevice\"&gt;"));  //39
  client.println(F("&lt;IoTStateObservation&gt;"));  //27
  client.println(F("&lt;Value&gt;"));
  client.println(tmp);
  client.println(F("&lt;/Value&gt;"));  //29
  client.println(F("&lt;/IoTStateObservation&gt;"));  //28
  client.println(F("&lt;/IoTProperty&gt;"));  //20
  client.println(F("&lt;/IoTEntity&gt;"));  //18
  client.println(F("&lt;/Content&gt;"));  //16
  client.println(F("&lt;/Event&gt;"));  //14
  client.println(F("</eventXmlString>"));  //17
  client.println(F("</publishXmlEvent>"));  //18
  client.println(F("</soap:Body>"));  //12
  client.println(F("</soap:Envelope>"));  //16
  client.println();
}

void loop()
{
  // if there are incoming bytes available 
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop(); 
  }
}

This code sends the SOAP message to the server on start up, and I’ve tried to get it to work in the loop as well without success.

OK, that's the sketch where you didn't try to send a POST in loop(). What does the sketch where you DID try to send a POST in loop() look like and in what way is it not working as desired?

I got it working fine after a few tries. Now it works exactly as I want.

I´m running into a similar problem, i´m posting to PunchThrough and it works fine the first time (inside the startup function) but it doesn't afterwords. Can you share what your problem was and how you fixed it?

Thanks

I solved it like this:

boolean oldRequestNotDone = true;   

void loop()
{
    if(oldRequestNotDone)
    {
          if(client.connect(server, port))
          {
                YOUR POST REQUEST HERE
          }
    }
    else
      {
        // if you didn't get a connection to the server:
        Serial.println("connection failed");
      }
      oldRequestNotDone = false;

     // if there are incoming bytes available 
     // from the server, read them and print them:
     if (client.available())
     {
       char c = client.read();
       Serial.print(c);
     }
     // if the server's disconnected, stop the client:
     if (!client.available() && !client.connected())
     {
       Serial.println();
       Serial.println("disconnecting.");
       client.stop();
    
      oldRequestNotDone = true;
  } 
}

This way you only send a POST request when the last one is completely done, which I believe was the problem in the first place.