Hi all,
First of all, i would like to apologize for frequently posting questions. But it is place where i last resort to... So here it is..
In the main program, i have defined a polling loop around 17seconds in real time for detecting incoming SMS. and 75/1000 seconds for sensing
temperature and voltage. Finally, the status is reported back to my cell phone at a regular time interval.
But the program seems to have problems associating with the status reporting (SMS sending by the Arduino). The performance is rather unstable, sometimes i got nothing
, sometimes i got them. Is there anything wrong in the codes? I'll be seriously in your debt if anyone can shed some light on this issue.. Your help is highly appreciated.
Regards,
#include <string.h>
#include <SoftwareSerial.h>
char a[5]={};
char incoming_char=0;
float t1;
float t2;
float t3;
float t4;
float t5;
float t6;
int j=1;
SoftwareSerial cell(2,3);
char mobilenumber[]="**********";
char textmessage[160];
char inchar;
int led1 = 9;
int led2 = 10;
int led3 = 11;
int led4 = 12;
void setup() {
Serial.begin(9600);
cell.begin(9600);
delay(30000);
cell.println("AT+CMGF=1");
delay(200);
cell.println("AT+CNMI=3,3,0,0");
delay(200);
pinMode(led1,OUTPUT); //relay decoy//
pinMode(led2,OUTPUT); //relay decoy//
pinMode(led3,OUTPUT); //relay decoy//
pinMode(led4,OUTPUT); //relay decoy//
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
pinMode(13,OUTPUT); //SMS status lamp
pinMode(12,OUTPUT);
/*setTime(11,40,00,19,11,12);//hour,minutes,seconds, date,month,year*/
}
void startSMS() {
digitalWrite(13,HIGH);
cell.println("AT+CMGF=1");
cell.print("AT+CMGS=");
cell.write(34);
cell.print(mobilenumber);
cell.write(34);
cell.println();
delay (500);
}
void endSMS() {
cell.write(26);
cell.println();
delay(30000);
digitalWrite(13, LOW);
}
void GSMTest()
{
if(cell.available() >0)
{
incoming_char=cell.read();
Serial.print(incoming_char);
}
if(Serial.available() >0)
{
incoming_char=Serial.read();
cell.print(incoming_char);
}
}
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
void TMP00 ()
{
float r;
float temperature;
r=readVcc()/1000.0;
temperature = analogRead(0);
temperature = (((r*temperature)/1024)-0.5)/0.01;
Serial.print("Measured voltage: ");
Serial.println( r, 2 );
Serial.print("Sensor_0 temperature:");
Serial.println(temperature,2);
}
void TMP01 ()
{
float r;
float temperature;
r=readVcc()/1000.0;
temperature = analogRead(1);
temperature = (((r*temperature)/1024)-0.5)/0.01;
Serial.print("Measured voltage: ");
Serial.println( r, 2 );
Serial.print("Sensor_1 temperature:");
Serial.println(temperature,2);
}
void Sense()
{
float r;
float temperature;
float r1;
float measuredV;
float correctfactor = 0.023;
float correctedV;
r=readVcc()/1000.0;
measuredV = analogRead(3);
measuredV = measuredV*r/1024.0;
correctedV = measuredV - measuredV*correctfactor;
Serial.print("Corrected voltage at voltage divider:");
Serial.println(correctedV);
r=readVcc()/1000.0;
temperature = analogRead(0);
temperature = (((r*temperature)/1024))/0.01;
Serial.print("Sensor_2 temperature:");
Serial.println(temperature,2);
}
void loop() {
startSMS();
cell.print("Hello!");
endSMS();
delay(100);
for(;;) {
t1=millis();
Serial.println("Sensing Loop!");
digitalWrite(9,HIGH);
for(int i = 1;i<=3000;i++) // each for loop is about 17.5 seconds
{
if(cell.available()>0)
{
inchar = cell.read();
if(inchar == 'c')
{
inchar = cell.read();
if(inchar=='a')
{
delay(10);
inchar = cell.read();
if(inchar =='m')
{
delay(10);
inchar = cell.read();
if(inchar=='1')
{
digitalWrite(10,HIGH);
delay(500);
digitalWrite(10,LOW);
cell.println("AT+CMGD=1,4");
}
}
}
}
} Serial.println(i);
}
//polling loop ends
t2=millis();
t3=t2-t1;
Serial.print("Polling time: ");
Serial.println(t3,2);
t4=millis();
digitalWrite(9,LOW);
Sense();
t5=millis();
t6=t5-t4;
Serial.print("Sensing Time: ");
Serial.println(t6,2);
//decision making loop
j++;
Serial.print("j");
Serial.println(j);
if(j==206) //status report loop, j=206 is equivalent to an hour approximately
{
j=1;
startSMS();
cell.print("Hello!");
endSMS();
}
}
}//end void loop () {}