uartat, le910: initial implementation.

uartat is the underlying UART driver, specifically for use with
AT-style command driven devices like modems.

The le910 support is provided in the form of examples that make use
of the uartat driver to interact with the device.

Signed-off-by: Jon Trulson <jtrulson@ics.com>
This commit is contained in:
Jon Trulson
2017-01-17 11:37:14 -07:00
parent 123e611f45
commit e8151640a1
17 changed files with 2067 additions and 0 deletions

View File

@ -373,3 +373,4 @@ endif()
add_custom_example (nmea_gps_i2c_example-cxx nmea_gps_i2c.cxx nmea_gps)
add_custom_example (mcp2515-txrx-example-cxx mcp2515-txrx.cxx mcp2515)
add_custom_example (ads1015-example-cxx ads1015.cxx ads1x15)
add_custom_example (le910-example-cxx le910.cxx uartat)

175
examples/c++/le910.cxx Normal file
View File

@ -0,0 +1,175 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iostream>
#include <string>
#include "uartat.hpp"
#include "upm_utilities.h"
using namespace std;
const size_t bufferLength = 256;
int main(int argc, char **argv)
{
//! [Interesting]
string defaultDev = string("/dev/ttyUSB0");
if (argc > 1)
defaultDev = string(argv[1]);
cout << "Using device: " << defaultDev << endl;
// Instantiate a UARTAT sensor on defaultDev at 115200 baud.
upm::UARTAT *sensor = new upm::UARTAT(defaultDev, 115200);
// This is a simplistic example that tries to configure the LE910,
// and use it's built-in socket capabilities to connect to a
// remote host, obtain a small piece of data, and return it. It's
// mainly intended to show you how you can use the various AT
// commands supported by the LE910 to perform simple tasks.
//
// You must have a valid SIM card with an active data plan for
// this example to do anything interesting.
//
// See the LE910 AT Commands reference for full information on
// what is possible with this device. The uartat driver is
// intended to make it a little easier to control AT-style
// devices, but is by no means a full-featured communication
// infrastructure. A "real" application will probably need to be
// much more sophisticated with regard to parsing, doing retries,
// etc.
//
// For experimenting with various AT commands, try using an
// interactive terminal emulator like minicom or screen.
// make sure we are in command mode
if (!sensor->inCommandMode())
{
cout << "Not in command mode, switching..." << endl;
sensor->commandMode("+++", 1000);
}
// flter out CR's in responses by default
sensor->filterCR(true);
cout << "Configuring modem..." << endl;
// discard any waiting characters
sensor->drain();
// reset modem
sensor->command("ATZ\r");
// turn off command echo, set verbosity to 1, enable data
// connection mode
sensor->command("ATE0 V1 +FCLASS=0\r");
sensor->drain();
// Now issue some commands and output the results.
cout << "Modem and SIM information:" << endl;
std::string buffer;
buffer = sensor->commandWithResponse("AT+ICCID\r", bufferLength);
if (!buffer.empty())
cout << "ICCID (SIM ID): "
<< buffer
<< endl;
buffer = sensor->commandWithResponse("AT+CGSN=1\r", bufferLength);
if (!buffer.empty())
cout << "IMEI: "
<< buffer
<< endl;
// see if we are on the network....
buffer = sensor->commandWithResponse("AT+CREG?\r", bufferLength);
if (!buffer.empty())
{
cout << buffer << endl;
// look for "CGREG: 0,1" or "CGREG: 0,5"
if (sensor->find(buffer, "CREG: 0,1") ||
sensor->find(buffer, "CREG: 0,5"))
{
cout << "Connected to the cell data network." << endl;
// wait up to 5 seconds for responses now...
sensor->setResponseWaitTime(5000);
// setup PDP context (socket 1). An ERROR repsonse is
// possible if the PDP context is already set up.
sensor->command("AT#SGACT=1,1\r");
// setup a TCP socket to nist.gov and read the timestamp.
cout << "Connecting to time-a.nist.gov, TCP port 13" << endl;
// Wait up to 60 seconds to find the NO CARRIER
// string, which will be present at the end, if the
// connection succeeded and the requested data was
// obtained.
buffer =
sensor->commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r",
bufferLength, "\nNO CARRIER\n", 60000);
if (!buffer.empty())
{
// print out the response
cout << "RESPONSE: "
<< endl
<< buffer
<< endl;
}
else
{
cout << "No response." << endl;
}
// destroy PDP context
sensor->setResponseWaitTime(250);
sensor->command("AT#SGACT=1,0\r");
}
else
{
cout << "You do not appear to be connected to the network..."
<< endl;
}
}
else
{
cout << "Error executing query\n" << endl;
}
// reset the modem
sensor->command("ATZ\r");
cout << "Exiting" << endl;
delete sensor;
//! [Interesting]
return 0;
}

View File

@ -154,3 +154,4 @@ add_custom_example (rpr220-intr-example-c rpr220-intr.c rpr220)
add_custom_example (md-stepper-example-c md-stepper.c md)
add_custom_example (button_intr-example-c button_intr.c button)
add_custom_example (mcp2515-txrx-example-c mcp2515-txrx.c mcp2515)
add_custom_example (le910-example-c le910.c uartat)

169
examples/c/le910.c Normal file
View File

@ -0,0 +1,169 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <unistd.h>
#include "uartat.h"
#include "upm_utilities.h"
const size_t bufferLength = 256;
int main(int argc, char **argv)
{
//! [Interesting]
char *defaultDev = "/dev/ttyUSB0";
if (argc > 1)
defaultDev = argv[1];
printf("Using device: %s\n", defaultDev);
// Instantiate a UARTAT sensor on defaultDev at 115200 baud.
uartat_context sensor = uartat_init_tty(defaultDev, 115200);
if (!sensor)
{
printf("uartat_init_tty() failed.\n");
return 1;
}
// This is a simplistic example that tries to configure the LE910,
// and use it's built-in socket capabilities to connect to a
// remote host, obtain a small piece of data, and return it. It's
// mainly intended to show you how you can use the various AT
// commands supported by the LE910 to perform simple tasks.
//
// You must have a valid SIM card with an active data plan for
// this example to do anything interesting.
//
// See the LE910 AT Commands reference for full information on
// what is possible with this device. The uartat driver is
// intended to make it a little easier to control AT-style
// devices, but is by no means a full-featured communication
// infrastructure. A "real" application will probably need to be
// much more sophisticated, and custom designed for the desired
// task.
//
// For experimenting with various AT commands, try using an
// interactive terminal emulator like minicom or screen.
char buffer[bufferLength];
// make sure we are in command mode
if (!uartat_in_command_mode(sensor))
{
printf("Not in command mode, switching...\n");
uartat_command_mode(sensor, "+++", 1000);
}
// flter out CR's in responses by default
uartat_filter_cr(sensor, true);
printf("Configuring modem...\n");
// discard any waiting characters
uartat_drain(sensor);
// reset modem
uartat_command(sensor, "ATZ\r");
// turn off command echo, set verbosity to 1, enable data
// connection mode
uartat_command(sensor, "ATE0 V1 +FCLASS=0\r");
uartat_drain(sensor);
// Now issue some commands and output the results.
printf("Modem and SIM information:\n");
if (uartat_command_with_response(sensor, "AT+ICCID\r",
buffer, bufferLength))
printf("ICCID (SIM ID): %s\n", buffer);
if (uartat_command_with_response(sensor, "AT+CGSN=1\r",
buffer, bufferLength))
printf("IMEI: %s\n", buffer);
// see if we are on the network....
if (uartat_command_with_response(sensor, "AT+CREG?\r",
buffer, bufferLength))
{
printf("%s\n", buffer);
// look for "CGREG: 0,1" or "CGREG: 0,5"
if (uartat_find(sensor, buffer, "CREG: 0,1") ||
uartat_find(sensor, buffer, "CREG: 0,5"))
{
printf("Connected to the cell data network.\n");
// wait up to 5 seconds for responses now...
uartat_set_response_wait_time(sensor, 5000);
// setup PDP context (socket 1). An ERROR response is
// possible if the PDP context is already set up.
uartat_command(sensor, "AT#SGACT=1,1\r");
// setup a TCP socket to nist.gov and read the timestamp.
printf("Connecting to time-a.nist.gov, TCP port 13\n");
// Wait up to 60 seconds to find the NO CARRIER
// string, which will be present at the end, if the
// connection succeeded and the requested data was
// obtained.
if (uartat_command_waitfor(sensor,
"AT#SD=1,0,13,\"time-a.nist.gov\"\r",
buffer, bufferLength,
"\nNO CARRIER\n", 60000))
{
// print out the response
printf("RESPONSE:\r%s\r", buffer);
}
else
{
printf("No response.\n");
}
// destroy PDP context
uartat_set_response_wait_time(sensor, 250);
uartat_command(sensor, "AT#SGACT=1,0\r");
}
else
{
printf("You do not appear to be connected to the network...\n");
}
}
else
{
printf("Error executing query\n");
}
// reset the modem
uartat_command(sensor, "ATZ\r");
printf("Exiting\n");
uartat_close(sensor);
//! [Interesting]
return 0;
}

View File

@ -185,3 +185,4 @@ if (OPENZWAVE_FOUND)
endif()
add_example_with_path(NMEAGPS_I2C_Example nmea_gps nmea_gps)
add_example_with_path(MCP2515_TXRX_Example mcp2515 mcp2515)
add_example_with_path(LE910_Example uartat uartat)

View File

@ -0,0 +1,167 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import upm_uartat.UARTAT;
public class LE910_Example
{
private static String defaultDev = "/dev/ttyUSB0";
static private final int bufferLength = 256;
public static void main(String[] args) throws InterruptedException
{
// ! [Interesting]
if (args.length > 0)
defaultDev = args[0];
System.out.println("Using device " + defaultDev);
System.out.println("Initializing...");
// Instantiate a UARTAT sensor on defaultDev at 115200 baud.
UARTAT sensor = new UARTAT(defaultDev, 115200);
// This is a simplistic example that tries to configure the LE910,
// and use it's built-in socket capabilities to connect to a
// remote host, obtain a small piece of data, and return it. It's
// mainly intended to show you how you can use the various AT
// commands supported by the LE910 to perform simple tasks.
//
// You must have a valid SIM card with an active data plan for
// this example to do anything interesting.
//
// See the LE910 AT Commands reference for full information on
// what is possible with this device. The uartat driver is
// intended to make it a little easier to control AT-style
// devices, but is by no means a full-featured communication
// infrastructure. A "real" application will probably need to be
// much more sophisticated with regard to parsing, doing retries,
// etc.
//
// For experimenting with various AT commands, try using an
// interactive terminal emulator like minicom or screen.
// make sure we are in command mode
if (!sensor.inCommandMode())
{
System.out.println("Not in command mode, switching...");
sensor.commandMode("+++", 1000);
}
// flter out CR's in responses by default
sensor.filterCR(true);
System.out.println("Configuring modem...");
// discard any waiting characters
sensor.drain();
// reset modem
sensor.command("ATZ\r");
// turn off command echo, set verbosity to 1, enable data
// connection mode
sensor.command("ATE0 V1 +FCLASS=0\r");
sensor.drain();
// Now issue some commands and output the results.
System.out.println("Modem and SIM information:");
String buffer;
buffer = sensor.commandWithResponse("AT+ICCID\r", bufferLength);
if (buffer.length() > 0)
{
System.out.println("ICCID (SIM ID): " + buffer);
}
buffer = sensor.commandWithResponse("AT+CGSN=1\r", bufferLength);
if (buffer.length() > 0)
{
System.out.println("IMEI: " + buffer);
}
// see if we are on the network....
buffer = sensor.commandWithResponse("AT+CREG?\r", bufferLength);
if (buffer.length() > 0)
{
System.out.println(buffer);
// look for "CGREG: 0,1" or "CGREG: 0,5"
if (sensor.find(buffer, "CREG: 0,1") ||
sensor.find(buffer, "CREG: 0,5"))
{
System.out.println("Connected to the cell data network.");
// wait up to 5 seconds for responses now...
sensor.setResponseWaitTime(5000);
// setup PDP context (socket 1). An ERROR repsonse is
// possible if the PDP context is already set up.
sensor.command("AT#SGACT=1,1\r");
// setup a TCP socket to nist.gov and read the timestamp.
System.out.println("Connecting to time-a.nist.gov, TCP port 13");
// Wait up to 60 seconds to find the NO CARRIER
// string, which will be present at the end, if the
// connection succeeded and the requested data was
// obtained.
buffer =
sensor.commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r",
bufferLength, "\nNO CARRIER\n", 60000);
if (buffer.length() > 0)
{
// print out the response
System.out.println("RESPONSE: ");
System.out.println(buffer);
}
else
{
System.out.println("No response.");
}
// destroy PDP context
sensor.setResponseWaitTime(250);
sensor.command("AT#SGACT=1,0\r");
}
else
{
System.out.println("You do not appear to be connected to the network...");
}
}
else
{
System.out.println("Error executing query\n");
}
// reset the modem
sensor.command("ATZ\r");
System.out.println("Exiting");
// ! [Interesting]
}
}

View File

@ -0,0 +1,166 @@
/*
* Author: Jon Trulson <jtrulson@ics.com>
* Copyright (c) 2017 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
var sensorObj = require('jsupm_uartat');
var defaultDev = "/dev/ttyUSB0";
// if an argument was specified, use it as the device instead
if (process.argv.length > 2)
{
defaultDev = process.argv[2];
}
console.log("Using device:", defaultDev);
console.log("Initializing...");
// Instantiate a UARTAT sensor on defaultDev at 115200 baud.
var sensor = new sensorObj.UARTAT(defaultDev, 115200);
// This is a simplistic example that tries to configure the LE910,
// and use it's built-in socket capabilities to connect to a
// remote host, obtain a small piece of data, and return it. It's
// mainly intended to show you how you can use the various AT
// commands supported by the LE910 to perform simple tasks.
//
// You must have a valid SIM card with an active data plan for
// this example to do anything interesting.
//
// See the LE910 AT Commands reference for full information on
// what is possible with this device. The uartat driver is
// intended to make it a little easier to control AT-style
// devices, but is by no means a full-featured communication
// infrastructure. A "real" application will probably need to be
// much more sophisticated with regard to parsing, doing retries,
// etc.
//
// For experimenting with various AT commands, try using an
// interactive terminal emulator like minicom or screen.
// make sure we are in command mode
if (!sensor.inCommandMode())
{
console.log("Not in command mode, switching...");
sensor.commandMode("+++", 1000);
}
// flter out CR's in responses by default
sensor.filterCR(true);
console.log("Configuring modem...");
// discard any waiting characters
sensor.drain();
// reset modem
sensor.command("ATZ\r");
// turn off command echo, set verbosity to 1, enable data
// connection mode
sensor.command("ATE0 V1 +FCLASS=0\r");
sensor.drain();
// Now issue some commands and output the results.
console.log("Modem and SIM information:");
var buffer;
var bufferLength = 256;
buffer = sensor.commandWithResponse("AT+ICCID\r", bufferLength);
if (buffer.length > 0)
{
console.log("ICCID (SIM ID):", buffer);
}
buffer = sensor.commandWithResponse("AT+CGSN=1\r", bufferLength);
if (buffer.length > 0)
{
console.log("IMEI: ", buffer);
}
// see if we are on the network....
buffer = sensor.commandWithResponse("AT+CREG?\r", bufferLength);
if (buffer.length > 0)
{
console.log(buffer);
// look for "CGREG: 0,1" or "CGREG: 0,5"
if (sensor.find(buffer, "CREG: 0,1") ||
sensor.find(buffer, "CREG: 0,5"))
{
console.log("Connected to the cell data network.");
// wait up to 5 seconds for responses now...
sensor.setResponseWaitTime(5000);
// setup PDP context (socket 1). An ERROR repsonse is
// possible if the PDP context is already set up.
sensor.command("AT#SGACT=1,1\r");
// setup a TCP socket to nist.gov and read the timestamp.
console.log("Connecting to time-a.nist.gov, TCP port 13");
// Wait up to 60 seconds to find the NO CARRIER
// string, which will be present at the end, if the
// connection succeeded and the requested data was
// obtained.
buffer =
sensor.commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r",
bufferLength, "\nNO CARRIER\n", 60000);
if (buffer.length > 0)
{
// print out the response
console.log("RESPONSE: ");
console.log(buffer);
}
else
{
console.log("No response.");
}
// destroy PDP context
sensor.setResponseWaitTime(250);
sensor.command("AT#SGACT=1,0\r");
}
else
{
console.log("You do not appear to be connected to the network...");
}
}
else
{
console.log("Error executing query\n");
}
// reset the modem
sensor.command("ATZ\r");
console.log("Exiting");
sensor = null;
sensorObj.cleanUp();
sensorObj = null;
process.exit(0);

157
examples/python/le910.py Executable file
View File

@ -0,0 +1,157 @@
#!/usr/bin/python
# Author: Jon Trulson <jtrulson@ics.com>
# Copyright (c) 2017 Intel Corporation.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
from __future__ import print_function
import time, sys, signal, atexit
from upm import pyupm_uartat as UARTAT
def main():
## Exit handlers ##
# This function stops python from printing a stacktrace when you
# hit control-C
def SIGINTHandler(signum, frame):
raise SystemExit
# This function lets you run code on exit, including functions from sensor
def exitHandler():
print("Exiting")
sys.exit(0)
# Register exit handlers
atexit.register(exitHandler)
signal.signal(signal.SIGINT, SIGINTHandler)
defaultDev = "/dev/ttyUSB0"
# if an argument was specified, use it as the device instead
if (len(sys.argv) > 1):
defaultDev = sys.argv[1]
print("Using device:", defaultDev);
# Instantiate a UARTAT sensor on defaultDev at 115200 baud.
sensor = UARTAT.UARTAT(defaultDev, 115200)
# This is a simplistic example that tries to configure the LE910,
# and use it's built-in socket capabilities to connect to a
# remote host, obtain a small piece of data, and return it. It's
# mainly intended to show you how you can use the various AT
# commands supported by the LE910 to perform simple tasks.
#
# You must have a valid SIM card with an active data plan for
# this example to do anything interesting.
#
# See the LE910 AT Commands reference for full information on
# what is possible with this device. The uartat driver is
# intended to make it a little easier to control AT-style
# devices, but is by no means a full-featured communication
# infrastructure. A "real" application will probably need to be
# much more sophisticated with regard to parsing, doing retries,
# etc.
#
# For experimenting with various AT commands, try using an
# interactive terminal emulator like minicom or screen.
# make sure we are in command mode
if (not sensor.inCommandMode()):
print("Not in command mode, switching...")
sensor.commandMode("+++", 1000)
# flter out CR's in responses by default
sensor.filterCR(True)
print("Configuring modem...")
# discard any waiting characters
sensor.drain()
# reset modem
sensor.command("ATZ\r")
# turn off command echo, set verbosity to 1, enable data
# connection mode
sensor.command("ATE0 V1 +FCLASS=0\r")
sensor.drain()
# Now issue some commands and output the results.
print("Modem and SIM information:")
bufferLength = 256
buffer = sensor.commandWithResponse("AT+ICCID\r", bufferLength)
if (buffer):
print("ICCID (SIM ID):", buffer)
buffer = sensor.commandWithResponse("AT+CGSN=1\r", bufferLength)
if (buffer):
print("IMEI: ", buffer)
# see if we are on the network....
buffer = sensor.commandWithResponse("AT+CREG?\r", bufferLength)
if (buffer):
print(buffer)
# look for "CGREG: 0,1" or "CGREG: 0,5"
if (sensor.find(buffer, "CREG: 0,1") or
sensor.find(buffer, "CREG: 0,5")):
print("Connected to the cell data network.")
# wait up to 5 seconds for responses now...
sensor.setResponseWaitTime(5000)
# setup PDP context (socket 1). An ERROR repsonse is
# possible if the PDP context is already set up.
sensor.command("AT#SGACT=1,1\r")
# setup a TCP socket to nist.gov and read the timestamp.
print("Connecting to time-a.nist.gov, TCP port 13")
# Wait up to 60 seconds to find the NO CARRIER
# string, which will be present at the end, if the
# connection succeeded and the requested data was
# obtained.
buffer = sensor.commandWaitFor("AT#SD=1,0,13,\"time-a.nist.gov\"\r",
bufferLength, "\nNO CARRIER\n", 60000)
if (buffer):
# print out the response
print("RESPONSE: ")
print(buffer)
else:
print("No response.")
# destroy PDP context
sensor.setResponseWaitTime(250)
sensor.command("AT#SGACT=1,0\r")
else:
print("You do not appear to be connected to the network...")
else:
print("Error executing query\n")
# reset the modem
sensor.command("ATZ\r")
if __name__ == '__main__':
main()