Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <my_global.h>
- #include <mysql.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdlib.h>
- #include <string.h>
- #include <math.h>
- #define MAX_BUF 50
- unsigned short Adc_read(unsigned int pin)
- {
- int fd,rval;
- char buf[MAX_BUF];
- char val[5]={0};
- sprintf(buf,"/sys/devices/ocp.2/helper.14/AIN%d", pin);
- fd = open(buf, O_RDONLY);
- if (fd < 0)
- {
- printf("unable to open the ADC device ");
- exit(1);
- }
- rval=read(fd, &val, sizeof val);
- if(rval<=0)
- {
- perror("read");
- //close(fd);
- //exit(1);
- }
- close(fd);
- return (unsigned short)atoi(val);
- }
- int main(int argc,char *argv[])
- {
- unsigned short Temp_Val,Humid_Val;
- unsigned int Temp_pin,Humid_pin;
- float temp,Rest,Rhumid;
- char query[100]={0};
- /* read the pin numbers from the Command line arguments */
- if(argc!=3)
- {
- printf("Usage ./readsensor <temppin1> <humidpin2> \n");
- exit(1);
- }
- Temp_pin=atoi(argv[1]);
- Humid_pin=atoi(argv[2]);
- if((Temp_pin < 0 || Temp_pin >6 ) || (Humid_pin < 0 || Humid_pin > 6))
- {
- printf("Enter the ADC pin numbers between 0 and 6 \n ");
- exit(1);
- }
- /* create an MYSQL object */
- MYSQL *con = mysql_init(NULL);
- if (con == NULL)
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- exit(1);
- }
- /* connect to the MYSQL database */
- if(mysql_real_connect(con,"localhost","sanpai","sanpai",0,0,0,0)==NULL)
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- mysql_close(con);
- exit(1);
- }
- /* Select the database */
- if(mysql_select_db(con,"TempDB")!=0)
- {
- fprintf(stderr,"%s \n",mysql_error(con));
- mysql_close(con);
- exit(1);
- }
- while(1)
- {
- // printf("Temppin = %d \n",Temp_pin);
- // printf("Humidpin = %d \n",Humid_pin);
- Temp_Val=Adc_read(Temp_pin);
- Humid_Val=Adc_read(Humid_pin);
- Humid_Val=Humid_Val*3/1000;
- printf("Temp_Val = %hd \n",Temp_Val);
- printf("Humid_Val = %hd \n",Humid_Val);
- if(Humid_Val!=0)
- {
- Rest=((5-Humid_Val)/Humid_Val)*3000;
- Rhumid=-9.371*log(Rest)+157.91;
- }
- else
- {
- }
- temp= ((float)Temp_Val)/10;
- printf("The temperature value is = %f C \n",temp);
- printf("The Humidity is = %f %% \n",Rhumid);
- // printf("Temp val = %hd \n",Temp_Val);
- //printf("Humid val = %hd \n",Humid_Val);
- memset(query,0,sizeof query);
- sprintf(query,"UPDATE Datavalues SET Temperature = %f,Humidity = %f,Date=NOW() WHERE Rownum=0",temp,Rhumid);
- if (mysql_query(con,query))
- {
- fprintf(stderr, "%s\n", mysql_error(con));
- mysql_close(con);
- exit(1);
- }
- usleep(1000000);
- temp=0;
- Rhumid=0;
- }
- mysql_close(con);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement