Hello,
I have a little problem with my code. If someone with a sim800/900 module and a gps, can test my code and see what is wrong.
What it dose: It works just fine when receiving the "Ok", "Test", "Voltage" commands, and if i keep sending this sms's they will always work. But when i send the "Where" sms, the arduino dose that and print that "text" string just fine, but it stops taking any other sms's. It's like the software serial for gsm dosen't receive anything anymore. If i call the reset function, arduino resets just fine and it works, but i don't want to keep reseting it( not sure if it's safe for gsm module).
Hope you guy understand what i'm trying to say and can help. Thanks in advance.
Reset function i used:
void(*resetFunc) (void) = 0; //reset function
My code:
#include<SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial sim900 (3, 2);
SoftwareSerial GPS (5, 6);
TinyGPS gps;
const unsigned int MAX_INPUT = 165;
static unsigned int input_pos = 0;
int SMS_location_number;
int voltage;
float flat, flon, altitude, speed;
String timestamp = "0";
String input=""; // Declare an Empty String Variable
String input_trimmed="";// a variable to store the trimmed string
String substring=""; // a variable to store the substring "PHONE NUMBER"
String txtMessage="";// a variable to store the txtMessage "The inComing Text Message"
int length_before_trim = 0, length_after_trim = 0, length_substring=0; // 3 variables for storing the string lengths
int freeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
void(*resetFunc) (void) = 0; //reset function
void setup() {
Serial.begin(115200);
sim900.begin(4800);
Serial.print("Start");
delay(8000);
config_sim900();
Serial.println("ready");
}
void loop() {
read_sim900();
}
void config_sim900() {
sim900.println("AT+CMGF=1");
delay(500);
//sim900.println("AT+CNMI=2,1,0,0,1"); //save SMS to sim
sim900.println("AT+CNMI=2,2,0,0,0"); // Print SMS to serial
delay(500);
sim900.println("AT+CMGD=1,4"); //Delete all SMS
delay(500);
sim900.println("AT+CHFA=1"); //Set audio to external 0=hedphones 1=external
delay(500);
sim900.println("AT+CLIP=1");
delay(500);
//sim900.print("AT+CBC");
}
void read_sim900() {
static char input_line [MAX_INPUT];
if (sim900.available() > 0) {
Serial.println("SIM900 OK!");
// digitalWrite(10, HIGH);
// delay(1500);
// digitalWrite(10, LOW);
while (sim900.available () > 0) {
char inByte = sim900.read();
switch (inByte) {
case '\n': // end of text
input_line [input_pos] = 0; // terminating null byte
// terminator reached! process input_line here ...
process_data (input_line);
// reset buffer for next time
input_pos = 0;
break;
case '\r': // discard carriage return
break;
default:
// keep adding if not full ... allow for terminating null byte
if (input_pos < (MAX_INPUT - 1))
input_line [input_pos++] = inByte;
break;
} // end of switch
} // end of while incoming data
} // end of if incoming data
}
void process_data (char * data) {
Serial.print("***Data:");
Serial.println(data);
// if(strstr(data, "+CMTI:")) { // An SMS has arrived
// char* copy = data + 12; // Read from position 12 until a non ASCII number to get the SMS location
// SMS_location_number = (byte) atoi(copy); // Convert the ASCII number to an int
// sim900.print("AT+CMGR=");
// sim900.println(SMS_location_number); // Print the SMS in Serial Monitor
// }
if(strstr(data, "+CLIP:")) {
input = "";
delay(100);
input += data;
input_trimmed = input;
substring = input_trimmed.substring(8, 20); // get the callers number.
//substring = input_trimmed.substring(10, 21); // 11 char number (cheap simcards);
Serial.print("Number: ");
Serial.println("+" + substring);
delay(500);
sim900.println("ATA");
delay(10000);
sim900.println("ATH");
}
// if(strstr(data, "+CMGR:")) {
// input = "";
// delay(100);
// input += data;
// input_trimmed = input;
// substring = input_trimmed.substring(22, 34); // get the callers number.
// Serial.print("Number: ");
// Serial.println(substring);
// }
if(strstr(data, "Test")) {
Serial.println("Sending SMS.");
sim900.print("AT+CMGS= \"+");
delay(100);
sim900.print(substring);
delay(100);
sim900.println("\"");
delay(100);
sim900.print("test");
delay(100);
sim900.println((char)26);
delay(100);
}
if(strstr(data, "Voltage")) {
delay(100);
sim900.println("AT+CBC");
delay(100);
}
if(strstr(data, "+CBC:")) {
char* volt = data + 8;
voltage = (byte) atoi(volt);
Serial.print("Voltage: ");
Serial.println(voltage);
Serial.println("Sending SMS.");
sim900.print("AT+CMGS= \"+");
delay(100);
sim900.print(substring);
delay(100);
sim900.println("\"");
delay(100);
sim900.print(voltage);
delay(100);
sim900.println((char)26);
delay(100);
}
if(strstr(data, "+CMT:")) { // get the phone number if the sms is displayed on Serial, not saved on SIM.
input = "";
delay(100);
input += data;
input_trimmed = input;
substring = input_trimmed.substring(8, 20); // get the callers number.
// Serial.print("Number: ");
// Serial.println(substring);
}
if(strstr(data, "Ok")) {
Serial.println("Tel:+" + substring);
}
if(strstr(data, "Send")) {
Serial.println("Sending SMS.");
sim900.print("AT+CMGS= \"+");
delay(100);
sim900.print(substring);
delay(100);
sim900.println("\"");
delay(100);
sim900.print("test");
delay(100);
sim900.println((char)26);
delay(100);
}
if(strstr(data, "Balance")) {
sim900.println("ATD*101#;");
}
if(strstr(data, "Where")) {
gps_sms();
Serial.println("Where"); //Arduino stops here
read_sim900();
sim900.println("ATI"); //This line dosen't show the sim800 version
Serial.println(freeRam()); //This line works
//resetFunc();
}
}
void gps_sms() {
Serial.println("Start");
GPS.begin(9600);
delay(100);
GPS.println("$PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*29");
delay(500);
bool newData = false;
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (GPS.available()) {
char c = GPS.read();
if (gps.encode(c))
newData = true;
}
}
if (newData)
{
Serial.println("FIX!");
gps.f_get_position(&flat, &flon);
speed = gps.f_speed_kmph();
altitude = gps.f_altitude();
int year;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths);
timestamp = String("");
timestamp += hour + 1 ;
timestamp += ":";
if (minute<10)
{
timestamp += "0";
timestamp += minute;
}
else if (minute>=10)
{
timestamp += minute;
}
Send();
}
}
void Send() {
String text = String();
text += "Full SMS - ";
text += " Altitude: ";
text += floatToString(altitude, 2);
text += " Speed: ";
text += floatToString(speed, 2);
text += " Time(GMT + 2): ";
text += timestamp;
text += " Map: http://maps.google.com/?q=";
text += floatToString(flat, 8);
text += ",";
text += floatToString(flon, 8);
Serial.println(text);
Serial.println(freeRam()); //this line works
GPS.flush();
GPS.end();
}
String floatToString(double number, uint8_t digits) {
String resultString = "";
// Handle negative numbers
if (number < 0.0) {
resultString += "-";
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i = 0; i < digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
resultString += int_part;
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
resultString += ".";
// Extract digits from the remainder one at a time
while (digits-- > 0) {
remainder *= 10.0;
int toPrint = int(remainder);
resultString += toPrint;
remainder -= toPrint;
}
return resultString;
}
final.ino (7.6 KB)