0

I am trying to modifiy the Arduino GSM library Xively example to geeknesia.com. The serial said "successconnecting.." but I didn't get any value change in the website. Did I make a mistake with this code?

//Project Name Test sensor
#include <GSM.h>

#define PINNUMBER ""

//APIkey untuk Geeknesia
#define APIKEY  "c3ce75cc23386ca4ca663c0ed11653c5"
#define GPRS_APN       "xlunlimited"   
#define GPRS_LOGIN     ""
#define GPRS_PASSWORD  ""

GSMClient client;
GPRS gprs;
GSM gsmAccess;
GSMServer server(80);    

char IPserver[]="geeknesia.com";
unsigned long lastConnectionTime = 0; 
boolean lastConnected = false;
const unsigned long postingInterval = 10*1000;
int ldr1;
char ldr1_ar[10];

void setup() {
   Serial.begin(9600);
   boolean notConnected = true;
   Serial.println("try to connect...");
   while (notConnected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
      notConnected = false;
    else {
      Serial.println("can't connect");
      delay(1000);
    }
  }
  Serial.println("connected");
}

void sendData(int thisData) {
  if (client.connect(IPserver, 80)) {
    Serial.println("connecting...");
    client.print("GET /api/data?api_key=c3ce75cc23386ca4ca663c******&attributes={");
    client.print("\"ldr1\":" + String(ldr1));
    client.print("}");
    client.print("\r\n\r\n");
    client.print("Content-Length: ");
    int thisLength = 8 + getLength(thisData);
    client.println(thisLength);
    client.println("Connection: close");
    client.println();
    //client.print("cahaya,");
    client.println(thisData);
  } else {
    Serial.println("connection failed");
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  lastConnectionTime = millis();
}

int getLength(int someValue) {
  int digits = 1;
  int dividend = someValue /10;
  while (dividend > 0) {
    dividend = dividend /10;
    digits++;
  }
  return digits;
}

void loop() {
  ldr1 = analogRead(A0);
  //sendData(ldr1);
  if (client.available()) {
     char c = client.read();
     Serial.print(c);
  }
  if (!client.connected() && lastConnected) {
    client.stop();
  }
  if(!client.connected() && ((millis() - lastConnectionTime) > postingInterval)) {
    sendData(ldr1);
  }
  lastConnected = client.connected();
}
1

Your GET request is ill-formed. Based on your server's API docs, it should look like this instead:

Serial.println("connecting...");
client.print("GET /api/data?api_key=c3ce75cc23386ca4ca663c******&attributes={");
client.print("\"ldr1\":" + String(ldr1));
client.print("}");
client.println(" HTTP/1.1");
client.println("Host: api.geeknesia.com");
client.println();

While it is technically not illegal to have a body in a GET request, according to the HTTP 1.1 specs, the body should have no effect on the server's response i.e. it should be basically meaningless. Your server's API docs indicate that a body isn't needed/supported when you send data with GET, as is the case with most other servers.

You should consider replacing Arduino Strings with C char arrays; they're a lot safer and reliable.

  • Hi thanks for reply, could you explain to me more simpler, i really new in HTTP world XD, so whats the esier way to upload the data to that API. I have solve the problem but, the data is look like temporary, i mean its not saved in the database also graph history doesn't show. – ZkyHigh Jun 8 '16 at 6:03
  • @ZkyHigh Has the data been posted to the server? Any changes at all? – TisteAndii Jun 10 '16 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.