Software
Waspmote library
Waspmote NB-IoT / Cat-M library
The files related to the NB-IoT / Cat-M module library are:
/BG96/WaspBG96.h
/BG96/WaspBG96.cpp
It is mandatory to include the NB-IoT / Cat-M library when using this module. So the following line must be added at the beginning of the code:
Class constructor
To start using the Waspmote NB-IoT / Cat-M library, an object from the WaspBG96
class must be created. This object, called BG96
, is already created by default inside the Waspmote NB-IoT / Cat-M library. It will be used along this guide to show how Waspmote works.
When using the class constructor, all variables are initialized to their default values.
API functions
Through this guide there are lots of examples, showing the use of functions. In these examples, API functions are called to execute the commands, storing in their related variables the parameter value in each case. The functions are called using the predefined object BG96.
All public functions return different possible values:
0: OK
Otherwise: ERROR. See corresponding function error code.
Switching on
The ON()
function switches on the NB-IoT / Cat-M module and it opens the MCU's UART bus for communicating with the module. After this step, the module will be able to receive commands to manage it.
Example of use:
Switching off
The OFF()
function allows the user to switch off the NB-IoT / Cat-M module and close the UART. This function must be called in order to save battery when the module is not going to be used.
Example of use:
SIM card
Entering PIN
The enterPIN()
function allows the user to enter the PIN (Personal Identification Number) of the SIM (Subscriber Identification Module) card. If the SIM card has no PIN (or the PIN was disabled on the SIM card), it is not necessary to use this function.
Example for entering the PIN:
Besides, there is another function prototype in order to set a new one. It is mandatory to specify the current PIN number and the new one.
Example for setting a new PIN:
Example of entering the PIN number: https://development.libelium.com/nb-iot-01-enter-pin-code/
Getting module information
The getInfo()
function can get more than one information field to the module. This function needs one input to indicate the type of information requested. The resulting information is stored in _buffer
, and _length
is the number of bytes in the buffer. The information possibilities are:
WaspBG96::INFO_HW
: To request the hardware revisionWaspBG96::INFO_MANUFACTURER_ID
: To request the manufacturer identifierWaspBG96::INFO_MODEL_ID
: To request the model identifierWaspBG96::INFO_REV_ID
: To request the firmware revisionWaspBG96::INFO_IMEI
: To request the IMEI (International Mobile Station Equipment Identity), which is the unique identifier of the module, similar to a MAC addressWaspBG96::INFO_IMSI
: To request the IMSIWaspBG96::INFO_ICCID
: To request the ICCID
Examples of use:
Related variables:
BG96._buffer
→ Buffer which stores the information requested
BG96._length
→ Number of bytes in buffer
Example of getting module info: https://development.libelium.com/nb-iot-02-get-module-info/
Setting operator parameters
When the NB-IoT / Cat-M module uses data services like TCP/UDP connections, HTTP services, SMTP or FTP transfers, it is mandatory to configure the parameters provided by the user's Mobile Network Operator (MNO): APN, login and password. The owner of a SIM should be notified with these parameters by the MNO.
The set_APN()
function allows the user to save these parameters into Waspmote memory. Later, when data connection functions are called, Waspmote will configure these parameters into the NB-IoT / Cat-M module.
Example of use:
The show_APN()
function allows the user to display the current settings stored in Waspmote's memory which are used by the libraries when data connections are performed.
Example of use:
Related variables:
BG96._apn
→ Stores the APN name
BG96._apn_login
→ Stores the APN login
BG96._apn_password
→ Stores the APN password
Checking network connection status
There are 2 functions to check the network connection status: checkConnection()
and checkDataConnection()
.
The checkConnection()
function checks the module's network connection status and returns whether the module:
is connected to a network
is not connected to a network
is searching for a new operator to register to
registration was denied
This function will wait for the module to be connected to a network for the specified time in second units.
Example of use:
Possible error codes for this function:
1: not registered, the Mobile Equipment (ME) is not currently searching for a new operator to register to 2: not registered, but ME is currently searching for a new operator to register to 3: registration denied 4: unknown
The checkDataConnection()
function checks the module's network connection status, connects the module to data service and returns whether the module:
is connected to data service
is not connected to a network
is searching for a new operator to register to
registration was denied
This function will wait for the module to be connected to a network for the specified time in second units.
Example of use:
Possible error codes for this function:
1: not registered, ME is not currently searching for a new operator to register to 2: not registered, but ME is currently searching for a new operator to register to 3: registration denied 4: unknown 6: not registered, ME is not currently searching for a new operator to register to 8: not registered, but ME is currently searching for a new operator to register to 9: registration denied 10: unknown 12: if error setting APN 13: if error setting login 14: if error setting password 15: if error activating GPRS connection
Example of getting network information: https://development.libelium.com/nb-iot-03-get-network-info/
Basic network connection to NB-IoT
To demonstrate a basic connection through the NB-IoT network, the 1^st^ step is to configure the connection using the function nbiotConnection()
.The APN, the desired band and the network operator should be provided as arguments. Each band has been defined as a constant in the header file.
Then, use the contextActivation()
function to connect the network.
Example of basic NB-IoT connection: https://development.libelium.com/nb-iot-04a-basic-connection-nb-iot/
Basic network connection to Cat-M
In the same way as for NB-IoT connection, to demonstrate a basic connection through the Cat-M network, the 1st step is to configure the connection with lteM1Connection()
function and then connect with contextActivation()
.
Example of basic Cat-M connection: https://development.libelium.com/nb-iot-04b-basic-connection-cat-m/
Basic network connection to EGPRS
Moreover, the EGPRS link can be also tested with GPRSConnection()
function.
Example of basic EGPRS connection: https://development.libelium.com/nb-iot-04c-basic-connection-egprs/
HTTP client
HTTP connections
HTTP is a great protocol because it is a standard, simple and light way to send information to web servers.
Libelium has created a little web service in order to allow 4G, 3G, GPRS, GPRS+GPS, NB-IoT / Cat-M or WiFi modules to test the HTTP mode. This web service is a little code, written in PHP, which is continuously listening to the HTTP port (port number 80) of our test server "pruebas.libelium.com". This is a kind of RESTful service. These communication modules can send HTTP instances to our web service.
HTTP instances should have the following structures so that our web service can understand.
GET method
In GET method the data are sent to the server append to the main URL with the '?' character. The base sentence to perform GET method is shown below:
pruebas.libelium.com/getpost_frame_parser.php?&&<...>& view=htmlium.com/getpost_frame_parser.php?&&<...>& view=html
getpost_frame_parser.php?
: It is the main URL, where the web service is running.variable1=value1
: It is a couple with the variable name and value which we want the web service to parse.view=html
: It is an optional argument. It shows a "pretty" response (HTML formatted).
All arguments must be separated by "&". The variable name and value must be separated by "=".
Some examples:
pruebas.libelium.com/getpost_frame_parser.php?var1=3.1415
pruebas.libelium.com/getpost_frame_parser.php?var1=3.1415&view=html
pruebas.libelium.com/getpost_frame_parser.php?var1=3.1415&var2=123456&var3=hello&view=html
POST method
Unlike GET method, with POST method the data are sent to the server into an extra data field. The URL only includes the site name and the PHP direction:
pruebas.libelium.com/getpost_frame_parser.php
The data field is very similar as the used in GET method:
<variable1=value1>&<variable2=value2>&<...>&view=html
Where:
<variable1=value1>
: It is a couple with the variable name andvalue which we want the web service to parse.
All arguments must be separated by "&". The variable name and value must be separated by "=".
Some examples of data field:
pruebas.libelium.com/getpost_frame_parser.php?variable1=3.141592 pruebas.libelium.com/getpost_frame_parser.php?var1=3.1415&var2=123456&var3=hello
Server response
If the web service receives one instance with the appropriate format, some actions will happen:
The web service grabs the string and parses it. So the PHP code creates couples with the variables name and value.
The web service responses to the sender device (to the sender IP) with an HTML-formatted reply.
Remember this PHP code is really simple and is offered with the only purpose of testing, without any warranty. The source code is available here:
downloads.libelium.com/waspmote-html-get-post-php-parser-tester.zip
The user may find it interesting to copy this code and make it run on his own server (physical or virtual). If the user wants to go further, he can complete the code. For example, once the couples are parsed, the user can modify the PHP to save data into a txt file, or insert couples into a database, or include a timestamp...
HTTP request methods
The http()
function configures HTTP parameters, performs the request selected by the user and handles the data returned from the server.
This function needs several parameters to work properly depending on the method used. The 1^st^ parameter required by the function is the request method. User can choose among 5 methods: GET, HEAD, DELETE, POST and PUT:
WaspBG96::HTTP_GET
WaspBG96::HTTP_HEAD
WaspBG96::HTTP_DELETE
WaspBG96::HTTP_POST
WaspBG96::HTTP_PUT
After choosing the method, the function needs the host URL, port and resource of the HTTP server requested. The data field is only necessary when POST or PUT methods are performed.
Example of use (GET, HEAD and DELETE methods):
Example of use (POST and PUT methods):
Once the request has been sent, the function waits for data from the server and stores it in _buffer
. It also stores the HTTP status code from the server in _httpCode
.
Related variables:
BG96._httpCode
→ Stores the HTTP code from the server
BG96._buffer
→ Stores data received from server
BG96._length
→ Stores data length
Example of HTTP GET: https://development.libelium.com/nb-iot-06-http-get/
Example of HTTP POST: https://development.libelium.com/nb-iot-07-http-post/
Sending Waspmote frames to Meshlium via HTTP
Since Meshlium Manager System v4.0.9, HTTPS method is the default method for sending data. HTTPS is the recommended technology because it provides many cyber security services. Therefore, the HTTPS service is always enabled on Meshlium.
However, Meshlium Manager System v4.1.0 and greater versions allow the user to enable the HTTP service in order to be able to receive HTTP non-secure requests. The user must go to: Manager System → System → Security → HTTP Service:
The sendFrameToMeshlium()
function has been developed to send Waspmote frames from Waspmote to Meshlium via non-secure HTTP request. Meshlium will parse (chop) the frame and will store it in its internal MySQL database. This function requires the following parameters:
Meshlium's IP address
Meshlium's remote port
Data to send:
frame.buffer
will be used from the generated frameData length:
frame.length
will be used from the generated frame
After calling the function, the response from Meshlium will be stored in _buffer
. Besides, it will store the HTTP status code from server in _httpCode
. Please refer to the Data Frame Guide in order to know more about how to create sensor frames with Waspmote libraries.
Example of use:
Related variables:
BG96._httpCode
→ Stores the HTTP code from the server
BG96._buffer
→ Stores data received from server
BG96._length
→ Stores data length
frame.buffer
→ Stores data frame that will be sent to Meshlium
frame.length
→ Stores data frame length
Sending Waspmote frames to Meshlium via HTTPS
Since Meshlium Manager System v4.0.9, HTTPS is the default method for sending data.
For HTTPS, the user must keep in mind that the Meshlium's certificate has to be installed in the Waspmote or Plug & Sense! radio prior to opening secure connections. The next picture shows how the user can download the Meshlium's certificate from Manager System → System → Users Manager → Download Certificate:
The downloaded certificate must be installed following the steps explained in the "SSL sockets" section and the proper library function. Also, the example linked at the end of this section shows how to perform the installation.
Example of sending frames to Meshlium via HTTPS: https://development.libelium.com/nb-iot-08b-sending-frames-to-meshlium-via-https/
Making TCP/UDP connections
Socket identifiers
The NB-IoT / Cat-M module permits to have up to 6 simultaneous TCP/UDP connections. For that purpose, the library defines the following socket identifiers to be used when handling the multi-socket connections:
WaspBG96::CONNECTION_1
WaspBG96::CONNECTION_2
WaspBG96::CONNECTION_3
WaspBG96::CONNECTION_4
WaspBG96::CONNECTION_5
WaspBG96::CONNECTION_6
The NB-IoT / Cat-M library defines different structures in order to store the information related to all socket identifiers. After opening sockets or sending/receiving data, the structures are updated. So this is useful in order to manage the most important settings of the connection.
Socket status structure
The SocketStatus_t
structure stores the status for all sockets. For each one of the connections, the status structure includes:
Socket identifier
Current socket status. The API defines several constants to describe it:
WaspBG96::STATUS_CLOSED
WaspBG96::STATUS_ACTIVE
WaspBG96::STATUS_SUSPENDED
WaspBG96::STATUS_SUSPENDED_DATA
WaspBG96::STATUS_LISTENING
WaspBG96::STATUS_INCOMING
WaspBG96::STATUS_OPENING
Local IP address
Local port
Remote IP address
Remote port
As it is possible to have up to 6 simultaneous connections, the global variable is defined as follows:
SocketStatus_t socketStatus[6];
The definition of the structure is:
The getSocketStatus()
function allows the user to update the socket status structure from the NB-IoT / Cat-M module. It is mandatory to indicate the identifier of the socket to be updated. It is possible to update all socket status by calling the getAllSocketStatus()
function which is faster than iterating through all different identifiers.
Example of use:
Related variables:
BG96.socketInfo[socketId].id
→ Socket identifier
BG96.socketInfo[socketId].state
→ Socket status
BG96.socketInfo[socketId].localIp
→ Local IP address
BG96.socketInfo[socketId].localPort
→ Local port
BG96.socketInfo[socketId].remoteIp
→ Remote IP address
BG96.socketInfo[socketId].remotePort
→ Remote port
Creating a TCP/UDP client socket
The openSocketClient()
function configures and opens a socket. This function expects several input parameters:
Socket ID: The 1st parameter indicates the identifier to be associated to the new TCP/UDP connection. This identifier is needed in order to send or receive data through the corresponding socket after creating it.
Protocol: This parameter indicates whether use TCP or UDP protocol for the new socket. The possibilities are:
WaspBG96::TCP
WaspBG96::UDP
Host: Address of the remote host, string type. This parameter can be either:
Any valid IP address in the format: "xxx.xxx.xxx.xxx"
Any host name to be solved with a DNS query
Any valid IPv6 address in the format: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx or xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx.xxx
Remote port: Remote host port to contact from 1 to 65535.
Local port: Parameter is valid for UDP connections only and has no effect (if used) for TCP connections. UDP connections local port from 1 to 65535.
Example of creating a TCP client connection:
Example of creating a UDP client connection:
Possible error codes for this function:
1: not registered, ME is not currently searching for a new operator to register to 2: not registered, but ME is currently searching for a new operator to register to 3: registration denied 4: unknown 6: not registered, ME is not currently searching for a new operator to register to 8: not registered, but ME is currently searching for a new operator to register to 9: registration denied 10: unknown 12: if error setting APN 13: if error setting login 14: if error setting password 15: if error activating GPRS connection 16: if error getting socket status 17: Socket with an active data transfer connection 18: Socket suspended 19: Socket suspended with pending data 20: Socket listening 21: Socket with an incoming connection. Waiting for the user accept or shutdown command. 22: Socket in opening process. The socket is not closed but still not in Active or Suspended. 23: if error in Socket Configuration 24: if error in Socket Configuration Extended 3 25: if error sending the open command 26: if timeout opening the socket
Sending data
The send()
function allows the user to send TCP/UDP packets once the socket is active. The function needs 2 different inputs parameters:
Socket ID: the socket identifier used for opening the connection.
Data: This is the stream of data to send to the TCP/UDP socket. This stream of data can be defined as a simple string message. On the other hand, the data can be defined as an array of bytes, specifying a 3rd input for the length of the array of bytes to send.
Example for sending a string message:
Example for sending an array of bytes:
Possible error codes for this function:
1: if error checking socket status 2: if incorrect socket status 3: if error sending data 4: if error waiting confirmation from module 5: if error getting socket status 6: if timeout getting socket status
All examples related to TCP/UDP sockets (both client and server) show how to send data: https://development.libelium.com/nb-iot-11-tcp-client/ https://development.libelium.com/nb-iot-13-udp-client/
Receiving data
The receive()
function allows the user to receive TCP/UDP packets once the socket is active. The function needs different inputs:
Socket ID: the socket identifier used for opening the connection.
Timeout (optional input):
If no timeout input is specified, the receive function is a non-blocking function which answers if data has been received.
If the timeout is inserted as new input, the function will block until a new packet is received or time is up in the case no packet is received. This timeout must be specified in milliseconds units.
Example for instant reception:
Example for blocking reception (i.e. 30 seconds):
Related variables:
BG96._buffer
→ Pointer to the buffer where received data is stored
BG96._length
→ Length of the data received
Possible error codes for this function:
1: if no data received 2: if error getting socket info 3: if timeout waiting for data 4: if error receiving data from module 5: if error parsing length of data 6: if error reading incoming bytes
Closing a socket
The closeSocketClient()
function allows the user to close a TCP/UDP client previously open. The function needs an input parameter for the socket identifier.
The closeSocketServer() function allows the user to close a TCP/UDP server previously open. The function needs 2 inputs:
Socket ID: the socket identifier used for opening the connection.
Protocol: This parameter indicates the protocol used by the socket:
WaspBG96::TCP
WaspBG96::UDP
SSL sockets
The NB-IoT / Cat-M module includes a stack for establishing SSL sockets. For this feature, the user must keep in mind that it is necessary to install the proper security data in the module. For handling the SSL socket new functions are defined for opening the socket, sending data, receiving data and closing the socket.
Currently, for SSL sockets only one single connection is permitted. So, regarding the socket identifiers the only available option is: WaspBG96::CONNECTION_1
.
The manageSSL()
function allows the user to store, delete and read security data (Certificate, CA certificate, private key) into the non volatile memory of the module. The function expects several inputs:
Socket ID: the socket identifier used for opening the connection.
Action: the action to perform:
WaspBG96::SSL_ACTION_DELETE
: Delete data from memoryWaspBG96::SSL_ACTION_STORE
: Store data into memoryWaspBG96::SSL_ACTION_READ
: Read data from memory
Data type:
WaspBG96::SSL_TYPE_CERT
: CertificateWaspBG96::SSL_TYPE_CA_CERT
: CA certificateWaspBG96::SSL_TYPE_RSA
: RSA Private key
Data (optional): this input is needed when the user selects to store a new security data into the module's memory.
Possible error codes for this function:
1 if error setting security data 2 if error waiting module confirmation 3 if error getting security data 4 if error deleting security data 5 if invalid action input
The openSocketSSL()
function allows the user to open a remote connection via socket secured through SSL. Several inputs are needed for calling this function:
Socket ID: The socket identifier used for opening the connection
Host: Remote SSL server address
Remote port: Remote TCP port to contact from 1 to 65535.
Possible error codes for this function:
1: not registered, ME is not currently searching for a new operator to register to 2: not registered, but ME is currently searching for a new operator to register to 3: registration denied 4: unknown 6: not registered, ME is not currently searching for a new operator to register to 8: not registered, but ME is currently searching for a new operator to register to 9: registration denied 10: unknown 12: if error setting APN 13: if error setting login 14: if error setting password 15: if error activating GPRS connection 16: if error getting SSL Socket Status 17: if socket disabled 19: if socket already open 20: if error opening the socket 21: if no response from module
The sendSSL()
function allows the user to send data through a secure socket. Several inputs are needed for calling this function:
Socket ID: the socket identifier used for opening the connection.
Data: Data to send.
Possible error codes for this function:
1: if error checking socket status 2: if incorrect socket status 3: if error sending data 4: if no response from module 5: if error getting socket status 6: if timeout waiting for correct socket status
The receiveSSL()
function allows the user to receive data through a secure socket. Several inputs are needed for calling this function:
Socket ID: the socket identifier used for opening the connection.
Timeout (optional input):
If no timeout input is specified, the receive function is a non-blocking function which answers if data has been received.
If the timeout is inserted as new input, the function will block until a new packet is received or time is up in the case no packet is received. This timeout must be specified in milliseconds units.
Possible error codes for this function:
1: if no answer from module 2: if SSL socket disconnected 3: if error code from module 4: if no response from module 5: if error parsing length of received data 6: if error getting received data 7: if error waiting module confirmation
The closeSocketSSL()
function allows the user to close a secure socket. The function needs an input parameter for the socket identifier.
Example for SSL socket: https://development.libelium.com/nb-iot-15-ssl-sockets/
GNSS - Getting position
The gpsStart()
function allows the user to power on the GNSS engine. By default, the module selects the best GNSS network to acquire the position in a transparent process. However, the user is able to select the desired GNSS network if needed.
The waitForSignal()
function waits until GNSS signal is received for valid data. The input parameter defines the timeout to wait for signal in millisecond units. If the function returns a correct answer means that the GNSS attributes have been updated:
Latitude
Latitude indicator: North or South
Longitude
Longitude indicator: East or West
Time
Date
Number of satellites
HDOP: Horizontal Dilution of precision. If this value is less than 1 indicates the highest possible confidence level to be used for applications.
The convert2Degrees()
function performs the conversion from the latitude and latitude variables given by the module to degrees so it is more legible and intuitive. The input parameters must be the latitude/longitude and the corresponding indicator. The returning value is the converted value in degrees units.
The gpsStop()
function powers down the GNSS engine of the module. It is possible to switch from a SUPL session to the autonomous GNSS mode. Firstly, the GNSS feature must be stopped, and then restart with the autonomous mode.
Example of GPS modes: https://development.libelium.com/nb-iot-16-gnss/
Sleep mode
In order to save battery, the user can simply switch the module off with the OFF()
function, but she must take into account that it will take about 20 seconds until the next on process is completed (the module and the network must redo the hand-shake process). So switching off could not be really efficient, because the power consumption during that 20 seconds is not trivial. To maximize the battery saving, the module offers sleep functions in which the module is not totally switched off, but it remains in a low-power state, sending paging frames to the network from time to time to keep the link active. Getting the module ready to work from a sleep state is much faster than from off state.
Thus, depending on the application, the user may find interesting not to switch the radio off, but to put it to sleep: nodes with frequent communications (<5 minutes) will probably prefer sleep mode over off mode. The user should test both modes and decide the best choice.
By default, the module sleep mode can be enabled by the nbiotSleepMode()
function.
However, the NB-IoT / Cat-M module allows several configuration modes to minimize the power consumption. For example, the module can be configured in Power Saving Mode (PSM) using the function nbiotSettingPSM()
, and the configuration can be check with nbiotGetPSMValues()
.
A more detailed description of sleep modes will be added in this section.
Example of how to use the sleep mode: https://development.libelium.com/nb-iot-25-sleep-modes/
eDRX setup
The enhanced Extended Discontinuous Reception (eDRX) can be configured using the function nbiotSettingeDRX()
.
For the proper configuration of the eDRX feature, first of all ensure that your operator has implemented this feature in its network (as of February 2019, many operators do not offer some advanced features yet).
Example of how to use the eDRX feature: https://development.libelium.com/nb-iot-24-edrx/
Scanning network operators
In order to see the coverage of NB-IoT or Cat-M in a certain location, use the function scanOperator()
.
The list of the detected operators is stored in _buffer
, and _length
is the number of bytes in the buffer.
Related variables:
BG96._buffer
→ Buffer which stores the information requested
BG96._length
→ Number of bytes in buffer
Example of how to scan network operators: https://development.libelium.com/nb-iot-23-scan-operator/
Sending AT commands
The NB-IoT / Cat-M library allows to send custom AT commands directly to the module using the sendATcommand()
function.
The AT command to be sent, the expected answer and an estimated timeout can be also provided as arguments to make the communication more robust.
Example of sending AT commands to the module: https://development.libelium.com/nb-iot-22-manual-at-command/
Last updated