How to use the GPS, DS18B20, analog input and SDCARD

Dependencies:   DS1820 SDFileSystem mbed RHT03

Fork of frdm_serial by Freescale

main.cpp

Committer:
fgmpinheiro
Date:
2015-09-03
Revision:
8:164088fcf57b
Parent:
7:986d5298b118
Child:
9:27360d48afcd

File content as of revision 8:164088fcf57b:

#include "mbed.h"
#include "SDFileSystem.h"
#include "DS1820.h"
/*******************************************************************************************/
DigitalOut myled(LED_GREEN);
Serial pc(USBTX, USBRX);
Serial gps(PTC17, PTC16);
AnalogIn analog_value(A1);
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
DS1820  ds1820(PTB3);    // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin    
/*******************************************************************************************/
FILE *fp;
char gps_data[255];
/*******************************************************************************************/

int main()
{
    int i,rlock,stn;
    char gps_data[256],str1[25],str2[25];
    char ns,ew,aval,modi,mv,mi;
    float utctime,hokui,tokei;
    float g_hokui,g_tokei;
    float d_hokui,m_hokui,d_tokei,m_tokei;
    int h_time,m_time,s_time,d_date,m_date,a_date;
    float speed,course,utcdate,magvar;
    int j;
    float v1,temp;
      
    wait(2);
    pc.baud(115200);
    gps.baud(9600);
    pc.printf("\nHello World! I am ALPINHA 4\n");
    mkdir("/sd/test1", 0777);
    fp = fopen("/sd/alpinha2.txt", "w");
    if (fp == NULL) 
    {          // that it was created.
        pc.printf("\nnao possivel abrir o arquivo\n");;           // Return error.
    } else 
    {
        pc.printf("\n arquivo aberto para escrita\n");;           // Return error.
    }
    fprintf(fp, "1.txt in test 1");
    fclose(fp);
    ds1820.begin();
    ds1820.setResolution(12);
    wait(1.0);                  // let DS1820 complete the temperature conversion
    pc.printf("temp = %3.3f\r\n", ds1820.read());     // read temperatura            
    while (1) 
    {      
      i=0;
      while(gps.getc()!='$');       
      while( (gps_data[i]=gps.getc()) != '\r') i++;      
      gps_data[i]='\0'; 
      aval='\0';
      for i=0 to 100
      v1 =v1+ analog_value;//.read();
      end;
      
      if((gps_data[2]=='R')&(gps_data[3]=='M')&(gps_data[4]=='C'))      
      {
        temp=ds1820.read();   
        ds1820.startConversion();        
          for(j=0;j<i-1;j++) if(gps_data[j]==',') gps_data[j]=' ';   
          sscanf(gps_data,"%s %f %c %f %c %f %c %f %f %f %s",   str1,&utctime,&aval,&hokui,&ns,&tokei,&ew,&speed,&course,&utcdate,str2);      
      //if(aval=='A')
        {
            //latitude
            d_hokui = int(hokui/100);
            m_hokui = (hokui - d_hokui*100)/60;
            g_hokui = d_hokui + m_hokui;
            //longitude
            d_tokei = int(tokei/100);
            m_tokei = (tokei - d_tokei*100)/60;
            g_tokei=d_tokei + m_tokei;
          
            //time set
            h_time = int(utctime/10000);
            m_time = int((utctime - h_time*10000)/100);
            s_time = int(utctime - h_time*10000 - m_time*100);
          
            //date set
            d_date = int(utcdate/10000);
            m_date = int((utcdate - d_date*10000)/100);
            a_date = int(utcdate - d_date*10000 - m_date*100);
          
            pc.printf("%02d/%02d/%02d %02d:%02d:%02d %c %4.6f %4.6f %4.6f %3.3f --->%f\n",d_date,m_date,a_date,h_time,m_time,s_time,aval,g_hokui,g_tokei,speed,temp,v1);       
            fp = fopen("/sd/alpinha2.txt", "a");
            if (fp == NULL) 
            {          // that it was created.
                 pc.printf("\nnao possivel abrir o arquivo\n");;           // Return error.
            } else {
                        pc.printf("\n turma do curso\n");;
                        fprintf(fp,"%2d/%2d/%2d %2d:%2d:%2d %c %4.6f %4.6f %4.6f %3.3f\r\n",d_date,m_date,a_date,h_time,m_time,s_time,aval,g_hokui,g_tokei,speed,temp);       
                    }
            fclose(fp);
        }           
          
    }
}
 }