0

I am trying to send data to a php file via http GET method.

I am able to send static data but I am unable to send dynamic data.

#include <SoftwareSerial.h>
SoftwareSerial gprsSerial(9, 10);

char inchar;
int ledPin = 13;
int sensorPin = A0;
int sensorValue = 0;

boolean gs = true;
boolean es = true;
boolean ss = false;

void setup()
{
  gprsSerial.begin(9600);
  gprsSerial.println("AT+CNMI = 2,2,0,0");
  Serial.begin(9600);
}
void loop()
{
   currentMillis = millis();
   if(gprsSerial.available()>0)
  {
    inchar = gprsSerial.read();
    if (inchar == '#')
    {
      delay(10);
      inchar = gprsSerial.read();
      if (inchar == '1')
      {
        Serial.println("System Enabled");
        ss = true;
        snd_msg(0);
      }
      else if (inchar == '0')
      {
        Serial.println("System Disabled");
        ss = false;
        snd_msg(1);        
      }
      delay(10);
      inchar = gprsSerial.read();
      if (inchar == '1')
      {
        Serial.println("Geofence Enabled");
        gs = true;
        snd_msg(2);        
      }
      else if (inchar == '0')
      {
        Serial.println("Geofence Disabled");
        gs = false;
        snd_msg(3);        
      }
      delay(10);
    }
  }
    gprs();
    delay(10000);
}

void toSerial()
{
  while(gprsSerial.available()!=0)
  {
    Serial.write(gprsSerial.read());
  }
}
 void snd_msg(int a)
{
  gprsSerial.println("AT+CMGF=1");    //Sets the GSM Module in Text Mode
  delay(1000);  // Delay of 1000 milli seconds or 1 second
  gprsSerial.println("AT+CMGS=\"+918148090646\"\r"); // Replace x with mobile number
  delay(1000);
  if ( a == 0)
  {
    gprsSerial.println("System Enabled");
  }
  else if ( a == 1)
  {
    gprsSerial.println("System Disabled");
  }
  else if ( a == 2)
  {
    gprsSerial.println("Geofence Enabled");
  }
  else if ( a == 3)
  {
    gprsSerial.println("Geofence Disabled");
  }     
  delay(100);
   gprsSerial.println((char)26);// ASCII code of CTRL+Z
  delay(1000);
  gprsSerial.println("AT+CNMI = 2,2,0,0");
  delay(1000);
}
void vibra_sensor()
{
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  for(int i = 0; i < 5; i++)
  {
  if (sensorValue > 500)
  {
    es = true;
    break;
  }
  else
    es = false;
  delay(500);
  }  
  delay(1000);
}
void gprs()
{
  vibra_sensor();
  Serial.println("Config SIM900...");
  delay(2000);
  Serial.println("Done!...");
  gprsSerial.flush();
  Serial.flush();

  // attach or detach from GPRS service 
  gprsSerial.println("AT+CGATT?");
  delay(100);
  toSerial();


  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=3,1,\"APN\",\"www\"");
  delay(2000);
  toSerial();

  // bearer settings
  gprsSerial.println("AT+SAPBR=1,1");
  delay(2000);
  toSerial();
  // initialize http service
  gprsSerial.println("AT+HTTPINIT");
   delay(6000); 
   toSerial();

   // set http param value 
 **gprsSerial.println("AT+HTTPPARA=\"URL\",\"adnansait74.000webhostapp.com/operation.php?timestamp=02:50:00&latitude=11.374545&longitude=76.728986&gfence=OFF&estatus=OFF&sstatus=ON\"");**
   delay(5000);
   toSerial();

   // set http action type 0 = GET, 1 = POST, 2 = HEAD
   gprsSerial.println("AT+HTTPACTION=0");
   delay(6000);
   toSerial();

   // read server response
   gprsSerial.println("AT+HTTPREAD"); 
   delay(1000);
   toSerial();

   gprsSerial.println("");
   gprsSerial.println("AT+HTTPTERM");
   toSerial();
   delay(300);

   gprsSerial.println("");
   delay(10000);
   gprsSerial.println("AT+CNMI = 2,2,0,0");
  delay(1000);
}

What I've Tried

1.) Saving URL in a string

String URL = "adnansait74.000webhostapp.com/operation.php?timestamp=02:50:00&latitude=11.374545&longitude=76.728986&gfence=OFF&estatus=OFF&sstatus=";
    URL += ss;
    gprsSerial.println("AT+HTTPPARA=\"URL\",\"+URL\"");

Doesn't Work

2.)Attaching values using print statement

gprsSerial.println("AT+HTTPPARA=\"URL\",\"adnansait74.000webhostapp.com/operation.php?timestamp=02:50:00&latitude=11.374545&longitude=76.728986&gfence=OFF&estatus=OFF&sstatus=");
gprsSerial.println(ss+"\"");

3.)Entering value directly in URL

gprsSerial.println("AT+HTTPPARA=\"URL\",\"adnansait74.000webhostapp.com/operation.php?timestamp=02:50:00&latitude=11.374545&longitude=76.728986&gfence=OFF&estatus=OFF&sstatus="+ss+"\");

Error

invalid operands of types 'const char*' and 'const char [2]' to binary 'operator+'

I'm using an arduino uno and a sim 900a GSM module.

QUESTION

How do I send dynamic values for gfence, sstatus, estatus?

I am also using a messaging service, the code is included. The help I need is in the gprs() function.

The data I need to send is,

gfence from gs

sstatus from ss

estatus from es

  • How does this not work? The GPS module returns an error? The HTTP server returns an error? Your PHP script returns an error? – gre_gor Apr 6 '18 at 17:52
  • it is unclear what you mean by static data and dynamic data. – jsotola Apr 6 '18 at 17:57
  • 1
    boolean ss = false; ..... URL += ss; .... ..... binary 'operator+' ............... you cannot concatenate a boolean value True .... you can concatenate a string value "abc123" or "True" – jsotola Apr 6 '18 at 18:07
  • +URL is not a valid URL. The syntax highlighting should have given you a hint. – gre_gor Apr 6 '18 at 18:33
1

You should split it up:

gprsSerial.print(F("AT+HTTPPARA=\"URL\",\"adnansait74.000webhostapp.com/operation.php?timestamp=02:50:00&latitude=11.374545&longitude=76.728986&gfence=OFF&estatus=OFF&sstatus="));
gprsSerial.print(ss);
gprsSerial.println(F("\""));

Note the use of print on each part up until the last part which is the only one that uses println in order to terminate the line.

Also note the use of the F() macro to reduce the wastage of RAM that string constants cause.

  • F() for one character? – Juraj Apr 7 '18 at 10:43
  • Sure. Frugality is key with 2k ram – Majenko Apr 7 '18 at 10:58
  • ok, I eventually googled that it is has no overhead in code – Juraj Apr 7 '18 at 12:52
  • 1
    @Juraj I could have switched to Serial.println('"'); (i.e., use a single char), but that would most likely have confused the OP. Best to keep things simple. – Majenko Apr 7 '18 at 12:54

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.