Okay, so I have some code that listens for an SMS, when the correct starting phrase is seen it processes the data and stores it.
however using
while(1)
this seems limited to about 7 messages incoming and then stops working completely on the 8th command sentm, it doesn't recognise the command sent even if the same command has been set each time.
SMS1078xxxxxxxx or
MSG1abcdefghijk
doesn't matter to be honest
I need to get it to go on indefinitely, coming out of the loop to process the command and then go back to listening. for the next command.
void RecieveMessage() {
Serial.println("listening");
while (1) {
if (mySerial.find("+CMT: \"")){
nor =mySerial.readString();
nors = nor.substring(0,(13));
Serial.println(nors);
mySerial.println(nor);
if(mySerial.find("SMS")){
no1 = mySerial.readString();j=4;
ProcessNumber();
}else {
if(mySerial.find("MSG")){no1 = mySerial.readString();j=3;
ProcessNumber();
}else {
if(mySerial.find("SITE")) {no1 = mySerial.readString();
Site();
}else {
if(mySerial.find("STATUS")) {no1 = mySerial.readString();
Status();
}else {
Serial.println("Command not recognised, please try again");
no1 = mySerial.readString();
Unrecon();
}
}
}
}
}
} //end while
}//end recieve messages function
it is then processed by
void ProcessNumber()
{
Serial.println(no1);
no2 =no1;
no2.remove(1);
delay(100);
int i = no2.toInt();
i=i-1;
// mySerial.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode
mySerial.print("AT+CMGS=\"" + nors +"\"\n\r"); // Replace x with mobile number mySerial.println("AT+CMGS=\"xxxxx\"\n\r");
if (no1.length()<14 && j==4){
mySerial.print("Not enough numbers, try again");
}else{
if (no1.length()>14){
if (j==4){mySerial.print("Too many numbers, try again");
}else if (j==3){
mySerial.print("Too many characters, try again");}
}else{
if (j==4){i=i;}else if (j==3){i=i+9;}
if(j<=4){
Serial.print("Saving variable string to address: ");
Serial.println(i * 14);
Serial.println(no1);
myString = no1;
myString.toCharArray(myStringChar, BUFSIZE); //convert string to char array
strcpy(buf, myStringChar);
eeprom_write_string((i * 14), buf);
Serial.print("Reading string from address : ");
Serial.println(i * 14);
eeprom_read_string((i * 14), buf, BUFSIZE);
Serial.println(buf);
if (j==4){mySerial.print("Number ");i=i;}
else if (j==3){mySerial.print("Message "); i=i-9;}
mySerial.print(i+1);
mySerial.print(": ");
no1s = no1.substring(1,(12));
mySerial.print("Stored as: ");
mySerial.print(no1s);
}else{
mySerial.print("Only allowed 5 Numbers");
}mySerial.println((char)26);// ASCII code of CTRL+Z
RecieveMessage();}}
}
// eventually all the recieved messages will be processed in the process number code above.
I am still looking into the loops and only been able to get the code to recieve infinite commands via SMS for SMS only and then nothing for MSG, not posted this code as still trying to work out why the loop sor of works but only processes te one command type.
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).
When using Cstrings you must use strcmp() to compare values rather than ==
If you need more help please post a complete program. Add the .ino file as an attachment if it is too long - but hopefully it won't be.
okay I will post all the code later out and about at the minute.
it doesnt store unlimited data, it needs to be able to process it, although once setup its unlikely to change much at all.
currently it would store in eeprom 5 numbers 4 messages, and a SITE ID.
the current code only recieves upto 7 commands via SMS before it flakes out.
to first program the information required I would need to send a minimum pf 10 SMS commands.
SMS1.xxxxx to SMS5.....
MSG1.xxxx to MSG4
SITE ID
these can be requested in STATUS command but that isnt storing any data. its just recalling all stored data.
the issue is CMDs being recieved that are sent bySMS seems to have a limit, rather than there shouldnt be a limit to how many cmds can be received.
currently to program the unit with the details required, i would have to reboot the unit once ie & + 7 commands giving me 4 spare.
ie 5 + 4 + 1 command, but I can only send 7 with it flaking out.
When something works and then stops working after a while, memory is most likely the issue with arduino's. Using "String" is a problem - but you are not posting all your code, so for now memory-issues related to the usage of the "String" class is the best guess.
/// whilst this isn't code its output thought it make sense to make it scrollable?
AT+DDET=1
OK
AT+GMD=1,4
OK
On pressing r, it opens RecieveMessage() and displays:-
Listening
From Mobile SEND SMS MSG1abcdefghij1 to 9 we get the following:-
listening
+4478xxxxxxxx
1abcdefghij1
1
1abcdefghij1
14
Saving variable string to address: 126
1abcdefghij1
Reading string from address : 126
1abcdefghij1
+
Command not recognised, please try again
listening
+447815698180
Command not recognised, please try again
listening
+4478xxxxxxxx
1abcdefghij2
1
1abcdefghij2
14
Saving variable string to address: 126
1abcdefghij2
Reading string from address : 126
1abcdefghij2
etc etc up till:-
+4478xxxxxxxx
1abcdefghij6
1
1abcdefghij6
14
Saving variable string to address: 126
1abcdefghij6
Reading string from address : 126
1abcdefghij6
listening
+44781xxxxxxx
1abcdefghij7
1
1abcdefghij7
14
Saving variable string to address: 126
1abcdefghij7
Reading string from address : 126
1abcdefghij7
listening
+ //at the point MSG1abcdefghij8 is submited
Command not recognised, please try again
listening
+44781xxxxxx //at the point MSG1abcdefghij9 is submited
Command not recognised, please try again
listening
note as its the same command it saves to the same address ie MSG1 saves to 126, it moves on if I use MSG2.... etc etc, but the max number of SMS's sent before it falls over is 7.
doesn't matter if I mix the commands with SMS instead of MSG, just the storage points change.
Please note all the code below is not finished, the processes involved in this case are
RecieveMessage(); then followed by ReturnMessage(); then back to RecieveMessage() ;
code in next post, be aware some code may be duplicated in seperate functions, but will be merged at a later point.
The "String" class is your biggest problem, so remove any use of it. Another thing that may become a problem is the excessive use of EEPROM write's - after 100.000 writes, EEPROM cells will be worn out. AFAICT you application and coding is more aimed at a Raspberry Pi than it is an Arduino - but with the right changes it will work with an arduino!
Maybe I am finding this a little too confusing at present.
The data needs permanently storing: so what are the options?
As for working the closest I have it to working, is it being limited to 7 commands being sent and being processed properly then it stops.
so I need the data to be read and processed every time its sent, but it must be permanently stored as it.
What is the issue with String? the code currently only occupies just over a third of the space available and the variables under half, although some of the strings will be easier to get rid of than others.
As there is too much code, due to duplication, I will simplify tomorrow probably, however I am interested in as to what you think I need to be looking as viable alternatives to what I have so far.
Removing your source code is not getting you any help.. The MMU (or the lack thereof) on an Arduino is very crude and it is not good at handling dynamic memory usage. For long term stability you should allocate all the memory you need before the "setup()" function completes. All memory allocated during the "loop()" phase requires expert knowledgement of how the memory on an Arduino works. The "String thing" uses dynamic memory "uncontrollably" and even an expert may not be able to use it without running into problems.
The data needs permanently storing: so what are the options?
Depends on how much data you need to permanently store. The builtin EEPROM isn't very big. An SD card is many orders of magnitude larger.
What is the issue with String?
Mostly, the fact that you do NOT know whether any given instance of the String class actually allocated the required memory to append the new data to the existing instance. The fact that all that dynamic memory allocation takes place, with little memory, means that you can end up not being able to allocate the required amount of memory. On a larger system, that would result in an exception being thrown. On the Arduino, exception handling consists of "Forget it. It wasn't that important".
however I am interested in as to what you think I need to be looking as viable alternatives to what I have so far.
Mostly what WE are interested in is the code you haven't posted. You CAN attach your code if it exceeds the 9000 character limit.
PaulS:
Mostly what WE are interested in is the code you haven't posted.
In fact he did post the entire code (Sigh.. 13 global instances of String and all the local ones on top of that), but he removed it again for some reason. I guess he is part of The Lone Gunmen which causes his code to be clandestine. LOL! :-/
I haven't posted all the code because it was too long at the time.... over 9000 character warning, 5 minute warning, 30 second warning got my well and truly fried and time for a break and refuelling, not all of us are super human.
as I can only post every 5 mins splittng got messy etc etc. (excuses but fact) so appeared and disappeared some it certainly did.
as for the strings, yes there were lots of them but in reality some of them werent in use and I just hadn't got round to cleaning up properly (bad I know).
as for the strings, yes there were lots of them but in reality some of them werent in use and I just hadn't got round to cleaning up properly (bad I know).
I don't understanding creating anything - arrays, Strings, or any type variable - until you NEED it. If you don't create horseshit in the first place, you don't need a shovel to clean it up later.