Instantly share code, notes, and snippets.

Embed
What would you like to do?
Electron Cellular Band Select API Example App
#include "application.h"
SYSTEM_MODE(SEMI_AUTOMATIC);
// ALL_LEVEL, TRACE_LEVEL, DEBUG_LEVEL, INFO_LEVEL, WARN_LEVEL, ERROR_LEVEL, PANIC_LEVEL, NO_LOG_LEVEL
//SerialDebugOutput debugOutput(9600, ALL_LEVEL);
void waitForEnter() {
while (!Serial.available()) {
Particle.process();
Serial.print("Press ENTER\r"); // prints in place over and over
delay(1000);
}
while (Serial.available()) Serial.read(); // Flush the input buffer
}
void setup()
{
Serial.begin(9600);
waitForEnter();
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\n");
Serial.println("Press a key to run a command:"
"\r\n[O|o] to turn the cellular modem ON|OFF"
"\r\n[g|s] get|set bands info\r\n");
}
/* This function loops forever --------------------------------------------*/
void loop()
{
if (Serial.available() > 0)
{
char c = Serial.read();
if (c == 'g') {
CellularBand band_avail;
if (Cellular.getBandAvailable(band_avail)) {
Serial.print("Available bands: ");
for (int x=0; x<band_avail.count; x++) {
Serial.printf("%d", band_avail.band[x]);
if (x+1<band_avail.count) Serial.printf(",");
}
Serial.println();
Serial.println(band_avail); // repeated to show printable
}
else {
Serial.printlnf("Bands available not retrieved from the modem!");
}
CellularBand band_sel;
if (Cellular.getBandSelect(band_sel)) {
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.printf(",");
}
Serial.println();
Serial.println(band_sel); // repeated to show printable
}
else {
Serial.printlnf("Bands selected not retrieved from the modem!");
}
}
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 (Cellular.setBandSelect(band.c_str())) {
Serial.printlnf("%s band(s) set! Value will be saved in NVM when powering off modem.", band.c_str());
}
else {
Serial.printlnf("%s band(s) not valid!", band.c_str());
}
}
}
else if (c == '1') {
Serial.println("Setting bands to 850");
CellularBand band_sel;
band_sel.band[0] = BAND_850;
band_sel.count = 1;
if (Cellular.setBandSelect(band_sel)) {
Serial.print(band_sel);
Serial.println(" band(s) set! Value will be saved in NVM when powering off modem.");
}
else {
Serial.print(band_sel);
Serial.println(" band(s) not valid!");
}
}
else if (c == '2') {
Serial.println("Setting bands to 1900");
CellularBand band_sel;
band_sel.band[0] = BAND_900;
band_sel.count = 1;
if (Cellular.setBandSelect(band_sel)) {
Serial.print(band_sel);
Serial.println(" band(s) set! Value will be saved in NVM when powering off modem.");
}
else {
Serial.print(band_sel);
Serial.println(" band(s) not valid!");
}
}
else if (c == '3') {
Serial.println("Setting bands to 850,1900");
CellularBand band_sel;
band_sel.band[0] = (MDM_Band)850;
band_sel.band[1] = (MDM_Band)1800;
band_sel.band[2] = (MDM_Band)3400; // forcing a bad value, still gets rejected
band_sel.band[3] = BAND_2100;
band_sel.count = 4;
if (Cellular.setBandSelect(band_sel)) {
Serial.print(band_sel);
Serial.println(" band(s) set! Value will be saved in NVM when powering off modem.");
}
else {
Serial.print(band_sel);
Serial.println(" band(s) not valid!");
}
}
else if (c == '4') {
Serial.println("Setting bands to Factory Defaults!");
CellularBand band_sel;
band_sel.band[0] = BAND_DEFAULT;
band_sel.count = 1;
if (Cellular.setBandSelect(band_sel)) {
Serial.println("Factory defaults set! Value will be saved in NVM when powering off modem.");
}
else {
Serial.println("Factory defaults not set!");
}
}
else if (c == 'O') {
Serial.println("Turning on the modem!");
Cellular.on();
}
else if (c == 'o') {
Serial.println("Turning off the modem!");
Cellular.off();
}
while (Serial.available()) Serial.read(); // Flush the input buffer
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment