Best Approach(es) to Handling SMS Messages Over Atmega Serial
hi,
first quiestion - sending sms messages
what best way convert function 1 can call @ , pass message sending of differing data types each time?
e.g.
float mymessage = 37.5;
call send sms function
and later in program want send different data type using same function..
char mymessage[] = "some sms text";
call send sms function
this i've got working far testing want pass different messages function rather have them hard coded in it.
second question - receiving , parsing sms commands
i trying develop function read , process sms commands after atmega has woken low power sleep , has powered gsm module check sms messages.
i found example code these forums i've adapted little can test out , it's close want it's not quite like.
i have dozen settings / options in program hope changed / set / updated via received sms commands.
the problem code above expects number of commands in order.
what able either receive single command , process or receive multiple commands in no set order , process them - i'm not sure changes make or how there.
one thing trying keep resource usage on atmega down as possible trying find efficient solution.
any / appreciated. not in form of code pointers in direction of right logical steps / approach help.
many thanks.
first quiestion - sending sms messages
what best way convert function 1 can call @ , pass message sending of differing data types each time?
e.g.
float mymessage = 37.5;
call send sms function
and later in program want send different data type using same function..
char mymessage[] = "some sms text";
call send sms function
this i've got working far testing want pass different messages function rather have them hard coded in it.
code: [select]
// send sms data / messages function
void txsms(){
if(powerstate == 0){
digitalwrite(pwr_en, high); // main power control - turn on main 3.7v power - low = off | high = on
delay(450);
powerstate = 1;
}
digitalwrite(gsm_pwr, high); // gsm power control - turns gsm on - low = off | high = on
delay(20000); // wait network connection establish (led on gsm blinks once every second)
serial.println(f("at+cmgf=1")); // set modem sms text mode ready sending sms
delay(100);
// send mobile number gsm
serial.print(f("at+cmgs=\""));
serial.print(mobnumber);
serial.print(f("\"\r"));
delay(100);
float temperature = averagetemperature(temperatures, ophours);
//float battlevel = averagebattlevel(battlevels, ophors);
// send message content (readings)
serial.print(f("average temp: "));
serial.print(temperature);
//serial.println("battery v: ");
//serial.print(battlevel);
serial.print((char)26);
delay(100);
serial.println();
delay(2000);
if(powerstate == 1){
digitalwrite(pwr_en, low); // main power control - turn off main 3.7v power - dc regulator - low = off | high = on
powerstate = 0;
}
digitalwrite(gsm_pwr, low); // gsm power control - turns gsm off - low = off | high = on
}
second question - receiving , parsing sms commands
i trying develop function read , process sms commands after atmega has woken low power sleep , has powered gsm module check sms messages.
i found example code these forums i've adapted little can test out , it's close want it's not quite like.
code: [select]
#include <softwareserial.h>
softwareserial mygsm(2,3);
// example 5 - receive start- , end-markers combined parsing
const byte numchars = 32;
char receivedchars[numchars];
char tempchars[numchars]; // temporary array use when parsing
// variables hold parsed data
char messagefrompc[numchars] = {0};
int integerfrompc = 0;
float floatfrompc = 0.0;
boolean newdata = false;
//============
void setup() {
serial.begin(9600);
mygsm.begin(9600);
serial.println("this demo expects 3 pieces of data - text, integer , floating point value");
serial.println("enter data in style <helloworld, 12, 24.7> ");
serial.println();
//set sms mode ascii
mygsm.println(f("at+cmgf=1\r\n"));
delay(50);
//start listening new sms message indications
mygsm.println(f("at+cnmi=2,1,0,0,0\r\n"));
delay(50);
mygsm.println(f("at+cmgr=1\r\n"));
}
//============
void loop() {
recvwithstartendmarkers();
if (newdata == true) {
strcpy(tempchars, receivedchars);
// temporary copy necessary protect original data
// because strtok() used in parsedata() replaces commas \0
parsedata();
showparseddata();
newdata = false;
}
}
//============
void recvwithstartendmarkers() {
static boolean recvinprogress = false;
static byte ndx = 0;
char startmarker = '<';
char endmarker = '>';
char rc;
while (mygsm.available() > 0 && newdata == false) {
rc = mygsm.read();
if (recvinprogress == true) {
if (rc != endmarker) {
receivedchars[ndx] = rc;
ndx++;
if (ndx >= numchars) {
ndx = numchars - 1;
}
}
else {
receivedchars[ndx] = '\0'; // terminate string
recvinprogress = false;
ndx = 0;
newdata = true;
}
}
else if (rc == startmarker) {
recvinprogress = true;
}
}
}
//============
void parsedata() { // split data parts
char * strtokindx; // used strtok() index
strtokindx = strtok(tempchars,","); // first part - string
strcpy(messagefrompc, strtokindx); // copy messagefrompc
strtokindx = strtok(null, ","); // continues previous call left off
integerfrompc = atoi(strtokindx); // convert part integer
strtokindx = strtok(null, ",");
floatfrompc = atof(strtokindx); // convert part float
}
//============
void showparseddata() {
serial.print("message ");
serial.println(messagefrompc);
serial.print("integer ");
serial.println(integerfrompc);
serial.print("float ");
serial.println(floatfrompc);
}
void delsms(){
if(messageend == true){
mygsm.println("at+cmgd=1,4\r\n");
messageend = false;
serial.println(f("messages deleted"));
}
}
i have dozen settings / options in program hope changed / set / updated via received sms commands.
the problem code above expects number of commands in order.
what able either receive single command , process or receive multiple commands in no set order , process them - i'm not sure changes make or how there.
one thing trying keep resource usage on atmega down as possible trying find efficient solution.
any / appreciated. not in form of code pointers in direction of right logical steps / approach help.
many thanks.
i believe have found answer question one.. , function overloading.
multiple copies of same function each 1 set handle different data type.
so need guidance question two:- receiving , parsing sms commands.
thanks.
multiple copies of same function each 1 set handle different data type.
so need guidance question two:- receiving , parsing sms commands.
thanks.
Arduino Forum > Using Arduino > Project Guidance > Best Approach(es) to Handling SMS Messages Over Atmega Serial
arduino
Comments
Post a Comment