Search code examples
htmlc++carduinoat-command

How do I customize HTML headers when doing a POST using AT commands?


ADDITIONAL INFO:

I am using an Arduino Nano with the SoftwareSerial library at 9600baud to talk to a (QUECTEL M95 based) GSM2 Click module from Micro Elektronika. I want to POST data online with custom HTML headers. I can't manage to get the QUECTEL module to POST successfully when I try to add custom headers using the QHTTPPOST command.

This is the first semi-useful guide I found to the HTTP AT commands: http://www.quectel.com/UploadImage/Downlad/Quectel_WCDMA_UGxx_HTTP_AT_Commands_Manual_V1.2.pdf

Here is the official Command Manual for the QUECTEL M95: http://www.quectel.com/product/prodetail.aspx?id=7

In Summary:

1) Registration on network works fine.

2) Posting data when NOT using custom request headers works fine.

3) The QHTTPCFG command is not on the official documentation for this module, but was suggested when contacting the QUECTEL support line.

4) AT Error codes being received: CME ERROR: 3804 "HTTP get no request"

  • Mobile operator: Vodacom South Africa
  • CSQ Signal Strength: 26

Required format for M95 AT commands:

Command < CR >


MY CODE

//Set the correct URL
mySerial.print("AT+QHTTPURL=34,1\r")
mySerial.print("http://posttestserver.com/post.php\r");

RESPONSE:

OK

//Set custom request headers
mySerial.print("AT+QHTTPCFG=\"requestheader\",1\r")

RESPONSE:

OK

//Initiate post requst (length 187 characters, 60s to write, 7s to read)
mySerial.print("AT+QHTTPPOST=187,60,7\r");

RESPONSE:

CONNECT

//SEND REQUEST HEADER
mySerial.print("POST/post.php HTTP/1.1\r\n");//25 chars
mySerial.print("HOST: posttestserver.com\r\n");//26 chars
mySerial.print("Accept: */*\r\n");//13 chars
mySerial.print("User-Agent: QUECTEL_MODULE2\r\n");//29 chars
mySerial.print("Connection: Keep-Alive\r\n");//24 chars
mySerial.print("Content-Type: application/x-www-form-urlencoded\r\n");//49 chars
mySerial.print("Content-Length: 0\r\n");//19 chars
mySerial.print("\r\n");//2 chars

... MISSING: After 60 seconds there should be an OK response to indicate successful sending.

mySerial.print("AT+QHTTPREAD=5\r");

RESPONSE:

+CME ERROR: 3804


Solution

  • Using the AT TCPIP commands instead of the AT HTTP commands I was able to solve my own problem. I have now successfully posted data along with customized headers.

    Here is the code that works:

    mySerial.print("AT+QIOPEN=\"TCP\",\"64.90.48.15\",80\r"); 
    

    WAIT for "CONNECT" response

    mySerial.print("AT+QISEND\r");//
    

    WAIT for ‘>’ response

    //The HTML Header
    mySerial.print("POST /post.php?dir=WPCN HTTP/1.1\r\n");
    mySerial.print("HOST: posttestserver.com\r\n");
    mySerial.print("Accept: application/json\"\r\n");
    mySerial.print("User-Agent: QUECTEL_MODULE2\r\n");
    mySerial.print("Content-Length: 7\r\n");
    //Signifies end of Header
    mySerial.print("\r\n");
    //Data:
    mySerial.print(“[hello]\r”);
    //This is the <CTRL+Z> character which signifies end of transmission
    mySerial.print("\x1A");
    

    Response "SEND OK"

    //End Data Mode
    delay(1000);
    mySerial.print("+++");
    delay(1000);
    //Close connection
    mySerial.print("AT+QCLOSE\r");