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
1 Answer 1
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
-
don't use serialEvent. it has no advantage. it is the same as checking Serial.available() in loop2019年04月03日 12:40:08 +00:00Commented 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 yourloop()
function. I wish Arduino had never implemented it - it's a stupid idea.Majenko– Majenko2019年04月03日 12:57:33 +00:00Commented Apr 3, 2019 at 12:57 -
@Juraj why no for using serialevent function? What harm does it causes?Vaibhav– Vaibhav2019年04月03日 16:39:05 +00:00Commented Apr 3, 2019 at 16:39
-
@Majenko had you tried serialevent function?Vaibhav– Vaibhav2019年04月03日 16:39:41 +00:00Commented 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...Majenko– Majenko2019年04月03日 16:40:51 +00:00Commented Apr 3, 2019 at 16:40
mySerial.println("AT+CMGL=\"REC UNREAD\"");