Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Debugging With the Particle Electron

DZone's Guide to

Debugging With the Particle Electron

Got a problem somewhere between your network and modem? See how you can use the Particle Electron to help with your debugging.

· IoT Zone ·
Free Resource

Developing IoT products is a multifaceted process — writing software, designing hardware, connecting it all to a cloud, etc. There are many points that can go wrong, which is why it is really important to have good tools for debugging. If the problem is somewhere between the network and the modem, this article might help.

The ETSI GSM 07.07 specifies a set of commands used to control a modem. Every command starts with "AT", which is why the commands are known as "AT commands." Some AT commands are device-specific, but a lot of them are universal. This makes them an invaluable tool for testing the compatibility between devices and SIM cards or getting information about the available networks.

Looking on the web, there were no lightweight programs for the Particle Electron that could give a simple response to any AT command entered by the user. All existing solutions were either too bulky or were really good, but only parsed some specific AT commands (like sending an SMS or getting a list of available operators). This is why we decided to create our own solution.

The first step in testing AT commands is getting a device that you can easily interface with. In this article, we use the Particle Electron 2G.

The Electron uses the u-blox SARA-G350 to connect to cellular networks. The manufacturer of the module has provided a really comprehensive guide of AT commands you can use on it.

The Particle Electron is also really easy to program and interface with. Just like the Arduino, all Particle Electron programs have two basic functions — setup() and loop().

The setup() function is executed once at startup, and then the loop() function will be executed forever.

To test the AT commands, we use Particle's Cellular API. The first thing we do is call Cellular.on() to turn on the cellular functionality. We also add a delay to wait for the cellular module to turn on and start the Serial interface so we can communicate to the device while it is running.

void setup() {
    Serial.begin(9600);
    Serial.println("Starting cellular, waiting 10s.");
    Cellular.on();
    delay(10000);
    Serial.println("Cellular started. Ready to receive AT commands.");
}


In the loop() function, we wait for the user to enter the command and call Cellular.command(). To see more specifics about using Cellular.command(), read the Particle documentation on it.

void loop() {
 
    if (Serial.available() > 0) {
           
        String input = Serial.readString();
 
        Serial.println("Received input: "+input);
 
        Serial.println("--BEGIN RESPONSE--");
        int point;
        Cellular.command(parseResponse, &point, AT_TIMEOUT, input+"\r\n");
        Serial.println("--END RESPONSE--");
    }
}


The first argument we pass into the command() function is actually another function that we define after the loop() function.

int parseResponse(int type, const char* buf, int len, int* point) {
 
    char line[1024+64];
    strncpy(line, buf, len);
    line[len] = '\0';
    String line_as_string = String(line);
    Serial.println(line_as_string.trim());
   
    if (type == TYPE_ABORTED) {
        return RESP_ABORTED;
    }
    else if (type == TYPE_PROMPT) {
        return RESP_PROMPT;
    }
    else if (type == TYPE_ERROR) {
        return RESP_ERROR;
    }
    else if (type == TYPE_OK) {
        return RESP_OK;
    }
    else {
        return WAIT;
    }
}


This code snippet is a modified version of the parsing function from the Electron Troubleshooting App written by Brett Walach, one of the engineers behind the Particle Electron.

We also pass AT_TIMEOUT into the Cellular.command() function. This has to be an integer that defines the number of milliseconds the program will wait for the response before calling a timeout. We have set this to 5 minutes (300000 milliseconds).

We are ready for testing now! Paste the code into your Particle Web IDE and flash it onto the device. You can do it over-the-air or use a USB cable. Once you have flashed the device, you have to connect it to your computer via the USB cable and open a serial monitor. The easiest way is to download the Arduino IDE and select the correct port from Tools -> Port. Open the serial monitor from the same menu and try some commands.

Some commands to try to make sure your SIM card is installed and working properly:

  • AT+CPIN? — a response saying "READY" means that the modem is connected to the SIM card and is not having a PIN code related problem.
  • AT+CCID — will give you the ICCID of the SIM card
  • AT+COPS=5 — this might take some time, but it should return information about the current available networks
  • AT+UDOPN=9 — tells you which network the device is currently connected to
  • AT+CPOL? — returns a list of preferred operators

Get the full program from our GitHub page. There is also a little bit of code there that helps you configure the device to work with a third-party SIM. We used our own SIMs (1oT SIM) and partner SIMs (1oT Partner SIM) for this experiment, and they worked great.

Topics:
iot ,particle electron ,debugging ,monitoring

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}