One little glitch to overcome and then my project will work.
I am trying to save a telephone number sent via sms to EEPROM, using Nano with m590 module.
everything works fine on the whole project except that the received sms string saves garbage to my EEPROM.
I have been fighting this glitch for a few full working days and tried different approaches to no avail.
Attached is a working summary of the part of my sketch that gets me as well as the results on my serial monitor.
It seems like so small a problem, but it got me stumped.
#include <EEPROM.h>
#include "SoftwareSerial.h"
SoftwareSerial MiSerial(A5, A4); //RX, TX
String B="";
String Message = "";
int telB = 32; //EEPROM Address to put and get data
int inByte = 0;
char inchar; // Will hold the incoming character from the GSM shield
char phoneB[11]; // length of string
void setup() {
Serial.begin(9600);//serial monitor frequency
MiSerial.begin(57600); //gsm frequency
//read data from EEPROM
EEPROM.get(32, phoneB); // get tel number from EEPROM address 32
Serial.println("Read from EEPROM when starting up: ");
Serial.println(phoneB); //print data read from EEPROM to monitor
Serial.println();
Serial.println("Waiting for SMS........: ");
Serial.println();
// Start-up ==================================================
MiSerial.println("AT+CSCS=\"GSM\"");
delay(1000);
MiSerial.println("AT+CMGF=1");
MiSerial.println("AT+CMGD=1,4"); // delete all SMS
MiSerial.print("AT+CNMI=2,2,0,0,0\r"); //Blurt out all sms's
}
void loop() {
//If a character comes in from the cellular module...
String Message = "";
for (int l=0; l<4; l++) { // Receive 4 times a '"' character before we can go on.
do {
while ( !MiSerial.available() ); // Wait till we receive a character.
}
while (MiSerial.read() != '"' ); // As long as it is not a '"' we go fetch the next character.
}
for (int l=0; l<2; l++) { // Receive another two characters before we can read the actual message.
while ( !MiSerial.available() ); // Wait till we receive a character.
inByte = MiSerial.read(); // Read the character, but do nothing with it.
}
// Now we are going to read the message.
while(1)
{
while ( !MiSerial.available() ); // Wait till we receive a character.
inByte = MiSerial.read(); // Store the character in inByte;
if ( inByte == '\r' ) // If we read an end of text marker we stop.
break;
else; Message += char(inByte); // Else we add the character to the Message String.
}
Check_Message(Message); // Go to subroutine
Message = ""; // Clear the Message afterwards.
}//=========end of loop==================================================
// Read message
void Check_Message(String M) { //this is the string read from sms
Serial.println("Read string from SMS (M): ");
Serial.println (M); // print the string from sms to verify
Serial.println ();
EEPROM.put(32, M); // put the string to EEPROM at address 32
delay(1000); // wait one second for stability
EEPROM.get(32, phoneB); //get the string from EEPROM with different variable name
Serial.println ();
Serial.println("Read what was put in EEPROM: "); //print data read from EEPROM
Serial.println (phoneB); // print the string from EEPROM to monitor
Serial.println ();
M = ""; // clear the string for next round
B = "";
Serial.println("Waiting for SMS........: ");
Serial.println ();
}
Serial monitor results:
Read from EEPROM when starting up:
a
Waiting for SMS........:
Read string from SMS (M):
"18/05/28,16:24:42+08"
Read what was put in EEPROM:
!
Waiting for SMS........:
Read string from SMS (M):
Hello
Read what was put in EEPROM:
Waiting for SMS........:
Read string from SMS (M):
1234567890
Read what was put in EEPROM:
Waiting for SMS........:
Read string from SMS (M):
1
Read what was put in EEPROM:
Waiting for SMS........: