How to change SMS content ( subject) in the void function

hi
i am not an expert in Arduino programing and i am a beginner in the Arduino

void sendSMS1() {
  //Serial.println("Switch was turned off ");
  GPRS.println("AT+CMGS=\"+917987553768\"");
  delay(500);
  GPRS.println ("Switch was turned off ");
  GPRS.write( 0x1a ); // ctrl+Z character*/
  delay(500);
}

This is my send SMS() function.

My questions are:

  1. How do I put the different SMS content (subject) in the function sendSMS() in different state ?

I don't want to create different SMS functions like send SMS1(),send SMS2(),send SMS3() etc,based on state changes.

Kindly help me to sort out and simplify the program.

thanks

void sendSMS1(char * message) {
  //Serial.println(message);
  GPRS.println("AT+CMGS=\"+917987553768\"");
  delay(500);
  GPRS.println (message);
  GPRS.write( 0x1a ); // ctrl+Z character*/
  delay(500);
}

Pass the message that you want to send as a parameter to the function

Something like this (untested)

void sendSMS1(char * message)
{
  GPRS.println("AT+CMGS=\"+917987553768\"");
  delay(500);
  GPRS.println (message);
  GPRS.write( 0x1a ); // ctrl+Z character*/
  delay(500);
}

and call it like this

sendSMS1("Switch was turned off ");
sendSMS1("Switch was turned on ");
sendSMS1("Some other message");

My SMS parameters includes like this

GPRS.println ("Switch was turned off "+(Temp))

GPRS.println ("Switch was turned ON "+(Humi))

In this "Temp" and "Humi" is an integar variable .Then how can i call the function Send SMS1()?

Are you using Strings or strings ?

String

If you feel that you absolutely must use Strings and not strings then change the parameter of the command variable in the function to String and pass it a String

void setup()
{
  Serial.begin(115200);
  testPrint("ONE");
  int temp = 123;
  String message = "The temp is " + (String)temp;
  testPrint(message);
}

void loop()
{
}

void testPrint(String command)
{
  Serial.println(command);
}

Strings have a bad reputation when used in the small memory of the average Arduino and C style strings are to be preferred but it is, of course, up to you wnat you use

When i used the strings instead of Strings compiler shows error.Why?

In my program also have memory issues,How i can use string , please provide more details.

Post a complete sketch showing what you tried

this is for checking and understanding purpose. In this program i have used "string" in place "String" but compiler shows error. Is this any particular library?




int count;
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int temp = 345;
  int humi = 125;

  count = count + 1;
  Serial.println(count);


  if (count == 5)
  {

    string message = string ("The temp is ") + (String) temp;
    testPrint(message);

  }
  else if (count == 10)
  {

    string message = "The temp is " + (string)humi;
    testPrint(message);
    count = 0;
  }
  delay(500);

}
void testPrint(String command)
{
  Serial.println(command);
}

if i have use "char" in place of String ,How i can convert this line String message = "The temp is " + (String)temp;

in this line i have stucked.Please help me

You don't have to. Just print them consecutively. All output goes to the same place. If you need to concatenate C strings in a buffer, you use strcat().

char message[50];
 
sprintf(message, "The temp is %d", temp);

i applied strcat() Here is my code

int val = 0;
char out_str[]="String problem";
void setup() {
  Serial.begin(9600);

}
void loop() {
  val = val + 1;
  Serial.println(val);
  if (val == 10)
  {
    strcat(out_str,(val));
    Serial.println(out_str);
    val = 0;
  }
  delay(500);
}

But value of variable not added in Serial output

serial monitor o/p

1
2
3
4
5
6
7
8
9
10
String problem
1

serial out put supposed to come String problem 10 but its not shows like that.....why?

strcat(out_str,(val));

The problem here is that val is not a string, it is an integer and you must convert it to a string using the itoa() function before it can be concatenated.

char valChars[8];  //somewhere to put the output string
itoa(val, valChars, 10);  //convert val to a string, put it in the buffer, use base 10 conversion
strcat(out_str,valChars);  //concat val as a string to the output
1 Like

here is my code

char valChars[10];
int val = 0;
char out_str[]="String problem ";

void setup() 
{
  Serial.begin(9600);

}
void loop() {
  val = val + 1;
  Serial.println(val);
  itoa(val, valChars, 10);
  if (val == 10)
  {
    
    strcat(out_str,valChars);
    Serial.println(out_str);
    val = 0;
  }
  delay(500);
}

But Serial monitor shows erratic values.

String problem 1010
1
2
3
4
5
6
7
8
9
101
2
3
4
5
6
7
8
9
10
String problem 10

why this happend?

char out_str[]="String problem ";

You have told the compiler to allocate mmeory for out_str that will hold that phrase, which is 16 characters long (15 actual characters and a terminating '/0') but then later you add more characters to it

strcat(out_str,valChars);

and trample all over memory used by some other part of the sketch. Declare the out_str array large enough to hold the fixed text and the added text of the value

char out_str[30]="String problem ";
1 Like

I have declared the size of char char out_str[30]="String problem ";

But serial monitor shows the "valChars" adding the old value to new value..?

String problem 11
1
2
3
4
5
6
7
8
9
10
11
String problem 1111
1
2
3
4
5
6
7
8
9
10
11
String problem 111111

Every time you use strcat() it concatenates the string you pass it to the existing string. Move the declaration of out_str to just before the concatenation so that when the strcat() occurs only the major portion of the string is in out_str and not what remains from the previous concatenation

If that still does not do what you want then please post the full sketch again