Retrieve cellular connection state information
Device
Name | Type | Description |
---|---|---|
callback | Function |
A function to be called when the cellular connection data has been gathered
|
Nothing
This method requests current connection state information from the cellular modem integrated into imps that feature cellular interfaces. It takes as its argument a function which will be called when this information has been retrieved. Gathering this data can take a significant period of time, so imp.net.getcellinfo() operates asynchronously to prevent your application from being blocked while the information is collated. The information is only available once the imp’s cellular modem is powered on.
The callback function should include a single parameter, cellInfo, which will receive a string of data from the modem. The contents of this string is very modem-dependent.
For example, an impC001 might return the string:
4G,6300,20,10,10,FDD,262,02,BF75,0345103,350,90,-94,-7,CONN
The data contained in the string varies according to the connected network type (2G, 3G, 4G, etc).
With an imp006 and a Quectel modem, you would get a string like:
2,5,"544D","8786",0
"GSM",-84
"EDGE","23410","GSM 900",121
ie. separate result strings (from three modem commands: AT+CGREG?
, AT+QCSQ
and AT+QNWINFO
), separated by newlines. Two key values can be extracted from the second value in the third string: the Mobile Country Code (MCC) and the Mobile Network Code (MNC). In this example we have 23410, which yields the MCC 234 (the UK) and the MCC 10 (O2). You can view all the possible values for these two identifiers here.
The third ("544D") and fourth ("8786") values from the top line are both hex values and give, respectively, the cell Location Area Code and the Cell ID (CID). Along with the MNC and MCC, these values can be used to determine the approximate location of the cell tower using a variety of services, for example Open Cell ID.
Run the following code on an impC001.
// Request cellular connection info
imp.net.getcellinfo(function(cellInfo) {
// Parse string 'cellInfo' - comma-separated values
local cellData = split(cellInfo, ",");
// Log the network type
local ACT = cellData[0];
server.log("Connected to a " + ACT + " network");
if (ACT == "4G") {
// Log the cell ID
server.log("Global Cell ID: " + cellData[9]);
}
});
Run the following code on an imp006 with a Quectel modem.
// Request cellular connection info
// Requires modem to be ready
// Request cellular connection info
imp.net.getcellinfo(function(cellInfo) {
// Remove quote marks from strings
do {
local a = cellInfo.find("\"");
if (a == null) break;
cellInfo = cellInfo.slice(0,a) + (a < cellInfo.len() - 1 ? cellInfo.slice(a + 1) : "");
} while (true);
// Parse string 'cellInfo' into its three newline-separated parts
local cellStrings = split(cellInfo, "\n");
// Log the cell country and network info
local cellData = split(cellStrings[2], ",");
server.log(format("Cell MCC %s, MNC %s", cellData[1].slice(0,3), cellData[1].slice(3)));
// Log the cell location and ID info
cellData = split(cellStrings[0], ",");
server.log(format("Cell LAC 0x%s, ID 0x%s", cellData[2], cellData[3]));
});