【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库

1、mysql

http://dev.mysql.com/doc/refman/5.5/en/tutorial.html

mysql+python

http://dev.mysql.com/doc/connector-python/en/connector-python-examples.html

先打开数据库连接:

conn=mdb.connect(host="localhost",user=‘root‘,passwd=‘******‘,db=‘sensor‘,charset=‘utf8‘)

再取得指针

cur=conn.cursor()

执行sql语句字符串

sql="insert into infrared_sensor(record_time,in_range) values(now(),‘%s‘)"%param
cur.execute(sql)

提交事务,关闭指针,关闭连接

conn.commit()
cur.close()
conn.close()


【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库
# -*- coding: utf-8 -*-
"""
Created on Mon Feb  3 21:23:08 2014

@author: pi
"""

import smtplib
from email.mime.text import MIMEText
import RPi.GPIO as gpio
import MySQLdb as mdb
import time

mail_to="*******@qq.com"

def store_in_database(param):
    conn=mdb.connect(host="localhost",user=root,passwd=******,db=sensor,charset=utf8)
    cur=conn.cursor()
    sql="insert into infrared_sensor(record_time,in_range) values(now(),‘%s‘)"%param
    cur.execute(sql)
    conn.commit()
    cur.close()
    conn.close()


def send_mail(to_list,title,content):
    mail_host="smtp.126.com"
    mail_user="*****"
    mail_pass="******"
    mail_postfix="126.com"
    me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
    msg=MIMEText(content)
    msg[Subject]=title
    msg[From]=mail_user
    msg[To]=to_list
    
    mail=smtplib.SMTP()
    mail.connect(mail_host)
    mail.login(mail_user,mail_pass)
    mail.sendmail(me,to_list,msg.as_string())
    mail.close()


def is_anybody_home():
    gpio.setwarnings(False)
    gpio.setmode(gpio.BOARD)
    gpio.setup(22,gpio.IN)
    if gpio.input(22)==1:
        return 1
    else:
        return 0
        
current_time="%d" % time.localtime().tm_hour+":"+ "%d" % time.localtime().tm_min  
        
        
if is_anybody_home()==1:
    send_mail(mail_to,"Is someone in sensor range?--"+current_time,"Yes--"+current_time)
    param="Yes"
    store_in_database(param)
else:
    send_mail(mail_to,"Is someone in sensor range?--"+current_time,"No--"+current_time)
    param="No"
    store_in_database(param)
    
【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库

【Raspberry pi+python+mysql】红外传感器-发邮件-存数据库

上一篇:[转载]MongoDB开发学习(2)索引的基本操作


下一篇:查询表的操作记录的sql