NB IoT
This category contains functions specific to NB-IoT devices.
- Overview
- Arguments
- Return
- Examples
Sends message to NB-IoT on specified IP, port and protocol type.
Maximum length of Rx and Tx messages is 512
Bytes. Maximum execution time is limited by timeout
in milliseconds.
api.nbSend(addr, port, msg, timeout, type, nownewdatatimeout, rxport)
- addr (string) - IP address
- port (integer) - Port
- msg (string) - Message to be sent to NB-IoT
- timeout (integer) - The maximum execution time in milliseconds
- type (string) - Protocol type:
"UDP"
,"TCP"
or"DTLS"
. - nownewdatatimeout (integer, optional) - The maximum delay between data chunks on reception
- rxport (integer, optional) - Port to use for reception, if not specified, Port value is used
api.nbSend(addr, port, bufferlen, timeout, type, nownewdatatimeout, rxport)
- addr (string) - IP address
- port (integer) - Port
- bufferlen (integer) - Scratchpad (api.nbReceiveBuffer) buffer is sent in given length
- timeout (integer) - The maximum execution time in milliseconds
- type (string) - Protocol type:
"UDP"
,"TCP"
or"DTLS"
- nownewdatatimeout (integer, optional) - The maximum delay between data chunks on reception
- rxport (integer, optional) - Port to use for reception, if not specified, Port value is used
api.nbSend(addr, port, msg, timeout, type, nownewdatatimeout, rxport)
- status (integer) - Zero for success, negative for failure
- rxbuffer (string) - Received data or
nil
for failure
api.nbSend(addr, port, bufferlen, timeout, type, nownewdatatimeout, rxport)
- status (integer) - Zero for success, negative for failure
- rxbufferlen (integer) - Length of the received data, zero for failure
Example how to send a message to NB-IoT via UDP:
-- sends "test message" string to IP 185.8.239.192 on port 5566 with 6s timeout
status,answer = api.nbSend("185.8.239.192", 5566, "test message", 6000, "UDP")
Example how to send a message to NB-IoT via TCP:
-- sends "test message" string to IP 185.8.239.192 on port 5566 with 6s timeout
status,answer = api.nbSend("185.8.239.192", 5566, "test message", 6000, "TCP")
- Overview
- Options
- Arguments
- Return
- Example
Activates, configures and uses NB-IoT scratchpad buffer.
- "activate" - Activates the buffer
- "deactivate" - Deactivates the buffer
- "dump" - Dumps content of the buffer
- "set-size" - Set size of the buffer
- "get-size" - Get size of the buffer
- start (integer) - Index/location where the first byte will be written
api.nbReceiveBuffer("activate")
- option (string)
api.nbReceiveBuffer("deactivate")
- option (string)
api.nbReceiveBuffer("dump")
- option (string)
- size (integer, optional) - Size of the buffer to dump
api.nbReceiveBuffer("set-size", size)
- option (string)
- size (integer) - Size of the buffer
api.nbReceiveBuffer("get-size")
- option (string)
api.nbReceiveBuffer(start, len, toWrite)
- start (integer) - Index/location where the first byte will be written
- len (integer) - Length of data to write into the buffer
- toWrite (string) - Data to write into the buffer
api.nbReceiveBuffer("get-size")
- size (integer) - Size of the buffer
api.nbReceiveBuffer(start, len, toWrite)
- size (integer) - Size of the written data, zero for failure
- data (string) - Data written into the buffer, empty for failure
-- activate the buffer
api.nbReceiveBuffer("activate")
-- set size
api.nbReceiveBuffer("set-size", 128)
-- insert first byte
api.nbReceiveBuffer(0,1,pack.pack("b",0xF3))
-- send byte (starting from buffer index 1)
api.nbSend("185.8.239.192", 5566, 1, 6000, "UDP")
- Overview
- Commands
- Arguments
- Return
- Examples
Sends an AT command to NB-IoT module with specified timeout. Or use one of the defined commands.
api.nbAT() is a direct access to the AT command interface of the NB-IoT module. It is advanced and should be used with caution.
For a backward-compatible interface, avoid using AT commands directly. Instead, use only api.nbSend() for sending.
- "AT_DEBUG_OFF" - Turn OFF printing of AT commands
- "AT_DEBUG_ON" - Turn ON printing of AT commands
- "REINITIALIZE" - Re-initialize the NB-IoT module
- "ICCID?" - Returns ICCID value of current SIM card
- "IMSI?" - Returns IMSI value of current SIM card
- "IMEI?" - Returns IMEI value of the modem
- "DOWNLINK_MAX_PERIOD" - If there is no downlink for given value, set timeout to 30 seconds and re-initialize the module.
- "REINIT_HARD_LIMIT_RETRY" - If NB-IoT module is initialized 5 times in a row -> sleep 4 hours and then reset
- "FORCED_REINIT_DONE?" - Monitors if "DOWNLINK_MAX_PERIOD" happened
- "PSM" - Returns power saving mode network status
- "ON_TAU_EVENT" - Enable/disable on tau event
- "SLEEP_MONITOR" - Enable/disable sleep monitor
- "AT_DEBUG_ON_TX" - Turn ON printing of AT commands on transmission
- "SET_CEREG_TRESHOLD" - Set AT+CEREG switch threshold
- "TAU_BLANKING" - Set Tau blanking interval
- "NET_STATUS?" - Returns network status
- "RADIO_OFF_SLEEP" - Turn OFF radio sleep (PSM)
- "UPDATE" - Switch to SIM module update mode
- "DTLS" - Change PSK and PSK ID for DTLS
- "TIME_SYNC" - Synchronize time with SIM module (by deafult done automatically on month change)
- "TRANSPARENT" - Set transparent mode
- "UPDATE" - Switch to SIM module update mode
- "SLEEP" - Put SIM module to sleep
- "APN=..." - Set APN
- "PLMNID=..." - Set PLMN ID
api.nbAT("AT_DEBUG_OFF")
- command (string)
api.nbAT("AT_DEBUG_ON")
- command (string)
api.nbAT("REINITIALIZE")
- command (string)
api.nbAT("ICCID?")
- command (string)
api.nbAT("IMSI?")
- command (string)
api.nbAT("IMEI?")
- command (string)
api.nbAT("DOWNLINK_MAX_PERIOD", period)
- command (string)
- period (integer) - Maximum period of time in milliseconds without downlink
api.nbAT("REINIT_HARD_LIMIT_RETRY", retries)
- command (string)
- retries (integer) - Number of retries before reset
api.nbAT("FORCED_REINIT_DONE?")
- command (string)
api.nbAT("PSM", psmState, edrxState, edrxPeriod, edrxPTW, keepSocketOpen, forceRaiSingleUL, forceRel13, compensateTimeoutForInactTimer)
- command (string)
- psmState (integer) - PSM state
- edrxState (integer) - Edrx state
- edrxPeriod (integer) - Edrx Requested Value (Period)
- edrxPTW (integer) - Edrx Requested Value (PTW)
- keepSocketOpen (integer) - Keep Socket Open
- forceRaiSingleUL (integer) - Force Rai Single UL
- forceRel13 (integer) - Force Rel 13
- compensateTimeoutForInactTimer (integer) - Compensate Timeout For Inact Timer
api.nbAT("ON_TAU_EVENT", enable)
- command (string)
- enable (integer) - 1 for enable, 0 for disable
api.nbAT("SLEEP_MONITOR", enable)
- command (string)
- enable (integer) - 1 for enable, 0 for disable
api.nbAT("AT_DEBUG_ON_TX")
- command (string)
api.nbAT("SET_CEREG_TRESHOLD", treshold)
- command (string)
- treshold (integer) - AT+CEREG switch threshold
api.nbAT("TAU_BLANKING", tau)
- command (string)
- tau (integer) - Tau blanking interval
api.nbAT("NET_STATUS?")
- command (string)
api.nbAT("RADIO_OFF_SLEEP")
- command (string)
api.nbAT("UPDATE")
- command (string)
api.nbAT("DTLS", pskid, psk)
- command (string)
- pskid (string) - PSK ID, empty for deactivation
- psk (string) - PSK, empty for deactivation
api.nbAT("TIME_SYNC", force, zone)
- command (string)
- force (integer, optional) -
1
for force time sync - zone (string, optional) - Time zone:
"UTC"
or"LOCAL"
api.nbAT("TRANSPARENT")
- command (string)
api.nbAT("UPDATE")
- command (string)
api.nbAT("SLEEP")
- command (string)
api.nbAT("APN=[apn]")
- command (string)
api.nbAT("PLMNID=[plmnid]")
- command (string)
Default SIMCom AT commands
api.nbAT(command, timeout, wakeUp, success, err, tryMore, setAwakeBefore, pollBeforeCommand, setAwakeOffAfter, receiveFixedBytes)
- command (string) - AT command
- timeout (integer) - The maximum execution time in milliseconds
- wakeUp (integer, optional) - Sending 1 will make sure that module is not asleep
- success (string, optional) - Specifies a custom success message. The default is "OK"
- err (string, optional) - Specifies a custom error message. The default is "ERROR"
- tryMore (integer, optional) - Number of retries, maximum number of retries is 5, 0 - no answer awaited
- setAwakeBefore (integer, optional) - Sending 1 will set module awake before command
- pollBeforeCommand (integer, optional) - Sending 1 will test if module is listening
- setAwakeOffAfter (integer, optional) - Sending 1 will set module asleep after command
- receiveFixedBytes (integer, optional) - Number of bytes to receive
api.nbAT("ICCID?")
- iccid (string) - ICCID value of current SIM card, empty for failure
- status (integer) - Zero for success, negative for failure
api.nbAT("IMSI?")
- imsi (string) - IMSI value of current SIM card, empty for failure
- status (integer) - Zero for success, negative for failure
api.nbAT("IMEI?")
- imei (string) - IMEI value of the modem, empty for failure
- status (integer) - Zero for success, negative for failure
api.nbAT("FORCED_REINIT_DONE?")
- reinits (integer) - Number of re-initializations since reset
api.nbAT("PSM", psmState, edrxState, edrxPeriod, edrxPTW, keepSocketOpen, forceRaiSingleUL, forceRel13, compensateTimeoutForInactTimer)
- psmState (integer) - PSM state
- edrxState (integer) - Edrx state
- edrxPeriod (integer) - Edrx Requested Value (Period)
- edrxPTW (integer) - Edrx Requested Value (PTW)
- keepSocketOpen (integer) - Keep Socket Open
- forceRaiSingleUL (integer) - Force Rai Single UL
- forceRel13 (integer) - Force Rel 13
- compensateTimeoutForInactTimer (integer) - Compensate Timeout For Inact Timer
api.nbAT("ON_TAU_EVENT", enable)
- status (integer) -
1
for enabled, zero for disabled
api.nbAT("SLEEP_MONITOR", enable)
- status (integer) -
1
for enabled, zero for disabled - timestamp (integer) - Last module suspended timestamp
- failureCount (integer) - Module sleep failure count
api.nbAT("TAU_BLANKING", tau)
- interval (integer) - Tau blanking interval
api.nbAT("NET_STATUS?")
- lastTac (integer) - Last Tac
- lastCellId (integer) - Last Cell Id
- lastPeriodicTau (integer) - Last Periodic Tau
- lastActiveTime (integer) - Last Active Time
- lastEdrxValue (integer) - Last Provided Edrx Value
- lastEdrxPtwValue (integer) - Last Provided Edrx Ptw Value
- edrxNegotiated (integer) - Negotiated Edrx
- timeoutForInactTimer (integer) - Compensate Timeout For Inact Timer Value
api.nbAT("DTLS", pskid, psk)
- status (integer) - Zero for success,
-1
for deactivation
api.nbAT("TIME_SYNC", force, zone)
- zoneShift (integer) - Time zone shift in minutes, returns only when optional argument is set
Default SIMCom AT commands
api.nbAT(command, timeout, wakeUp, success, err, tryMore, setAwakeBefore, pollBeforeCommand, setAwakeOffAfter, receiveFixedBytes)
- answer (string) -
nil
or non-zero length string with an answer to a AT command - status (integer) - Zero for success, negative for failure
Example how to get IMSI number of SIM card:
-- Get IMSI number of SIM card with timeout of 4 seconds
res = api.nbAT("AT+CIMI", 4000)
print(res)
Example how to get IMEI number of the modem:
-- IMEI --
x=api.nbAT("AT+CGSN")
start = string.find(x, "\n")
finish = string.find(x, "\r", start+1)
imei = string.sub(x,(start+1),(finish-1))
print("IMEI: " .. imei)
Example how to get signal strength:
-- Get signal strength
res,b = api.nbAT("AT+CSQ")
print(res)
Example how to turn on debug mode:
-- AT_DEBUG_OFF - turn off
-- AT_DEBUG_ON - turn on
-- REINITIALIZE - re-initialize the NB-IoT module
-- ICCID? - returns ICCID value of current SIM card
-- IMSI? - returns IMSI value of current SIM card
-- IMEI? - returns IMEI value of the modem
api.nbAT("AT_DEBUG_ON")
Example how to set maximum downlink period and reinitialize the module:
-- If there is no downlink for given value, set timeout to 30 seconds and re-initialize the module.
- api.nbAT("DOWNLINK_MAX_PERIOD", 3*24*3600*1000)
--if NB-IoT module is initialized 5 times in a row -> sleep 4 hours and then reset
- api.nbAT("REINIT_HARD_LIMIT_RETRY", 5)
-- "1" if DOWNLINK_MAX_PERIOD situation happened since reset.
- api.nbAT("FORCED_REINIT_DONE?")
Example how to establish a connection to MQTT server:
-- Starting MQTT connection
mqtt_server = "server.cz" --mqtt server
mqtt_user = "uuuuu"
mqtt_password = "pppppp"
mqtt_port = 1883 --mqtt port
at("AT+CSQ")
at("AT+CEREG?")
at("AT+COPS?")
--at("AT+CSSLCFG=\"authmode\",0,0") --,2 for SSL
at("AT+CMQTTSTART", 5000, 0, "+CMQTTSTART: 0") -- start mqtt
at('AT+CMQTTACCQ=0,"'.. getIMSI() .. '"') -- ,1 for SSL
at("AT+CMQTTCONNECT=0,\"tcp://".. mqtt_server..":".. mqtt_port .."\",60,1,\""..mqtt_user.."\",\""..mqtt_password.."\"", 10000, 0, "+CMQTTCONNECT: 0,0")