zoukankan      html  css  js  c++  java
  • 【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()


    # -*- 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)
        
  • 相关阅读:
    关于react-native遇到Can't find variable: TouchableHighlight
    安卓---app自动更新
    安卓---android:versionCode和android:versionName 用途
    安卓---读取照片---拍照
    运营商如何识别电信诈骗用户
    再要你命3K的任务总结
    连接kettle(6.1)与vm上的apache hadoop(2.6.1)
    如何从数据上知道某个用户即将去香港
    什么是撼动社会的产品?
    与Y哥的谈话。
  • 原文地址:https://www.cnblogs.com/colipso/p/3537861.html
Copyright © 2011-2022 走看看