Created
January 29, 2019 05:08
-
-
Save benstr/8fa45bea9f1261030aad211b3132bbb8 to your computer and use it in GitHub Desktop.
NB MKR1500 example [ugly]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Hologram Demo - GSM Network Jumping | |
This example shows how to switch between carriers using a Hologram SIM. | |
Circuit: | |
* MKR NB 1500 board | |
* Antenna | |
* Hologram SIM card | |
Created 12 Dec 2018 | |
by Benstr | |
*/ | |
// libraries | |
#include <MKRNB.h> | |
// initialize the library instance | |
NB nbAccess('true'); // include a 'true' parameter to enable debugging | |
NBScanner scannerNetworks; | |
NBModem modemTest; | |
GPRS gprs; | |
NBClient client; | |
// Webserver Variables | |
char server[] = "arduino.cc"; | |
char path[] = "/asciilogo.txt"; | |
int port = 80; | |
// Carriar Codes | |
String ATT_MNC = "310410"; | |
String VER_MNC = "311480"; | |
String TMO_MNC = "310260"; | |
// Save data variables | |
String IMEI = ""; | |
// connection state | |
bool connected = false; | |
// baud rate used for both Serial ports | |
unsigned long baud = 115200; | |
void setup() { | |
// Reset the ublox module | |
modemTest.begin(); | |
// Eat your Serial | |
Serial.begin(baud); | |
SerialSARA.begin(baud); | |
while (!Serial) { | |
; // wait for serial port to connect. Needed for Leonardo only | |
} | |
Serial.println("Hologram MKR1500 Network Jumper"); | |
scannerNetworks.begin(); | |
// Get modem unique identifier | |
Serial.print("Modem IMEI: "); | |
IMEI = modemTest.getIMEI(); | |
IMEI.replace("\n", ""); | |
if (IMEI != NULL) { | |
Serial.println(IMEI); | |
} | |
delay(3000); | |
//printCarrierInfo(); | |
Serial.println("Pick an available network by typing one of the following:"); | |
Serial.println("\"att\": AT&T LTE-M/CAT-M1 (MNC 310-410)"); | |
Serial.println("\"verizon\": Verizon LTE-M/CAT-M1 (MNC 311-480)"); | |
Serial.println("\"tmobile\": T-Mobile NB-IoT (MNC 310-260)"); | |
} | |
void loop() { | |
// if there are incoming bytes available | |
// from the server, read them and print them: | |
while (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
while (SerialSARA.available()) { | |
Serial.print(SerialSARA.read()); | |
} | |
if (Serial.available()) { | |
String command = Serial.readString(); | |
//Serial.print(command); | |
if (command.indexOf("att") >= 0) | |
{ | |
Serial.println(); | |
Serial.println("Jumping to AT&T LTE-M"); | |
jumpCarrier(String("AT+COPS=1,2,"+ATT_MNC+",8\r\n")); | |
get_arduinocc(); | |
} | |
else if (command.indexOf("ver") >= 0) | |
{ | |
Serial.println(); | |
Serial.println("Jumping to Verizon LTE-M"); | |
jumpCarrier(String("AT+COPS=1,2,"+VER_MNC+",8\r\n")); | |
get_arduinocc(); | |
} | |
else if (command.indexOf("tmo") >= 0) | |
{ | |
Serial.println(); | |
Serial.println("Jumping to T-Mobile NB-IoT"); | |
jumpCarrier(String("AT+COPS=1,2,"+TMO_MNC+",9\r\n")); | |
} | |
else if (command.indexOf("netz") >= 0) | |
{ | |
Serial.println(); | |
// scan for existing networks, displays a list of networks | |
Serial.println("Scanning available networks. May take a few minutes."); | |
//scannerNetworks.begin(); | |
Serial.println(scannerNetworks.readNetworks()); | |
Serial.println("Network scan complete. If there are no networks scan again."); | |
} | |
else if (command.indexOf("at+") >= 0 || command.indexOf("AT+") >= 0) | |
{ | |
SerialSARA.write(command.c_str()); | |
} | |
} | |
} | |
bool jumpCarrier(String network) | |
{ | |
bool good = true; | |
do | |
{ | |
Serial.println("jumping from previous network..."); | |
good = sendGSMCommand(String("AT+CGATT=0\r\n")); | |
good = sendGSMCommand(String("AT+COPS=2\r\n")); | |
Serial.println("success!"); | |
Serial.println("... jumping to new network"); | |
good = sendGSMCommand(network); | |
good = sendGSMCommand(String("AT+COPS=0\r\n")); | |
good = sendCREGCommand(String("AT+CREG?\r\n")); | |
good = sendGSMCommand(String("AT+CGATT=1\r\n")); | |
//good = sendGSMCommand(String("AT+UPSD=0,1,\"hologram\"\r\n")); | |
//good = sendGSMCommand(String("AT+UPSD=0,7,\"0.0.0.0\"\r\n")); | |
//good = sendGSMCommand(String("AT+UPSDA=0,3\r\n")); | |
//good = sendGSMCommand(String("AT+UPSND=0,8\r\n")); | |
// if(gprs.attachGPRS("hologram","","")==GPRS_READY){ | |
// Serial.println("success!"); | |
// Serial.println(); | |
// | |
// printCarrierInfo(); | |
// | |
// return good; | |
// } else { | |
// good = false; | |
// } | |
} while(good == false); | |
Serial.println("Error when joining"); | |
} | |
void printCarrierInfo() | |
{ | |
// currently connected carrier | |
Serial.print("Current carrier: "); | |
Serial.println(scannerNetworks.getCurrentCarrier()); | |
Serial.println(); | |
// returns strength and ber | |
// signal strength in 0-31 scale. 31 means power > 51dBm | |
// BER is the Bit Error Rate. 0-7 scale. 99=not detectable | |
Serial.print("Signal Strength: "); | |
Serial.print(scannerNetworks.getSignalStrength()); | |
Serial.println(" out of a possible 31"); | |
} | |
bool sendGSMCommand(String command) | |
{ | |
Serial.println(command.c_str()); | |
SerialSARA.write(command.c_str()); | |
String response; | |
while(!SerialSARA.available()) { | |
; | |
} | |
if(SerialSARA.available()) | |
{ | |
response = SerialSARA.readString(); | |
Serial.println(response); | |
if (response.indexOf("OK") >= 0) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
} | |
bool sendCREGCommand(String command) | |
{ | |
String response; | |
Serial.println(command.c_str()); | |
while (response.indexOf("0,5") == -1) { | |
SerialSARA.write(command.c_str()); | |
while(!SerialSARA.available()) { | |
; | |
} | |
if(SerialSARA.available()) | |
{ | |
response = SerialSARA.readString(); | |
Serial.println(response); | |
} | |
delay(2000); | |
} | |
return true; | |
} | |
void get_arduinocc() { | |
Serial.println("Connecting to Arduino.cc"); | |
// if you get a connection, report back via serial: | |
if (client.connect(server, port)) { | |
Serial.println("Connected! Getting file."); | |
// Make a HTTP request: | |
client.print("GET "); | |
client.print(path); | |
client.println(" HTTP/1.1"); | |
client.print("Host: "); | |
client.println(server); | |
client.println("Connection: close"); | |
client.println(); | |
Serial.println("Arduino.cc Connection Complete."); | |
} else { | |
// if you didn't get a connection to the server: | |
Serial.println("Connection failed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment