dht11的树莓派的驱动:
https://blog.csdn.net/u010900754/article/details/53078615?locationNum=13&fps=1
注意要把counter > 16改为counter>32
树莓派连接mysql,并插入数据
https://blog.csdn.net/qq_28877125/article/details/63796191
并且搭建环境:
https://blog.csdn.net/tennysonsky/article/details/53984818
最终代码:
nclude <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include<stdio.h>
#include<time.h>
#define MAXTIMINGS 85
#define DHTPIN 7
int dht11_dat[5] = { 0, 0, 0, 0, 0 };
void read_dht11_dat()
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
float f; /* fahrenheit */
dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;
/* pull pin down for 18 milliseconds */
pinMode( DHTPIN, OUTPUT );
digitalWrite( DHTPIN, LOW );
delay( 18 );
/* then pull it up for 40 microseconds */
digitalWrite( DHTPIN, HIGH );
delayMicroseconds( 40 );
/* prepare to read the pin */
pinMode( DHTPIN, INPUT );
/* detect change and read data */
for ( i = 0; i < MAXTIMINGS; i++ )
{
counter = 0;
while ( digitalRead( DHTPIN ) == laststate )
{
counter++;
delayMicroseconds( 1 );
if ( counter == 255 )
{
break;
}
}
laststate = digitalRead( DHTPIN );
if ( counter == 255 )
break;
/* ignore first 3 transitions */
if ( (i >= 4) && (i % 2 == 0) )
{
/* shove each bit into the storage bytes */
dht11_dat[j / 8] <<= 1;
if ( counter > 32 )
dht11_dat[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) &&
(dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )
{
f = dht11_dat[2] * 9. / 5. + 32;
printf( "Humidity = %d.%d %% Temperature = %d.%d *C (%.1f *F)\n",
dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );
}else {
printf( "Data not good, skip\n" );
}
}
int main(int argc, char *argv[])
{
int k=0;
time_t t;
struct tm *lt;
char str[60];
char str1[40]={"insert into hum values("};
if ( wiringPiSetup() == -1 )
exit( 1 );
MYSQL my_connection;
int res;
mysql_init(&my_connection);
/*mysql_real_connect(&mysql,主机名,用户名,密码,数据库名,0,NULL,0) == NULL)*/
/*mysql_real_connect(&mysql,host,user,passwd,dbname,0,NULL,0) == NULL)*/
if (mysql_real_connect(&my_connection, "ip地址或者本地数据库", "qpc", "qpc.123","qpc",0,NULL,CLIENT_FOUND_ROWS))
{
printf("Connection success\n");
while(1)
{
read_dht11_dat();
delay( 3000 ); /* wait 1sec to refresh */
time(&t);
lt=localtime(&t);
int k1=dht11_dat[0],k2=dht11_dat[2];
if(k2>40||k2<10||k1>95||k1<10)
continue;
sprintf(str,"%s%d,%d,%d,'%d-%d-%d %d:%d:%d')",str1,k,k1,k2,lt->tm_year+1900,lt->tm_mon+1,lt->tm_mday,lt->tm_hour,lt->tm_min,lt->tm_sec);
delay( 1000 );
printf("%s\n",str);
delay(1000);
res = mysql_query(&my_connection, str);//insert into hum values(1,1, 2,'2018-12-9')
if (!res)
{
printf("Inserted %lu rows\n",(unsigned long)mysql_affected_rows(&my_connection));
/*里头的函数返回受表中影响的行数*/
}
else
{
//分别打印出错误代码及详细信息
fprintf(stderr, "Insert error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
k++;
}
mysql_close(&my_connection);
}
else
{
fprintf(stderr, "Connection failed\n");
if (mysql_errno(&my_connection))
{
fprintf(stderr, "Connection error %d: %s\n",mysql_errno(&my_connection),mysql_error(&my_connection));
}
}
return 1;// "EXIT_SUCCES"; kai
}
我之后闲的没事调调树莓派的串口:
我用的树莓派2,可以直接弄串口,树莓派3不能直接,下面是区别。
https://blog.csdn.net/qq_31396093/article/details/58640995
串口函数:
https://blog.csdn.net/nicekwell/article/details/78618678
我的串口代码:
#include <wiringSerial.h>
int main(void)
{
int fd;
if((fd = serialOpen ("/dev/ttyAMA0",9600))<0)
{
printf("serial err\n");
}
while(1)
{
UartBuff[0]=serialGetchar(fd);
if(UartBuff[0]=='a')
{
serialPutchar(fd,UartBuff[0]);
}
sleep(0.5);
}
return EXIT_SUCCESS;
}
树莓派3还可以串口转TTL,/dev/ttyAMA0,把挂在的外设口变成usb(/dev/usb).