UDP Connection with Cellular Shield/HAT

In this tutorial, we will learn how to create a UDP connection with Cellular IoT HAT and Cellular IoT App Shield.

Remote Host Details

The following table lists the remote server details.

Variable

Values

IP Address

your_ip

Port

your_port

Start a UDP Service and Send Data

Initially, it's required to configure and activate a context. In our example, we are using a Sixfab Connect SIM and the APN may be different in your case.

Before continuing on please make sure you disable the Power Save Mode: AT+CPSMS=0

Configure a Context and Activate

If you are using Super SIM from Sixfab, the following parameters are used:

Variable

Values

APN

super

Context Number

1


Send the following AT Commands using minicom by following the tutorial here.

Configure and Activate Context:

AT+QICSGP=1,1,"super","","",1 OK

Activate the context:

AT+QIACT=1 OK

Check the active context:

AT+QIACT? +QIACT: 1,1,1,"100.66.113.251" OK

“100.66.113.251” is the IP address associated to our active context. address will be different in your case.

Start a UDP Service

There are three types of data access modes available when opening a socket service.

  1. Buffer access mode
  2. Direct push mode
  3. Transparent access mode

In our example we shall use direct push mode. Please refer to the Quectel BG96 TCP/IP AT Commands Manual document for a detailed explanation of the data access modes.

AT+QIOPEN=1,2,"UDP SERVICE","127.0.0.1",0,3030,0 OK +QIOPEN: 2,0 // Start UDP service successfully

Start a UDP service, is 2 and is 1. Before using AT+QIOPEN, the host should activate the context with AT+QIACT first.

Query if the connection status of the associated context id, in our case it's 1.

AT+QISTATE=0,1 //Query if the connection status of is 1. +QISTATE: 2,"UDP SERVICE","10.7.157.1",0,3030,2,1,2,0,"usbmodem" OK

Send Data to the Remote

We will now send the text “hello" to the remote server. For this we shall use the fixed length AT+QISEND command.

AT+QISEND=2,6,“your_ip",your_port // Send 6 bytes data to remote whose IP is "your_ip" and remote port is "your_port". >

After the “>” is prompted we are ready to send the text.

>hello SEND OK +QIURC: "recv",2

Receive Data From Remote

Read the echoed text with the following command. One whole UDP packet will be outputted. There is no need to specify the read length.

AT+QIRD=2 +QIRD: 6,"your_ip",your_port hello OK
AT+QIRD=2 +QIRD: 0 //No data in buffer. OK

Close the UDP Service

To be polite we shall close the active UDP service with the following command:

AT+QICLOSE=2 OK

Ping

Ping www.baidu.com in context 1. Before pinging the destination IP address, the host should activate
the context by AT+QIACT first.

AT+QPING=1,"www.sixfab.com" OK +QPING: 0,"104.26.8.221",32,357,255 +QPING: 0,"104.26.8.221",32,397,255 +QPING: 0,"104.26.8.221",32,360,255 +QPING: 0,"104.26.8.221",32,360,255 +QPING: 0,4,4,0,357,397,368