thandana
Junior Member level 1
Hi All,
I have interfaced an LM35 with PIC18F2550 and with SIM900 device. I send the this SMS code to set the temperature threshold to 30 degrees celcuis(20!4#30) and i store this on the eeprom. To read the threshold is send this SMS (20!4#rt) I then read the temperature by sending this code to the SIM900 (20!4#gt).
All the above work perfectly.
I have an issue when i compare the threshold value to the current temperature. Here is my requirement:
- when the current temperature reading is below the threshold stored on eeprom, no sms must be sent. I have no issue here
- when the temperature reading is above the threshold, it must sent me SMS informing user msg7[] = "Temperature is high"; - THIS IS NOT HAPPENING, PLEASE HELP ME SEE WHERE IM GOING WRONG.
I have interfaced an LM35 with PIC18F2550 and with SIM900 device. I send the this SMS code to set the temperature threshold to 30 degrees celcuis(20!4#30) and i store this on the eeprom. To read the threshold is send this SMS (20!4#rt) I then read the temperature by sending this code to the SIM900 (20!4#gt).
All the above work perfectly.
I have an issue when i compare the threshold value to the current temperature. Here is my requirement:
- when the current temperature reading is below the threshold stored on eeprom, no sms must be sent. I have no issue here
- when the temperature reading is above the threshold, it must sent me SMS informing user msg7[] = "Temperature is high"; - THIS IS NOT HAPPENING, PLEASE HELP ME SEE WHERE IM GOING WRONG.
Code:
//Defines
#define ON 1
#define OFF 0
#define LOW 0
#define HIGH 1
#define OPEN 1
#define CLOSE 0
// GSM3 click module connections, declaring pins for PWRKEY, STAT and GSM_RST
sbit PWRKEY at LATB5_bit;
sbit STAT at RB3_bit;
sbit GSM_RST at LATB4_bit;
sbit PWRKEY_Direction at TRISB5_bit;
sbit STAT_Direction at TRISB3_bit;
sbit GSM_RST_Direction at TRISB4_bit;
// end of GSM3 click module connections
//Outputs pins for ALARM, LIGHT, GATE and LED's
sbit LIGHT at LATB7_bit;
sbit ALARM at LATC2_bit;
sbit GATE at LATB6_bit;
sbit NV_LED at LATC0_bit;
sbit UV_LED at LATC1_bit;
sbit OV_LED at LATB2_bit;
//Variables
//Flags, setting the output flags to record status
char myFlags = 0;
sbit GateStatus at myFlags.B0;
sbit MainsStatus at myFlags.B1;
sbit AlarmStatus at myFlags.B2;
sbit LightStatus at myFlags.B3;
sbit doOnce at myFlags.B4;
sbit doOnce2 at myFlags.B5;
sbit turnOFFLights at myFlags.B6;
//Temperature Sensor
double temperature = 0.0, prevTemperature = 0.0; // A double is a type of floating-point number. Could use float but double is accurate
unsigned int temp = 0, prevTemp = 0;
unsigned char threshold = 80;
unsigned int delayCounter = 0;
//EEPROM
int addr = 0;
int val = 0;
//UART
char uartBuffer[270]; // setting the uartbuffer size
char msg[50];
char buff[50];
char buff2[50];
char sms[20];
char smsIndex[4];
char value[4];
char strTemp[23];
unsigned int index = 0;
char attempt = 0;
//Events
unsigned char event = 15, sendSms = 0;
char port = 0;
unsigned int mainsVoltage = 0, prevMainsVoltage = 0;
//GSM Constant Strings
const char cmd1[] = "AT\r";
const char cmd2[] = "ATE0\r";
const char cmd3[] = "ATE1\r";
const char cmd4[] = "AT+IPR=9600\r";
const char cmd5[] = "AT+CMGF=1\r";
const char cmd6[] = "AT+CPMS=\"SM\",\"SM\",\"SM\"\r";
const char cmd7[] = "AT+CNMI=2,1\r";
const char cmd8[] = "AT+CMGR=";
const char cmd9[] = "AT+CMGS=\"+xxxxx\"\r";
const char cmd10[] = "AT+CMGD=1,4\r";
const char resp1[] = "\r\nOK\r\n";
const char resp2[] = "ERROR";
const char resp3[] = "+CMTI: \"SM\",";
const char resp4[] = "> ";
const char resp5[] = "RING";
const char resp6[] = "NO CARRIER";
const char resp7[] = "NO ANSWER";
const char resp8[] = "+CFUN: 1";
const char resp9[] = "+CPIN: READY";
const char secretCode[] = "20!4";
const char secretCode0[] = "20!4#l1"; //to turn lights ON
const char secretCode1[] = "20!4#l0"; //to turn lights OFF
const char secretCode2[] = "20!4#ls"; //to get STATUS of lights
const char secretCode3[] = "20!4#gt"; //to get temperature reading
const char secretCode4[] = "20!4#da"; //to de-activate the alarm
const char secretCode5[] = "20!4#aa"; //to activate the alarm
const char secretCode6[] = "20!4#as"; //to get the STATUS of the alarm
const char secretCode7[] = "20!4#cg"; //to close the electric gate
const char secretCode8[] = "20!4#og"; //to open the electric gate
const char secretCode9[] = "20!4#gs"; //to get the STATUS of the electric gate to see if its OPEN/CLOSED
const char secretCode10[] = "20!4#ps"; //to get the STATUS of the power supply if its interrupted or not
const char secretCode11[] = "20!4#rt"; //to get temperature threshold value
const char msg1[] = "Light is ON";
const char msg2[] = "Light is OFF";
const char msg3[] = "Gate is open";
const char msg4[] = "Gate is closed";
const char msg5[] = "Alarm is ON";
const char msg6[] = "Alarm is OFF";
const char msg7[] = "Temperature is high";
const char msg8[] = "Power supply is OK";
const char msg9[] = "Power supply not OK";
const char msg10[] = "Threshold is set to ";
const char msg11[] = " degree C";
const char msg12[] = "Temperature is ";
const char msg13[] = "Cannot control Gate. Voltage is not Ok";
const char msg14[] = "You have entered incorrect code";
//Function Prototypes
char *CopyConst2Ram(char *dest, const char *src); // copy const to ram string
void DelayXSec(unsigned long int sec);
void wait();
//Function Prototypes
char *CopyConst2Ram(char *dest, const char *src);
void GSM_RESET();
void Extract_SMS(char *s1, char *s2);
void Get_SMS_Index(char *s1, char *s2);
void Get_GSM_Time(char *s1, char *s2);
void Get_Code(char *s1, char *s2);
void GSM_Send_Const_Cmd(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *attempt, char clr);
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *attempt, char *s6, char clr);
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *attempt, char clr);
//Timer1
//Prescaler 1:8; TMR1 Preload = 15536; Actual Interrupt Time : 200 ms
//Place/Copy this part in declaration section
void InitTimer1(){
T1CON = 0x31; // 1:8 prescale and running
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
TMR1IE_bit = 1;
INTCON = 0xC0;
}
//Interrupt routines
void interrupt() {
if(INT0IF_bit) {
INT0IF_bit = 0;
event = 7;
sendSms = 1;
}
if(RCIF_bit) {
if(OERR_bit) { //If overflow error
OERR_bit = 0; //Clear overflow error bit
CREN_bit = 0; //Disable Continuous Receive Enable bit
CREN_bit = 1; //Enable Continuous Receive Enable bit
}
uartBuffer[index++] = UART1_Read(); //Read contents of RCREG into uart buffer and increment buffer index
uartBuffer[index] = '\0';
RCIF_bit = 0; //Clear UART receive interrupt flag
}
if(TMR1IF_bit) {
TMR1IF_bit = 0;
TMR1H = 0x3C;
TMR1L = 0xB0;
//Enter your code here
if(++delayCounter == 1500) {
event = 3;
delayCounter = 0;
sendSms = 1;
TMR1ON_bit = 0;
}
}
}
char *CopyConst2Ram(char *dest, const char *src){
char *d ;
asm clrwdt // Assembly command to reset WDT
d = dest;
for(;*dest++ = *src++;)asm clrwdt;
return d;
}
void DelayXSec(unsigned long int sec) {
while(sec != 0) {
Delay_ms(1000);
asm clrwdt
--sec;
}
}
void GSM_RESET() {
GSM_RST = 1;
asm clrwdt
Delay_ms(20);
GSM_RST = 0;
asm clrwdt
}
void Extract_SMS(char *s1, char *s2) {
unsigned int i = 0;
asm clrwdt
while(*s1) { //While character in string is not a null character then do the loop
if(*s1 == '\n') //If character is equal to '\n' increment i
++i;
asm clrwdt //increment char pointer once in each loop so that it points to next character in string
*s1++;
//if i equals to 2 that is if 2 x '\n' detected then exit loop
if(i == 2)break;
}
asm clrwdt
while((*s1) && (*s1 != '\r')) { //If current character is true and it is not equal to '\r' then do the loop
*s2++ = *s1++; //assign string1 to string 2
asm clrwdt
}
*s2 = '\0'; //Terminate the string2 with null character
asm clrwdt
}
void Get_SMS_Index(char *s1, char *s2) {
while(*s1 != 'C') {
*s1++;
asm clrwdt
}
while(*s1 != 'M') {
*s1++;
asm clrwdt
}
while(*s1 != 'T') {
*s1++;
asm clrwdt
}
while(*s1 != 'I') {
*s1++;
asm clrwdt
}
while(*s1 != ',') {
*s1++;
asm clrwdt
}
*s1++;
while(*s1 != '\r') {
*s2++ = *s1++;
asm clrwdt
}
*s2 = '\0';
asm clrwdt
}
void Get_GSM_Time(char *s1, char *s2) {
while(*s1 != ',') {
*s1++;
asm clrwdt
}
*s1++;
while(*s1 != '+') {
*s2++ = *s1++;
asm clrwdt
}
*s2 = '\0';
asm clrwdt
}
void Get_Value(char *s1, char *s2) {
while(*s1 != '#') {
*s1++;
asm clrwdt
}
*s1++;
*s2++ = *s1++;
*s2++ = *s1;
*s2 = '\0';
asm clrwdt
}
void GSM_Send_Const_Cmd(char *s1, char *s2, const char *s3, const char *s4, unsigned int *idx, char *attempt, char clr) {
asm clrwdt
while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
UART1_Write_Text(CopyConst2Ram(s2, s4));
asm clrwdt
DelayXSec(1);
asm clrwdt
if((*attempt)++ == 3) {
GSM_RESET();
asm clrwdt
memset(uartBuffer, '\0', sizeof(uartBuffer));
(*attempt) = 0;
(*idx) = 0;
}
}
if(clr)memset(uartBuffer, '\0', sizeof(uartBuffer));
asm clrwdt
(*attempt) = 0;
(*idx) = 0;
}
void GSM_Get_SMS(char *s1, char *s2, const char *s3, const char *s4, char *s5, unsigned int *idx, char *attempt, char *s6, char clr) {
asm clrwdt
while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
CopyConst2Ram(s2, s4);
asm clrwdt
strcat(s2, s5);
strcat(s2, "\r");
UART1_Write_Text(s2);
asm clrwdt
DelayXSec(2);
asm clrwdt
if((*attempt)++ == 3) {
GSM_RESET() ;
asm clrwdt
(*attempt) = 0;
(*idx) = 0;
memset(uartBuffer, '\0', sizeof(uartBuffer));
}
}
(*idx) = 0;
Extract_SMS(s1, s6);
asm clrwdt
if(clr)memset(uartBuffer, '\0', sizeof(uartBuffer));
asm clrwdt
}
void Send_SMS(char *s1, char *s2, const char *s3, const char *s4, const char *s5, const char *s6, const char *s7, char *s8, unsigned int *idx, char *attempt, char clr) {
while(strstr(s1, CopyConst2Ram(s2, s3)) == 0) {
asm clrwdt
GSM_Send_Const_Cmd(s1, s2, s3, s5, idx, attempt, 1);
GSM_Send_Const_Cmd(s1, s2, s3, s6, idx, attempt, 1);
GSM_Send_Const_Cmd(s1, s2, s4, s7, idx, attempt, 1);
asm clrwdt
UART1_Write_Text(s8);
Delay_ms(500);
UART1_Write(0x1A);
asm clrwdt
DelayXSec(6);
}
memset(uartBuffer, '\0', sizeof(uartBuffer));
(*idx) = 0;
asm clrwdt
}
// pause
void wait() {
Delay_ms(3000); // 3 seconds delay
}
void main() {
CMCON = 0x07;
ADCON1 = 0x0D;
TRISA = 0xC3;
TRISB = 0x09;
TRISC = 0xC0;
asm clrwdt
PORTA = 0x00; // initializing PORTA
PORTB = 0x00;
PORTC = 0x00;
asm clrwdt
LATA = 0x00;
LATB = 0x00;
LATC = 0x00;
asm clrwdt
INTEDG0_bit = 1;
INT0IE_bit = 1;
UART1_Init(9615);
Delay_ms(200);
asm clrwdt
GIE_bit = 0;
threshold = EEPROM_Read(0x01); //Read threshold value from eeprom and store it in threshold variable
Delay_ms(50); //Delay between eeprom read/write
event = EEPROM_Read(0x02); //Read event value from eeprom and store it in event variable
Delay_ms(50); //Delay between eeprom read/write
asm clrwdt
RCIE_bit = 1; //Enable UART receive Interrupt
PEIE_bit = 1; //Enable Peripheral Interrupts
GIE_bit = 1; //Enable Global Interrupts
asm clrwdt
PWM1_Init(2500); //Configure PWM frequency to 2500 Hz
PWM1_Set_Duty(0); //Set PWM duty to 0
asm clrwdt
Delay_ms(200);
asm clrwdt
//Clear INT0 interrupt flag
INT0IF_bit = 0;
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd1, &index, &attempt, 1); //Send AT Command
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd4, &index, &attempt, 1); //Send AT+IPR=9600 Command
//and set SIM900 baudrate to 9600 bps
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd5, &index, &attempt, 1); //Send AT+CMGF-1 Command
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd6, &index, &attempt, 1); //Send AT+CPMS Command
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd7, &index, &attempt, 1); //Send AT+CNMI Command
//and Enable New SMS received notification
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd10, &index, &attempt, 1); //Send AT+CMGD Command
asm clrwdt //and delete all SMS in SIM Card
//threshold = 45;
while(1) {
mainsVoltage = ADC_Read(1) * 229.0 / 1024.0; //Read main Voltage
//5V dc adc input is equal to 250.0 V AC
Delay_ms(20); //Delay between adc acqusition //If previous voltage is not equal to current mains voltage
if(prevMainsVoltage != mainsVoltage) { //then do the if condition
if((mainsVoltage >= 180.0) && (mainsVoltage <= 240.0)) { //If mains voltage is between 180 V and 240V then
NV_LED = 1; //Turn ON Normal Voltage LED
UV_LED = 0; //Turn OFF Under Voltage LED
OV_LED = 0; //Turn OFF Over Voltage LED
MainsStatus = 0; //Clear MainsStatus flag
}
else if(mainsVoltage < 180.0) { //If mains voltage is less than 180 V
UV_LED = 1; //Turn ON Under Voltage LED
NV_LED = 0; //Turn OFF Normal Voltage LED
OV_LED = 0; //Turn OFF Over Voltage lED
MainsStatus = 1; //Set MainsStatus flag to indicate abnormal mains voltage
}
else if(mainsVoltage > 240.0) { //If mains voltage is greater than 240 V
OV_LED = 1; //Turn ON Over Voltage LED
NV_LED = 0; //Turn OFF Normal Voltage LED
UV_LED = 0; //Turn OFF Under Voltage lED
MainsStatus = 1; //Set MainsStatus flag to indicate abnormal mains voltage
}
prevMainsVoltage = mainsVoltage;
}
asm clrwdt
temperature = temp = ADC_Read(0); //Read temperatture value into unsigned int and float variables
Delay_ms(20); //Delay between ADC acqusition
temperature /= 2.046; //Convert adc value (float value) to temperature
temp /= 2; //Convert unsignet int temperature value to approximate value of temperature
if(prevTemp != temp) { //If previous value of temperature is not equal to current temperature then do the if condition
if(temperature > threshold) { //If temperature is greater than threshold value then
event = 1; //Set event to 1
//Send sendSms flag to 1 so that it is called once
}
else if(temperature <= threshold) { //If temperature is less than threshold then set event value to 0
event = 0;
}
prevTemp = temp; //Assign current temperature value to previous temperature variable
}
if(temperature > threshold){
if(doOnce2 == 0) {
event = 1; sendSms = 1; doOnce = 0;
doOnce2 = 1;
}
}
else if (temperature <= threshold)doOnce2 = 0;
asm clrwdt
if(strstr(uartBuffer, CopyConst2Ram(buff, resp3))) { //If response 3 (new SMS received notification) is received then do the if condition
Delay_ms(2000); //Wait till SMS index is received
Get_SMS_Index(uartBuffer, smsIndex); //Extract the SMS index from AT+CMTI: SM,x response
//x is the SMS index
asm clrwdt
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd1, &index, &attempt, 1); //Send AT Command
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd5, &index, &attempt, 1); //Send AT+CMGF=1 Command
//Read the SMS by issuing AT+CMGR=x Command, x is the SMS index obtained previously
GSM_Get_SMS(uartBuffer, buff, resp1, cmd8, smsIndex, &index, &attempt, sms, 1);
//Get the device Code which is the actual SMS
//After the below function is executed string variable sms will contain the actual SMS
Get_Value(sms, value);
asm clrwdt
if(strstr(sms, CopyConst2Ram(buff, secretCode)) != 0) { //If string sms contains the secret code header (20!4) then do the if condition
asm clrwdt
if(strcmp(sms, CopyConst2Ram(buff, secretCode0)) == 0) { //If string sms contains secretcode0
event = 2; //then set event value to 2
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode1)) == 0) { //If string sms contains secretcode1
event = 3; //then set event value to 3
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode2)) == 0) { //If string sms contains secretcode2
event = 4; //then set event value to 4
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode3)) == 0) { //If string sms contains secretcode3
event = 5; //then set event value to 5
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode4)) == 0) { //If string sms contains secretcode4
event = 6; //then set event value to 6
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode5)) == 0) { //If string sms contains secretcode5
event = 7; //then set event value to 7
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode6)) == 0) { //If string sms contains secretcode6
event = 8; //then set event value to 8
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode7)) == 0) { //If string sms contains secretcode7
event = 9; //then set event value to 9
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode8)) == 0) { //If string sms contains secretcode8
event = 10; //then set event value to 10
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode9)) == 0) { //If string sms contains secretcode9
event = 11; //then set event value to 11
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode10)) == 0) { //If string sms contains secretcode10
event = 12; //then set event value to 12
}
//If value contains 2 or 3 digits numerical value (string format) then set event to 13
//value contains threshold value that is set using SMS
//If value is "48" then threshold variable contains integer value 48 and this
//will be the threshold value for temperature
else if(((value[0] >= '0') && (value[0] <= '9')) && ((value[1] >= '0') && (value[1] <= '9'))) {
event = 13;
}
else if(((value[0] >= '0') && (value[0] <= '9')) && ((value[1] >= '0') && (value[1] <= '9')) && ((value[2] >= '0') && (value[2] <= '9'))) {
event = 13;
}
else if(strcmp(sms, CopyConst2Ram(buff, secretCode11)) == 0) { //If string sms contains secretcode11
event = 14; //then set event value to 14
}
else {
event = 15;
sendSms = 1; //Set sendSms flag
}
asm clrwdt
EEPROM_Write(0x02, event); //Write event value to eeprom address 0x02
Delay_ms(20); //A delay so that next eeprom read/write occurs after 20 ms
asm clrwdt
memset(sms, '\0', sizeof(sms)); //Clear sms[] variable
asm clrwdt
sendSms = 1; //Set sendSms flag
}
else {
event = 15;
sendSms = 1; //Set sendSms flag
}
}
asm clrwdt
if(sendSms) {
asm clrwdt
//Messages are brought to RAM to form SMS for sending SMS
switch(event) {
case 0: //If event is 0
asm clrwdt
break;
case 1: //If event is 1
asm clrwdt
CopyConst2Ram(msg, msg7); //Bring msg7[] to RAM
break;
case 2: //If event is 2
asm clrwdt
LIGHT = 1; //Turn ON relay controlling light
LightStatus = 1;
//Set LightStatus flag
CopyConst2Ram(msg, msg1); //Bring msg1[] to RAM
InitTimer1();
break;
case 3: //If event is 3
//Turn OFF relay controlling light
LIGHT = 0; //Clear LightStatus flag
LightStatus = 0;
CopyConst2Ram(msg, msg2); //Bring msg2[] to RAM
asm clrwdt
break;
case 4: //If event is 4
if(LightStatus) { //If LightStatus is set then
CopyConst2Ram(msg, msg1); //Bring msg1[] to RAM
}
else if(!LightStatus) { //If LightStatus is cleared then
CopyConst2Ram(msg, msg2); //Bring msg2[] to RAM
}
asm clrwdt
break;
case 5: //If event is 5
CopyConst2Ram(msg, msg12); //Bring msg12[] to RAM
FloatToStr(temperature, buff); //Convert temperature value (float value)
//to string and store it in buff[]
Ltrim(buff); //Remove leading spaces from the string
Rtrim(buff); //Remove trailing spaces from the string
strcat(msg, buff); //Concatanate string in msg[] and buff[], result in msg[]
strcat(msg, " degree C"); //Concatanate string in msg[] and " degree C" string and
//store resulting string in msg[]
asm clrwdt
break;
case 6: //If event is 6
//Clear AlaramStatus flag
AlarmStatus = 0; //Stop Buzzer
PWM1_Stop();
CopyConst2Ram(msg, msg6); //Bring msg6[] to RAM
asm clrwdt
break;
case 7: //If event is 7
AlarmStatus = 1; //Set AlarmStatus flag
PWM1_Set_Duty(127); //Set PWN Duty to 50% for Buzzer
PWM1_Start(); //Turn ON Buzzer
CopyConst2Ram(msg, msg5); //Bring msg5[] to RAM
INT0IF_bit = 0; //Clear INT0 interrupt flag
//INT0 is used to get the motion detection input
asm clrwdt
break;
case 8: //If event is 8
if(AlarmStatus) { //If AlaramStatus flag is set then
CopyConst2Ram(msg, msg5); //Bring msg5[] to RAM
}
else if(!AlarmStatus) { //Else if AlarmStatus flag is cleared then
CopyConst2Ram(msg, msg6); //Bring msg6[] to RAM
}
asm clrwdt
break;
case 9: //If event is 9
if(MainsStatus == 0) { //If MainsStatus flag is cleared that is
//If mains voltage is normal then
GATE = 1; //Close Gate by giving a pulse to motor
Delay_ms(2000); //Delay for pulse
GATE = 0; //Stop pulse
CopyConst2Ram(msg, msg4); //Bring msg4[] to RAM
GateStatus = 0; //Clear GataStatus flag
}
else {
CopyConst2Ram(msg, msg13);
GateStatus = 0;
}
asm clrwdt
break;
case 10: //If event is 10
if(MainsStatus == 0) { //If MainsStatus flag is cleared that is
//If mains voltage is normal then
GATE = 1; //Open Gate by giving a pulse to motor
Delay_ms(2000); //Delay for pulse
GATE = 0; //Stop pulse
CopyConst2Ram(msg, msg3); //Bring msg3[] to RAM
GateStatus = 1; //Set GataStatus flag
}
else {
CopyConst2Ram(msg, msg13); //Bring msg13[] to RAM
GateStatus = 0; //Clear GateStatus flag
}
asm clrwdt
break;
case 11: //If event is 11
if(GateStatus) { //If GataStatus flag is set then
CopyConst2Ram(msg, msg3); //Bring msg3[] to RAM
}
else if(!GateStatus) { //If GataStatus flag is cleared then
CopyConst2Ram(msg, msg4); //Bring msg4[] to RAM
}
asm clrwdt
break;
case 12:
if(!MainsStatus) { //If MainsStatus flag is cleared that is if
//mains voltage is normal then
CopyConst2Ram(msg, msg8); //Bring msg8[] to RAM
}
else if(MainsStatus) { //If MainsStatus flag is set that is
//if mains voltage is abnormal then
CopyConst2Ram(msg, msg9); //Bring msg9[] to RAM
}
asm clrwdt
break;
case 13:
//threshold = ((deviceCode[0] - 0x30) * 10) + (deviceCode[1] - 0x30);
threshold = atoi(value); //Convert string value to integer and
//store it in threshold variable
GIE_bit = 0; //Temporarily disable Interrupts
//So that eeprom write is not affected
EEPROM_Write(0x01, threshold); //Write threshold value to address 0x01 of eeprom
Delay_ms(50); //A delay is used to ensure that
//another eeprom read/write is not done before 50 ms
GIE_bit = 1; //Enable Global Interrupts
IntToStr(threshold, msg); //Convert integer value in threshold variable to string
//and store it in msg[] variable
asm clrwdt
Ltrim(msg); //Remove leading spaces from the string
Rtrim(msg); //Remove trailing spaces from the string
CopyConst2Ram(buff, msg10); //Bring msg10[] to buff[] (from ROM to RAM)
strcat(buff, msg); //Concatanate string in buff[] with
//string in msg[] and store the resulting
//string in buff[]
CopyConst2Ram(buff2, msg11); //Bring msg11[] (ROM string) to buff2[] (RAM variable)
strcat(buff, buff2); //Concatanate string in buff[] with string in buff2[] and
//store the resulting string in buff[]
strcpy(msg, buff); //Copy the string in buff[] to msg[]
asm clrwdt
break;
case 14:
GIE_bit = 0; //Temporarily disable Interrupts
//So that eeprom read is not affected
threshold = EEPROM_Read(0x01); //Read value from eeprom and store it
//in threshold variable
Delay_ms(50); //Temporarily disable Interrupts
//So that eeprom write is not affected
GIE_bit = 1; //Enable Global Interrupts
IntToStr(threshold, msg); //Convert integer value in threshold variable to string
//and store it in msg[] variable
asm clrwdt
Ltrim(msg); //Remove leading spaces from the string
Rtrim(msg); //Remove trailing spaces from the string
CopyConst2Ram(buff, msg10); //Bring msg10[] to buff[] (from ROM to RAM)
strcat(buff, msg); //Concatanate string in buff[] with
//string in msg[] and store the resulting
//string in buff[]
CopyConst2Ram(buff2, msg11); //Bring msg11[] (ROM string) to buff2[] (RAM variable)
strcat(buff, buff2); //Concatanate string in buff[] with string in buff2[] and
//store the resulting string in buff[]
strcpy(msg, buff); //Copy the string in buff[] to msg[]
asm clrwdt
break;
case 15:
CopyConst2Ram(buff, msg14);
break;
};
asm clrwdt
if((event != 1) && (event != 0)) //If event is not equal to 1 then send SMS
Send_SMS(uartBuffer, buff, resp1, resp4, cmd1, cmd5, cmd9, msg, &index, &attempt, 1);
else if(event == 1) { //If event equal to 1 then
if(!doOnce) //If doOnce flag is not set then send SMS
Send_SMS(uartBuffer, buff, resp1, resp4, cmd1, cmd5, cmd9, msg, &index, &attempt, 1);
doOnce = 1; //Set doOnce flag. This is to make sure that SMS is not sent repeateadly
}
GSM_Send_Const_Cmd(uartBuffer, buff, resp1, cmd10, &index, &attempt, 1); //Send AT+CMGD Command
asm clrwdt
event = 0;
sendSms = 0; //Clear Send SMS flag
asm clrwdt
}
}
}