0

I am developing GPRS HTTP send request code using a GSM/GPRS module. I found the following code via google:

/*************************************************************************
* SIM800 GPRS/HTTP Library
* Distributed under GPL v2.0
* Written by Stanley Huang <stanleyhuangyc@gmail.com>
* For more information, please visit http://arduinodev.com
*************************************************************************/

#include <Arduino.h>

// change this to the pin connect with SIM800 reset pin
#define SIM800_RESET_PIN 7

// change this to the serial UART which SIM800 is attached to
#define SIM_SERIAL Serial


// define DEBUG to one serial UART to enable debug information output
//#define DEBUG Serial

typedef enum {
    HTTP_DISABLED = 0,
    HTTP_READY,
    HTTP_CONNECTING,
    HTTP_READING,
    HTTP_ERROR,
} HTTP_STATES;

typedef struct {
  float lat;
  float lon;
  uint8_t year; /* year past 2000, e.g. 15 for 2015 */
  uint8_t month;
  uint8_t day;
  uint8_t hour;
  uint8_t minute;
  uint8_t second;
} GSM_LOCATION;

class CGPRS_SIM800 {
public:
    CGPRS_SIM800():httpState(HTTP_DISABLED) {}
    // initialize the module
    bool init();
    // setup network
    byte setup(const char* apn);
    // get network operator name
    bool getOperatorName();
    // check for incoming SMS
    bool checkSMS();
    // get signal quality level (in dB)
    int getSignalQuality();
    // get GSM location and network time
    bool getLocation(GSM_LOCATION* loc);
    // initialize HTTP connection
    bool httpInit();
    // terminate HTTP connection
    void httpUninit();
    // connect to HTTP server
    bool httpConnect(const char* url, const char* args = 0);
    // check if HTTP connection is established
    // return 0 for in progress, 1 for success, 2 for error
    byte httpIsConnected();
    // read data from HTTP connection
    void httpRead();
    // check if HTTP connection is established
    // return 0 for in progress, -1 for error, bytes of http payload on success
    int httpIsRead();
    // send AT command and check for expected response
    byte sendCommand(const char* cmd, unsigned int timeout = 2000, const char* expected = 0);
    // send AT command and check for two possible responses
    byte sendCommand(const char* cmd, const char* expected1, const char* expected2, unsigned int timeout = 2000);
    // toggle low-power mode
    bool sleep(bool enabled)
    {
      return sendCommand(enabled ? "AT+CFUN=0" : "AT+CFUN=1");
    }
    // check if there is available serial data
    bool available()
    {
      return SIM_SERIAL.available(); 
    }
    char buffer[256];
    byte httpState;
private:
    byte checkbuffer(const char* expected1, const char* expected2 = 0, unsigned int timeout = 2000);
    void purgeSerial();
    byte m_bytesRecv;
    uint32_t m_checkTimer;
};

The problem is I have no idea how to change or define Rx and Tx Pin. If I define SIM_SERIAL serial1 it's showing me error. I want to change it to pins 10 and 11.

Here is other code whenever I try to compile it shows me error on "serial1" online. above code is sim800.h file

here is program code of send data through http

"SIM800.h"

#define APN "connect"
#define con Serial
static const char* url = "http://arduinodev.com/datetime.php";

CGPRS_SIM800 gprs;
uint32_t count = 0;
uint32_t errors = 0;

void setup()
{
  con.begin(9600);
  while (!con);

  con.println("SIM800 TEST");

  for (;;) {
    con.print("Resetting...");
    while (!gprs.init()) {
      con.write('.');
    }
    con.println("OK");

    con.print("Setting up network...");
    byte ret = gprs.setup(APN);
    if (ret == 0)
      break;
    con.print("Error code:");
    con.println(ret);
    con.println(gprs.buffer);
  }
  con.println("OK");
  delay(3000);  

  if (gprs.getOperatorName()) {
    con.print("Operator:");
    con.println(gprs.buffer);
  }
  int ret = gprs.getSignalQuality();
  if (ret) {
     con.print("Signal:");
     con.print(ret);
     con.println("dB");
  }
  for (;;) {
    if (gprs.httpInit()) break;
    con.println(gprs.buffer);
    gprs.httpUninit();
    delay(1000);
  }
  delay(3000);
}

void loop()
{

  char mydata[16];
  sprintf(mydata, "t=%lu", millis());
  con.print("Requesting ");
  con.print(url);
  con.print('?');
  con.println(mydata);
  gprs.httpConnect(url, mydata);
  count++;
  while (gprs.httpIsConnected() == 0) {
    // can do something here while waiting
    con.write('.');
    for (byte n = 0; n < 25 && !gprs.available(); n++) {
      delay(10);
    }
  }
  if (gprs.httpState == HTTP_ERROR) {
    con.println("Connect error");
    errors++;
    delay(3000);
    return; 
  }
  con.println();
  gprs.httpRead();
  int ret;
  while ((ret = gprs.httpIsRead()) == 0) {
    // can do something here while waiting
  }
  if (gprs.httpState == HTTP_ERROR) {
    con.println("Read error");
    errors++;
    delay(3000);
    return; 
  }

  // now we have received payload
  con.print("[Payload]");
  con.println(gprs.buffer);

  // show position
  GSM_LOCATION loc;
  if (gprs.getLocation(&loc)) {
    con.print("LAT:");
    con.print(loc.lat, 6);
    con.print(" LON:");
    con.print(loc.lon, 6);
    con.print(" TIME:");
    con.print(loc.hour);
    con.print(':');
    con.print(loc.minute);
    con.print(':');
    con.println(loc.second);
  }

  // show stats  
  con.print("Total Requests:");
  con.print(count);
  if (errors) {
    con.print(" Errors:");
    con.print(errors);
  }
  con.println();
}

i already know how to send data through http using AT command and software serial library here is my code

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.write("AT\n");
  delay(1000);
  mySerial.write("AT+CSQ\n");
  delay(1000);
  mySerial.write("AT+SAPBR=3,1,\"APN\",\"internet\"\n");
  delay(2000);
  mySerial.write("\nAT+SAPBR=1,1\n");
  delay(1000);
  mySerial.write("AT+HTTPINIT\n");
  delay(1000);




}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
    delay(1000)
    serial.print(enter key)
  }
  if (Serial.available()) {
    delay(2000);
     mySerial.write("AT+HTTPPARA=\"URL\",\"http://www.mydomain.in/index.php?dt=testdata\"\n");
     delay(2000);
     mySerial.write("AT+HTTPACTION=0\n");
      delay(2000);
      mySerial.write("\n");
    mySerial.write(Serial.read());
  }

In this code I define Rx Tx (UART)pin. Can you please suggest me any idea how can i define RX Tx pin in sim800.h file. because serial1 give me error on compiling time

  • That doesn't look like your code. – Ignacio Vazquez-Abrams Oct 19 '15 at 10:17
  • yes i said i found code on google – Allex Oct 19 '15 at 10:23
  • How would you possibly expect us to be able to tell you what to change it to without seeing any of your code? – Ignacio Vazquez-Abrams Oct 19 '15 at 10:38
1
SoftwareSerial mySerial(10, 11); // RX, TX

Well there you are.

#define SIM_SERIAL mySerial
  • thank you. should i add SoftwareSerial mySerial(10, 11); // RX, TX code in sim800.h file above #define SIM_SERIAL – Allex Oct 19 '15 at 11:08
0

I think Serial1 has to do with the Hardware serial Port(tx1,rx1) on Arduino mega . But since you are not using Serial.print i suggest you try "Serial/Serial0" if you are using UNO. If you want (10,11) like you said then i think you should use Softwareserial
#include SoftwareSerial.h //check spelling online not sure SoftwareSerial allex(10,11);
#define SIM_SERIAL allex
try it and see if it works.

  • should i change #define con Serial also.. – Allex Oct 19 '15 at 11:39
  • i set default serial port using only serial declaration but default RX and TX pin not working – Allex Oct 19 '15 at 11:42
  • I change sim800.h code "#define SIM_SERIAL mySerial" to "#define SIM_SERIAL Serial' – Allex Oct 19 '15 at 11:42

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.