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

Send Latitude and Longitude by module sim908 (FTP server).

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



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

Send Latitude and Longitude by module sim908 (FTP server).
PostPosted: Sat Apr 26, 2014 4:47 am     Reply with quote

I want to code for send GPS signal by FTP server. But code it doesn't run.
Help me!!
Thanks
Code here is send GPS data to web and send sms(GPS, temperature by DHT11) to phone when have a messenger.
Code:

#include <18F4620.h>
#include <string.h>
#INCLUDE <stdlib.h>
#include <ctype.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=57600, parity=N, xmit=PIN_C6, rcv=PIN_C7, stream=GPS )  // Jumpers: 8 to 11, 7 to 12
void erase_buffer(void);   // erase character in buffer[]
void init_sim900(void);    // Function to boot sim900
void read_sms();           // read sms message
void send_sms();           // send sms message
void call();               // call to number phone`
void at(void);             // test sim900
void sleep_mode(void);     // set mode sleep for sim900g1gi
short wait_ok (void);      // Funtion to wait string "OK" from SIM900
short wait_error (void);   // Funtion to wait string "ERROR" from SIM900
#use fast_IO(B)
#define DHT11_pin PIN_A0
#define  buffer_size          160      //Buffer size you can adjust this size
   char     buffer[buffer_size];          // SAVE SMS MESSAGE 
   int      xbuff=0x00;                   //
   char     c=0x00;                       // the first characters recived of SMS message
   char     c1=0x00;                    // the end characters recived of SMS message
   char     c2=0x00; 
   short    sms_mode=false;               // //It's a flag when is true, call read_sms();

short wait_ok (void);      // Funtion to wait string "OK" from SIM900
char latitude[15];
char longitude[15];
char chua[20];
char chun[20];
unsigned char values[5];

#int_RDA
RDA_isr()
   {
  // if(c=='\n'){c1=c; xbuff=0;}
//if(c=='\r' && c1=='\n');
   c=getc();
      if(c=='#'|| c=='\n'){                 
        c1=c;              //first character of phone number
        xbuff=0;   
        }
   if (c=='.' && c1=='#') // the end character OR the first character
      {
       sms_mode=true;
      }
   else if(c=='\r' && c1=='\n');
   if (xbuff >= buffer_size) 
      {xbuff=0;}
      else{  buffer[xbuff++]=c;} // save character of SMS message
   return 0;
   }
//************************** erase character in buffer[]***********************
short wait_2D (void){
char *ptr,*ptr1;
char r1_ok[]="2D Fix";
char    r1_ok1[]="3D Fix";
ptr1=strstr(buffer,r1_ok1);
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,6)==0){
   delay_ms(500);
   return true;
   }
else if(strncmp(ptr1,r1_ok1,6)==0 )
   {
   delay_ms(500);
   return true;
   }
 else{
   return false;
   }
   erase_buffer();
}
//-----------------------------------------------------------------------------------------------


//------------------------compare with +FTPPUT:1,1, -------------------------------------------
short wait_FTPPUT1 (void){
char *ptr;
char r1_ok[]="+FTPPUT:1,1,";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,11)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
   erase_buffer();
}

short wait_FTPPUT1_1 (void){
char *ptr;
char r1_ok[]="+FTPPUT:1,6";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,10)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
   erase_buffer();
}

short wait_FTPPUT11 (void){
char *ptr;
char r1_ok[]="+FTPPUT:1,64";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,12)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
   erase_buffer();
}


short wait_FTPPUT2 (void){
char *ptr;
char r1_ok[]="+FTPPUT:2,";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,9)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
   erase_buffer();
}
//-------------------------------------------------------------------------


//--------------------------------compare with +FTPPUT:1,0 -----------------------------
short wait_FTPPUTOK(void){
char *ptr;
char r1_ok[]="+FTPPUT:1,0";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,10)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
   erase_buffer();
}
//--------------------------------------------------------------------------------------------

//--------------------------wait signal gps ready-------------------------------------------------
void wait_gps(void){
            do
            {
            printf("AT+CGPSSTATUS?\r");
            delay_ms(1000);
            }
            while((wait_2D() == false));
            }       
//----------------------------------------------------------------------------------------------           


//------------------------------GPS initial---------------------------------------------
void GPS_init(void)
{
    do
   {
   printf("ATZ\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
    do
   {
   printf("AT+IPR=57600\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
    do
   {
   printf("AT+CGPSPWR=1\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   do
   {
   printf("AT+CGPSRST=1\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 }
//---------------------------------------------------------------------------------------

//------------------------FTP initial--------------------------------------------------
 void ftp_init(void)
{
         do
   {
   printf("AT+SAPBR=3,1,\"Contype\",\"GPRS\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 
    do
   {
   printf("AT+SAPBR=3,1,\"APN\",\"v-internet\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
    do
   {
   printf("AT+SAPBR=3,1,\"USER\",\"\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
    do
   {
   printf("AT+SAPBR=3,1,\"PWD\",\"\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 
   do
   {
   printf("AT+SAPBR=1,1\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+FTPCID=1\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+FTPSERV=\"ftp.freevnn.com\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 
   do
   {
   printf("AT+FTPPORT=21\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+FTPUN=\"freev_14414390\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 
   do
   {
   printf("AT+FTPPW=\"12345678\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
 
   do
   {
   printf("AT+FTPPUTNAME=\"gps1.txt\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+FTPPUTPATH=\"/htdocs/\"\r");
   delay_ms(2000);
   }
   while(wait_ok()==false);
}
//-----------------------------------------------------------------------------------


//--------------------------------------get gps signal and split to get latitude,longitude----------------------------
void toado()
{
   char term[3];
   char don[3];
   char xuongdong[3];
   erase_buffer();
   printf("AT+CGPSINF=32\r");
   delay_ms(1000);
  strcpy(term,",");
   strcpy(don,".");
   strcpy(xuongdong,"\r");
    strtok(buffer, term);
    strcpy(chua,strtok(0, term)); // Gets longitude
    strcpy(latitude,strtok(0, term)); // Gets latitude
    strcpy(chun,strtok(0, term));
    strcpy(longitude,strtok(0, term));
}
//----------------------------------------------------------------------------------------------


//----------------------------------------send latitude,longitude to FTP server----------------------------
void sendweb()
{
   int i=0;
   c:
   do
   {
   printf("AT+FTPPUT=1\r\n");
   delay_ms(1000);
   }
   while(wait_ok()==false);
   
   do
   {
   
   // i=i+1;
    //if(wait_FTPPUT1_1()==false)
    //goto c;
   // else if(i==20)
   // goto c;
  //  else
    delay_ms(2000);
   }
   while(wait_FTPPUT1()==false);
 
   do
   {
   printf("AT+FTPPUT=2,%d\r",strlen(latitude)+strlen(longitude)+1);
   delay_ms(1000);
   }
   while(wait_FTPPUT2()==false);
   
   do
   {
   printf("%s,%s",latitude,longitude);   
   delay_ms(1000);
   }
   while(wait_FTPPUT1()==false);
   
   do
   {
   printf("AT+FTPPUT=2,0\r");
   delay_ms(1000);
   }
   while(wait_ok()==false);
   
   do
   {
   delay_ms(1000);
   }
   while(wait_FTPPUTOK()==false);
}
//---------------------------------------------------------------------


void DHT11_init();
unsigned char get_byte();
unsigned char get_data();

void DHT11_init()
{
   output_float(DHT11_pin);
   delay_ms(1000);
}

unsigned char get_byte()
{
   unsigned char s = 0;
   unsigned char value = 0;

   for(s = 0; s < 8; s += 1)
   {
      value <<= 1;
      while(!input(DHT11_pin));
      delay_us(30);

      if(input(DHT11_pin))
      {
          value |= 1;
      }
      while(input(DHT11_pin));
   }
   return value;
}
void setup()
{
   disable_interrupts(global);
   port_B_pullups(FALSE);
   setup_timer_0(T0_internal | T0_8_bit);
   set_timer0(0);
   DHT11_init();

}


int get_data()
{
   short chk = 0;
   unsigned char s = 0;
   unsigned char check_sum = 0;

   output_high(DHT11_pin);
   output_low(DHT11_pin);
   delay_ms(18);
   output_high(DHT11_pin);
   delay_us(26);
   
   chk = input(DHT11_pin);
   if(chk)
   {
      return 0;
   }
   delay_us(80);
   
   chk = input(DHT11_pin);
   if(!chk)
   {
      return 0;
   }
   delay_us(80);

   for(s = 0; s <= 4; s += 1)
   {
       values[s] = get_byte();
   }
 
   output_high(DHT11_pin);
   
   for(s = 0; s < 4; s += 1)
   {
       check_sum += values[s];
   }
   
   if(check_sum != values[4])
   {
      return 0;
   }
   else
   {
      return 1;
   }
}

   
//**************************** Interrupt USART **********************************

void erase_buffer(void)
   {
   int i;
   for(i=0;i<buffer_size;i++){ buffer[i]=0x00;  }
   xbuff=0x00;
   c=0x00;
   c1=0x00;
   sms_mode=false;
   return;
   }
//************************ Function to boot sim900 *****************************   

void init_sim900(void){ 
   do
   {
   printf("AT\r");//Selecciono la mem sim para recibir y leer sms
   delay_ms(500);
   }
   while(wait_ok()==false);
   do
   {
   printf("ATE0\r");//Selecciono la mem sim para recibir y leer sms
   delay_ms(500);
   }
   while(wait_ok()==false);
   do
   {
   printf("AT+CPMS=\"SM\",\"SM\"\r");//Selecciono la mem sim para recibir y leer sms
   delay_ms(500);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+CNMI=2,2,0,0,0\r");//los sms se envían directamente al puerto serie
   delay_ms(500);
   }
   while(wait_ok()==false);
   
   do
   {
   printf("AT+CMGF=1\r"); //Habita el modo texto de los sms
   delay_ms(500);
   }
   while(wait_ok()==false);
   
}

void send_sms1()
{
   unsigned char state = 0;
   state = get_data();
   printf("AT+CMGS=");     // at command to send sms
   putc(34); // "
   printf("01678119857");//xxxxxxxx is your cellphone number
   putc(34); // "
   putc(13);   // CR
   delay_ms(500);
 
   printf("Toa do: %s,%s \n",latitude,longitude); 
   if(state)
   {
   printf( "Nhiet do: %3u.%03u \n", values[2], values[3]);
   printf("Do am: %3u.%03u \n ", values[0], values[1]);
   }
   delay_ms(500);
   putc(26);//  ctrl + Z
   delay_ms(2000);
}


void send_sms(int n)
{
   unsigned char state = 0;
   state = get_data();
   printf("AT+CMGS=");     // at command to send sms
   putc(34); // "
   printf("01678119857");//xxxxxxxx is your cellphone number
   putc(34); // "
   putc(13);   // CR
   delay_ms(500);
   if(state)
   {
   switch(n){
case 1: { printf( "Nhiet do: %3u.%03u \n", values[2], values[3]); break;}
case 2: {printf("Do am: %3u.%03u \n ", values[0], values[1]); break;}
default: break;
}
   }
   delay_ms(500);
   putc(26);//  ctrl + Z
   delay_ms(2000);
}
//*************************read_sms()*******************************************
 void read_sms() 
   {
   char *ptr;
   char r2_on[]="Nhietdo";
   char r3_off[]="Doam";
   
     ptr=strstr(buffer,r2_on);   //search string "On" in buffer[]
      if(strncmp(ptr,r2_on,7)==0)
         {
         send_sms(1);
         }
     ptr=strstr(buffer,r3_off);       //search string "Off" in buffer[]
      if(strncmp(ptr,r3_off,4)==0)     // compare result with string "Off". if it is true then function return is 0 and else is 1
         {
          send_sms(2);             
         }
   }
   
//************************************send_sms() *******************************


//**************************Function to call*****************************************
void call()
   {
   at();    //test at command before call
   do
      {
      printf("ATD");
      printf("xxxxxxxx");//xxxxxxxx is your cellphone number
      printf(";");
       putc(13);  // CR
      delay_ms(2000);
      }
      while (wait_ok()==false);
      return;
   }

//*********************************************************************************
void sleep_mode(void){
   do
   {
   printf("AT+CSCLK=2");
   putc(13);
   delay_ms(500);
   }
   while (wait_ok()==false);
   return;
}

//*********************************************************************************
void at(void)
{
   do                     //SENT COMMAND "AT" TEST MODULE SIM900
   {
   printf("AT");
   putc(13);
   delay_ms(1000);
   }
   while (wait_ok()==false);
   return;
}

//***************Funtion to wait string "OK" from SIM900****************************
short wait_ok (void){
char *ptr;
char r1_ok[]="OK";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,2)==0){
   delay_ms(500);
   return true;
   }
   else{
   return false;
   }
erase_buffer();
}

//******************Funtion to wait string "OK" from SIM900**************************
short wait_error (void){
char *ptr;
char r1_ok[]="ERROR";
ptr=strstr(buffer,r1_ok);
if(strncmp(ptr,r1_ok,5)==0){
   return true;
   }
   else{
   return false;
   }
erase_buffer();
}



//******************************************************************************
// Programa principal
//******************************************************************************

void main(void){
   setup();
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);       
   init_sim900();
   GPS_init();
   ftp_init();
while(1)
{
if(sms_mode==true)//si sms_mode=true se pasa a la función lee_sms()
{
  send_sms1();
  //read_sms();
  //erase_buffer();
  sms_mode==false;
  goto a;
}
else
{
 a:
  wait_gps();
  toado();
  delay_ms(1000);
  sendweb();
  delay_ms(1000);
}
}
}



Last edited by thanvuongv on Mon Jul 28, 2014 12:02 pm; edited 8 times in total
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 26, 2014 5:51 am     Reply with quote

basic help....

1) does the PIC run the classic '1 Hz LED program'?

2) does the PIC run the 'Hello PC' program'?

3) does the GPS work with a PC ?

4) what interface does the GPS have ?

5) add 'errors' to the use rs232(...options..)?

6) send 'messages to PC ' for EVERY GPS communication

7) most GPS units are usually 3 volt, your PIC is 5 volt !!

8) delay_ms(xx) CANNOT be greater than 65535 !!

9) 'it doesn't run' is NOT helpful. You need to breakdown the program into sections that do. Send messages to a PC or local LCD module to confirm an action has been done(true or false).Use a PC the way your PIC program is coded..does THAT work ?

10) you need to use 'streams' for serial communications. Set one up for 'GPS' UART, the other for the 'SERVER'. Right now ALL communications go to the UART on C6-C7.

recode/recompile/retest/report back
always happy to help

jay
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 6:10 am     Reply with quote

thank for your help.
I connected to my PC and test ok. GPS and FTP server is running follow mind.

So. I have all AT-command to connect module sim908.
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 6:14 am     Reply with quote

delay_ms(xxx) less than 65535. But when I send a AT-command is AT+FTPPUT=1. The firsly is "OK" receive and after about 7second is receive +FTPPUT:1,1,1200. So I use delay_ms more than 65535. Can you help me the error.
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 6:18 am     Reply with quote

I connected pic18f4620 with PC and when I send a command from pic to module sim908 is receive. Not error.

I use only one UART because I can use AT-command to get GPS signal.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 26, 2014 9:01 am     Reply with quote

hmmm...
1)
#use rs232(baud=56000, parity=N, xmit=PIN_C6, rcv=PIN_C7)

baudrate looks 'wrong' to me, maybe should be 57600 ??


2) always add 'errors' to the use rs232(...options....). This will prevent UART from 'hanging'(stopping) due to overrun condition( more than 3 characters coming in before program handles them).

3) delay_ms(65535) is over 1 minute( 65.535 seconds)..... way too ling for ANY serial communications...

4) you need to add some kind of verification that EACH 'print to GPS' is correct. Usually you take an unused pin,configure as a software serial port, and send messages to a PC terminal program OR send to a local LCD module. You need this for properly debugging what is going wrong.

recode/recompile/retest/report back

hth
jay
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 9:25 am     Reply with quote

thank you. I repaired baud.

Last edited by thanvuongv on Sat Apr 26, 2014 11:24 am; edited 1 time in total
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 9:45 am     Reply with quote

ok

Last edited by thanvuongv on Sat Apr 26, 2014 11:22 am; edited 4 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Sat Apr 26, 2014 9:54 am     Reply with quote

Just a comment.

A 'delay_ms(65535);', is _65.5 seconds_. 7 seconds is only delay_ms(7000)....
thanvuongv



Joined: 26 Apr 2014
Posts: 8
Location: Vietnam

View user's profile Send private message Yahoo Messenger

PostPosted: Sat Apr 26, 2014 11:16 am     Reply with quote

Thank for your help.

My project is complete
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