树莓派加温湿度传感器,并上传至mysql

温湿度传感器采集温湿度信息并通过树莓派传输到MySQL数据库上

目录

硬件部分

编程部分

1、下载安装Adafruit提供的python驱动

3、把以上代码保存为dht22.py,然后执行

4、安装pymysql

4.1输入python3或者pyhton确认版本

4.2输入pip,检查pip是否安装

4.3安装PyMySQL

4.4测试连接

5、添加开机启动脚本

6、整合代码,实现间隔[m]分钟自动采集传感器数据,并传给mysql


硬件部分

使用的传感器是DHT22,有3个引脚,分别是电源正、数据、和电源负。正负不可接反,否则烧传感器

接线方式,借用一下https://www.cnblogs.com/junjun001/p/9335246.html的图

编程部分

1、下载安装Adafruit提供的python驱动


 
 
  1. #sudo apt-get update
  2. #sudo apt-get install build-essential python-dev
  3. #git clone https://github.com/adafruit/Adafruit_Python_DHT.git
  4. #cd Adafruit_Python_DHT
  5. #sudo python setup.py install

如果不出错的话就能安装好了。

2、dht22的代码如下,(如果是dht11,把sensor = Adafruit_DHT.DHT22改为sensor = Adafruit_DHT.DHT11)

在Adafruit_Python_DHT目录外新建一个.py文件


 
 
  1. #!/usr/bin/python
  2. import Adafruit_DHT
  3. sensor = Adafruit_DHT.DHT22
  4. pin = 4 #GPIO4
  5. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  6. if humidity is not None and temperature is not None:
  7. print( 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
  8. else:
  9. print( 'Failed to get reading. Try again!')

3、把以上代码保存为dht22.py,然后执行

#sudo python dht22.py
 
 

4、安装pymysql

4.1输入python3或者pyhton确认版本


 
 
  1. #python3
  2. Python 3.5 .3 ( default, Sep 27 2018, 17: 25: 39)
  3. [ GCC 6.3.0 20170516] on linux
  4. Type "help", "copyright", "credits" or "license" for more information.

4.2输入pip,检查pip是否安装


 
 
  1. #pip
  2. Usage:
  3. pip < command> [options]
  4. Commands:
  5. install Install packages.

4.3安装PyMySQL


 
 
  1. pi@raspberrypi :~/ $ sudo pip install pymysql
  2. Collecting pymysql
  3. Downloading https:/ /files.pythonhosted.org/packages /ed/ 39/ 15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL- 0. 9.3-py2.py3-none-any.whl ( 47kB)
  4. 100% |████████████████████████████████| 51kB 330kB/s
  5. Installing collected packages: pymysql
  6. Successfully installed pymysql- 0. 9.3

4.4测试连接


 
 
  1. import pymysql
  2. con = pymysql.connect(
  3. host = '192.168.0.2',
  4. port = 3306,
  5. user = 'root',
  6. password = 'yourpassword',
  7. db = 'yourdatabase',
  8. charset = 'utf8'
  9. )
  10. cur = con.cursor()
  11. cur.execute( "show tables")
  12. data=con.fetchall
  13. print( data)

5、添加开机启动脚本

编辑/etc/rc.local 在exit 0 前添加对应的脚本 


 
 
  1. pi@raspberrypi:~ $ cat /etc/rc.local
  2. #!/bin/sh -e
  3. #
  4. # rc.local
  5. #
  6. # This script is executed at the end of each multiuser runlevel.
  7. # Make sure that the script will "exit 0" on success or any other
  8. # value on error.
  9. #
  10. # In order to enable or disable this script just change the execution
  11. # bits.
  12. #
  13. # By default this script does nothing.
  14. # Print the IP address
  15. _IP=$(hostname -I) || true
  16. if [ "$_IP" ]; then
  17. printf "My IP address is %s\n" "$_IP"
  18. fi
  19. python /home/pi/ht.py
  20. exit 0

6、整合代码,实现间隔[m]分钟自动采集传感器数据,并传给mysql

mysql中添加数据库

create database yourdb COLLATE utf8_general_ci;
 
 

添加表格 


 
 
  1. create table ht(
  2. ID int primary key not null auto_increment,
  3. Time datetime,
  4. Position varchar( 20),
  5. Temperature float( 3, 1),
  6. Humidity float( 3, 1)
  7. );

整合后的ht.py 


 
 
  1. #!/usr/bin/python
  2. import Adafruit_DHT
  3. import pymysql
  4. import time
  5. import datetime
  6. pos = "weizhi1" #传感器安装位置
  7. def caiji(pos):
  8. sensor = Adafruit_DHT.DHT22
  9. pin = 4 #GPIO4
  10. humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
  11. #mysql
  12. con = pymysql.connect(
  13. host = '192.168.0.2',
  14. port = 3306,
  15. user = 'root',
  16. password = 'youerpassword',
  17. db = 'yourdb',
  18. charset = 'utf8'
  19. )
  20. cur = con.cursor()
  21. if humidity is not None and temperature is not None:
  22. print( 'Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))
  23. cur.execute( "insert into ht(Time,Position,Temperature,Humidity) value(now(),'%s','%s','%s')" %(pos,temperature,humidity))
  24. con.commit()
  25. else:
  26. print( 'Failed to get reading. Try again!')
  27. cur.close()
  28. con.close()
  29. def main(m=0):
  30. print( "Start collection data,interval [%s] minutes" %(m));
  31. while True:
  32. while True:
  33. now=datetime.datetime.now()
  34. if now.minute % m == 0:
  35. break
  36. #过20秒再检测一次
  37. time.sleep( 20)
  38. print(datetime.datetime.now())
  39. caiji(pos)
  40. #采集一次后等待一段时间再判断
  41. if m> 1:
  42. time.sleep((m -1)* 60)
  43. else:
  44. time.sleep( 60)
  45. main( 10) #10分钟采集一次
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值