Trying to send data and receive SMS in the same time using SIM900

Hi !

I'm working on a project that involve the monitoring of a PV installation using a GSM module. I choose for simplicity to use ThingSpeak as a solution of IOT to see all of my data. Also, I wanted throught a SMS of the user to receive several informations in respond of this one.

I'm also using a protothread library because i find that it simplify my code.

I'm quite new to arduino less than a years for informations same as AT command that i'm using in the code.

**How i want to my code to work ? **

I want that it check permanently if he received a new SMS of any cell phone number. If it received one, and the text is "STATUS" then it respond to it with information (the code above below just send a text). Then every 20 sec, the code will send a data to thingspeak (here the number 400 is sent). And then it go back to sleep waiting for the 20 sec or for a new SMS from users.

I'm aware of a problem that can occure, that if a user send a SMS while data are sending to Thingspeak then nothing will be sent to the users. But for now using a timer big enought to reduce the chance of that scenario is the easier way.

How it work for now :

The GSM module is starting good, i can send a first SMS to receive my information before the 20 sec and the sending of data. Then I can't ask for information throught SMS and it keep sending dat every 20 sec as it should. But I feel like it can't get trought m "if(gsm.available()>0 but i dont know why.

So I as hoping with my post ton find an awnser or maybe a track of a solution.

Parts that I'm using :
-Arduino MEGA 2560,
-GSM Module SIM900.

#include <pt.h>
#include <SoftwareSerial.h>

SoftwareSerial gsm(10,11); 

static struct pt pt1, pt2;

String textMessage;
String CellNumtemp;
String CellNum;

void setup() 
{
  PT_INIT(&pt1);
  PT_INIT(&pt2);
  Serial.begin(9600);
  gsm.begin(9600);
  delay(5000);
  Serial.println("GPRS ready...\r\n");
  gsm.println("AT+CMGF=1\r"); // use full functionality (calls, sms, gprs) - see app note
  ShowSerialData();
  delay(100);
  gsm.println("AT+CLIP=1\r"); // enable presentation number
  ShowSerialData();
  delay(100);
  gsm.println("AT+CSCS=\"GSM\"\r"); // configure sms as standard text messages
  ShowSerialData();
  delay(100);
  gsm.println("AT+CNMI=1,2,0,0,0\r"); // alert our GSM shield and now whenever it will receive message
  ShowSerialData();
  delay(100); 
      
}


void send_data()
{
  gsm.println("AT");
  delay(1000);
  gsm.println("AT+CPIN?");
  delay(1000);
  gsm.println("AT+CREG?");
  delay(1000);
  gsm.println("AT+CGATT?");
  delay(1000);
  gsm.println("AT+CIPSHUT");
  delay(1000);
  gsm.println("AT+CIPSTATUS");
  delay(2000);
  gsm.println("AT+CIPMUX=0");
  delay(2000);
  gsm.println("AT+CSTT=\"free\"");//start task and setting the APN,
  delay(1000);
  gsm.println("AT+CIICR");//bring up wireless connection
  delay(3000);
  gsm.println("AT+CIFSR");//get local IP adress
  delay(2000);
  gsm.println("AT+CIPSPRT=0");
  delay(3000);
  gsm.println("AT+CIPSTART=\"TCP\",\"api.thingspeak.com\",\"80\"");//start up the connection
  delay(6000); 
  gsm.println("AT+CIPSEND");//begin send data to remote server
  delay(4000);
  String str="GET https://api.thingspeak.com/update?api_key=KBXJB73STRDHZJVL&field1=400"; //+ String(U) +"&field2="+String(I);
  gsm.println(str);//begin send data to remote server
  delay(4000);
  gsm.println((char)26);//sending
  delay(5000);//waitting for reply, important! the time is base on the condition of internet 
  gsm.println("AT+CIPSHUT");//close the connection
  delay(100);
  gsm.println("AT+CMGF=1\r"); // use full functionality (calls, sms, gprs) - see app note
  ShowSerialData();
  delay(1000);
  gsm.println("AT+CLIP=1\r"); // enable presentation number
  ShowSerialData();
  delay(1000);
  gsm.println("AT+CSCS=\"GSM\"\r"); // configure sms as standard text messages
  ShowSerialData();
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0\r"); // alert our GSM shield and now whenever it will receive message
  ShowSerialData();
  delay(5000);    
} 

static int protothread1(struct pt *pt) 
{
  PT_BEGIN(pt);
  if(gsm.available()>0)
  {
    textMessage = gsm.readString();
    Serial.println(textMessage);
    CellNumtemp = textMessage.substring(textMessage.indexOf("+33")); //locate the cell number
    CellNum = CellNumtemp.substring(0,12); //stock upcoming cell number in CellNum

    if (textMessage.indexOf("STATUS")>=0)
    {   

      gsm.print("AT+CMGS=\"");
      gsm.print(CellNum);
      gsm.print("\"\r"); 
      ShowSerialData();
      delay(500);
      gsm.println("Test Protothread SMS+ThingSpeak");
      gsm.print((char)26);
      ShowSerialData();
      delay(500);
      gsm.println("AT+CMGD=1,4"); //delete all sms read/undread in SIM
      ShowSerialData();
      delay(100);
     }
   PT_END(pt);
  }
}

void ShowSerialData()
{
  while(gsm.available()!=0)
    Serial.write(gsm.read());
}

static int protothread2(struct pt *pt, int interval) 
{
  static unsigned long timestamp = 0;
  PT_BEGIN(pt);
  while(1)
  {
    PT_WAIT_UNTIL(pt, millis() - timestamp > interval );
    timestamp = millis();
    send_data();
  }       
  PT_END(pt);
}

void loop() {
  protothread1(&pt1);
  protothread2(&pt2,20000);
}

All the part are working fine were not put together, but trying to receive an SMS while sending data to a website make my life a nightmare ahah.

Thanks for the reading, if I forgot to mention something feel free to ask.

Thanks you in advance,

NawaeK.

Hello,

As per your explanation, you're getting data on start, first call to protothread2, then at every 20 seconds. When you send SMS, the data is not sent, meaning this part is not working as expected.

You can try following,

  • Add some delay for reading data from SIM900 via serial. Read every 1 second or so. Use a delay library for better code management, like AsyncDelay is my favorite. Also make new a sketch just to test out SMS receive functionality, then incorporate in main sketch.

  • Wait for Serial data to arrive using serialEvent function, this will eliminate need to continuously check for data. https://www.arduino.cc/en/Tutorial/BuiltInExamples/SerialEvent

Hope this helps,

Hi,

Thanks for your answer,

I went for a simpler solution using the AsyncDelay library and it works.

I also found out that using the command "AT+CIPCLOSE" at the end of my code helps me to close all the TCP connections that I opened ealier.

NawaeK,

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.