manuel.c 5.63 KB
/*-------------------------------------------------------------------------------
  --------------------------PROJET BALLON ATMOSPHERIQUE ---------------------------
  ----------------------Baptiste GRILLERE - Olivier MAHIEUX------------------------
  --------------------------------------IMA4---------------------------------------
  ---------------------------------------------------------------------------------
  --------------------------------PROGRAMME MANUEL---------------------------------
  ---------------------------------------------------------------------------------*/

#include<stdlib.h>
#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<termios.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ioctl.h>
#include<linux/serial.h>

//INITIALISATIONS
char chaine[25] = "ici raspberry, over\n";
char reception[200]; //reception lora
//chaines servant à la comparaison avec la reception LORA
char veille[10] = "veille\n";
char demande_gps[4] = "GPS";
char demande_char[5] ="char";
char demande_veille[7] ="veille";
char demande_altitude[4] = "alt";
char demande_longitude[5] = "long";
char demande_latitude[4] = "lat";
char demande_time[5] = "time";
char press[9] = "pression";



char data_gps[1024]; //buffer dans la fonction lecture_GPS()
char trame[69], header[64], time[64], lat[64], longi[64],alt[64], nord[64], est[64];
char metres[64] = "metres";
int retoursscanf = 0;
int portGPS;
int portCAPTEUR;
int portLORA;
FILE* fp; //utilisation pour le traitement sémantique des données GPS
char pression[64]; //buffer pour la lecture du capteur

int init_serial(char*device,int speed){
  struct termios new;
  struct termios saveterm;
  int fd = open(device,O_NOCTTY|O_RDWR|O_NONBLOCK);
  if(fd<0){perror(device); exit(-1);}
  tcgetattr(fd,&saveterm); /* save current port settings */
  bzero(&new,sizeof(new));
  new.c_cflag=CLOCAL|CREAD|speed|CS8;
  new.c_iflag=0;
  new.c_oflag=0;
  new.c_lflag=0;  /* set input mode (non-canonical, no echo,...) */
  new.c_cc[VMIN]=1; /* blocking read until 1 char received */
  tcflush(fd, TCIFLUSH);
  tcsetattr(fd,TCSANOW,&new);
  return fd;
}    


void init_PORT(){

  portGPS = init_serial("/dev/ttyUSB0",B9600);
  portLORA = init_serial("/dev/ttyACM1",B9600);
  portCAPTEUR = init_serial("/dev/ttyACM0",B9600);
  fp=fdopen(portGPS,"r");

}

//Le mode initial est "en veille"
int mode = 0;


//Verification et changement éventuel de mode
void check_mode(){
  if(memcmp(reception,demande_veille,sizeof(demande_veille)-1)==0)
    mode = 0;
  if(memcmp(reception,demande_char,sizeof(demande_char)-1)==0)
    mode = 1;
  if(memcmp(reception,demande_gps,sizeof(demande_gps)-1)==0)
    mode = 2;
  if(memcmp(reception,demande_altitude,sizeof(demande_altitude)-1)==0)
    mode = 3;
  if(memcmp(reception,demande_longitude,sizeof(demande_longitude)-1)==0)
    mode = 4;
  if(memcmp(reception,demande_latitude,sizeof(demande_latitude)-1)==0)
    mode = 5;
  if(memcmp(reception,demande_time,sizeof(demande_time)-1)==0)
    mode = 6;
  if(memcmp(reception,press,sizeof(press)-1)==0)
    mode = 7;
}

//Fonction lisant ce que le module LORA receptionne
void lecture_LORA(){
  if ( read(portLORA,reception,sizeof(reception)) != -1){
    printf("reception lora: %s\n",reception); 
  }
  check_mode();

}

//Fonction stockant dans data_GPS les données provenant du GPS
void lecture_GPS(){
  printf("lecture GPS....\n");
  
  while(retoursscanf != 7 || (memcmp(header,"$GPGGA",7) != 0)) //Acquisition d'une trame complète et débutant par $GPGGA
    { 
      //Acquisition trame GPS
      fgets(data_gps,sizeof(data_gps),fp);
      //traitement de la trame
      retoursscanf = sscanf(data_gps,"%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%*[^,],%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*s", header, time, lat, nord, longi, est, alt);
			    printf("header: %s\n",header);
			    }
	printf("heure : %s\n", time);
      printf("header : %s\n", header);
      printf("latitude : %s\n", lat);
      printf("longitude : %s\n", longi);
      printf("altitude : %s\n", alt);
  
      //Envoi au sol des données traitées
      if(mode == 6) 
	write(portLORA,&time,sizeof(time));
      if(mode == 5){
	write(portLORA,&lat,sizeof(lat));
	write(portLORA,&nord,sizeof(nord));
      }
      if(mode == 4) {
	write(portLORA,&longi,sizeof(longi));
	write(portLORA,&est,sizeof(est));
      }
      if(mode == 3){
	write(portLORA,&alt,sizeof(alt));
	write(portLORA,&metres,sizeof(metres));
      }
      if(mode==2){
	write(portLORA,&time,sizeof(time));
	write(portLORA,&lat,sizeof(lat));
        write(portLORA,&nord,sizeof(nord));
        write(portLORA,&longi,sizeof(longi));
        write(portLORA,&est,sizeof(est));
	write(portLORA,&alt,sizeof(alt));
        write(portLORA,&metres,sizeof(metres));
      }
    }

        void lecture_capteur(){
        printf("Mode envoi valeur pression\n"); //valeur en hPa
        read(portCAPTEUR,&pression,sizeof(pression));
        printf("pression = %s\n",pression);
        write(portLORA,&pression,sizeof(pression));
        sleep(1);
      }

     	  int main(void) {        

	    init_PORT();

	    lecture_LORA();
	    while(1) {
	      //MODE VEILLE
	      while(mode==0){
		printf("Mode veille actif\n");
		write(portLORA,&veille,sizeof(veille));
		lecture_LORA(); //ecoute de la station au sol
		sleep(2);
	      }

	      //MODE ENVOI DE CHAINE
	      while(mode==1){
		//EMISSION
		printf("Mode envoi de chaine\n");
		write(portLORA,&chaine,sizeof(chaine));
	      }

	      //MODE GPS
	      while(2 <= mode && mode <= 6){
		printf("Mode GPS\n");
		lecture_GPS();
		lecture_LORA();
		sleep(10);
	      }

	      //MODE CAPTEUR DE PRESSION
	      while(mode == 7){
		lecture_capteur();
		lecture_LORA();
		sleep(2);
	      }
	    }

	    return 0;
	  }