Go Down

Topic: Long time between POST http requests (Read 384 times) previous topic - next topic

Sandufi

Hello guys,

Im having a trouble when doing consecutive POST requests. I can see that the values are posted correctly on the web server, but it seems that each value is posted every minute aprox. I would like the value to be posted every 5 seconds. Im using and Arduino Shield 2! Here I post my code and see if anyone can help me :)

Code: [Select]


#include <GSM.h>

// PIN Number
#define PINNUMBER ""

// APN data
#define GPRS_APN       "" // replace with 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;
uint16_t reset = 0;

boolean oldRequestNotDone = true;

String idvariable = "";   // replace with your Ubidots Variable ID
String token = "";  // replace with your Ubidots token

void setup() {
  // initialize serial communications and wait for port to open:
  Serial.begin(9600);
  Serial.println("Starting Arduino web client.");
  // connection state
  boolean notConnected = true;
  // After starting the modem with GSM.begin()
  // attach the shield to the GPRS network with the APN, login and password
  while (notConnected) {
    if ((gsmAccess.begin(PINNUMBER) == GSM_READY) & (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
      notConnected = false;
      Serial.println("Connected to network!");
    } else {
      Serial.println("Not connected");
      delay(1000);
    }
  }

}

void loop() {

  int value = analogRead(A1);

  if (oldRequestNotDone)
  {
    if (save_value(value)) {
      Serial.println("HTTP request sent!");
    }
  }
  else {
    reset++;
    //Serial.println("Data transmission failed!");
    if (reset == 10) {
      //asm volatile ("  jmp 0");    // reset the Arduino board if the data transmission fail
    }
  }
  oldRequestNotDone = false;

  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();
    Serial.println("Stop the client!");
    oldRequestNotDone = true;
  }
}



boolean save_value(int value) {
  Serial.println("connecting...");
  int num = 0;
  String var = "{\"value\":" + String(value) + "}";
  num = var.length();
  // if you get a connection, report back via serial:
  if (client.connect("things.ubidots.com", 80)) {
    Serial.println("connected");
    // Make a HTTP request:
    client.print("POST /api/v1.6/variables/" + idvariable + "/values HTTP/1.1\nContent-Type: application/json\nContent-Length: " + String(num) + "\nX-Auth-Token: " + token + "\nHost: things.ubidots.com\n\n");
    client.println(var);
    client.println();
    return true;
  } else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
    return false;
  }

}



Go Up