Advertisement
Guest User

Untitled

a guest
Jan 16th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. //Read temperature and humidity from DHT11 or DHT22 sensor
  2.  
  3. #include <wiringPi.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <mysql/mysql.h>
  8.  
  9. #define MAX_TIMINGS 85
  10. #define DHT_PIN 3 /* GPIO-22 */
  11.  
  12. int data[5] = { 0, 0, 0, 0, 0 };
  13. float temp_f;
  14.  
  15. static char *host = "XXX.XXX.XXX.XXXX";
  16. static char *user = "XXX";
  17. static char *password = "XXXX";
  18. static char *dbname = "XXXX";
  19.  
  20. unsigned int port = 3306;
  21. static char *unix_socket = NULL;
  22. unsigned int flag = 0;
  23.  
  24.  
  25. //Function to connect to MIA Sql DB
  26. void MIA_mysql_connection()
  27. {
  28. // Connecting to the Database
  29. MYSQL *conn;
  30.  
  31. conn = mysql_init(NULL);
  32.  
  33. if (!(mysql_real_connect(conn, host, user, password, dbname, port, unix_socket, flag)))
  34. {
  35. fprintf(stderr, "n Error: %s [%d] n", mysql_error(conn),mysql_errno(conn));
  36. exit (1);
  37. }
  38.  
  39. //Connected
  40. printf ("We Are Connected n");
  41.  
  42. }
  43.  
  44. void MIA_mysql_close(MYSQL *conn)
  45. {
  46.  
  47. conn = mysql_init(NULL);
  48. mysql_close(conn);
  49.  
  50. }
  51.  
  52.  
  53. int MIA_mysql_insert_data(float f, float h)
  54. {
  55. MYSQL *conn;
  56. conn = mysql_init(NULL);
  57. //Inserting into MySQL Table
  58. if(mysql_query(conn, "INSERT INTO `temperature` (`ID`, `Date`,`Time`, `Temperature`, `Humidity`) VALUES (NULL, CURRENT_DATE(), CURRENT_TIME(), 1, 2)") !=0)
  59. {
  60. fprintf(stderr, "%sn", mysql_error(conn));
  61. exit (-1);
  62. } else {
  63. printf("Rows were insert n");
  64. }
  65. return 0;
  66.  
  67. }
  68.  
  69.  
  70.  
  71. // Predefined Code to read the Temp Sensor
  72. void read_dht_data()
  73. {
  74. uint8_t laststate = HIGH;
  75. uint8_t counter = 0;
  76. uint8_t j = 0, i;
  77.  
  78. data[0] = data[1] = data[2] = data[3] = data[4] = 0;
  79.  
  80. /* pull pin down for 18 milliseconds */
  81. pinMode( DHT_PIN, OUTPUT );
  82. digitalWrite( DHT_PIN, LOW );
  83. delay( 18 );
  84.  
  85. /* prepare to read the pin */
  86. pinMode( DHT_PIN, INPUT );
  87.  
  88. /* detect change and read data */
  89. for ( i = 0; i < MAX_TIMINGS; i++ )
  90. {
  91. counter = 0;
  92. while ( digitalRead( DHT_PIN ) == laststate )
  93. {
  94. counter++;
  95. delayMicroseconds( 1 );
  96. if ( counter == 255 )
  97. {
  98. break;
  99. }
  100. }
  101. laststate = digitalRead( DHT_PIN );
  102.  
  103. if ( counter == 255 )
  104. break;
  105.  
  106. /* ignore first 3 transitions */
  107. if ( (i >= 4) && (i % 2 == 0) )
  108. {
  109. /* Insert each bit into the storage bytes */
  110. data[j / 8] <<= 1;
  111. if ( counter > 16 )
  112. data[j / 8] |= 1;
  113. j++;
  114. }
  115. }
  116.  
  117. /*
  118. * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
  119. * print it out if data is good
  120. */
  121. if ( (j >= 40) &&
  122. (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
  123. {
  124. float h = (float)((data[0] << 8) + data[1]) / 10;
  125. if ( h > 100 )
  126. {
  127. h = data[0]; // for DHT11
  128. }
  129. float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
  130. if ( c > 125 )
  131. {
  132. c = data[2]; // for DHT11
  133. }
  134. if ( data[2] & 0x80 )
  135. {
  136. c = -c;
  137. }
  138. float f = c * 1.8f + 32;
  139.  
  140. //printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)n", h, c, f );
  141. printf ("Temp of the room is : %.1f n",f);
  142.  
  143. //Insert Data into MIA Table
  144. MIA_mysql_insert_data(f,h);
  145.  
  146. }else {
  147. printf( "Data not good, skipn" );
  148. }
  149. }
  150.  
  151.  
  152. //MAIN FUNCTION
  153. int main(void)
  154. {
  155. printf( "Raspberry Pi DHT11/DHT22 temperature/humidity testn" );
  156.  
  157. if ( wiringPiSetup() == -1 )
  158. exit( 1 );
  159.  
  160.  
  161.  
  162. while ( 1 )
  163. {
  164. MIA_mysql_connection();
  165. read_dht_data();
  166. MIA_mysql_close(NULL);
  167. delay(10000 ); //delay
  168.  
  169. }
  170.  
  171. return(0);
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement