0

I have the a problem with my arduino MEGA 2560 and the SIM900 module. I want to save the outputs of the AT commands, but I never get a chance. If I print mySerial.available() I only get 0. Sending SMS works fine on the other hand.

Initializing...
AT+CMGF=1
AT+CNMI=2,2,0,0,0
OK
Done
AT+CMGL="REC UNREAD"
OK
0
0
0
0
0
0
0
0
0
0
0

This is the sketch I use:

#include <SoftwareSerial.h>
//Create software serial object to communicate with SIM900
SoftwareSerial mySerial(11, 10); //SIM900 Tx & Rx is connected to Arduino #7 & #8
String incoming="";
String incoming2="";
String messages="Test";
String auth_num="+49xxxxxxxxx";
char incoming_char=0;
String response = "";
const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;
int period = 6000;
unsigned long time_now = 0;
void setup()
{
 //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
 Serial.begin(9600);
 //Begin serial communication with Arduino and SIM900
 mySerial.begin(9600);
 Serial.println("Initializing..."); 
 delay(1000);
 mySerial.println("AT+CMGF=1");
 mySerial.println("AT+CNMI=2,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
 //mySerial.println("ATE0"); 
 updateSerial();
 Serial.println("Done");
 //sms_versenden("xxxxxxxx", "text");
}
void loop()
{
 updateSerial();
 Serial.println(mySerial.available());
 if(millis() - time_now > period){
 time_now = millis();
 if(sms_erhalten()){
 sms_autorisiert(); 
 }
 }
}
void updateSerial()
{
 delay(500);
 while (Serial.available()) 
 {
 mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
 }
 while(mySerial.available()) 
 {
 Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
 }
}
void sms_versenden(String rufnummer,String text)
{
 mySerial.println("AT"); //Handshaking with SIM900
 updateSerial();
 mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
 updateSerial();
 if(rufnummer.startsWith("0")){
 rufnummer.remove(0,1);
 mySerial.print("AT+CMGS=\"+49");
 mySerial.print(rufnummer);
 mySerial.println("\"");
 }else if(rufnummer.startsWith("+49")){
 mySerial.print("AT+CMGS=\"");
 mySerial.print(rufnummer);
 mySerial.println("\"");
 }else{
 return;
 }
 updateSerial();
 mySerial.print(text); //text content
 updateSerial();
 mySerial.write(26);
}
boolean sms_erhalten()
{
 mySerial.print("AT+CMGL=\"REC UNREAD\"\r");
 //recvWithEndMarker();
 Serial.println(mySerial.available());
 delay(30);
 while (mySerial.available()){
 String inData = mySerial.readStringUntil('\n');
 Serial.println("Got reponse from SIM900: " + inData);
 }
 /*if(mySerial.available()>0){
 incoming = mySerial.readString();
 Serial.print(incoming); 
 delay(10);
 }*/ 
 updateSerial();
 if(incoming.startsWith("+CMGL:"))
 {
 return true; 
 }
 else{
 return false;
 }
}
void recvWithEndMarker() {
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 while (mySerial.available() != 0 && newData == false) {
 rc = mySerial.read();
 updateSerial();
 if (rc != endMarker) {
 receivedChars[ndx] = rc;
 ndx++;
 if (ndx >= numChars) {
 ndx = numChars - 1;
 }
 }
 else {
 receivedChars[ndx] = '0円'; // terminate the string
 ndx = 0;
 newData = true;
 }
 }
}
void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChars);
 newData = false;
 }
}
String getResponse() {
 while(mySerial.available()>0){
 response += mySerial.read();
 }
 return response;
}

Thanks in advance for every idea. Bravodelta

asked Apr 3, 2019 at 11:50
4
  • if you turn on echo, it doesn't print the commands back? check the wiring Commented Apr 3, 2019 at 13:32
  • @Juraj The echo is on and as you can see in the first code block I get an answer, but I can't save it in a String. Commented Apr 4, 2019 at 8:10
  • try mySerial.println("AT+CMGL=\"REC UNREAD\""); Commented Apr 4, 2019 at 9:13
  • @Juraj Sadly this doesn't work either Commented Apr 8, 2019 at 6:31

1 Answer 1

-2

Rather than creating your own function used SerialEvent function in arduino. It acts as a serial interrupt in arduino. For example:

String inputString;
void serialEvent() {
 while (Serial.available()) {
 // get the new byte:
 char inChar = (char)Serial.read();
 // add it to the inputString:
 inputString += inChar;
 // if the incoming character is a newline, set a flag so the main loop can
 // do something about it:
 if (inChar == '\n') {
 Serial.Println(inputString);
 }
 }
 }
answered Apr 3, 2019 at 12:23
6
  • don't use serialEvent. it has no advantage. it is the same as checking Serial.available() in loop Commented Apr 3, 2019 at 12:40
  • The serial event system does not act as an interrupt. It is no different to adding if (Serial.available())... in your loop() function. I wish Arduino had never implemented it - it's a stupid idea. Commented Apr 3, 2019 at 12:57
  • @Juraj why no for using serialevent function? What harm does it causes? Commented Apr 3, 2019 at 16:39
  • @Majenko had you tried serialevent function? Commented Apr 3, 2019 at 16:39
  • 1
    @Vaibhav Worse than that - I have had to implement it on a compatible platform, and had to take a shower in sodium hydroxide afterwards to get the taint off my skin... Commented Apr 3, 2019 at 16:40

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.