Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <mysql.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <termios.h>
- typedef int serialPort;
- serialPort SerialLib_open(const char * portPath);
- int i;
- int t=0;
- void finish_with_error(MYSQL *con)
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- mysql_close(con);
- }
- int main(int argc, char **argv)
- {
- char format[128]; // variable stokage date
- MYSQL *con = mysql_init(NULL);
- char *server = "localhost";
- char *user = "root";
- char *password = "password";
- char *database = "stationmeteo_local";
- char ecriture[128]; // char trame acquisition
- t=0;
- if (con == NULL)
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- }
- if (mysql_real_connect(con, server, user, password, database, 0, NULL, 0)== NULL)
- {
- finish_with_error(con);
- }
- while(1)
- {
- serialPort p = SerialLib_open("/dev/ttyAMA0"); // ouverture port AMA0
- char buffer[100];
- while (t==0)
- {
- i=read(p,buffer,100); // lecture port et stokage dans un buffer
- t=1;
- if (i>0)
- {
- if ((i==21) && (buffer[0]='D') && (buffer[19]='F')) // acquisition de 21 données
- {
- buffer[20]='\0'; // fin de chaine
- printf("trame recu: %s, long %d \n",buffer,i);
- char temp[3],humidite[2],dvent[3],vvent[3],p1[3],p2[3],pluie[1]; // séparation de la$
- temp[0]=buffer[1];
- temp[1]=buffer[2];
- temp[2]=buffer[3];
- temp[3]='\0';
- humidite[0]=buffer[4];
- humidite[1]=buffer[5];
- humidite[2]='\0';
- dvent[0]=buffer[6];
- dvent[1]=buffer[7];
- dvent[2]=buffer[8];
- dvent[3]='\0';
- vvent[0]=buffer[9];
- vvent[1]=buffer[10];
- vvent[2]=buffer[11];
- vvent[3]='\0';
- p1[0]=buffer[12];
- p1[1]=buffer[13];
- p1[2]=buffer[14];
- p1[3]='\0';
- p2[0]=buffer[15];
- p2[1]=buffer[16];
- p2[2]=buffer[17];
- p2[3]='\0';
- pluie[0]=buffer[18];
- pluie[1]='\0';
- time_t temps;
- struct tm date;
- time(&temps);
- date=*localtime(&temps);
- strftime(format,128,"'%Y-%m-%d %X'",&date); // création du format datetime
- strcpy(ecriture,"INSERT INTO t_acquisition VALUES('',");
- strcat(ecriture,format);
- strcat(ecriture,",'");
- strcat(ecriture,temp);
- strcat(ecriture,"','");
- strcat(ecriture,humidite);
- strcat(ecriture,"','");
- strcat(ecriture,dvent);
- strcat(ecriture,"','");
- strcat(ecriture,vvent);
- strcat(ecriture,"','");
- strcat(ecriture,p1);
- strcat(ecriture,"','");
- strcat(ecriture,p2);
- strcat(ecriture,"','");
- strcat(ecriture,pluie);
- strcat(ecriture,"')");
- printf("chaine %s\n",ecriture); // affichage requète SQL
- if (mysql_query(con,ecriture)) // envoie sur la base de données
- {
- finish_with_error(con);
- }
- }
- }
- else t=1;
- }
- close(p); // fermeture du port
- t=0;
- }
- mysql_close(con);
- }
- serialPort SerialLib_open(const char * serialPortPath)
- {
- int fd;
- fd = open(serialPortPath, O_RDONLY | O_NOCTTY );
- if (fd == -1)
- {
- printf("Could not open the serial port : %s - ", serialPortPath);
- }
- else
- {
- struct termios options;
- tcgetattr(fd, &options);
- options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; // reglage port 1200 bauds en lecture
- options.c_iflag = 0;
- options.c_oflag = 0;
- options.c_lflag = ICANON;
- tcsetattr(fd, TCSANOW, &options);
- }
- return (serialPort)fd;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement