0

I'm still inexperienced in programming and i apologize in advance if my mistakes are stupidly obvious. This sketch I'm working on is a basic GSM project using SIM900a Mini and an Arduino UNO that could send a message to a recipient with a press of a button. I created a function so that if i send a command to the GSM, i could change the recipient and check the current recipient through my phone.

One thing to note is that before i resorted to creating this sketch, I created an almost working project with the same concept using SIM800L and an arduino Nano and i used the GSM Library. However, when it was hooked up to a variable Step-up converter for the needed supply of the ~4 V using a 3.7V Li-Po battery, it was damaged because the booster suddenly supplied 12V to the GSM. The SIM800L won't connect to the network anymore after that incident. I resorted to using SIM900a-Mini and it wasn't compatible with the GSM Library i guess because it can't get pass the initialization stage. Though i've seen youtube videos using both the SIM900 and the GSM Library, though i can't seem to make it work even though the two share the same chipset (i think). This current sketch works with some issues. Here's the sketch:

#include <SoftwareSerial.h>
#include <SIM900.h>
#include <sms.h>
#define button 10
SMSGSM sms;

char senderNumber[15], recipientNumber[15] = "No recipient", newRecipient[15], commandkey[30] = {0};
char SmsText[30], smsRecieved[30];;
boolean validRecip, GSMconnected = false;
int buttonState = 0;

void setup(){
  Serial.begin(9600);
  Serial.println("Micro_GSM_Project");
  Serial.print("Initializing Serial:");
  pinMode(button, INPUT);
  pinMode(buzzer, OUTPUT);
  while (!Serial){
    Serial.println("Serial not connected");
  }
  delay(500);
  Serial.println(" Done");
  delay(1000);
  Serial.print("Initializing GSM Module:");
  if (gsm.begin(2400)) {
    Serial.println("\nWaiting for messages");
    GSMconnected = true;
    delay(1000);
  }
  else Serial.print("GSM Connecting");
}

void loop() {
  if (GSMconnected)
    {
    buttonState = digitalRead(button);
    if (buttonState == HIGH)
    {
      activate();
    }
    if (gsm.readSMS(SmsText, 30, senderNumber, 15))
    {
      Serial.print("\nMessage received from: +");
      Serial.println(senderNumber);
      Serial.println(SmsText);
      parseData();

     if (strcmp(commandkey, "CURRENT RECIP") == 0) 
        {
          currentRecip();
        }
     else if (strcmp(commandkey, "SET RECIP") == 0) 
        {
          changeRecip();
          confirmation();
        }
     else
     Serial.println("Invalid Command");
     sms.SendSMS(senderNumber, "The command you sent is invalid");
    }
  }
}

void activate() {
  delay(800);
  Serial.println("I have become self aware. Run");
  sms.SendSMS(recipientNumber,"I have become self aware. Run");
}

void parseData() {
  char * strtokIndx;
  strtokIndx = strtok(SmsText, ",");  //Read first part of Command either "SET RECIP" or "CURRENT RECIP"
  strcpy(commandkey, strtokIndx);         //store to commandkey variable

  strtokIndx = strtok(NULL, ",");         //Read where the last call left off, separated by a comma
  strcpy(newRecipient, strtokIndx);       //store to newRecipient variable

  Serial.print("Command Selected:");
  Serial.println(commandkey);
}

void currentRecip() {
  Serial.print("Current Recipient is: ");
  Serial.println(recipientNumber);
  delay(1000);
  sms.SendSMS(senderNumber, "Current Recipient is: recipientNumber");// i currently dont know how to log the variable recipientNumber to this string
}

void changeRecip() 
    {
  validate();
    if (validRecip == true) 
    {
    strcpy(recipientNumber, newRecipient);
    sms.SendSMS(senderNumber, "The Recipient number you have entered is valid");
    Serial.print("Recipient Number: ");
    Serial.print(recipientNumber);
    Serial.println("a");
    Serial.print("Sender Number");
    Serial.println(senderNumber);
    }
    if (validRecip == false) 
    {
      Serial.println("Invalid Recipient");
      sms.SendSMS(senderNumber, "The Recipient number you have entered is invalid");
    }
    }

void validate() 
  {
  if (strlen(newRecipient) == 13)   //I've put 13 here as a reference because the output strlen of the parsed newRecipient is 13 instead of the 12 characters composing the newRecipient i sent through sms. I think this is the null terminating character used by the strtokIndx? Please correct me if I'm wrong
  {       
    Serial.println("Recipient is Valid\n");
    validRecip = true;
  }
  else
  {
    validRecip = false;
  }
}

void confirmation() 
{
    if (validRecip == true)
    {
    Serial.println("Recipient Number successfully changed");    //Serial Monitor confirmation (for troubleshooting)
    Serial.print("New Recipient: +");
    Serial.println(recipientNumber);
    Serial.println("Informing Admin...");
    }
    if (sms.SendSMS(senderNumber, "Recipient Number successfully changed\nNew recipient: recipientNumber"))  //same here
    {  
    Serial.println("Message Sent\n\n");
    }
    else 
    {
    Serial.println("Invalid Recipient");
    sms.SendSMS(senderNumber, "The Recipient number you have entered is invalid");     //uncomment for SMS confirmation, need load
    }
}


These are the current issues with my current code (in order of priority) .

  1. Even though the serial monitor displays the correct new recipient, **the recipient doesn't receive any message when the device is activated.**I think this is because of the data parsing method i used which creates a null character which increases the byte size of the recipientNumber variable. Through strlen, I've found out that the recipientNumber's size is 13 while the senderNumber fetched by the library has a size of 12. Even though the displayed number of characters in the serial are both 12. I tried changing the byte size of the array to 12 but once i did that, whenever i send the command to change the recipient, the program doesnt recognize the parsed command. It comes as blank.

  2. If i send the command "CURRENT RECIP" the serial outputs both the currentRecip() and the "Invalid command". If i put the "CURRENT RECIP" in the else if statement, swapping with the "SET RECIP", the "CURRENT RECIP" command works properly. For some reason, the command which is put in the else if statement works properly. I've tried adding another statement. i honestly don't fully understand what i wrote there myself. Still doesn't work tho.

else if ((strcmp(commandkey, "CURRENT RECIP") != 0) || (strcmp(commandkey, "SET RECIP") != 0){
Serial.println("Invalid Command");
}
  1. The project is supposed to activate at a simple push of a button. However, with my current sketch, it only activates when the button is long pressed. The issue i have here is i dont know where to properly put the activate(). I've tried placing it at the beginning of the loop and inside the if (GSMconnected) statement. However, both have the same result. I still have to press it long enough for the arduino to stop checking for available messages and proceed to the if (buttonState) statement.
if (buttonState == HIGH){
activate();
}

This sketch uses this GSM Library and i implemented the data parsing method from Serial Input Basics. The sketch properly separates the command i send which is "SET RECIP,XX**********" and proceeds to save it to the recipientNumber variable. The button can then be pressed and the GSM sends the message to the newly set recipient. Unfortunately, I am currently in a deadline and i dont think i have to time to scrap this sketch i made and just resort to AT commands. Please feel free to ask about any details i haven't covered. Any help would be appreciated and again i apologize in advance because some issues here might be fixed with just a simple syntax correction. Thank you in advance.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.