Good evening!
I am troubleshooting now why my Arduino which is coworking with GSMSHIELDhttp://www.ebay.com/itm/GSM-GPRS-Shield-For-Arduino-/230911052682?pt=LH_DefaultDomain_0&hash=item35c35f0b8a, rebooting itself after send sms function. And what is the most weird - that sms that was sent by GSMSHIELD was sucsessfully delivered to my cellphone! But after that Arduino reboots itself! I can not figure out why??
I've checked the amount of free SRAM memory before sending - 700 bytes, and after sending - 500 bytes..so it's not a problem of lack of memory, am I right?
Here is the code:
//Library for GSM
#include <call.h>
#include <gps.h>
#include <GSM.h>
#include <HWSerial.h>
#include <inetGSM.h>
#include <LOG.h>
#include <SIM900.h>
#include <sms.h>
#include "sms.h"
#include <Streaming.h>
#include <WideTextFinder.h>
#include <SoftwareSerial.h>
SMSGSM sms;
//DHT temperature sensor
#include <DHT11.h>
//standart lib
#include <stdlib.h>
#include <stdio.h>
//Set DHT temperature sensor on 11 PIN
int dhtpinfirst = 11;
DHT11 dhtfirst(dhtpinfirst);
int unit1 = 6; //Relay on 6 PIN
float delta = 0.5;
float t_1;
float h_1;
float autoTemp1 = 14; //default temperature 14 degree
String statusFloor;
char sendernum[13];
char msg[50];
byte position; //will determine the position of the unread SMS to be treated
byte repeatposition; //repeats the previous (position) - as previous reset because of the library in which so registered, I want to keep the value of position in repeatposition
unsigned long currentTime;
unsigned long loopTime;
boolean unit1status = false ; //relay - reflect status of relay
boolean isAuto_first = true; //
boolean gsm_on = false; //To verify that the device is on - SMS is sent only once
String currStr = "";
void setup()
{
pinMode(unit1, OUTPUT);
digitalWrite(unit1, HIGH);
//GSM force on
Serial.begin(9600);
if (gsm.begin(4800))
{
Serial.println("status=READY");
}
else Serial.println("status=IDLE");
delay(200);
currentTime = millis();
loopTime = currentTime;
}
void loop()
{
position = sms.IsSMSPresent(SMS_UNREAD);
Serial.println(position);
delay(200);
repeatposition = position;
if(repeatposition > 0)
{
if(sms.GetSMS(repeatposition, sendernum, msg, 50))
{
currStr = msg;
//snumber[13]= sendernum[13];
Serial.println("The msg is: ");
Serial.println(currStr);
if (currStr.startsWith("*"))
{
Serial.println("sms with star");
ParseSMSoverSMS(currStr);
}
else if(currStr.startsWith("#"))
{
if(currStr[1] == ('f')) //assumed that the message will be: "#first(22)"
{
String temperatura = currStr.substring(7,9); //rip out the number of brackets sms
Serial.print("Substring is: ");
Serial.println(temperatura);/// //display the value on the screen
delay(300);
autoTemp1 = stringToNumber(temperatura); //assign a number to a variable, which continuously compares the later cycle with a temperature of the room at the moment
delay(300);
Serial.println(autoTemp1);
isAuto_first = true; //Sets the mode of the thermostat - true
Serial.println("OK - first floor is set up to " + temperatura + " *C"); // such sms should come back
sendTextMessage("OK - first floor is set up to " + temperatura + " *C");
Serial.println("DONE - after send");
}
}
//***********************if the sms is wrong, device display:"Unknown command", delete it, and work properly
else
{
Serial.println("Unknown command");
}
}
//reset variables
msg[160] = 0;
sendernum[13] = 0;
currStr="";
delay(200);
while(repeatposition > 0)
{
int error = sms.DeleteSMS(repeatposition);
if (error==1)
{
Serial.println("SMS deleted");
repeatposition = repeatposition - 1;
}
else { Serial.println("SMS not deleted"); ; }
}
}
Serial.println("END of gsm-loop part");
if(gsm_on == false)
{
int freemem;
freemem = freeRam();
Serial.println(freemem);
sendTextMessage("I'm ON - yours SMART house");
freemem = freeRam();
Serial.println(freemem);
Serial.println("Sended sms about turning on");
gsm_on = true;
}
// Temperature control is on the 1st floor!
//*******************************************************************
if (isAuto_first==true)
{
//Temperature measurements can be carried out no more than once every 4 seconds
currentTime = millis();
if(currentTime >= (loopTime + 4000))
{
int err;
if((err=dhtfirst.read(h_1, t_1))==0)
{
Serial.print("First floor temperature:");
Serial.println(t_1);
Serial.print("First floor humidity:");
Serial.print(h_1);
Serial.println();
loopTime = currentTime;
}
else
{
Serial.print("Error No :");
Serial.println(err);
}
}
if (t_1 > autoTemp1+delta)
{
if ( unit1status==true)
{
digitalWrite(unit1, HIGH);delay(200);
unit1status=false;
}
}
if (t_1 < autoTemp1-delta)
{
if ( unit1status==false)
{
digitalWrite(unit1, LOW);delay(200);
unit1status=true;
}
}
}
}
void GetSeparateDatchik(char currStr)
{
switch (currStr)
{
case 'f':
{
delay(300);
statusFloor = String("First floor is: ") + int(t_1)+String(" *C");
delay(300);
sendTextMessage(statusFloor);
break;
}
}
}
void ParseSMSoverSMS(String currStr)
{
switch(currStr[1])
{
case 'f':
{
GetSeparateDatchik('f');
break;
} //assumed "*first?"
}
}
//The function of sending SMS-message
void sendTextMessage(String text)
{
char ret_val = -1;
int len = text.length();
char chtext[len];
for(int i = 0; i<=len; i++) { chtext[i] = text[i]; }
Serial.println("SENDING");
ret_val = sms.SendSMS("+380935947490", chtext);
Serial.println(ret_val);
delay(200);
}
//Convert STRING ? INT
int stringToNumber(String thisString)
{
int value = 0;
value = atoi (thisString.c_str());
return value;
}
//SRAM
int freeRam ()
{
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
So what the problem is?? Why it reboots? HELP PLEASE!
code.txt (6.5 KB)