I have forked the Adafruit_FONA library and added the ability to set the APN, username and password. This allows the GPRS dependent parts of the library to work correctly.
https://github.com/darrensi/Adafruit_FONA_Library
Please feel free to use and let me know if you run into any issues.
Fona GPRS APN setting
Moderators: adafruit_support_bill, adafruit
Please be positive and constructive with your questions and comments.
- adafruit_support_rick
- Posts: 35092
- Joined: Tue Mar 15, 2011 11:42 am
Re: Fona GPRS APN setting
Thanks! I'll pass this along to the engineers.
- solardude
- Posts: 145
- Joined: Fri Oct 18, 2013 12:47 am
Re: Fona GPRS APN setting
Darren can you explain more about your updated code. Is it required for the GPRS data stream to operate correctly on the FONA?
- darrensillett
- Posts: 19
- Joined: Wed Aug 06, 2014 6:05 pm
Re: Fona GPRS APN setting
Sure.
The current version of the library is hard-coded to use an APN called "FONAnet". This appears to be just a placeholder as you would need the correct APN for the carrier associated with your SIM card.
I added a method to set the APN correctly (and username and password if the APN requires it). After doing this and adding one line to the FONAtest program to set my APN, I was then able to easily download a file through HTTP.
The current version of the library is hard-coded to use an APN called "FONAnet". This appears to be just a placeholder as you would need the correct APN for the carrier associated with your SIM card.
I added a method to set the APN correctly (and username and password if the APN requires it). After doing this and adding one line to the FONAtest program to set my APN, I was then able to easily download a file through HTTP.
- tdicola
- Posts: 1074
- Joined: Thu Oct 17, 2013 9:11 pm
Re: Fona GPRS APN setting
Thanks for making the change and sending the pull request! I tested it out and it works great. Made a few small tweaks to switch from a command buffer in RAM to use flash memory strings and merged it in.
For folks that need to set the APN, username, or password grab the latest version of the library from github and look at the comment added at the end of the setup() function. You should just need to uncomment it and fill in the APN and optional username and password.
Thanks again for sending the change!
For folks that need to set the APN, username, or password grab the latest version of the library from github and look at the comment added at the end of the setup() function. You should just need to uncomment it and fill in the APN and optional username and password.
Thanks again for sending the change!
- solardude
- Posts: 145
- Joined: Fri Oct 18, 2013 12:47 am
Re: Fona GPRS APN setting
Thanks Darren for the explanation. I'm following the FONA threads closely so when I get a chance to put my FONA's to use on future projects I'll be on top of how to get it up and running correctly quickly. It looks like your new tweak will save others hours or days of headaches of trying to figure out why they can't connect to wireless data stream to send and receive data through their carrier. Thank you!
I'm planning on using Ubidots to receive, log, graph, and react via SMS & Emails to pre defined events that can be triggered by incoming sensor data from a FONA or any wifi enabled device. The FONA is the ultimate low power wireless device if you want it work outside of the normal range of a wifi hotspot.
Ubidots has the FONA and they plan to create a tutorial showing how to use the FONA + Arduino to send sensor data to Ubidots so you can view what is going on and easily trigger things to happen at just about any predefined setpoint. They already have this up and running on a SIM900 cellular board and just need to modify the code a bit to get it working properly on the FONA. I'll make sure they know about your revised code so they can include this bit of info into their tutorial.
I don't work for Ubidots, I just find their service a dream come true when it comes to interfacing with other web connected devices Aka "Internet of Things" devices. It allows you to easily view your data stream in alot of different ways + most importantly easily have a SMS or Email be sent out if your temp goes above a certain point. I think its perfect for the FONA type setups.
Here are some pictures of how I'm using their service with a temp and humidity sensor + Spark Core. I'm wanting to drop the Wifi for FONA's Cellular network.
I'm planning on using Ubidots to receive, log, graph, and react via SMS & Emails to pre defined events that can be triggered by incoming sensor data from a FONA or any wifi enabled device. The FONA is the ultimate low power wireless device if you want it work outside of the normal range of a wifi hotspot.
Ubidots has the FONA and they plan to create a tutorial showing how to use the FONA + Arduino to send sensor data to Ubidots so you can view what is going on and easily trigger things to happen at just about any predefined setpoint. They already have this up and running on a SIM900 cellular board and just need to modify the code a bit to get it working properly on the FONA. I'll make sure they know about your revised code so they can include this bit of info into their tutorial.
I don't work for Ubidots, I just find their service a dream come true when it comes to interfacing with other web connected devices Aka "Internet of Things" devices. It allows you to easily view your data stream in alot of different ways + most importantly easily have a SMS or Email be sent out if your temp goes above a certain point. I think its perfect for the FONA type setups.
Here are some pictures of how I'm using their service with a temp and humidity sensor + Spark Core. I'm wanting to drop the Wifi for FONA's Cellular network.
- darrensillett
- Posts: 19
- Joined: Wed Aug 06, 2014 6:05 pm
Re: Fona GPRS APN setting
That's great! Thanks for merging it in. Looks like your refactoring to use flash memory strings will be useful for other functions.tdicola wrote:Thanks for making the change and sending the pull request! I tested it out and it works great. Made a few small tweaks to switch from a command buffer in RAM to use flash memory strings and merged it in.
For folks that need to set the APN, username, or password grab the latest version of the library from github and look at the comment added at the end of the setup() function. You should just need to uncomment it and fill in the APN and optional username and password.
Thanks again for sending the change!
For others, looking at this thread - please use the main Adafruit repo as the changes have been merged in.
- Leonides123
- Posts: 5
- Joined: Fri Jul 17, 2015 3:31 pm
Re: Fona GPRS APN setting
please help i'm using the code from instructables fona gsm tracking. everything seems to be working fine until i get to this problem
Set Connection Type To GPRS:
Set APN: OK.
GPRS Already on
ERROR: OK
sayin my gprs already on and then doesnt engage in gprs to give me my data.
how do i turn the gprs off so that when the code tries to enable it it will work
Set Connection Type To GPRS:
Set APN: OK.
GPRS Already on
ERROR: OK
sayin my gprs already on and then doesnt engage in gprs to give me my data.
how do i turn the gprs off so that when the code tries to enable it it will work
- tdicola
- Posts: 1074
- Joined: Thu Oct 17, 2013 9:11 pm
Re: Fona GPRS APN setting
Thanks for checking out the FONA! Just to check, can you paste the exact command and response that's failing? Is it the AT+SAPBR=1,1 command? That can help point us towards possible errors that the datasheet might mention.
To disable a GPRS connection manually you can load the FONA library's FONAtest sketch and use the 'g' command to stop GPRS connections (see how to install the library here: https://learn.adafruit.com/adafruit-fon ... duino-test )
Also if you want to run the raw commands to disable GPRS (like if you're using the sketch from the instructables project) you can see them here in the source of the FONA library: https://github.com/adafruit/Adafruit_FO ... #L782-L791
To disable a GPRS connection manually you can load the FONA library's FONAtest sketch and use the 'g' command to stop GPRS connections (see how to install the library here: https://learn.adafruit.com/adafruit-fon ... duino-test )
Also if you want to run the raw commands to disable GPRS (like if you're using the sketch from the instructables project) you can see them here in the source of the FONA library: https://github.com/adafruit/Adafruit_FO ... #L782-L791
- Leonides123
- Posts: 5
- Joined: Fri Jul 17, 2015 3:31 pm
Re: Fona GPRS APN setting
thanks for replying i downloaded the fona library fona test i got gprs location send messages and everything works fine i put g to disable the gprs then went to the instructables code and this is what it reads
FONA Already On, Did Nothing
Initializing: please wait 10 sec...
Disable echo: OK
Set to TEXT Mode:
Attach GPRS: OK
Set Connection Type To GPRS:
Set APN: OK.
GPRS Already on
ERROR: OK
i dont get gprs print out location like everyone else it says gprs already on b/c its trying to turn on but gets error b/c already on
FONA Already On, Did Nothing
Initializing: please wait 10 sec...
Disable echo: OK
Set to TEXT Mode:
Attach GPRS: OK
Set Connection Type To GPRS:
Set APN: OK.
GPRS Already on
ERROR: OK
i dont get gprs print out location like everyone else it says gprs already on b/c its trying to turn on but gets error b/c already on
- Leonides123
- Posts: 5
- Joined: Fri Jul 17, 2015 3:31 pm
Re: Fona GPRS APN setting
this so far so good on the instructables test passed flying colors
Serial Ready
Software Serial Ready
AT OK
at+cmgf=1 OK
at+sapbr=3,1,"contype","gprs" OK
at+sapbr=3,1,"apn","att.mvno" OK
at+sapbr=1,1 OK
at+cipgsmloc=1,1 +CIPGSMLOC: 0,-111.823471,33.345276,2015/07/20,15:28:18
OK
now the gsm location diffrent story. maybe my APN is wrong. i bought the gsm from ting so it said apn is wholesale is this correct or do i set up the apn through a phone first then plug and play on fona
Serial Ready
Software Serial Ready
AT+CMGF=1 OK
AT+CGATT=1 OK
AT+SAPBR=3,1,"CONTYPE","GPRS" OK
AT+SAPBR=3,1,"APN","WHOLESALE" OK
AT+SAPBR=1,1 +CME ERROR: 3
Serial Ready
Software Serial Ready
AT OK
at+cmgf=1 OK
at+sapbr=3,1,"contype","gprs" OK
at+sapbr=3,1,"apn","att.mvno" OK
at+sapbr=1,1 OK
at+cipgsmloc=1,1 +CIPGSMLOC: 0,-111.823471,33.345276,2015/07/20,15:28:18
OK
now the gsm location diffrent story. maybe my APN is wrong. i bought the gsm from ting so it said apn is wholesale is this correct or do i set up the apn through a phone first then plug and play on fona
Serial Ready
Software Serial Ready
AT+CMGF=1 OK
AT+CGATT=1 OK
AT+SAPBR=3,1,"CONTYPE","GPRS" OK
AT+SAPBR=3,1,"APN","WHOLESALE" OK
AT+SAPBR=1,1 +CME ERROR: 3
- tdicola
- Posts: 1074
- Joined: Thu Oct 17, 2013 9:11 pm
Re: Fona GPRS APN setting
Try "wholesale" in lowercase instead of uppercase. I'm not sure it's case sensitive but "wholesale" lowercase is what I use with the Ting SIM card. You can see more about their config settings here if it helps too: https://help.ting.com/hc/en-us/articles ... N-Settings If that still fails you might pop the card in a phone if possible just to double check, or call the Ting support to double check your specific SIM uses the APN value wholesale.
- Leonides123
- Posts: 5
- Joined: Fri Jul 17, 2015 3:31 pm
Re: Fona GPRS APN setting
hello guys i made some progress but still getting location get failed and the code reads to power down once location get is completed. i am using the teensy 3.1 i'll attach the code i'm using see if you guys see any thing wrong.
also im really bad a programing if i wanted to send live info to a cloud network service would i sill need the public key and private key info or just the url info.
disable echo: OK
long errors:
at+cmgf=1: OK
at+cgatt=1:
Connection Type: GPRS: OK
Set APN:wholesale
Engaged GPRS
ERROR: OKOK+CME ERROR: operation not allowed
Location GET Failed. powering down...
FONA Already On, Did Nothing
Initializing please wait 10sec...
+CIPGSMLOC: 0,-111.823463,33.345284,2015/07/29,18:04:08
also im really bad a programing if i wanted to send live info to a cloud network service would i sill need the public key and private key info or just the url info.
disable echo: OK
long errors:
at+cmgf=1: OK
at+cgatt=1:
Connection Type: GPRS: OK
Set APN:wholesale
Engaged GPRS
ERROR: OKOK+CME ERROR: operation not allowed
Location GET Failed. powering down...
FONA Already On, Did Nothing
Initializing please wait 10sec...
+CIPGSMLOC: 0,-111.823463,33.345284,2015/07/29,18:04:08
- Leonides123
- Posts: 5
- Joined: Fri Jul 17, 2015 3:31 pm
Re: Fona GPRS APN setting
here is where code starts to fail
OK
Code: Select all
void makeRequest() { //Make HTTP GET request and then close out GPRS connection
Serial.println("HTTP BEGUN!");
Serial.print("HTTPINIT: ");
//this checks if it is on. If it is, it's turns it off then back on again. (This Is probably not needed. )
if(sendATCommand("AT+HTTPINIT")){ //initialize HTTP service. If it's already on, this will throw an Error.
if(response != "OK") { //if you DO NOT respond OK (ie, you're already on)
Serial.print("term: ");
if(sendATCommand("AT+HTTPTERM")) { //TURN OFF
Serial.print("init: ");
if(sendATCommand("AT+HTTPINIT")) { //TURN ON
Serial.println(response);
}
}
} else {
Serial.println(response);
}
Serial.println(response);
}
if(sendATCommand("AT+HTTPPARA=\"CID\",1")){ //Mandatory, Bearer profile identifier
Serial.print("HTTPPARA, CID: ");
Serial.println(response);
}
/*
if(sendATCommand("AT+HTTPPARA=\"URL\",\"https://data.sparkfun.com/streams/ZGg0azOJX5hQb9492ywM\"")){ //you can also just make a simple GET request to any URL using this command
Serial.print("URL:https://data.sparkfun.com/streams/ZGg0azOJX5hQb9492ywM
Serial.println(response);
}
*/
Serial.print("Set URL: https://data.sparkfun.com/streams/ZGg0azOJX5hQb9492ywM/update/2mAJlWRG68uq2GBGkaMn");
if(sendURL()){ //builds the URL for Sparkfun. Lots of other options, see the datasheet:
//http://www.amescon.com/media/13865/sim800_series_at_command_manual_v101.pdf
Serial.println(response);
}
Serial.print("GET REQUEST: ");
if(sendATCommand("AT+HTTPACTION=0")){ //make get request =0 - GET, =1 - POST, =2 - HEAD
Serial.println(response);
}
Serial.println(">>> delay 2k");
delay(2000); //wait for a bit for stuff to complete
if(sendATCommand("AT")) { //clear out the serial Portat
Serial.println(response);
}
Serial.println(">>> delay 2k");
delay(2000); //wait for a bit more (may not be needed)
Serial.print("HTTP Read: ");
if(sendATCommand("AT+HTTPREAD")){ //Read the HTTP response and print it out
Serial.println(response);
}
Serial.println(">>> delay 2k");
delay(2000);//wait some more
if(sendATCommand("AT")) { // clear out the Serial port again
Serial.println(response);
}
Serial.println(">>> delay 2k");
delay(2000); //hang out some more, make sure everything is done working
Serial.print("HTTPTERM: ");
if(sendATCommand("AT+HTTPTERM")){ //Terminate HTTP session. (You can make multiple HTTP requests while HTTPINIT is active. Maybe even to multiple URL's? I don't know)
Serial.println(response);
}
Serial.print("Disengage GPRS: ");
if(sendATCommand("AT+SAPBR=0,1")){ //disengages the GPRS context.
Serial.println(response);
}
/*
Serial.print("X2");
if(sendATCommand("AT+SAPBR=0,1")){
Serial.println(response);
}
Serial.print("ATZ: ");
if(sendATCommand("ATZ")){
Serial.println(response);
}
if(sendATCommand("AT+CIPGSMLOC=1,1")){
Serial.println(response);
}
*/
}
boolean sendURL() { //builds url for Sparkfun GET Request, sends request and waits for reponse
int complete = 0;
char c;
String content;
unsigned long commandClock = millis(); // Start the timeout clock
fonaSS.print("AT+HTTPPARA=\"URL\",\"");
fonaSS.print("");
fonaSS.print(publicKey);
fonaSS.print("?private_key=");
fonaSS.print(privateKey);
for (int i=0; i<NUM_FIELDS; i++) {
fonaSS.print("&");
fonaSS.print(fieldNames[i]);
fonaSS.print("=");
fonaSS.print(fieldData[i]);
}
fonaSS.print("\"");
fonaSS.println();
while(!complete && commandClock <= millis() + ATtimeOut) {
while(!fonaSS.available() && commandClock <= millis()+ATtimeOut);
while(fonaSS.available()) {
c = fonaSS.read();
if(c == 0x0A || c == 0x0D);
else content.concat(c);
}
response = content;
complete = 1;
}
if (complete ==1) return 1;
else return 0;
}
boolean getLocation() { //sends GET LOCATION request and parses out response into Lat/Lon/Date/Time
int complete = 0;
char c;
String content;
unsigned long commandClock = millis(); // Start the timeout clock
fonaSS.println("AT+CIPGSMLOC=1,1");
while(!complete && commandClock <= millis()+ATtimeOut) { // Need to give the modem time to complete command
while(!fonaSS.available() && commandClock <= millis()+ATtimeOut); //wait while there is no data
while(fonaSS.available()) { // if there is data to read...
c = fonaSS.read();
if(c == 0x0A || c == 0x0D);
else content.concat(c);
}
if(content.startsWith("+CIPGSMLOC: 0,")) {
Serial.println("~~~~~~~~~~Got Location~~~~~~~~~~~~~~"); //+CIPGSMLOC: 0,-73.974037,40.646976,2015/02/16,21:05:11OK
Lon = content.substring(14, 24);
Lat = content.substring(25, 34);
Date = content.substring(35, 45);
Time = content.substring(46,54);
Serial.println(Lat);
Serial.println(Lon);
Serial.println(Date);
Serial.println(Time);
return 1;
} else {
Serial.print("ERROR: ");
Serial.println(content);
return 0;
}
complete = 1; //this doesn't work.
}
}
boolean sendATCommand(char Command[]) { //Send an AT command and wait for a response
int complete = 0; // have we collected the whole response?
char c; //capture serial stream
String content; //place to save serial stream
unsigned long commandClock = millis(); //timeout Clock
fonaSS.println(Command); //Print Command
while(!complete && commandClock <= millis() + ATtimeOut) { //wait until the command is complete
while(!fonaSS.available() && commandClock <= millis()+ATtimeOut); //wait until the Serial Port is opened
while(fonaSS.available()) { //Collect the response
c = fonaSS.read(); //capture it
if(c == 0x0A || c == 0x0D); //disregard all new lines and carrige returns (makes the String matching eaiser to do)
else content.concat(c); //concatonate the stream into a String
}
//Serial.println(content); //Debug
response = content; //Save it out to a global Variable (How do you return a String from a Function?)
complete = 1; //Lable as Done.
}
if (complete ==1) return 1; //Is it done? return a 1
else return 0; //otherwise don't (this will trigger if the command times out)
/*
Note: This function may not work perfectly...but it works pretty well. I'm not totally sure how well the timeout function works. It'll be worth testing.
Another bug is that if you send a command that returns with two responses, an OK, and then something else, it will ignore the something else and just say DONE as soon as the first response happens.
For example, HTTPACTION=0, returns with an OK when it's intiialized, then a second response when the action is complete. OR HTTPREAD does the same. That is poorly handled here, hence all the delays up above.
*/
}
void turnOnFONA() { //turns FONA ON
if(! digitalRead(FONA_PS)) { //Check if it's On already. LOW is off, HIGH is ON.
Serial.print("FONA was OFF, Powering ON: ");
digitalWrite(FONA_KEY,LOW); //pull down power set pin
unsigned long KeyPress = millis();
while(KeyPress + keyTime >= millis()) {} //wait two seconds
digitalWrite(FONA_KEY,HIGH); //pull it back up again
Serial.println("FONA Powered Up");
} else {
Serial.println("FONA Already On, Did Nothing");
}
}
void turnOffFONA() { //does the opposite of turning the FONA ON (ie. OFF)
if(digitalRead(FONA_PS)) { //check if FONA is OFF
Serial.print("FONA was ON, Powering OFF: ");
digitalWrite(FONA_KEY,LOW);
unsigned long KeyPress = millis();
while(KeyPress + keyTime >= millis()) {}
digitalWrite(FONA_KEY,HIGH);
Serial.println("FONA is Powered Down");
} else {
Serial.println("FONA is already off, did nothing.");
}
}
Last edited by adafruit_support_rick on Wed Jul 29, 2015 3:11 pm, edited 1 time in total.
Reason: please use code tags (</> button)when posting code
Reason: please use code tags (</> button)when posting code
- tdicola
- Posts: 1074
- Joined: Thu Oct 17, 2013 9:11 pm
Re: Fona GPRS APN setting
If it's still failing are you sure the card is activated and the account is ready to give you a GPRS connection? If it's a brand new Ting SIM card you need to contact Ting to have them activate it and get your account setup. I believe you just need to go this page and follow all the steps to get activated: https://ting.com/go/devices
Please be positive and constructive with your questions and comments.