简体   繁体   中英

How can I include a body while sending a POST request using HTTP AT commands with a SIM7000A module and an Arduino?

I'm crossposting this from where I posted it on the Arduino forum in hopes that maybe someone here has an answer. I am working on a project that involves sending POST requests to a Twilio function using an Arduino Uno and a SIMCOM SIM7000A breakout board using AT commands. So far with the Uno, I can only POST an empty request that contains headers but no body. The following code is a test sketch where I am POSTing to a RequestBin repository.

#include <AltSoftSerial.h>

unsigned char data = 0;
AltSoftSerial SIM7000;
char incomingChar;


void setup() {

  Serial.begin(9600);
  SIM7000.begin(19200);

  // put your setup code here, to run once:
  SIM7000.print("AT+CMGF=1\r");  //put in text mode
  delay(100);
  SIM7000.print("AT+CGDCONT=1\"IP\",\"super\"\r"); //create profile.  "super" is APN for twilio super sim
  delay(1000);
  SIM7000.print("AT+COPS=1,2,\"310410\"\r"); //log on to AT&T network
  delay(5000);
  SIM7000.print("AT+SAPBR=3,1,\"APN\",\"super\"\r"); //create SAPBR profile.  same APN.
  delay(300);
  SIM7000.print("AT+SAPBR=1,1\r");
  delay(2000);
  Serial.println("Setup complete.  Module should be online and fast-blinking\n from successful SAPBR profile setup.\n Entering loop.  Send \"CHECK\" to test POST");
}

void loop() {


  SIM7000.print("AT+CMGL\r");

  if (SIM7000.available() > 0) {
    incomingChar = SIM7000.read();
    if (incomingChar == 'C') {
      delay(10);
      Serial.print(incomingChar);
      incomingChar = SIM7000.read();
      if (incomingChar == 'H') {
        delay(10);
        Serial.print(incomingChar);
        incomingChar = SIM7000.read();
        if (incomingChar == 'E') {
          delay(10);
          Serial.print(incomingChar);
          incomingChar = SIM7000.read();
          if (incomingChar == 'C') {
            delay(10);
            Serial.print(incomingChar);
            incomingChar = SIM7000.read();
            if (incomingChar == 'K') {
              delay(10);
              Serial.print(incomingChar);
              incomingChar = "";
              Serial.println(F("\nGOOD CHECK"));
              Serial.println(F("SENDING POST REQUEST NOW")) ;
              sendText();
              Serial.println("POST SENT");
              return;
            }
          }
        }
      }
    }
  }
  incomingChar = "";
  return;
}

void sendText()
{


  SIM7000.print("AT+HTTPINIT\r");
  delay(1000);
  getResponse();
  SIM7000.print("AT+HTTPPARA=\"CID\",1\r");
  delay(1000);
  getResponse();
  SIM7000.print("AT+HTTPPARA=\"URL\",\"http://engt62k9mgbgo.x.pipedream.net\"\r");
  delay(1000);
  getResponse();
  SIM7000.print("AT+HTTPPARA=\"CONTENT\",\"text/plain\"\r");
  delay(1000);
  getResponse();
  SIM7000.print("AT+HTTPDATA=100,5000\r\n");
  delay(100);
  getResponse();
  SIM7000.print("\"TEST MESSAGE\"\r\n");
  SIM7000.print("\r\n");
  delay(5000);
  getResponse();
  SIM7000.print("AT+HTTPACTION=1\r");
  delay(1000);
  getResponse();
  SIM7000.print("AT+HTTPTERM\r");
  getResponse();
}

void getResponse()
{
  if (SIM7000.available())
  {
    while (SIM7000.available())
    {
      data = SIM7000.read();
      Serial.write(data);
    }
    data = 0;
  }
}
//NOV 3 2020, note: need to include AT command AT+CMGD=0,4 for routinely deleting messages
//message storage limited and not automatically overwritten

I've included the output on the Serial monitor below.

Output on Serial Monitor

I'm thinking that the problem lies between

SIM7000.print("AT+HTTPDATA=100,5000\r\n");

and

SIM7000.print("AT+HTTPACTION=1\r");

You can see on the output where it prompts for DOWNLOAD, but the "TEST MESSAGE" that I try to insert here doesn't go. I've also attached a screenshot of the RequestBin output to show that it's showing up as an empty POST with nothing but headers and a content length of 0. Output in RequestBin

When I've done similar POST requests using the same SIM7000A module with the same AT commands on PuTTY via USB, all I have to do is enter my text during the time specified in the AT+HTTPDATA command and hit enter. Whatever I enter during that time as it says "DOWNLOAD" shows up as the body of my POST. But I have as of yet been unable to do the same using the Uno. Any help you can offer is much appreciated.

First of all, you must to send CR NL char after all AT commands...

SIM7000.print("AT+HTTPDATA=100,5000\r\n");

Note: \r and \n = CR and NL respectively.

Second, try the steps below:

// SEND HTTP PROCESS
  SIM7000.println("AT+HTTPINIT\r\n");
  delay(1500);
  Serial.println(SIM7000.readString());
  SIM7000.println("AT+HTTPPARA=\"CID\",1\r\n");
  delay(1500);
  Serial.println(SIM7000.readString());
  char httpSend[105] = "[URL]";
  SIM7000.println(httpSend + String("\r\n"));
  delay(1500);
  Serial.println(SIM7000.readString());
  SIM7000.println("AT+HTTPSSL=1\r\n"); // THIS LINE FOR SSL URL
  delay(1500);
  Serial.println(SIM7000.readString());
  SIM7000.println("AT+HTTPACTION=0\r\n"); //GET REQUEST = 0 | POST REQUEST = 1
  delay(7000);
  Serial.println(SIM7000.readString());
  SIM7000.println("AT+HTTPREAD\r\n");
  delay(1500);
  Serial.println(SIM7000.readString());
  SIM7000.println("AT+HTTPTERM\r\n");
  delay(1500);
  Serial.println(SIM7000.readString());

Try adding ctrl-z Character (decimal 26) to the end of the data buffer you are sending after seeing DOWNLOAD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM