1、定时执行脚本

http://tech.it168.com/a2011/0707/1214/000001214830_all.shtml

/sbin/service crond start //启动服务
  /sbin/service crond stop //关闭服务
  /sbin/service crond restart //重启服务
  /sbin/service crond reload //重新载入配置
  可以将这个服务在系统启动的时候也自动启动:
  在/etc/rc.d/rc.local这个脚本的末尾加上:
  /sbin/service crond start
  1.直接用crontab命令编辑
  cron服务提供crontab命令来设定cron服务的,以下是这个命令的一些参数与说明:
  crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
  crontab -l //列出某个用户cron服务的详细内容
  crontab -r //删除没个用户的cron服务
  crontab -e //编辑某个用户的cron服务
  比如说root查看自己的cron设置:
  crontab -u root -l
  再例如,root想删除fred的cron设置:
  crontab -u fred -r
  在编辑cron服务时,编辑的内容有一些格式和约定,输入:
  引用:
  crontab -u root -e
  进入vi编辑模式,编辑的内容一定要符合下面的格式:
  */1 * * * * ls >> /tmp/ls.txt
  这个格式的前一部分是对时间的设定,后面一部分是要执行的命令,如果要执行的命令太多,可以把这些命令写到一个脚本里面,然后在这里直接调用这个脚本就可以了,调用的时候记得写出命令的完整路径。时间的设定我们有一定的约定,前面五个*号代表五个数字,数字的取值范围和含义如下:
  引用:
  分钟 (0-59)
  小時 (0-23)
  日期 (1-31)
  月份 (1-12)
  星期 (0-6)//0代表星期天

  除了数字还有几个个特殊的符号就是"*"、"/"和"-"、",",*代表所有的取值范围内的数字,"/"代表每的意思,"*/5"表示每5个单位,"-"代表从某个数字到某个数字,","分开几个离散的数字。以下举几个例子说明问题:
  引用:
  每天早上6点
  0 6 * * * echo "Good morning." >> /tmp/test.txt //注意单纯echo,从屏幕上看不到任何输出,因为cron把任何输出都email到root的信箱了。
  每两个小时
  0 */2 * * * echo "Have a break now." >> /tmp/test.txt
  晚上11点到早上8点之间每两个小时,早上八点
  0 23-7/2,8 * * * echo "Have a good dream:)" >> /tmp/test.txt
  每个月的4号和每个礼拜的礼拜一到礼拜三的早上11点
  0 11 4 * 1-3 command line
  1月1日早上4点
  0 4 1 1 * command line
  2.编辑/etc/crontab 文件配置cron
  cron服务每分钟不仅要读一次/var/spool/cron内的所有文件,还需要读一次/etc/crontab,因此我们配置这个文件也能运用cron服务做一些事情。用crontab配置是针对某个用户的,而编辑/etc/crontab是针对系统的任务。此文件的文件格式是:
  SHELL=/bin/bash
  PATH=/sbin:/bin:/usr/sbin:/usr/bin
  MAILTO=root //如果出现错误,或者有数据输出,数据作为邮件发给这个帐号
  HOME=/
  # run-parts
  01 * * * * root run-parts /etc/cron.hourly //每个小时去执行一遍/etc/cron.hourly内的脚本
  02 4 * * * root run-parts /etc/cron.daily //每天去执行一遍/etc/cron.daily内的脚本
  22 4 * * 0 root run-parts /etc/cron.weekly //每星期去执行一遍/etc/cron.weekly内的脚本
  42 4 1 * * root run-parts /etc/cron.monthly //每个月去执行一遍/etc/cron.monthly内的脚本
  使用者 运行的路径
  注意"run-parts"这个参数了,如果去掉这个参数的话,后面就可以写要运行的某个脚本名,而不是文件夹名了。

2、读数据驱动&发邮件

# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 11:18:00 2014 @author: pi
"""
import smtplib
from email.mime.text import MIMEText
import RPi.GPIO as gpio
import time mail_to_1="====@outlook.com"
mail_to_2="====@qq.com"
mail_to_3='=====@qq.com'
mail_to_4="====@qq.com" def send_mail(to_list,title,content):
mail_host="smtp.126.com"
mail_user="colipso"
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 get_temperature_humidity():
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
time.sleep(1)
data=[]
def delay(i): #20*i usdelay
a=0
for j in range(i):
a+1
j=0
#start work
gpio.setup(12,gpio.OUT)
#gpio.output(12,gpio.HIGH)
#delay(10)
gpio.output(12,gpio.LOW)
time.sleep(0.02)
gpio.output(12,gpio.HIGH)
i=1
i=1 #wait to response
gpio.setup(12,gpio.IN) while gpio.input(12)==1:
continue while gpio.input(12)==0:
continue while gpio.input(12)==1:
continue
#get data while j<40:
k=0
while gpio.input(12)==0:
continue while gpio.input(12)==1:
k+=1
if k>100:break
if k<3:
data.append(0)
else:
data.append(1)
j+=1 #print "Sensor is working"
#get temperature
humidity_bit=data[0:8]
humidity_point_bit=data[8:16]
temperature_bit=data[16:24]
temperature_point_bit=data[24:32]
check_bit=data[32:40] humidity=0
humidity_point=0
temperature=0
temperature_point=0
check=0 for i in range(8):
humidity+=humidity_bit[i]*2**(7-i)
humidity_point+=humidity_point_bit[i]*2**(7-i)
temperature+=temperature_bit[i]*2**(7-i)
temperature_point+=temperature_point_bit[i]*2**(7-i)
check+=check_bit[i]*2**(7-i) tmp=humidity+humidity_point+temperature+temperature_point if check==tmp:
return temperature,humidity
else:
return 0,0
#current_time="%d" % time.localtime().tm_year+"."+"%d" % time.localtime().tm_mon+"."+ "%d" % time.localtime().tm_mday+" "+ "%d" % time.localtime().tm_hour+":"+ "%d" % time.localtime().tm_hour current_time=" "
tem_humi=get_temperature_humidity()
#print tem_humi
if tem_humi[0]!=0:
content1=current_time+" The home temperature is %d" % tem_humi[0]
content2=current_time+" The home humidity is %d" % tem_humi[1]
content=content1+"C. "+content2+"%"
send_mail(mail_to_1,'My home',content)
send_mail(mail_to_2,'My home',content)
send_mail(mail_to_3,'My home',content)
send_mail(mail_to_4,'My home',content)
#print content

【Raspberry Pi】定时运行python程序读温湿度传感器数据&发邮件的更多相关文章

  1. 设置PATH 环境变量、pyw格式、命令行运行python程序与多重剪贴板

    pyw格式简介: 与py类似,我认为他们俩卫衣的不同就是前者运行时候不显示终端窗口,后者显示 命令行运行python程序: 在我学习python的过程中我通常使用IDLE来运行程序,这一步骤太过繁琐( ...

  2. 编程语言类别;运行Python程序的方式;变量和常量;Python程序的垃圾回收机制;

    目录 编程语言分类 运行Python程序的两种方式 1.交互式 变量与常量 1.变量 2.常量 3.小整数池 垃圾回收机制 编程语言分类 编程语言分为: 1.机器语言:直接用二进制的0和1和计算机(C ...

  3. Linux-Centos 用crontab定时运行python脚本详细步骤

    服务器总是要定时运行某个程序,而我在解决这个问题的时候遇到很多困难, 特此记录下来. 1.编辑crontab配置 crontab -e 服务器一般会安装好crontab,若没有安装请按命令安装 yum ...

  4. 运行python程序

    1 在windows下运行python程序 1)从DOS命令行运行python脚本 用python解释器来执行python脚本,在windows下面python解释器是python.exe,我的pyt ...

  5. editplus3运行Python程序

    editplus3是一款不错的编辑器,他可以编译,运行java,php等各种程序,现把他运行Python程序的方法贴出来,首先得安装python,然后打开editplug3,工具——配置用户工具——组 ...

  6. 如何使用sublime编辑器运行python程序

    现在越发喜欢sublime编辑器了,不仅界面友好美观.文艺,可扩展性还特别强. sublime本身是不具备运行python程序的能力的,需要做些设置才可以.以下是安装好sublime后设置的步骤: 点 ...

  7. Linux(9)后台运行python程序并输出到日志文件

    后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...

  8. 周一02.3运行python程序的两种方式

    一.运行python程序的两种方式 方法一:交互式:                     优点:输入一行代码立刻返回结果                      缺点:无法永久保存代码 方法二: ...

  9. 解释器、环境变量、如何运行python程序、变量先定义后引用

    python解释器的介绍.解释器的安装.环境变量的添加为什么加环境变量.如何调取不同的解释器版本实现多版本共存.python程序如何运行的.python的变量定义 一.python解释器: 用来翻译语 ...

随机推荐

  1. iOS文件上传文件URL错误Invalid parameter not satisfying: fileURL'

    一:iOS文件上传提示URL错误 Invalid parameter not satisfying: fileURL' 二:解决方法: NSString *imagePath = [[NSBundle ...

  2. 【转】C++ 虚函数&纯虚函数&抽象类&接口&虚基类

    1. 动态多态 在面向对象语言中,接口的多种不同实现方式即为多态.多态是指,用父类的指针指向子类的实例(对象),然后通过父类的指针调用实际子类的成员函数. 多态性就是允许将子类类型的指针赋值给父类类型 ...

  3. vue 仿QQ 开发流程

    技术客栈: vue-cli vue2 vue-router vuex axios stylus webpack2 muse-ui 1.安装脚手架 npm install -g vue-cli 2.开始 ...

  4. MVC 强类型传值Model。和弱类型传值ViewData[&quot;&quot;]。及用EF进行增删查改(母版页的使用)

    <1> 控制器 </pre><pre name="code" class="csharp">using MvcTest.Mo ...

  5. Android 6.0 超级简单的权限申请 (Permission)

    代码地址如下:http://www.demodashi.com/demo/13369.html 背景描述 随着Android系统的不断升级,谷歌对用户的隐私是越来越注重了,给我们开发者带来了更多的繁琐 ...

  6. String、StringBuilder、StringBuffer对比

    参考:http://swiftlet.net/archives/1694 http://www.cnblogs.com/springcsc/archive/2009/12/03/1616326.htm ...

  7. CSS3实现文字扫光效果

    本篇文章由:http://xinpure.com/css3-text-light-sweep-effect/ CSS3 实现的文字扫光效果,几乎可以和 Flash 相媲美了 效果解析 我们分析一下实现 ...

  8. <转>LuaTinker的bug和缺陷

    LuaTinker的bug和缺陷 LuaTinker是一套还不错的C++代码和Lua代码的绑定库,作者是韩国人Kwon-il Lee,作者应该是参考了LuaBind后,为了简化和避免过重而实现的.其官 ...

  9. bootstrap 警告

    本章将讲解警告(Alerts)以及Bootstrap所提供的用于警告的class.警告(Alerts)向用户提供了一种定义消息样式的方式.它们为典型的用户操作提供了上下文信息反馈. 您可以为警告框添加 ...

  10. ASP.NET MVC使用IoC

    也许你会问ASP.NET MVC为什么会爱上IoC? 相爱的理由常常很简单,就像一首歌中所唱——“只为相遇那一个眼神”. 而ASP.NET MVC爱上IoC只为IoC能实现MVC控制器的依赖注入. 下 ...