4

I am using an Arduino Mega board with a SIM900 GSM.

My goal is to be able to text the GSM with a value of two digits (ex: 79) and then store that value as a variable in the code. I know how to set up the Mega and the GSM in terms of wiring and initialization of ports, but how would I write a short function to test the incoming values and save them to a variable?

I thought about testing each bit individually, storing each one as a separate variable, and then adding them for the total. For example, if the GSM received "#79" as a text I stored the value 7 as an integer variable named One and the value 9 as an integer variable named Two. Then I did

Total = (One*10)+Two. 

I am posting my code below that used this method, although it has not proved to be useful. If anyone has any suggestions I would greatly appreciate it.

#include <SoftwareSerial.h> //GSM Library 
int Max_temp; 
float threshold; 
SoftwareSerial SIM900(18, 19); 
String destinationNumber = "+17186578989"; 
String outMessage1 = "Please enter the maximum desired room temp"; 
char inchar; // Will hold the incoming character from the GSM shield when user texts it 
int M_one;
int M_zero;
void GSM_SEND_MAX(); 
void GSM_RECEIVE_MAX(); 
void setup() 
{
 Serial.begin(9600); 
//GSM setup: 
 Serial1.begin(19200); // wake up the GSM shield
 delay(20000); // give time to log on to network.
 Serial1.print("AT+CMGF=1\r"); // set SMS mode to text
 delay(100);
 Serial1.print("AT+CNMI=2,2,0,0,0\r"); // blurt out contents of new SMS upon receipt to the GSM shield's serial out
 delay(100);
}// end void set up() 
void loop() 
{
 GSM_SEND_MAX();
 GSM_RECEIVE_MAX();
}// end void loop ()
/////Functions 
void GSM_SEND_MAX() 
{
 Serial.println("sending text"); 
 Serial1.begin(19200); 
 delay(20000); // give time to log on to network.
 Serial1.print("AT+CMGF=1\r");
 delay(1000);
 Serial1.println("AT + CMGS = \"" + destinationNumber +"\"");
 delay(1000);
 Serial1.print(outMessage1);
 delay(1000);
 Serial1.write((char)26); //ctrl+z
 delay(1000);
 Serial.println("send text");
}//end GSM_SEND_MAX funtion 
void GSM_RECEIVE_MAX() 
{
 //If a character comes in from the cellular module...
 while (Serial1.peek() != '#' ) 
 {
 inchar=Serial1.read(); 
 Serial.print("bad char "); 
 Serial.println(inchar); 
 delay(500);
 }// end while #1 
 inchar=Serial1.peek(); 
 Serial.println("out of loop 1"); 
 Serial.println(inchar); 
 while (Serial1.available() != 0) //While #2 reads incoming char 
 {
 Serial.println("in loop 2"); 
 inchar=Serial1.read(); 
if (inchar=='#') 
 {
 delay(10);
 Serial.print("got #"); 
 M_one=Serial1.read(); 
 if (M_one >= 0 )
 {
 delay(10);
 Serial.println();
 Serial.print("got M_one"); 
 Serial.print(M_one);
 M_zero=Serial1.read(); 
 if (M_zero >= 0)
 {
 Serial.println();
 Serial.print("got M_zero"); 
 Serial.print(M_zero); 
 }//end m_zero 
 }//end m_one 
 Max_temp = (M_one *10) + M_zero; 
 Serial.println();
 Serial.print("Max_temp=");
 Serial.print(Max_temp); 
 } //end if (inchar==#)
 }// end while #2 loop 
}//end GSM_RECEIVE_MAX function
Greenonline
3,1527 gold badges36 silver badges48 bronze badges
asked Apr 19, 2016 at 1:57
2
  • There is a code formatting button in the post editor. Highlight your code and click the {} button. Commented Apr 19, 2016 at 3:46
  • The code uses a char as int without conversion. See electronics.stackexchange.com/questions/38845/… for details on how to convert. Commented Aug 30, 2017 at 19:38

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.