Skip to content

Instantly share code, notes, and snippets.

@technobly
Last active September 8, 2016 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save technobly/33b20753dd7eaea68db0 to your computer and use it in GitHub Desktop.
Save technobly/33b20753dd7eaea68db0 to your computer and use it in GitHub Desktop.
Band Selection on the Particle Electron
// COMPILE from firmware/modules $
// make clean all PLATFORM_ID=10 APPDIR=~/code/fw-apps/band-select COMPILE_LTO=n DEBUG_BUILD=y -s program-dfu
//
// NOTE: APPDIR=~/code/fw-apps/band-select can be APP=band-select if that's easier
/* Includes ------------------------------------------------------------------*/
#include "application.h"
#include "cellular_hal.h"
typedef struct {
int band[4];
int count;
} MDM_BAND;
MDM_BAND _band_sel;
MDM_BAND _band_avail;
// ALL_LEVEL, TRACE_LEVEL, DEBUG_LEVEL, INFO_LEVEL, WARN_LEVEL, ERROR_LEVEL, PANIC_LEVEL, NO_LOG_LEVEL
Serial1DebugOutput debugOutput(115200, ALL_LEVEL);
SYSTEM_MODE(SEMI_AUTOMATIC);
static inline int _cbCCID(int type, const char* buf, int len, char* ccid)
{
if ((type == TYPE_PLUS) && ccid) {
if (sscanf(buf, "\r\n+CCID: %[^\r]\r\n", ccid) == 1)
/*nothing*/;
}
return WAIT;
}
static inline int _cbBANDAVAIL(int type, const char* buf, int len, MDM_BAND* data)
{
if ((type == TYPE_PLUS) && data) {
memset(data, 0, sizeof(&data));
// \r\n+UBANDSEL: (0,850,900,1800,1900)\r\n
if ((data->count = sscanf(buf, "\r\n+UBANDSEL: (0,%d,%d,%d,%d)\r\n", &data->band[0],&data->band[1],&data->band[2],&data->band[3])) > 0) {
/* do nothing */
}
}
return WAIT;
}
static inline int _cbBANDSEL(int type, const char* buf, int len, MDM_BAND* data)
{
if ((type == TYPE_PLUS) && data) {
memset(data, 0, sizeof(&data));
// \r\n+UBANDSEL: 850\r\n
// \r\n+UBANDSEL: 850,1900\r\n
if ((data->count = sscanf(buf, "\r\n+UBANDSEL: %d,%d,%d,%d\r\n", &data->band[0],&data->band[1],&data->band[2],&data->band[3])) > 0) {
/* do nothing */
}
}
return WAIT;
}
// 3rd Party SIM credentials
//STARTUP(cellular_credentials_set("broadband", "", "", NULL)); // AT&T
void setup()
{
Serial.begin(9600);
Serial.println("Press ENTER");
while (!Serial.available()) Particle.process();
while (Serial.available()) Serial.read(); // Flush the input buffer
Serial.println("Running in SEMI_AUTOMATIC mode, cellular modem is OFF at boot, not connecting to the cloud."
"\r\nIf you select a band that never connects, press the RESET button and change the band."
"\r\nConnect a serial to USB adapter on the TX pin to see debugging output.\r\n");
Serial.println("Press a key to run a command:"
"\r\n[O|o] to turn the cellular modem ON|OFF"
"\r\n[g] get bands info"
"\r\n[s] set new band info"
"\r\n[c|d] to connect|disconnect the cloud"
"\r\n[p] publish a \"b\" event"
"\r\n[i] read the SIM ICCID"
"\r\n[r] get the RSSI and QUAL values\r\n");
}
/* This function loops forever --------------------------------------------*/
void loop()
{
if (Serial.available() > 0)
{
char c = Serial.read();
if (c == 'g') {
if (RESP_OK == Cellular.command(_cbBANDAVAIL, &_band_avail, 5000, "AT+UBANDSEL=?\r\n")
&& (_band_avail.count > 0)) {
Serial.print("Available bands: 0");
for (int x=0; x<_band_avail.count; x++) {
Serial.printf(",%d", _band_avail.band[x]);
}
Serial.println();
}
if (RESP_OK == Cellular.command(_cbBANDSEL, &_band_sel, 5000, "AT+UBANDSEL?\r\n")
&& (_band_sel.count > 0)) {
Serial.print("Selected bands: ");
for (int x=0; x<_band_sel.count; x++) {
Serial.printf("%d", _band_sel.band[x]);
if(x+1 < _band_sel.count) {
Serial.print(",");
}
}
Serial.println();
}
}
else if (c == 's') {
Serial.print("Enter one or more comma delimited bands in MHz (0 will set back to factory defaults,"
"\r\nPress ESC to cancel the operation): ");
int done = false;
String band = "";
while (!done) {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '\r') {
done = true;
Serial.println();
}
else if (c == 27) { // ESC
band = "";
done = true;
Serial.println("\r\nBand select cancelled!");
}
else {
band += c;
Serial.print(c); // echo
}
}
Particle.process();
}
if(band != "") {
if (RESP_OK == Cellular.command("AT+UBANDSEL=%s\r\n", band.c_str())) {
Serial.printlnf("%s MHz band set! Value will be saved in NVM when powering off modem.", band.c_str());
}
else {
Serial.printlnf("%s MHz was not recognized by the modem!", band.c_str());
}
}
}
else if (c == 'c') {
Serial.println("Let's connect to the cloud!");
Particle.connect();
}
else if (c == 'd') {
Serial.println("Let's disconnect from the cloud!");
Particle.disconnect();
}
else if (c == 'O') {
Serial.println("Turning on the modem!");
Cellular.on();
}
else if (c == 'o') {
Serial.println("Turning off the modem!");
Cellular.off();
}
else if (c == 'p') {
Serial.println("Publishing the \"b\" event name!");
Particle.publish("b");
}
else if (c == 'i') {
Serial.println("Get the SIM card ID (ICCID)...");
char ccid[32] = "";
if ((RESP_OK == Cellular.command(_cbCCID, ccid, 1000, "AT+CCID\r\n"))
&& (strcmp(ccid,"") != 0))
{
Serial.printlnf("Got it! -> %s\r\n", ccid);
}
}
else if (c == 'r') {
CellularSignal s = Cellular.RSSI();
Serial.printlnf("RSSI: %ddBm, QUAL: %ddB", s.rssi, s.qual);
}
else {
Serial.println("Bad command!");
}
while (Serial.available()) Serial.read(); // Flush the input buffer
}
}
Press ENTER
Running in SEMI_AUTOMATIC mode, cellular modem is OFF at boot, not connecting to the cloud.
If you select a band that never connects, press the RESET button and change the band.
Connect a serial to USB adapter on the TX pin to see debugging output.

Press a key to run a command:
[O|o] to turn the cellular modem ON|OFF
[g] get bands info
[s] set new band info
[c|d] to connect|disconnect the cloud
[p] publish a "b" event
[i] read the SIM ICCID
[r] get the RSSI and QUAL values

Turning on the modem!
Available bands: 0,850,900,1800,1900
Selected bands: 1800,1900
Enter one or more comma delimited bands in MHz (0 will set back to factory defaults,
Press ESC to cancel the operation): 1900
1900 MHz band set! Value will be saved in NVM when powering off modem.
Available bands: 0,850,900,1800,1900
Selected bands: 1900
Turning off the modem!
Turning on the modem!
Enter one or more comma delimited bands in MHz (0 will set back to factory defaults,
Press ESC to cancel the operation): 
Band select cancelled!
Available bands: 0,850,900,1800,1900
Selected bands: 1900
Enter one or more comma delimited bands in MHz (0 will set back to factory defaults,
Press ESC to cancel the operation): 1800,900
1800,900 MHz band set! Value will be saved in NVM when powering off modem.
Enter one or more comma delimited bands in MHz (0 will set back to factory defaults,
Press ESC to cancel the operation): 1800,1900
1800,1900 MHz band set! Value will be saved in NVM when powering off modem.
Available bands: 0,850,900,1800,1900
Selected bands: 1800,1900
RSSI: 0dBm, QUAL: 0dB
Let's connect to the cloud!
RSSI: -91dBm, QUAL: 0dB
Publishing the "b" event name!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment