打印

基于GD32F303和Python温湿度数据保存到数据库

[复制链接]
953|36
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
#申请原创#一、硬件开发平台硬件开发平台采用兆易创新的gd32f303vc-EVAL开发板,传感器采用DHT11,单片机连接关系为PB11--------》DATA

使用特权

评论回复
沙发
zeshoufx|  楼主 | 2021-2-25 17:17 | 只看该作者
二、传感器参数及程序
void DHT11_io_set(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
   
    gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_MAX,GPIO_PIN_11);
}
static void DHT11_out_set(void)
{
        gpio_init(GPIOB,GPIO_MODE_OUT_PP,GPIO_OSPEED_MAX,GPIO_PIN_11);
}
static void DHT11_in_set(void)
{
        gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_MAX,GPIO_PIN_11);
}

static void DHT11_rst(void)
{
    DHT11_out_set();
    DHT11_out=0;
    delay_ms(20);
    DHT11_out=1;
    delay_us(30);
}
u8 DHT11_check(void)
{
    u8 retry=0;
    DHT11_in_set();
    while((DHT11_in==SET)&&retry<100)
    {
        retry++;
        delay_us(1);
    }
    if(retry>100)
    {
        return ERROR;
    }
    else
    {
        retry=0;
    }
   
    while((DHT11_in!=SET)&&retry<100)
    {
        retry++;
        delay_us(1);
    }
    if(retry>100)
    {
        return ERROR;
    }
    else
    {
        return SUCCESS;
    }
}


static u8 DHT11_read_bit(void)
{
    u8 retry=0;
   
    while((DHT11_in==SET)&&retry<100)
    {
        retry++;
        delay_us(1);
    }
   
    retry=0;
   
    while((DHT11_in!=SET)&&retry<100)
    {
        retry++;
        delay_us(1);
    }
    delay_us(40);
   
    if(DHT11_in==SET)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
u8 DHT11_get_zvalue(u8* humi,u8* temp)
{
    u8 buf[5]={0};
    u8 i=0;
    DHT11_rst();
    if(DHT11_check()==SUCCESS)
    {
        for(i=0;i<5;i++)
        {
            buf[i]=DHT11_read_byte();
        }
        if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
        {
            *humi=buf[0];
            *temp=buf[2];
        }
    }
    else
    {
        return ERROR;
    }
   
    return SUCCESS;
}


u8 DHT11_get_fvalue(float* humi,float* temp)
{
    u8 buf[5]={0};
    u8 i=0;
    DHT11_rst();
    if(DHT11_check()==SUCCESS)
    {
        for(i=0;i<5;i++)
        {
            buf[i]=DHT11_read_byte();
        }
        if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
        {
            *humi=((buf[0]*10.0)+buf[1])/10.0;
            *temp=((buf[2]*10.0)+buf[3])/10.0;
        }
    }
    else
    {
        return ERROR;
    }
   
    return SUCCESS;
}


使用特权

评论回复
板凳
zeshoufx|  楼主 | 2021-2-25 17:21 | 只看该作者
zeshoufx 发表于 2021-2-25 17:17
二、传感器参数及程序

三、Python脚本平台
GD32F303将温湿度数据通过串口发送出去,Python实现将温湿度接收并保存到sqlite数据库
import sqlite3
import serial

ser=serial.Serial("COM4",9600,timeout=6)
print("串口参数:",ser)
   
conn=sqlite3.connect('lx.db')

cursor=conn.cursor()

for i in range(200):
    r1=ser.read()
    r1_list=int.from_bytes(r1,byteorder='big', signed=False)
    r2=ser.read()
    r2_list=int.from_bytes(r2,byteorder='big', signed=False)
   
    data=[r1_list,r2_list]
    print(data)
   
    sql_humi='insert into temp_data(湿度,温度) values(?,?)'
    cursor.execute(sql_humi,data)
   

cursor.close()

conn.commit()

conn.close()

ser.close()
在Python 的IDLE里可以看到数据结果如下:

使用特权

评论回复
地板
zeshoufx|  楼主 | 2021-2-25 17:24 | 只看该作者
zeshoufx 发表于 2021-2-25 17:21
三、Python脚本平台
GD32F303将温湿度数据通过串口发送出去,Python实现将温湿度接收并保存到sqlite数据 ...

四、数据库平台数据库平台采用sqlite3,sqlite的占位符为“?”,通过利用Navicat软件查看数据如图:


使用特权

评论回复
5
zeshoufx|  楼主 | 2021-2-25 17:27 | 只看该作者
zeshoufx 发表于 2021-2-25 17:24
四、数据库平台数据库平台采用sqlite3,sqlite的占位符为“?”,通过利用Navicat软件查看数据如图:

五、注意的地方:
1、ser=serial.Serial("COM4",9600,timeout=6)超时参数一定要大于单片机串口发送的数据的延时,否则会在数据库接收到一些乱码
2、Python接收到的数据为字符串,需要通过下面语句进行转换
    r1=ser.read()
    r1_list=int.from_bytes(r1,byteorder='big', signed=False)

3、sqlite的占位符为“?”,MySQL的占位符为“%s”

使用特权

评论回复
6
zeshoufx|  楼主 | 2021-2-25 17:33 | 只看该作者
zeshoufx 发表于 2021-2-25 17:27
五、注意的地方:
1、ser=serial.Serial("COM4",9600,timeout=6)超时参数一定要大于单片机串口发送的数据 ...

GD32f303工程和Python脚本

demo.zip

7.52 MB

工程文件

lianxi2.zip

482 Bytes

Python脚本

使用特权

评论回复
7
zeshoufx|  楼主 | 2021-2-26 11:21 | 只看该作者
zeshoufx 发表于 2021-2-25 17:33
GD32f303工程和Python脚本

在MySQL平台下实现,在原来基础上,增加了ID序号,作为主键,增加采集的时间
import pymysql
import serial
'''from datetime'''
import datetime

ser=serial.Serial("COM4",9600,timeout=6)
print("串口参数:",ser)
   
conn=pymysql.connect(host="localhost",user="root",password="123456",db="pytest",port=3306)

cursor=conn.cursor()

cursor.execute('create table temp_data1(id int(4) not null auto_increment,湿度 int(2),温度 int(2),采集时间 datetime,primary key(id))')

for i in range(8):
    r1=ser.read()
    r1_list=int.from_bytes(r1,byteorder='big', signed=False)
    r2=ser.read()
    r2_list=int.from_bytes(r2,byteorder='big', signed=False)

    time_now=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    data=[r1_list,r2_list,time_now]
    print(data)
   
    sql_humi='insert into temp_data1(湿度,温度,采集时间) values(%s,%s,%s)'
    cursor.execute(sql_humi,data)
   

cursor.close()

conn.commit()

conn.close()

ser.close()


使用特权

评论回复
8
zeshoufx|  楼主 | 2021-2-26 11:23 | 只看该作者
zeshoufx 发表于 2021-2-26 11:21
在MySQL平台下实现,在原来基础上,增加了ID序号,作为主键,增加采集的时间

在Python IDLE和MySQL运行的结果如图:




使用特权

评论回复
9
zeshoufx|  楼主 | 2021-2-26 11:25 | 只看该作者
zeshoufx 发表于 2021-2-26 11:23
在Python IDLE和MySQL运行的结果如图:

基于MySQL的Python脚本

lianxi3.zip

663 Bytes

Python的MySQL脚本

使用特权

评论回复
10
zeshoufx|  楼主 | 2021-2-26 16:54 | 只看该作者
zeshoufx 发表于 2021-2-26 11:25
基于MySQL的Python脚本

为了直观将温湿度的变化绘出来,对Python脚本进行了修改
import matplotlib.pyplot as plt
import pymysql
import serial
'''from datetime'''
import datetime

sd=[]
wd=[]

ser=serial.Serial("COM4",9600,timeout=6)
print("串口参数:",ser)
   
conn=pymysql.connect(host="localhost",user="root",password="123456",db="pytest",port=3306)

cursor=conn.cursor()

#cursor.execute('create table temp_data1(id int(4) not null auto_increment,湿度 int(2),温度 int(2),采集时间 datetime,primary key(id))')

for i in range(50):
    r1=ser.read()
    r1_list=int.from_bytes(r1,byteorder='big', signed=False)
    r2=ser.read()
    r2_list=int.from_bytes(r2,byteorder='big', signed=False)

    time_now=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    data=[r1_list,r2_list,time_now]
    print(data)
   
    sql_humi='insert into temp_data1(湿度,温度,采集时间) values(%s,%s,%s)'
    cursor.execute(sql_humi,data)

    sd.append(r1_list)
    wd.append(r2_list)

cursor.close()

conn.commit()

conn.close()

ser.close()

plt.plot(list(range(50)),sd)
plt.plot(list(range(50)),wd)
plt.show()


使用特权

评论回复
11
zeshoufx|  楼主 | 2021-2-26 16:55 | 只看该作者
zeshoufx 发表于 2021-2-26 16:54
为了直观将温湿度的变化绘出来,对Python脚本进行了修改

修改后的运行结果为:蓝色为湿度,橙色为温度,由于数据量小,很简单

使用特权

评论回复
12
kkzz| | 2021-3-3 22:41 | 只看该作者
ESP8266+AIR202/302终端管理  

使用特权

评论回复
13
hudi008| | 2021-3-3 22:41 | 只看该作者
GD32相关软件和工具  

使用特权

评论回复
14
lzmm| | 2021-3-3 22:41 | 只看该作者
分布式温湿度采集系统  

使用特权

评论回复
15
minzisc| | 2021-3-3 22:42 | 只看该作者
采用数字式温湿度传感器SHT11采集环境的湿度值  

使用特权

评论回复
16
selongli| | 2021-3-3 22:42 | 只看该作者
温度湿度检测功能怎么实现  

使用特权

评论回复
17
fentianyou| | 2021-3-3 22:42 | 只看该作者
谢谢楼主分享 的     

使用特权

评论回复
18
xiaoyaodz| | 2021-3-3 22:42 | 只看该作者
温湿度监测系统数据库怎么修改和删除

使用特权

评论回复
19
febgxu| | 2021-3-3 22:43 | 只看该作者
怎么与DHT11温湿度传感器连接  

使用特权

评论回复
20
sdlls| | 2021-3-3 22:43 | 只看该作者
-温湿度传感器模块(DHT11)实验

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

66

主题

1947

帖子

14

粉丝