CAT1 module EC800M HTTP usage summary record

分享记录一下 CAT1 模块EC800 HTTP 协议使用流程  ...... by 矜辰所致

foreword

The CAT1 module EC800 has been used recently, and the project requirement is to use the CAT1 module to report data to the server through the HTTP protocol.

Previous projects used the MQTT protocol a lot, but they didn’t have a deep understanding of HTTP. The purpose of writing this article is to record the usage process of the EC800 HTTP protocol, and to provide you with an example of how to quickly make the product normally designed without a deep understanding of the HTTP protocol. ヾ(◍°∇°◍)ノ゙!

Because it involves issues related to mobile communication networks, for some professional basic knowledge, some previous blog posts will be quoted in this article, and the references in the article will give the original link.

I am caused by Jinchen, with the same name on the whole network, try my best to write each series of articles well, not exaggerating, not compromising, and we who are serious about learning knowledge, caused by Jinchen, gold and stone are open!

1. Basic description

When receiving the project requirements, you have to have a certain understanding of some basic background knowledge as soon as possible, such as what is CAT1? The basic application information of the module, the relevant knowledge of HTTP and so on.

1.1 CAT1 and 4G

The full name of Cat.1 is LTE UE-Category1. In 2009, Cat.1-5 was specially divided by 3GPP for the future IoT application market. The ultimate goal of Cat.1 is to serve the IoT and realize low-power and low-cost LTE connections.

LTE English "Long-Term Evolution", the Chinese name is long-term evolution technology. It is a wireless network standard for mobile communication and one of the main standards of 4G (fourth generation) mobile communication technology.
UE English "User Equipment" refers to the user terminal, which is the classification of the wireless performance of the user terminal equipment under the LTE network. 3GPP uses Cat.1~20 to measure the wireless performance of user terminal equipment, that is, to divide the terminal rate level.

Cat.1 belongs to the 4G series and can completely reuse existing 4G resources. Cat.1 is the user terminal level configured as the lowest version parameter, which allows the industry to design low-end 4G terminals at low cost.

With the development of the Internet of Things, Cat.1 is becoming more and more important in the field of Internet of Things. Compared with NB-IoT and 2G modules, Cat.1 has advantages in network coverage, speed and delay. Compared with traditional Cat.4 modules, it has the advantages of low cost and low power consumption.

1.2 EC800M module

EC800M-CN is an ultra-small LTE Cat 1 wireless communication module specially designed by Quectel for the M2M and IoT fields, supporting a maximum downlink rate of 10 Mbps and a maximum uplink rate of 5 Mbps.

For the module, Quectel will provide you with a complete set of explanatory materials, including hardware design, software design, and use process. If you know something about this field, it will basically take nothing to use it, and you don’t need to look for additional information at all.

insert image description here

1.3 HTTP

If you understand HTTP and some basics of 4G network, then you can directly use the AT command directly according to the manual of the above module.

If you haven’t learned about it, it really takes time for the flower shop to understand, here is an article recommended:

HTTP Super Detailed Tutorial

For our application, there are several places we must understand, the first HTTP protocol format, such as the HTTP protocol format (the picture is quoted from the above recommended article):

HTTP request format:

insert image description here
HTTP response format:
insert image description here

HTTP protocol format:

insert image description here

The GET method is used to obtain the data on the server. For our project, we only need to use the HTTP POST method. The data to be transmitted by the POST request is generally transmitted through the body.

For example, an example of a POST request to enter a username and password to the server (the picture is quoted from the above recommended article):

insert image description here

For us to use the CAT1 module, what we finally need to do is to assemble a data packet similar to the above content in the program and send it out.

Of course, in general, in IoT situations, the package required for sensor reporting data is much simpler than the above.

2. Start using

After understanding the basic knowledge, the next step is to start testing and using it.

2.1 Hardware design part

The hardware part basically follows the official documentation. It’s not difficult at all. There’s really nothing to say, so it’s omitted. For details, please refer to "Quectel_EC800M-CN_Hardware Design Manual_V1.1.pdf" (different types of chips have different corresponding manuals, just follow the manual).

Symbolic previous schematic:

insert image description here

2.2 Module power-on process

Although the official module will provide documentation to explain the use process of HTTP commands, as shown in the following figure:

insert image description here

But we generally don’t use it as soon as it is powered on. To put it simply, you must first ensure that the module communicates normally and is attached to the network normally. Then there will be a module initialization process in our normal use (although the module will automatically connect to the Internet when everything is normal).

We need to go through some AT command interactions after power-on to ensure that the module is connected to the normal network, and then we can start to follow the process in the above manual.

step instruction illustrate
1 AT Serial communication basic test
OK Return "OK" means the communication with the module is normal
2 AT+CPIN? Check if the SIM card is inserted
+CPIN: READY OK Return "READY" to indicate successful card reading
3 AT+CSQ Query signal quality
+CSQ: 23,99 OK Parameter 1: Signal quality (0~31), generally fishing 8 is less than 31. Parameter 2: Ignore
4 AT+CEREG? Query network registration status
+CEREG: 0,1 OK Parameter 2: 1 means the registration is successful
5 AT+CGATT? Query the network attachment status
+CGATT: 1 OK 1 means success, 0 means failure

The above can be used as a means of checking when the module is powered on. After completing the above steps, you can start the following operations. In the program, we can regard these operations as the initialization of the module.

At the end of the above instructions, a carriage return is required. When defining a string in the program, remember to add

insert image description here

The test code is as follows:

void ec800_init()
{
    
    
    u16 cat1_timeout = 0;

    while(Iot_SendCmd(AT,"OK", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            printf(" uart false\r\n");
            return;   
        }
    }
    cat1_timeout = 0;
    printf("uart ok\r\n");
    while(Iot_SendCmd(CPIN,"READY", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("simcard ok\r\n");
    while(Iot_SendCmd(RSSI,"+CSQ", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    while(Iot_SendCmd(CEREG,"0,1", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    printf("网络注册 ok\r\n");
    cat1_timeout = 0;
    while(Iot_SendCmd(CGATT,"+CGATT: 1", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    printf(" 网络附着 ok\r\n");
}

2.3 PDP context

When we read the document, there will be a concept of PDP context, here we need to explain this concept.

PDP, Packet Data Protocol, packet data protocol, a protocol used by mobile communication users when sending and receiving packet data, applied to packet networks such as GPRS and WCDMA.
.
PDP context is the PDP context, which saves all information of user plane tunnel forwarding, including RNC / GGSN user plane IP address, tunnel ID and QoS, etc.

In fact, if you don’t understand the network and read the basic concepts above, you still don’t understand it, so I will briefly explain it in vernacular:

That is to say, we need to use PDP (Packet Data Protocol) to transmit data, so we must establish a background for him, the so-called context, you have to tell SGSN and GGSN (what are SGSN and GGSN, look below, they are similar to transfer stations, gateways) some basic information: local identifier (cid), pdp_type you want (here is IP), your access point information.

Different operators of access points are different:
Unicom: UNINET Mobile: CMNET Telecom: CTNET

We do not use professional terms to understand PDP context. Simply put, PDP context is some basic configuration that must be done before you are ready to connect to the Internet to transmit data. A communication card will be sent to you, and you can transmit data later. Activating a PDP context means initiating a packet data service call.

2.3.1 What are SGSN and GGSN?

I mentioned SGSN and GGSN when I introduced PDP above. Regarding this piece, I also refer to previous blog posts:

Working principle of mobile data communication network (SGSN&GGSN)

The picture is quoted from the recommended blog post above:

insert image description here

The content is quoted from the recommended blog post above:

SGSN
SGSN is mainly used to deliver data packets for mobile stations within its geographic range, which is equivalent to a routing node in a wireless network. It can perform packet routing and forwarding, mobility management (attach, detach and location management), logical link management, authentication and charging functions. The location register of SGSN saves location information, such as the current cell.
The main functions of the SGSN include:
1. Complete the communication with the GGSN, transmit the user data to the GGSN through the GTP protocol, and transmit the data returned by the GGSN to the user.
2. Perform mobility management when the geographical location of the user changes.
GGSN
GSGN, as the gateway of the entire GPRS/UMTS network, is located between the GPRS network and the external packet switching network (Internet). The role of the gateway is to convert data in one protocol format into data in another format.
GGSN converts GPRS data packets from SGSN into appropriate packet data protocol format, such as IP, and then sends them to corresponding packet data network, such as wide area wired network. vice versa.
The difference between SGSN and GGSN
Therefore, the main difference between GGSN and SGSN is that GGSN, as a gateway, converts protocols in different communication networks, while SGSN, as a router, only sends, receives and delays its data packets in a network using the same protocol.
In addition, GGSN can implement address conversion, such as converting the internal address of the wireless network (PDP address) into a packet data network protocol address (IP address), while SGSN can only implement PDP address mapping, that is, according to one address, it is mapped to another address of the same type. It can be seen that the IP addresses of the 3G and 4G networks we often talk about are actually the egress IP addresses corresponding to GGSN.

At this point, some basic issues that need to be understood when starting to use have been explained, so the use of HTTP can be directly started below.

3. HTTP process

The above preparatory work is done, we will proceed to the following.

3.1 Client

The client is actually our board, and we have given the EC800M power-on initialization process above.

After completing the above initialization, we can proceed according to the official manual. In this part, we mainly use illustrations and actual test codes to give instructions.

Let's review the above process (here we use the example diagram in the official document to illustrate):

insert image description here

The above process is a simple example given by the official, in general, just follow the order.

It should be noted that there is a parameter setting sample that is not included, that is, the data type. The data type is actually related to the server that everyone needs to POST. This requires you to understand what data type the server needs:

insert image description here

For example, in this test, I used "AT+QHTTPCFG=\"contenttype\",1\r\n"is set to "text/plain"type.

The above is still used to AT+QIACT=1activate the PDP context, but it is also mentioned in the figure that it is activated by default. I found that if it is activated, using this command will return ERROR (to be determined).

There is no problem with the others, so let's go directly to the code here.

3.1.1 PDP context configuration

void ec800_pdp_prepare(){
    
    
    u16 cat1_timeout = 0;
    while(Iot_SendCmd("AT+QHTTPCFG=\"contextid\",1\r\n","OK", 200)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("设置 QHTTPCFG ok\r\n");

    while(Iot_SendCmd("AT+QIACT?\r\n","OK", 3000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("PDP_CHECK one ok\r\n");

    
    while(Iot_SendCmd("AT+QHTTPCFG=\"contenttype\",1\r\n","OK", 3000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("CFG ok\r\n");

    // while(Iot_SendCmd("AT+QHTTPCFG=\"rspout/auto\",1\r\n","OK", 3000)){
    
    
    //     HAL_Delay(1);
    //     cat1_timeout ++;
    //     if(cat1_timeout >= 2000){
    
    
    //         return;   
    //     }
    // }
    // cat1_timeout = 0;
    // printf("auto header ok\r\n");

    /*
    "AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n"
    APN 联通:UNINET   移动:CMNET   电信:CTNET
    */
    while(Iot_SendCmd("AT+QICSGP=1,1,\"CMNET\",\"\",\"\",1\r\n","OK", 3000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("PDP_CONFIG ok\r\n");
    while(Iot_SendCmd("AT+QIACT?\r\n","+QIACT", 3000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("PDP_CHECK two ok\r\n");

    // while(Iot_SendCmd("AT+QIACT=1\r\n","OK", 500)){
    
    
    //     HAL_Delay(1);
    //     cat1_timeout ++;
    //     if(cat1_timeout >= 2000){
    
    
    //         return;   
    //     }
    // }
    // cat1_timeout = 0;
    // printf("PDP_激活 ok\r\n");
}

3.1.2 URL Settings

The next step is to set the URL. Where the URL comes from is provided by the server. For example, the description of HTTP on the ONENET platform is as follows:

insert image description here

Here, the URL we set in POST is the Address+URL above, which corresponds to this parameter in the following program char *url:

void http_set_url(char *url)
{
    
    
    u16 cat1_timeout = 0;
    char message[32];
    snprintf(message, sizeof(message), "AT+QHTTPURL=%d,%d\r\n", strlen(url), 5);
    while(Iot_SendCmd(message,"CONNECT", 1000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("ready to send url\r\n");
    while(Iot_SendCmd(url,"OK", 5000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("url set OK\r\n");
}

So far, the overall call flow in our program is shown in the following figure:

insert image description here

3.1.3 POST request

After the above URL setting is completed, we can send POST requests at any time. For example, we are a sensor device that collects sensor data periodically. When the time set by ourselves is reached, we can directly POST. The following is the implementation function of the POST request:


void http_post_message(const char *message) {
    
    
    int length = strlen(message);
    char at_post[32];
    u16 cat1_timeout = 0;
    snprintf(at_post, sizeof(at_post), "AT+QHTTPPOST=%d,%d,%d\r\n", length, 5, 10);
    while(Iot_SendCmd(at_post,"CONNECT", 500)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("ready to send post message!\r\n %s\r\n", message);
    while(Iot_SendCmd(message,"OK", 5000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("http post OK\r\n");
    //"AT+QHTTPREAD=1\r\n"
    while(Iot_SendCmd("AT+QHTTPREAD=5\r\n","+QHTTPREAD", 1000)){
    
    
        HAL_Delay(1);
        cat1_timeout ++;
        if(cat1_timeout >= 2000){
    
    
            return;   
        }
    }
    cat1_timeout = 0;
    printf("HTTPREAD OK\r\n");
}

Of course, the above parameters const char *messageneed to be packaged by ourselves. Generally speaking, JSON format is used more often. For example, during the test, my program is as follows:

insert image description here

Finally, the function called in the above process Iot_SendCmdis also up:

int Iot_SendCmd(const char* cmd, char* reply, int wait)
{
    
    
	u8 i=0;
    char* rss_str;
    int rssi,res;
    CLEAR_EC800_Buffer(EC800_RX_Data);

    Uart3_sendBuffer((u8*)cmd,strlen(cmd));
    
    while(EC800ReceiveState != true)
    {
    
    
        HAL_Delay(1);
        i++;
        if(i >= wait){
    
    
            printf("cat1 check out\r\n");
            return 0xFF;   
        }
    }
    EC800ReceiveState = false;

    if (!strcmp(reply,"+CSQ"))
    {
    
    
       rss_str = strstr((char*)EC800_RX_BUF, "+CSQ:");
        if (!rss_str) {
    
    
            return 1;
        }

        sscanf(rss_str, "+CSQ:%d,%d", &rssi, &res);
        if (rssi != 99) {
    
    
            printf("RSSI is %d\r\n",rssi);
            CLEAR_EC800_Buffer(EC800_RX_Data);
            return 0;
        }
    }
    
    else if (strstr((char*)EC800_RX_BUF, reply)){
    
      
        printf("\r\n%s\r\n", EC800_RX_BUF);
        CLEAR_EC800_Buffer(EC800_RX_Data);
        return 0;
    }

    return 1;  
}

3.1.4 Precautions

(This is to be updated, and some detailed explanations need to be added later)

1. About fonts

The first thing to pay attention to is the font, which must match the font of the server;

By "AT+QHTTPCFG=\"contenttype\",1\r\n"setting the font, this has been mentioned above;

2. About HTTP response

The command AT+QHTTPCFG="responseheader",1 is to enable the output of HTTP response header information:

insert image description here

If enabled, use AT+QHTTPREADthe read HTTP response message as follows:

insert image description here

At this time, there are many returns, and the response is more than 600 bytes, which is close to 700 bytes. Here, you need to consider the size of the serial port buffer when writing programs.

Without this enabled, the read HTTP response message is as follows:

insert image description here

Returns less than 200 bytes at this time

3. About the processing of the data returned by the cat1 module

This article uses the STM32F103 chip, and we need to pay attention to the serial port processing of AT commands. Generally speaking, for ordinary AT commands, we can directly use the IDLE interrupt to determine whether the reception is complete. The program processing is as follows:

insert image description here

But for AT+QHTTPPOSTand AT+QHTTPREAD, what they return is not a frame of data, but segmented data. If the above processing is used, there will be problems, so we need to process these two instructions separately:

	/*省略*/
 	Uart3_sendBuffer((u8*)cmd,strlen(cmd));
    
    /*
    此处串口回的不止是一帧数据,所以使用 IDLE 中断不合适
    */
    if ((!strcmp(reply,"+QHTTPREAD:"))||(!strcmp(reply,"+QHTTPPOST:"))){
    
    
        //读取和发送的处理,直接等一段时间
        HAL_Delay(1000);// 500 600 800 1000 一直加大   
    }
    /*
        另外的设置指令大多都是等待一个 OK 返回,属于一帧数据
        所以可以用 IDLE 中断
    */
    else{
    
    
        while(EC800ReceiveState != true)
        {
    
    
            HAL_Delay(1);
            i++;
            if(i >= wait){
    
    
                printf("cat1 check out\r\n");
                return 0xFF;   
            }
        }
    }
    EC800ReceiveState = false;

Of course, in fact, for the AT command module, because it will not send messages irregularly, in fact, all the return results after sending AT commands can be processed without IDLE interruption. After sending AT commands, just wait for a certain period of time to read the serial port data.

3.2 Server side

On the server side, you can choose some cloud platforms that are commonly used now, such as Alibaba Cloud, ONENET, Huawei Cloud, etc. supported by the Internet of Things model. The server side only needs to perform some basic product creation and configuration, and then follow the above process according to the development documents provided by the platform, for example:

insert image description here

This test uses the company's own server, and colleagues from the software department are responsible for it, so the server-side configuration process is omitted.

epilogue

The original purpose of writing this article is just to record the usage steps of EC800M HTTP, but because some basic knowledge of communication involved has been reviewed by myself, so the final content is still more than expected. In addition to explaining the module HTTP test process to everyone, I also mentioned some content related to network communication, and I also gained a lot.

The code provided in this article is your own test code. As for the actual project use in the later stage, you need to improve it according to your own needs.

Well, that's all for this article, thank you all!

Guess you like

Origin blog.csdn.net/weixin_42328389/article/details/131262687