Ubidots POST ok, GET not so much

Hello good people,

Had my first encounter with Ubidots, not using the library they published, I prefer to do the code manually so I know exactly what happens for debugging purposes. I got my test device - just a mega with SIM900 module - to send variable to ubidots, updates regularly without fail. Now I'm trying to get a value from one varible in the cloud in order to switch on pin13, html get request sends OK but the ubidots server does not respond. The only response I get is 408 (timout) a couple minutes after the request was sent.

Ubidots support says the syntax is received correct at their end (???) and that it must be with my device. After the request is sent it goes over to loop where it continually checks the SIM900 for a response. I know this part of the code is good because the same code works perfectly to receive the response from the server when I send a POST request.

Code below. Any ideas why the server ignores what I send?

Thanks

PS: I realise there is a lot of use of Strings - please don't stone me about this, I'll address this as soon as I get the device to switch pin13 on and off

loop that checks Serial for incoming response:

void loop()
{

  while(Serial2.available() != 0)
    Serial.write(Serial2.read());
  while (Serial.available() != 0) 
    Serial2.write(Serial.read()); 

  if ((millis() - sendstart) > betweenSENDS)
  {
    int A0sensorValue = analogRead(A0);
    A0voltage = A0sensorValue * (5.0 / 1023.0);
    dtostrf(A0voltage, 4, 2, A0voltageChar);
    
    int p13sensorValue = analogRead(13);
    p13voltage = p13sensorValue * (5.0 / 1023.0);
    dtostrf(p13voltage, 4, 2, p13voltageChar);
    sendbasic();
    sendstart = millis();    
  }

  if ((millis() - receivestart) > betweenRECEIVES)
  {
    receivebasic();   
    receivestart = millis();    
  }
}

The send request - works fine

  void sendbasic() 
  {
       
    (sendATcommand(F("AT+CIFSR"), F(""), 1000));
    (sendATcommand(F("AT+CIPSPRT=0"), F("OK"), 1000));
    (sendATcommand(F("AT+CIPSTART=\"TCP\",\"http://things.ubidots.com\",\"80\""), F("CONNECT OK"), 3000));   
    String url=" /api/v1.6/collections/values/?token=xxxxxxxxxxx";   
    String A0voltagestr(A0voltageChar);
    String p13voltagestr(p13voltageChar);    
    String values ="[{\"variable\": \"xxxxxxxxxxxxxx\", \"value\":" + A0voltagestr +"}, {\"variable\": \"xxxxxxxxxxx\", \"value\":" + p13voltagestr +"}]";    
    int valueslength = (values.length());
    String postRequest ="POST" + url + " HTTP/1.1\r\n" + "Host: things.ubidots.com \r\n" + "Content-Type: application/json \r\n" + "Content-Length: " + valueslength + "\r\n\n" +  values +"\r\n" ;
    Serial2.print(F("AT+CIPSEND="));
    Serial2.println(postRequest.length());
    Serial.print(F("AT+CIPSEND="));
    Serial.println(postRequest.length());
    delay(1000);
    Serial2.println(postRequest);
    while (Serial2.available() > 0)
    {
      Serial.write(Serial2.read());
      delay(5);
    }  
  }

The get request - no response from server except for timeout:

void receivebasic() 
  {      
    (sendATcommand(F("AT+CIFSR"), F(""), 1000));
    (sendATcommand(F("AT+CIPSPRT=0"), F("OK"), 1000));
    (sendATcommand(F("AT+CIPSTART=\"TCP\",\"http://things.ubidots.com\",\"80\""), F("CONNECT OK"), 3000));
    String url=" /api/v1.6/devices/demovolts/pin13switch/lv";   
    String getRequest ="GET" + url + " HTTP/1.1\r\n" + "Host: things.ubidots.com\r\n" + "X-Auth-Token: xxxxxxxxxxx\r\n";
    Serial2.print(F("AT+CIPSEND="));
    Serial2.println(getRequest.length());
    delay(1000);
    Serial2.println(getRequest);
    Serial2.println();
    while (Serial2.available() > 0)
    {
      Serial.write(Serial2.read());
      delay(5);
    }  
  }

Serial comms:

AT+CIPSTART="TCP","http://things.ubidots.com","80"

OK

CONNECT OK

AT+CIPSEND=134
GET /api/v1.6/devices/demovolts/pin13switch/lv HTTP/1.1
Host: things.ubidots.com
X-Auth-Token: xxxxxxxxxxxxxxxxxxx



SEND OK
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>


CLOSED

What happens if you use their library with the example sketches provided with that library?

I tried it (halfheartedly) but I couldn't get the MEGA to talk with the SIM900. The library uses software serial, I don't know how to change the syntax to use my onboard Serial2 on the MEGA. This is a chunk of the code out of Ubidots_Arduino_GPRS.h:

private:
        SoftwareSerial _client = SoftwareSerial(7, 8);      
        bool httpInit();

I thought it time better spent to tinker with what I already have, I'm a big fan of doing things manually instead of using an off the shelf library.

Looking at the ubidots response, it says "Your browser didn't send a complete request in time." Can anyone see what is wrong with my request? What's missing? I used what they suggest from their REST API docs, and a support technician said it looked ok.

I tried different formats of the GET request, as per the REST API docs and several posts I found in forums, all give the same result. The one below is from the REST API docs, https://ubidots.com/docs/api/index.html#create-a-variable

AT+CIPSTART="TCP","http://things.ubidots.com","80"

OK

CONNECT OK

AT+CIPSEND=123
GET /api/v1.6/devices/demovolts/pin13switch/?token=BBFF-U75pxqjfbPeP3Q56C6FntSlkGH9GbQ HTTP/1.1
Host: things.ubidots.com



SEND OK
HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>


CLOSED

Got it, for those who are interested.

Needed an additional \r\n at the end of the request before the server recognised it as a request.

Working beautifully now.

heinburgh: Got it, for those who are interested.

Needed an additional \r\n at the end of the request before the server recognised it as a request.

Working beautifully now.

Hi,

Could you kindly post the entire code to GET?

I will. Even though I take the risk of being stoned for my excessive use of String…

This is the GET section, to grab a value from the server.

void receiveVAR() 
  {      
    receiveBUSY = true;
    boolean result = false;
    Serial.println();
    Serial.println(F("<<< receiveVAR START>>>"));
    Serial.println();  
    String url=" /api/v1.6/devices/demovolts/pin13switch/lv?token=" + token;   
    String getRequest ="GET" + url + " HTTP/1.1\r\n" + "Host: things.ubidots.com\r\n\r\n";    
    int reqLen = (getRequest.length());
    String ATcomm = "AT+CIPSEND=" + String(reqLen);
    while (sendATcommand(ATcomm, F("+CME ERROR:"), 2000) == true)
    {
      ModemOn();            
      SetupGPRS(); 
      connectServer();
    }  
    delay(1000);
    Serial2.print(getRequest);  
    delay(1000);
    byte u = "";
    byte v = "";
    byte w = "";  
    while ((Serial2.available() > 0) && (!result))
    {  
      delay(10);
      w = v; 
      v = u;       
      u = Serial2.read();
      Serial.write(u);
      if ((w == char(48)) && (v == char(46)) && (u == char(48)))   // test for "0.0"
      {
        Serial.print(F("                 << RESULT!! LED OFF >>"));
        digitalWrite(8, LOW);
        result = true;
      }  
      if ((w == char(49)) && (v == char(46)) && (u == char(48)))   // test for "1.1"
      {
        Serial.print(F("                 << RESULT!! LED ON >>"));
        digitalWrite(8, HIGH);
        result = true;
      }   
    } 
    delay(300);
    while (Serial2.available() > 0)
      Serial.write(Serial2.read());  
    Serial.println();  
    Serial.println(F("<<< receiveVAR END >>>")); 
    Serial.println();
    receiveBUSY = false;
    receivestart = millis();
  }