Sensor value via SMS

hello,

i'm using my gsm shield to send the sensor value on A0 to my cellphone but i don't know how to attach the value which is in the loop to my message. will you help me to do it. I want to receive msg with sensor value on my cellphone like this
"Value of A0: ###"

the sketch was followed:

void setup() {
  Serial.begin(38400);
  delay(3000);
   
  initGSM();
  send_msg("09263749393", "Value of A0:");
}

void loop() {

  if(millis()<15000){
  float a = analogRead(A0);
  float b = analogRead(A1);
  delay(100);}
  
}

void send_msg(char *number, char *msga)
{
  char at_cmgs_cmd[30] = {'\0'};
  char msg1[160] = {'\0'};
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msga,ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
  
  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg1);
  delay(100);
}

thank you in advance

by the way i write sketch this way but it doesn't work.

  sprintf(msg1, "%s%c", msga,[b]a[/b],ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);

where a is a float value

There are several obvious problems.

You are trying to send the message by calling the gsm sending function in setup(), which is before you do the analog reading in loop(). Read one of the arduino tutorials to understand how setup() and loop() work.

sprintf() does not work work properly on arduino. To send a float value with a fractional part, you need to work around this problem.

To send a float value with a fractional part, you need to work around this problem.

That can be done using dtostrf().

You need to pay attention, in the sprintf() call to match the format specifiers to the argument types, AND to match the number of arguments to the number of format specifiers.

paulS,

i'm working on temp sensor. i have working sketch that displays temp value in serial monitor however i want it also to send via sms for remote monitoring.

my sketch which uses dtostrf() was this :

void setup() {
  Serial.begin(9600);
  pinMode(ledout,OUTPUT);
  delay(1000);
}


void loop() {
  if(millis()<30000){
  int sensor2 = analogRead(A1);
  digitalWrite(ledout,HIGH);
  float sensorvoltage = sensor2 * (5.0/1023.0);
  float temp = (sensorvoltage * 100.0) - 3.18 ;
  Serial.print("Body Temp(degree celcius): ");
  Serial.println(temp);
  char msg = dtostrf(temp, 2, 2, "Body Temp(degree celcius): %f");   // may i ask some help on how to use dtostrf since this is my first time also to use it.
  delay(50);
   }
   
  
  if(millis()>30000&&millis()<35000){ 
    initGSM();
    delay(2000);
    send_msg("09263749393", msg);
    delay(2000);
    }    
}
 

void send_msg(char *number, msg)
{
  char at_cmgs_cmd[30] = {'\0'};
  char msg1[160] = {'\0'};
  char ctl_z = 0x1A;

  sprintf(msg1, "%s%c", msg, ctl_z);
  sprintf(at_cmgs_cmd, "AT+CMGS=\"%s\"\r\n",number);
  
  sendGSM(at_cmgs_cmd);
  delay(100);
  delay(100);
  delay(100);
  sendGSM(msg);
  delay(100);
}


void sendGSM(char *string){
  Serial.write(string);
  delay(90);
}

your definition of send_msg() has no type specified for the second argument. It suspect this is invalid, it probably defaults to "int" and not to "char *" which you seem to be expecting.

You should write

void send_msg ( char* number,  char* msg )
{
}

may i ask some help on how to use dtostrf since this is my first time also to use it.

You certainly can, and google will be perfectly happy to answer.

hello,

will somebody correct my sketch? it doesn't have error however not executing.
this is my sketch

void loop(){  
    int swtchstate = digitalRead(swtch);
  if(swtchstate==HIGH){
    int sensor1 = analogRead(A0);
    float sensorvoltage = sensor1 * (5.0/1023.0);
    float temp = (sensorvoltage * 100.0) - 3.18 ;
    delay(60000);
    char msgx[160] = {'\0'};
    char buffer[10];
    dtostrf(temp,2,2,buffer);
    sprintf(msgx,"Temperature Value in degree Celcius: %s%c",buffer,ctl_z);
    initGSM();
    delay(1);
    send_msg("09159345239",msgx);
    delay(1800000);
  }

thanks a lot

it doesn't have error however not executing.

How do you know? How is your switch wired? Where is your setup() function? How is swtch defined?

paulS

the wire is just a jumper wire. this is the code on my setup();

void setup(){
  Serial.begin(9600);
  pinMode(ledout,OUTPUT);
  pinMode(swtch,INPUT);
  pinMode(indicator1,OUTPUT);

it's not working because i/m not receiving sms from arduino

actually, this was my code

int ledout = 13;
int swtch = 12;
int indicator1 = 10;
int indicator2 = 9; 

char Rx_data[50];
unsigned char Rx_index = 0;
int i = 0;
char msg[160];
int sig;
char ctl_z = 0x1A;



//setup
void setup(){
  Serial.begin(9600);
  pinMode(ledout,OUTPUT);
  pinMode(swtch,INPUT);
  pinMode(indicator1,OUTPUT);
  pinMode(indicator2,OUTPUT);
  delay(5000);
}



//Device Starts
void loop(){  
    int swtchstate = digitalRead(swtch);
   if(swtchstate==HIGH){
     int sensor1 = analogRead(A0);
    float sensorvoltage = sensor1 * (5.0/1023.0);
    float temp = (sensorvoltage * 100.0) - 3.18 ;
    delay(60000);
    char msgx[160] = {'\0'};
    char buffer[10];
    dtostrf(temp,2,2,buffer);
    sprintf(msgx,"Temperature Value in degree Celcius: %s%c",buffer,ctl_z);
    Serial.println(msgx);
    initGSM();
    delay(1);
    send_msg("09159345239",msgx);
    delay(1800000);}

else {
}
}

i'm sorry. i'm inputted wrong number so i didn't receive.

That delay is rather long. I think it will just sit there for half an hour ? Is that what you want ?