CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PIC16f877a -GSM Help!

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rennelmallari



Joined: 02 Feb 2013
Posts: 8

View user's profile Send private message

PIC16f877a -GSM Help!
PostPosted: Mon Feb 18, 2013 2:49 am     Reply with quote

Hi everyone! i just would like to ask for your help regarding my codes..

My project is about SMS based Fish Monitoring System

My problem is why i cant send command to the GSM to do a certain task.. And also, since our system works wirelessly thru SMS when the owner is away from the fish farm, there is also scenario that the owner is there, then he can just push buttons without sending SMS to do a certain task, like activating the submersible pump. How do i do that? Do i need to use interrupts? but i dont know them..pls help help..

Here's my code which i learned from this site. Help pls.. Smile

Quote:
// final program
#include <16f877a.h>
#device adc=10
#include <stdlib.h>
#use delay(clock=20000000)
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,NOPUT
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, stream=GSM)
#include "math.h"
#include <lcd.c>

void scankey();

void initialize();
void rtd_value();
void water_level();



int DM();
void read_sms();
int get_cmd();
void do_cmd();
void del_sms();

short sms_mode=0;

volatile int counter_search=0; //counter to traverse the array
volatile int counter_read=0; //counter to fill the array

Volatile int HitCounter=0; // for string comparison
Volatile int Next_CMD=0;


volatile Char Recieve_String[70];



void main()
{
lcd_init();

initialize();
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(true)
{
if(input(PIN_C0))
{
water_level();
scankey();
rtd_value();
}

if(sms_mode==1)
{
lcd_putc("\f");
lcd_gotoxy(4,1);
printf(lcd_putc,"%c",Recieve_String[counter_read]);
delay_ms(3000);
DM();
read_sms();
get_cmd();
do_cmd();
del_sms();
sms_mode=0;
counter_read=0;
}

lcd_putc("\f");
}
}

void initialize()
{
printf("AT\n\r");
delay_ms(500);

printf("AT+CMGF=1\n\r"); //It inhabits the text mode sms
delay_ms(500);

lcd_putc("\f");
lcd_putc("\fGSM MODEM\n");
lcd_putc("INITIALIZED");
delay_ms(1000);

}






void rtd_value()
{

float value,rtdv,rtdr,x,y,temp;
int8 deg=0xDF;
//configure the A/D.....
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);



value=read_adc();
rtdv=value/1024; //volts per bit


//calculate the rtd resistance rtdr
rtdr=rtdv*1000.0/(5.0-rtdv);

//calculate the temperature temp
x=0.15274-(rtdr-100.0)*2310.0e-7;
if(y>=0) y=sqrt(x);

temp=(y-0.39083)/(-0.0001155); //final temp

lcd_putc("\fTemp\n");
printf(lcd_putc,"%f %cC",temp,deg); // Send test data again
lcd_putc("\a");
delay_ms(1000);

if (temp>25)
{
output_high(PIN_B1);
lcd_putc("\fHeat Alarm!");
delay_ms(500);

lcd_putc("\fOxygen Pump");
lcd_putc("Activated");
delay_ms(500);

fprintf(GSM,"at+cmgs=\"09061594088\"\r\n");
delay_ms(300);
fprintf(GSM, "Temp exceeds 25 'C.\n"); // Text message
fprintf(GSM, "OXYGEN PUMP Activated!\n"); // Text message

putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);


lcd_putc("\fSending Msg");
delay_ms(500);

lcd_putc("\fMessage Sent!");
delay_ms(1500);
}

else output_low(PIN_B1);
delay_ms(500);

}






void water_level()
{

int16 time;

set_timer1(0);
setup_timer_1(t1_external|T1_DIV_BY_1);
delay_ms(1000/3);

setup_timer_1(T1_DISABLED);
time=get_timer1();

lcd_putc("\fWater Level\n");
printf(lcd_putc,"%LU HZ ",time);
lcd_putc("\a");
delay_ms(2000);

if(time<294)
{
output_high(PIN_B2);

lcd_putc("\fCritical Low!");
delay_ms(500);

lcd_putc("\fSub Pump Activated");
delay_ms(500);

fprintf(GSM,"at+cmgs=\"09061594088\"\r\n");
delay_ms(300);
fprintf(GSM, "Water level is in critical low.\n"); // Text message
fprintf(GSM, "SUBMERSIBLE PUMP Activated!\n"); // Text message

putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);


lcd_putc("\fSending Msg");
delay_ms(500);

lcd_putc("\fMessage Sent!");
delay_ms(1500);
}
else output_low(PIN_B2);

if(time==294)
{
output_high(PIN_B4);
lcd_putc("\fNormal Level");
delay_ms(500);
}
else output_low(PIN_B4);


if(time>294)
{
output_high(PIN_B5);
lcd_putc("\fCritical High!");
delay_ms(500);

lcd_putc("\fSub Pump Activated");
delay_ms(500);

fprintf(GSM,"at+cmgs=\"09061594088\"\r\n");
delay_ms(300);
fprintf(GSM, "Water level is in critical high.\n"); // Text message
fprintf(GSM, "SUBMERSIBLE PUMP Activated!\n"); // Text message

putc(0x1A);// Print (Ctr+Z) to send message
delay_ms(300);


lcd_putc("\fSending Msg");
delay_ms(500);

lcd_putc("\fMessage Sent!");
delay_ms(100);
}
else output_low(PIN_B5);
}




int DM()
{

const char StrCMTI[]={"+CMTI"};

counter_read=0;

While(counter_read<=16) // read all data
{
output_high(PIN_B1); // signal ready to receive sms
}
counter_read=0;

Delay_ms(500); // ignore all remaining characters recieved..if any.
output_low(PIN_B1); // off receive ready light.
counter_search=2;

HitCounter=0;

while((HitCounter!=5)&&(counter_search<8))
{
if(Recieve_String[counter_search]==StrCMTI[counter_search-2])
HitCounter++;

counter_search++;
}

if(HitCounter==0x05)
Return(1);
else return(0);
}

void read_sms()
{
counter_read=0;
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("READ SMS");
delay_ms(1000);

fprintf(GSM, "AT+CMGR=1\r\n"); // send command, MEMORY LOCATION OF SMS IS ALWAYS ONE, SINCE I DELETE THEM AFTER PROSCESING

delay_ms(3000);
}

int get_cmd()
{
const char CMD1[8][7]={"ON1","ON2","ON3","ON4","ON5","ON6","ON7","ON8"};

int offset=0;
int store_counter=0;
counter_search=0;


while((Recieve_String[counter_search]!='!')&&(counter_search<69)) // wait till command indicator is found '!'
{
counter_search++;
}

counter_search=counter_search+1; // increment one to actual first letter of command
store_Counter=counter_search; // store current position for multiple searches
NEXT_CMD=0; // NEXT_CMD keeps track of the command being read, thus used

while((HitCounter!=6)&&(NEXT_CMD<8)) // compare to all commands in list.
{
counter_search=store_Counter; // initialize counter search with stored counter value.
offset=0; // since value of counter is unknown use a separate counter for the const array
HitCounter=0; // counts number of equal letters found.

while((HitCounter!=6)&&(offset<=6)) // keeps the counters in check...to not overshoot. and compares the strings
{
if(Recieve_String[counter_search]==CMD1[NEXT_CMD][offset]) // if letter is found
{HitCounter++;
offset++;
counter_search++;
}
}

if(HitCounter==6) // if 6 chars found...exit..
{
Return(1);
}

NEXT_CMD++; // if increase to search next command.

}

Return(0);
}

void do_cmd()
{
lcd_putc("\f");
lcd_gotoxy(0,1);
lcd_putc("do command");
delay_ms(1000);

if(NEXT_CMD==0)output_low(PIN_B4);
if(NEXT_CMD==1)output_high(PIN_B4);

if(NEXT_CMD==2)output_low(PIN_B5);
if(NEXT_CMD==3)output_high(PIN_B5);

if(NEXT_CMD==4)output_low(PIN_B6);
if(NEXT_CMD==5)output_high(PIN_B6);

if(NEXT_CMD==6)output_low(PIN_B7);
if(NEXT_CMD==7)output_high(PIN_B7);
}

void del_sms()
{
counter_read=0;

printf("AT+CMGD=1"); //send command
putchar(0x0D); //send return

While(counter_read<=15) // get all returned text
{
}

counter_read=0;

Delay_ms(500); // ignore all remaining characters received

}

#INT_RDA
void SerialInt()
{
while (kbhit())
{
Recieve_String[counter_read]=getchar();

if(Recieve_String[counter_read]=='+'){//first character of phone number
counter_read=0;//to begin to write the var buffer from the 1 st position
output_high(PIN_B7);
sms_mode=1;
}
counter_read++;
}
return;
}


void scankey()
{
output_c(60); output_low(PIN_C2);
if(!input(PIN_C2))
{ output_high(PIN_B1); delay_ms(50); while(!input(PIN_C2)) { } ; }


output_c(60); output_low(PIN_C3);
if(!input(PIN_C3))
{ output_high(PIN_B2); delay_ms(50); while(!input(PIN_C3)) { } ; }


output_c(60); output_low(PIN_C4);
if(!input(PIN_C4))
{ output_high(PIN_B4); delay_ms(50); while(!input(PIN_C4)) { } ; }


output_c(60); output_low(PIN_C5);
if(!input(PIN_C5))
{ output_high(PIN_B5); delay_ms(50); while(!input(PIN_C5)) { } ; }
}
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Feb 18, 2013 8:26 am     Reply with quote

try to use the Code button instead of the Quote button...
Code:
points_for_trying++;



I really need to update that SMS code... sigh...
(Im almost done with a new driver...)

The code you are using is mine... its really only an example and I would really never use it for a really product/project...



If you can wait a bit, ill be posting a new "driver" soon...and that "should" be plug and play...



As a word of advise, you should work your code as modules...
first get the SMS part working, then get the Pumps and other things you want to do individually working.. and then slowly start to merge codes...

if you try to get it all to work at once... you are going to have a bad time...


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Feb 18, 2013 11:27 am     Reply with quote

Are you using Proteus for simulation?
I guessed so because your XT fuse setting is incompatible with a 20MHz crystal. One of the main reasons why we don't like Proteus is that it is full of bugs like this.

I didn't look too deep in the phone handling code. As Gabriel already pointed out, it was his first project and things should be improved. It's up to you if you want to wait for Gabriel's improved version or that you want to have your own designed version sooner (and learn something while doing so).
We can help you to create your own program. My first advice would then be to break up the program in smaller pieces and get those working before merging it all into one large program. For example, get the button reading working right first.

A few other minor points:
- When you re-use code from another project or from the Code library, then say so in your code. It will help people looking at your code to understand why there are different mixed coding styles. Also, doing so is a nice gesture to the original person offering the code for free.
- Try to avoid 'magical' numbers in your code.
Code:
output_c(60);
WTF is 60? At least add some explaining comments. But even better, make the code 'self explaining':
Code:
#define    ACTIVATE_COUNT_DOWN_PROCEDURE    60           // Writing this value to port C will start the Count Down procedure for firing the missiles.
....
output_c(ACTIVATE_COUNT_DOWN_PROCEDURE);

The same applies to all your other output functions where you use the PIN_XX define. Something like the following is much easier to understand:
Code:
#define BUTTON_FIRE    PIN_C1
#define BUTTON_CANCEL  PIN_C2
...
if(!input(BUTTON_CANCEL))
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Mon Feb 18, 2013 12:07 pm     Reply with quote

@ckielstra:
Quote:
Count Down procedure for firing the missiles.


and by that i guess you mean:

Quote:
"Count Down procedure for firing 'Ze' missiles."

http://www.albinoblacksheep.com/flash/end



G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Feb 19, 2013 2:12 am     Reply with quote

Gabriel wrote:
and by that i guess you mean:

Quote:
"Count Down procedure for firing 'Ze' missiles."

http://www.albinoblacksheep.com/flash/end
I didn't know this one, but it is similar to what I had in mind. Very Happy
rennelmallari



Joined: 02 Feb 2013
Posts: 8

View user's profile Send private message

PostPosted: Fri Feb 22, 2013 8:55 am     Reply with quote

Hi Sir Gabriel, yes i think i got the codes from you..

Can u help me regarding sending command to my system?? cause i got my problem with it, i dont know how to start it..

sorry ..pls help.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group