0

Feather 32u4 FONA AT+CBC value to sms
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Fri Nov 13, 2020 10:30 am

Hello!

How can I send the AT+CBC value as a sms ?

I've tried:
Declared "batt" as char, string, int. All I get are weird signs on the sms to the phone.
But it show right on the serial monitor.

fona.println("AT+CBC");
updateSerial(); //loop function --> serial.available
batt = fona.read();
....
...
fona.print(batt); // to send as sms.
delay(1000);
fona.print((char)26);
...


and i also tested:

fona.sendSMS(callerIDbuffer, batt);
same result.

Regards
Cristian

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Re: Feather 32u4 FONA AT+CBC value to sms

by rskup on Fri Nov 13, 2020 6:03 pm

Just tossing this out there, but have you looked at the AT+CMGF (SMS Message Format) command?

You may have AT+CMGF=0 set, you probably want AT+CMGF=1

rskup
 
Posts: 60
Joined: Sat Aug 01, 2020 9:04 pm

Re: Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Sun Nov 15, 2020 8:51 am

Hi!

Thanks for the answer/guess :-). Yes I already have the AT+CMGF=1.

Do you know what "declaration" (char/String/int...) is using when sending SMS. Do I have to use char? or something else?

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Re: Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Sun Nov 15, 2020 9:58 am

When I use the AT+CBC command I get in the serial monitor:
+CBC: 0,99,4194

but...
How do I save/send that as an SMS ?

When I use:
batt = fona.read();

It seems to save it "wrong" beacouse when I use Serial.println(batt) it shows strange signs dependent on how I have declared "batt".

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Re: Feather 32u4 FONA AT+CBC value to sms

by rskup on Sun Nov 15, 2020 11:16 am

Ok, next toss (guess):

What about using fona.readString() rather than fona.read()?

https://www.arduino.cc/reference/tr/lan ... eadstring/

If that doesn't work, will fire up my fona 3g and try to duplicate what you're doing, sending the battery voltage as an SMS would be a useful addition for a gps tracker I'm trying to build, I like that idea.

rskup
 
Posts: 60
Joined: Sat Aug 01, 2020 9:04 pm

Re: Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Mon Nov 16, 2020 7:22 am

Hi!
Thanks rskup!
It works whit fona.readString() :-)
Now I get the entire SMS (AT + CBC) string to my phone. BUT I also hade to remove "updateSerial();" after the "fona.println("AT+CBC\r\n");"
otherwise it does not seem as if there is any value left to retrieve.

The result in the phone:
+CBC: 0,93,4144

OK


So I am very happy.. but I will try to break out a certain part of the string and just send one part of it ...
maybe using substring()?!
https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/substring/

This is the working code:

Code: Select all | TOGGLE FULL SIZE
String batt = "";
.
.

void battery()
{
  fona.println("AT+CMGF=1\r");
  updateSerial();
  fona.println("AT+CBC\r\n"); //Battery level
  //updateSerial();
  batt = fona.readString();
  Serial.println("Sending Data..."); 
 
  delay(1000);
  fona.print("AT+CMGS=\"+******************\"\r");  //phone number and country code
  delay(1000);
  Serial.println(batt);
  delay(1000);
  fona.print(batt);  //This string is sent as SMS
  delay(1000);
  fona.print((char)26);//Tell the module that it can send the SMS
  delay(1000);
  fona.println();
  Serial.println("Data Sent.");
  delay(1000);
 
}

void updateSerial()
{
  delay(500);
  while (Serial.available())
  {
    fona.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(fona.available())
  {
    Serial.write(fona.read());//Forward what Software Serial received to Serial Port
  }
}

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Re: Feather 32u4 FONA AT+CBC value to sms

by rskup on Mon Nov 16, 2020 2:36 pm

Hey Cristian, cool, things are starting to work.

If you're just looking to only send the first line, I have used the "replace()" command for stripping out parts of a reply I didn't want.

So I played around a bit with my FONA 3G and I think (keyword - think) this will work for you:

batt = fona.readString();
batt.replace("\r","");
batt.replace("\n","");
batt.replace("OK","");

That should strip out the return character, the new line character, and the "OK".

Then your "batt" string should just be the "+CBC: 0,xx,yyyy" and nothing else.

fyi, I did try combining all three replace() lines into one like:

batt.replace("\r\nOK","");

but that did not work at all.

Ok, hope things keep going well!

rskup
 
Posts: 60
Joined: Sat Aug 01, 2020 9:04 pm

Re: Feather 32u4 FONA AT+CBC value to sms

by rskup on Mon Nov 16, 2020 6:22 pm

Yep, me again.

If you only want to pull the milli-volts out of batt, then try:

batt = fona.readString();
String battSub = batt.substring(batt.lastIndexOf(",")+1,batt.lastIndexOf(",")+5);

rskup
 
Posts: 60
Joined: Sat Aug 01, 2020 9:04 pm

Re: Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Tue Nov 17, 2020 8:07 am

Hi!

Thanks again rskup! Thank you very much for helping me whit the code! :-)

I used your code line and added so that the sms shows ***** mV and % left of the battery.
For example:

The message in the SMS

4200 mV
99%


Code: Select all | TOGGLE FULL SIZE
void battery()
{
  fona.println("AT+CMGF=1\r"); //Set the module to SMS mode
  updateSerial();
  fona.println("AT+CBC\r\n"); //Battery level
  batt = fona.readString();
  String battSubV = batt.substring(batt.lastIndexOf(",")+1,batt.lastIndexOf(",")+5);
  String battSubP = batt.substring(batt.lastIndexOf(",")+0,batt.lastIndexOf(",")-2);
 

  Serial.println("Sending Data...");   
  delay(500);
  fona.print("AT+CMGS=\"+******************\"\r");// Phone number,  include country code
  delay(500);
  Serial.println(battSubV);
  Serial.println(battSubP);
  delay(500);
  fona.print(battSubV + " mV" + "\r\n" + battSubP + " %");  //This string is sent as SMS
  delay(500);
  fona.print((char)26);//Tell the module that it can send the SMS
  delay(500);
  fona.println();
  Serial.println("Data Sent.");
  delay(500);

}

void updateSerial()
{
  delay(500);
  while (Serial.available())
  {
    fona.write(Serial.read());//Forward what Serial received to Software Serial Port
  }
  while(fona.available())
  {
    Serial.write(fona.read());//Forward what Software Serial received to Serial Port
  }
}

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Re: Feather 32u4 FONA AT+CBC value to sms

by rskup on Tue Nov 17, 2020 2:19 pm

Hey Cristian, I like how that looks, nice addition to any Fona project!

Dawned on me -- the "percent battery" could be one to three digits depending on its value (1-100).

Just replace your:

String battSubP = batt.substring(batt.lastIndexOf(",")+0,batt.lastIndexOf(",")-2);

with this:

String battSubP = batt.substring(batt.indexOf(",")+1,batt.lastIndexOf(","));

Now it extracts the percent number between the two commas, whether one, two, or three digits.

Shouldn't have to worry about the millivolts, will always be 4 digits since the FONA won't work below about 3 volts, much less 1 volt.

Ok, hope all's well!

rskup
 
Posts: 60
Joined: Sat Aug 01, 2020 9:04 pm

Re: Feather 32u4 FONA AT+CBC value to sms

by Cristian_S on Wed Nov 18, 2020 8:49 am

Hello rskup!

Thanks again :-)
I noticed yesterday when I tested and the battery was fully charged that it showed 00 :-) then understood that I have to look at that line of code again. But then you had already fixed the solution for me :-) Thank you!

Regards
Cristian

Cristian_S
 
Posts: 9
Joined: Fri Nov 13, 2020 10:15 am

Please be positive and constructive with your questions and comments.