Advertisement
sanpai

Reading Sensor in c

Apr 10th, 2014
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. #include <my_global.h>
  2. #include <mysql.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <math.h>
  9.  
  10. #define MAX_BUF 50
  11.  
  12. unsigned short Adc_read(unsigned int pin)
  13. {
  14. int fd,rval;
  15. char buf[MAX_BUF];
  16. char val[5]={0};
  17.  
  18. sprintf(buf,"/sys/devices/ocp.2/helper.14/AIN%d", pin);
  19.  
  20. fd = open(buf, O_RDONLY);
  21. if (fd < 0)
  22. {
  23. printf("unable to open the ADC device ");
  24. exit(1);
  25. }
  26.  
  27. rval=read(fd, &val, sizeof val);
  28.  
  29. if(rval<=0)
  30. {
  31. perror("read");
  32. //close(fd);
  33. //exit(1);
  34.  
  35. }
  36.  
  37. close(fd);
  38.  
  39. return (unsigned short)atoi(val);
  40.  
  41.  
  42.  
  43. }
  44.  
  45. int main(int argc,char *argv[])
  46. {
  47. unsigned short Temp_Val,Humid_Val;
  48. unsigned int Temp_pin,Humid_pin;
  49. float temp,Rest,Rhumid;
  50. char query[100]={0};
  51.  
  52. /* read the pin numbers from the Command line arguments */
  53.  
  54. if(argc!=3)
  55. {
  56. printf("Usage ./readsensor <temppin1> <humidpin2> \n");
  57. exit(1);
  58. }
  59.  
  60. Temp_pin=atoi(argv[1]);
  61. Humid_pin=atoi(argv[2]);
  62.  
  63. if((Temp_pin < 0 || Temp_pin >6 ) || (Humid_pin < 0 || Humid_pin > 6))
  64. {
  65. printf("Enter the ADC pin numbers between 0 and 6 \n ");
  66. exit(1);
  67. }
  68.  
  69. /* create an MYSQL object */
  70.  
  71. MYSQL *con = mysql_init(NULL);
  72.  
  73. if (con == NULL)
  74. {
  75. fprintf(stderr, "%s\n", mysql_error(con));
  76. exit(1);
  77. }
  78.  
  79. /* connect to the MYSQL database */
  80.  
  81. if(mysql_real_connect(con,"localhost","sanpai","sanpai",0,0,0,0)==NULL)
  82. {
  83. fprintf(stderr, "%s\n", mysql_error(con));
  84. mysql_close(con);
  85. exit(1);
  86. }
  87.  
  88. /* Select the database */
  89.  
  90. if(mysql_select_db(con,"TempDB")!=0)
  91. {
  92. fprintf(stderr,"%s \n",mysql_error(con));
  93. mysql_close(con);
  94. exit(1);
  95. }
  96.  
  97. while(1)
  98. {
  99. // printf("Temppin = %d \n",Temp_pin);
  100. // printf("Humidpin = %d \n",Humid_pin);
  101. Temp_Val=Adc_read(Temp_pin);
  102. Humid_Val=Adc_read(Humid_pin);
  103. Humid_Val=Humid_Val*3/1000;
  104.  
  105. printf("Temp_Val = %hd \n",Temp_Val);
  106. printf("Humid_Val = %hd \n",Humid_Val);
  107.  
  108. if(Humid_Val!=0)
  109. {
  110. Rest=((5-Humid_Val)/Humid_Val)*3000;
  111. Rhumid=-9.371*log(Rest)+157.91;
  112. }
  113. else
  114. {
  115.  
  116.  
  117. }
  118.  
  119. temp= ((float)Temp_Val)/10;
  120.  
  121.  
  122. printf("The temperature value is = %f C \n",temp);
  123. printf("The Humidity is = %f %% \n",Rhumid);
  124. // printf("Temp val = %hd \n",Temp_Val);
  125. //printf("Humid val = %hd \n",Humid_Val);
  126.  
  127. memset(query,0,sizeof query);
  128.  
  129. sprintf(query,"UPDATE Datavalues SET Temperature = %f,Humidity = %f,Date=NOW() WHERE Rownum=0",temp,Rhumid);
  130.  
  131. if (mysql_query(con,query))
  132. {
  133. fprintf(stderr, "%s\n", mysql_error(con));
  134. mysql_close(con);
  135. exit(1);
  136. }
  137.  
  138.  
  139. usleep(1000000);
  140. temp=0;
  141. Rhumid=0;
  142. }
  143.  
  144. mysql_close(con);
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement