My father is a farmer and needs to keep his fruit warm during winter in his greenhouse.
I bought this arduino and made him this sensor which he can send a text to, to receive the greenhouse temperature and moisture levels.
The menu I programmed in the arduino is very basic:
Send 'S' -> Get current temperature and moisture level in greenhouse.
Send 'A' -> Activate or de-active alarm at a certain hard-coded temperature level.
Now, with the last one I had in mind to just program so that when he sends "T+" or "T-", the temperature value gets increased/decreased.
But here is the problem, I can not read strings from the received text message.
When reading the text message with the MKR library, I get the text message in characters.
My question is how can I put the text message in a string format?
TL;DR - I need to read a string from GSM MKR library gsm.read().
Put each character in an array of chars as they arrive, increment the array index and put a '\0' in the next array position. When you have received the last character the message will be in the array in the form of a C string
void loop(){
if (sms.available()) {
sms();
}
}
void sms(){
char c;
int ndx = 0;
char receivedText[5];
while (c = sms.read()){
receivedText[ndx] = c;
ndx++;
}
}
If I send "Hi" for example and I print the receivedText array I get:
Hi?????????????????????????????????????????????.....(question marks to infinity).
When I add the following to the while loop:
if (ndx > sizeof(receivedText)){
break;
}
It does "Hi??? "
Still not what I want.
I know I must append '\0' to make it a String.
But I want those question marks removed first.
I think it has something to do with the fact that the sms.read() returns a -1 and thus is not a printable character. But how to avoid it?
I have tried adding
if (c < 0){
break;
}
But that still gives me the question marks.
In the tutorial you sent me, they suggest using a start and end bracket for each message.
IMO this is not a solution, rather a quick fix.
Tells you that there is at least one character available to read, but maybe only one. However, you take it as a signal that the whole message has been received and attempt to read it
That is not the only method described, but have it your own way. Without at least an end of message marker or a fixed length message how will you know that the whole message has been received
It is certainly not a bad way, but try explaining this to my 62 year old father that he must type '>' after every text. He can barely work with a phone.
What I can do is what you suggested, to make it a fixed length message only. Like 3 chars or something.
That still does not explain the question marks, or how to avoid them at least.
No. You append a '\0' to turn the char array into a string (lowercase s). There is a big difference between the two
sms.read() returns a -1 when there is nothing available in the buffer to read. Don't read a character unless there is one available or read a character and ignore the -1s