Repeated HTTP requests using GSM/GPRS Shield

Hi all

I am struggling to submit sensor data to my Google Form repeatedly, it only submits the first time the program runs through the main for loop. The program does not get stuck, as I put in a “Start of infinite for loop” checkpoint, it kinda just “ignores” some stuff :confused: please could someone advise? See code below [going to sensor out confidential info and unnecessary stuffs]

#include <GSM.h>

// PIN Number
#define PINNUMBER ""

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

#define trigPin 13
#define echoPin 12

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


char server[] = "api.pushingbox.com";
int port = 80; // 80 for HTTP

long duration, distance;


unsigned long lastConnectionTime = 0;          // last time you connected to the server, in milliseconds
boolean lastConnected = false;                 // state of the connection last time through the main loop
const unsigned long postingInterval = 60*1000;  // delay between updates, in milliseconds

void setup()
{
  // initialize serial communications
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  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);
    }
  }

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

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

char getBeans(){
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  return distance;
}

void submitBeans(int datapoint){
   client.print("GET /pushingbox?devid=[censored]&beans=");
   client.print(datapoint, DEC);
   client.println(" HTTP/1.0");
   client.println("Host: api.pushingbox.com");
   client.println();
}

void loop()
{
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }
  
  // if you're not connected, and ten seconds have passed since
  // your last connection, then connect again and send data:
  if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) {
    Serial.println("Posting to Form");
    submitBeans(getBeans());
    Serial.print("Posted ");
    Serial.print(distance, DEC);
    Serial.println(" to Beans");
  }

  // store the state of the connection for next time through
  // the loop:
  lastConnected = client.connected();
  
  delay(2000);
}

After the first successful connection, do you see the message "disconnecting" on the serial monitor? If so, which would be expected, the connection has been closed by the server and your code, then you must connect to the server again to send another request.

The only connection attempt is in your setup function, which only runs once.

@imbatronics, were you able to solve your problem, how? I'm facing a very similar situation and will appreciate any hint.

Thanks

@simon

I can't remember exactly what I did, but I did get it working.

If I recall correctly I removed all of the "unnecessary" stuff like lastconnected time. What I think is important is that you stop or close the connection after each iteration.

Give me feedback, if it doesn't work I can find some working code

@imbatronics,, thanks for your response.

I was able to fix it, I was having a PIN 3 conflict as I was using it for an interrupt but since it is used by the GSM shield...

BTW I have trying to find a way to be able to have at least one interrupt available for my sketch using the UNO + GSM field with no success till now. I guess PinChangeInt library is the solution but can't make it work. if you have any hint it will be appreciated.

Best!

Simon