Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Read temperature and humidity from DHT11 or DHT22 sensor
- #include <wiringPi.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <mysql/mysql.h>
- #define MAX_TIMINGS 85
- #define DHT_PIN 3 /* GPIO-22 */
- int data[5] = { 0, 0, 0, 0, 0 };
- float temp_f;
- static char *host = "XXX.XXX.XXX.XXXX";
- static char *user = "XXX";
- static char *password = "XXXX";
- static char *dbname = "XXXX";
- unsigned int port = 3306;
- static char *unix_socket = NULL;
- unsigned int flag = 0;
- //Function to connect to MIA Sql DB
- void MIA_mysql_connection()
- {
- // Connecting to the Database
- MYSQL *conn;
- conn = mysql_init(NULL);
- if (!(mysql_real_connect(conn, host, user, password, dbname, port, unix_socket, flag)))
- {
- fprintf(stderr, "n Error: %s [%d] n", mysql_error(conn),mysql_errno(conn));
- exit (1);
- }
- //Connected
- printf ("We Are Connected n");
- }
- void MIA_mysql_close(MYSQL *conn)
- {
- conn = mysql_init(NULL);
- mysql_close(conn);
- }
- int MIA_mysql_insert_data(float f, float h)
- {
- MYSQL *conn;
- conn = mysql_init(NULL);
- //Inserting into MySQL Table
- if(mysql_query(conn, "INSERT INTO `temperature` (`ID`, `Date`,`Time`, `Temperature`, `Humidity`) VALUES (NULL, CURRENT_DATE(), CURRENT_TIME(), 1, 2)") !=0)
- {
- fprintf(stderr, "%sn", mysql_error(conn));
- exit (-1);
- } else {
- printf("Rows were insert n");
- }
- return 0;
- }
- // Predefined Code to read the Temp Sensor
- void read_dht_data()
- {
- uint8_t laststate = HIGH;
- uint8_t counter = 0;
- uint8_t j = 0, i;
- data[0] = data[1] = data[2] = data[3] = data[4] = 0;
- /* pull pin down for 18 milliseconds */
- pinMode( DHT_PIN, OUTPUT );
- digitalWrite( DHT_PIN, LOW );
- delay( 18 );
- /* prepare to read the pin */
- pinMode( DHT_PIN, INPUT );
- /* detect change and read data */
- for ( i = 0; i < MAX_TIMINGS; i++ )
- {
- counter = 0;
- while ( digitalRead( DHT_PIN ) == laststate )
- {
- counter++;
- delayMicroseconds( 1 );
- if ( counter == 255 )
- {
- break;
- }
- }
- laststate = digitalRead( DHT_PIN );
- if ( counter == 255 )
- break;
- /* ignore first 3 transitions */
- if ( (i >= 4) && (i % 2 == 0) )
- {
- /* Insert each bit into the storage bytes */
- data[j / 8] <<= 1;
- if ( counter > 16 )
- data[j / 8] |= 1;
- j++;
- }
- }
- /*
- * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
- * print it out if data is good
- */
- if ( (j >= 40) &&
- (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
- {
- float h = (float)((data[0] << 8) + data[1]) / 10;
- if ( h > 100 )
- {
- h = data[0]; // for DHT11
- }
- float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
- if ( c > 125 )
- {
- c = data[2]; // for DHT11
- }
- if ( data[2] & 0x80 )
- {
- c = -c;
- }
- float f = c * 1.8f + 32;
- //printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)n", h, c, f );
- printf ("Temp of the room is : %.1f n",f);
- //Insert Data into MIA Table
- MIA_mysql_insert_data(f,h);
- }else {
- printf( "Data not good, skipn" );
- }
- }
- //MAIN FUNCTION
- int main(void)
- {
- printf( "Raspberry Pi DHT11/DHT22 temperature/humidity testn" );
- if ( wiringPiSetup() == -1 )
- exit( 1 );
- while ( 1 )
- {
- MIA_mysql_connection();
- read_dht_data();
- MIA_mysql_close(NULL);
- delay(10000 ); //delay
- }
- return(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement