【树莓派3 定时上传CUP温度到yeelink 代码】

  • CPUtemperature.py 代码
    
    #!/usr/bin/env python  
    
    import requests  
    import json  
    import time  
    
    file = open("/sys/class/thermal/thermal_zone0/temp")  
    CPUtemperature = float(file.read()) / 1000  
    file.close
    
    print "CPU Temperature :", CPUtemperature
    
    topost_CPUtemperature_payload={'value':CPUtemperature}
    
    url_CPUtemperature='你的传感器URL' 
    header={'U-ApiKey':'你的Apikey', 'content-type': 'application/json'}
    
    post_CPUtemperature = requests.post(url_CPUtemperature,headers=header,data=json.dumps(topost_CPUtemperature_payload))
    
  • yeelink.sh 代码
    
        sudo python /home/pi/yeelink.py
    
  • 运行
    
        sudo chmod +x /home/pi/yeelink.sh
    
  • 设置定时5分钟运行一次
    
        sudo crontab -e
    
  • 加入
    
    */5 * * * * /home/pi/yeelink.sh
    
  • 【树莓派3+DHT11温湿度传感器+定时上传yeelink 代码+数据过滤】

  • DHT11.py 代码
    
    #!/usr/bin/python
    
    import RPi.GPIO as GPIO
    import time
    import requests
    import json
    
    #Read data from DHT11
    def DHT11():
      channel = 17
      data = []
      j = 0
    
      GPIO.setmode(GPIO.BCM)
      time.sleep(1)
      GPIO.setup(channel, GPIO.OUT)
      GPIO.output(channel, GPIO.LOW)
      time.sleep(0.02)
      GPIO.output(channel, GPIO.HIGH)
      GPIO.setup(channel, GPIO.IN)
    
      while GPIO.input(channel) == GPIO.LOW:
           continue
    
      while GPIO.input(channel) == GPIO.HIGH:
           continue
    
      while j < 40:
           k = 0
           while GPIO.input(channel) == GPIO.LOW:
               continue
    
           while GPIO.input(channel) == GPIO.HIGH:
               k += 1
               if k > 100:
                   break
           if k < 8:
               data.append(0)
           else:
               data.append(1)
    
           j += 1
    
      #print "sensor is working."
      #print data
    
      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]
    
      global humidity
      global temperature
      global check
      humidity_point = 0
      temperature_point = 0
      check = 0
      temperature = 0
      humidity = 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)
    
      global tmp
      tmp = humidity + humidity_point + temperature + temperature_point
      GPIO.cleanup()
    
    def yeelink():
      print "DHT11 Temperature & Humidity Sensor"
      print "temperature :", temperature, "*C, humidity :", humidity, "%"
    
      mytemp = '%f' %temperature
      myhumi = '%f' %humidity
    
      topost_tmp_payload={'value':mytemp}
      topost_humidity_payload={'value':myhumi}
    
      url_tmp='你的传感器URL' 
      url_humidity='你的传感器URL'
      header={'U-ApiKey':'你的Apikey', 'content-type': 'application/json'}
    
      post_tem = requests.post(url_tmp,headers=header,data=json.dumps(topost_tmp_payload))
      post_humidity = requests.post(url_humidity,headers=header,data=json.dumps(topost_humidity_payload))
    
    DHT11()
    #Ignore the bad data
    while check != tmp:
        DHT11()
    yeelink() 
    
  • DHT11.sh 代码
    
        sudo python /home/pi/DHT11.py
    
  • 运行
    
        sudo chmod +x /home/pi/DHT11.sh
    
  • 设置定时5分钟运行一次
    
        sudo crontab -e
    
  • 加入
    
    */5 * * * * /home/pi/DHT11.sh
    
  • 【外网访问校园网树莓派–花生壳内网穿透】

  • 现在公网ip紧缺,如何用外网访问连接校园网的树莓派?请看:(1.2.3.4均在终端下执行)


    1.下载花生壳树莓派客户端(http://hsk.oray.com/download/)到树莓派home目录

    2.解压得到 phddns2 文件夹

    3.进入phddns2文件夹,运行:

    
    sudo ./oraynewph start
    

    如提示 Oraynewph start success 说明花生壳成功安装运行

    4.查看运行状态及SN码,在任意路径命令行执行 

    
    oraynewph status
    

    即可看到花生壳 运行状态 以及 SN码

    5.在花生壳管理页面(http://b.oray.com),输入SN码与密码(首次登录默认密码admin),首次登录需要修改默认密码,完善手机验证和邮箱信息完成初始化,还需要绑定花生壳账号使用。

    6.绑定登录成功,可以直接点击页面中间橙色模块 内网映射 添加映射。

    7.尽情享受吧

    附:

    
    oraynewph status #状态
    oraynewph start  #开启
    oraynewph stop   #停止
    oraynewph reset  #重置
    
  • 【树莓派apt加速 替换apt源为国内镜像】

  • 因为众所周知的原因,Linux默认的apt源在国内神慢……换吧


  • 1.改sources.list
    
    sudo vim /etc/apt/sources.list
    


    内容全部改为

    
    deb http://mirrors.ustc.edu.cn/raspbian/raspbian jessie main contrib non-free rpi
    deb-src http://mirrors.ustc.edu.cn/raspbian/raspbian jessie main contrib non-free rpi
    


  • 2.改raspi.list
    
    sudo vim /etc/apt/sources.list.d/raspi.list
    


    内容全部改为

    
    deb http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ jessie main ui
    deb-src http://mirrors.ustc.edu.cn/archive.raspberrypi.org/debian/ jessie main ui
    

    爽快!