传感器Python DHT11

时间:2018-11-21 10:05:07

标签: python firebase sensor

我尝试遵循本教程: https://medium.com/@dvd.ciri/raspberry-pi-firebase-home-automation-d5a237f18fb5

但是我对adafruit有问题。有人可以帮助我吗?

import RPi.GPIO as GPIO
from time import sleep
import datetime
from firebase import firebase
import pigpio
import Adafruit_DHT
import urllib2, urllib, httplib
import json
import os 
from functools import partial

# Global variables
LOGGER = 1
sensor_pin_number = 4;
firebase = firebase.FirebaseApplication('https://p8.firebaseio.com/', None)


# LOGGER
def printlog(text):
    if(LOGGER):
        print(text)
# LOGGER


# Start script
# ============================================================================
printlog("Script Started\n")

#Information about temperature, humidity, cpu, disk, RAM
# Initiate GPIO for pigpio
pi = pigpio.pi()
# Setup the sensor
sensor= Adafruit_DHT.DHT22



def readDHT22():
    # Get a new reading
    # Save our values
    humidity,temp = Adafruit_DHT.read_retry(sensor, sensor_pin_number)

    return (humidity, temp)


# Return CPU temperature as a character string                                      
def getCPUtemperature():
    res = os.popen('vcgencmd measure_temp').readline()
    return(res.replace("temp=","").replace("'C\n",""))

# Return RAM information (unit=kb) in a list                                        
# Index 0: total RAM                                                                
# Index 1: used RAM                                                                 
# Index 2: free RAM                                                                 
def getRAMinfo():
    p = os.popen('free')
    i = 0
    while 1:
        i = i + 1
        line = p.readline()
        if i==2:
            return(line.split()[1:4])

# Return % of CPU used by user as a character string                                
def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\
)))

# Return information about disk space as a list (unit included)                     
# Index 0: total disk space                                                         
# Index 1: used disk space                                                          
# Index 2: remaining disk space                                                     
# Index 3: percentage of disk used                                                  
def getDiskSpace():
    p = os.popen("df -h /")
    i = 0
    while 1:
        i = i +1
        line = p.readline()
        if i==2:
            return(line.split()[1:5])

##Date time formatting
dateString = '%d/%m/%Y %H:%M:%S'


def updatePiInfo():

    printlog("## Updating Firebase Info.. ##")
    firebase.put("/Settings", "/last_update_datetime", datetime.datetime.now().strftime(dateString))

    ##retrive max & min humidity (remove the %)
    maxHumidity = firebase.get("/Controls/Sensors/Humidity/max_inside", None)
    maxHumidity = maxHumidity[:-1]

    minHumidity = firebase.get("/Controls/Sensors/Humidity/min_inside", None)
    minHumidity = minHumidity[:-1]

    #retrieve max & min temperature (remove the C)
    maxTemperature = firebase.get("/Controls/Sensors/Temperature/max_inside", None)
    maxTemperature = maxTemperature[:-1]

    minTemperature = firebase.get("/Controls/Sensors/Temperature/min_inside", None)
    minTemperature = minTemperature[:-1]

    #add current value
    humidity, temperature = readDHT22()
    firebase.put("/Controls/Sensors", "/Humidity/current_inside", ""+humidity+"%")
    firebase.put("/Controls/Sensors", "/Temperature/current_inside", ""+temperature+"C")

    ##check for max values
    if float(humidity) > float(maxHumidity):
        firebase.put("/Controls/Sensors", "/Humidity/max_inside", ""+humidity+"%")
        printlog("Updated Humidity max_inside")
    if float(temperature) > float(maxTemperature):
        firebase.put("/Controls/Sensors", "/Temperature/max_inside", ""+temperature+"C")
        printlog("Updated Temperature max_inside")

    ## cehck for min values
    if float(humidity) < float(minHumidity):
        firebase.put("/Controls/Sensors", "/Humidity/min_inside", ""+humidity+"%")
        printlog("Updated Humidity min_inside")
    if float(temperature) < float(minTemperature):
        firebase.put("/Controls/Sensors", "/Temperature/min_inside", ""+temperature+"C")
        printlog("Updated Temperature min_inside")

    #CPU INFO
    CPU_temp = getCPUtemperature()
    CPU_usage = getCPUuse()
    firebase.put("/PI/CPU", "/temperature", CPU_temp)

    #RAM INFO
    RAM_stats = getRAMinfo()
    RAM_total = round(int(RAM_stats[0]) / 1000,1)
    RAM_used = round(int(RAM_stats[1]) / 1000,1)
    RAM_free = round(int(RAM_stats[2]) / 1000,1)
    firebase.put("/PI/RAM", "/free", str(RAM_free)+"")
    firebase.put("/PI/RAM", "/used", str(RAM_used)+"")
    firebase.put("/PI/RAM", "/total", str(RAM_total)+"")

    #DISK INFO
    DISK_stats = getDiskSpace()
    DISK_total = DISK_stats[0]
    DISK_free = DISK_stats[1]
    DISK_perc = DISK_stats[3]
    DISK_used = float(DISK_total[:-1]) - float(DISK_free[:-1])
    firebase.put("/PI/DISK", "/total", str(DISK_total[:-1]))
    firebase.put("/PI/DISK", "/free", str(DISK_free[:-1]))
    firebase.put("/PI/DISK", "/used", str(DISK_used))
    firebase.put("/PI/DISK", "/percentage", str(DISK_perc))


    printlog(datetime.datetime.now().strftime(dateString))
    printlog("Humidity: Current["+humidity+"], Max["+maxHumidity+"], Min["+minHumidity+"]")
    printlog("Temperature: Current["+temperature+"], Max["+maxTemperature+"], Min["+minTemperature+"]")
    printlog("CPU temperature: "+CPU_temp)
    printlog("RAM total["+str(RAM_total)+" MB], RAM used["+str(RAM_used)+" MB], RAM free["+str(RAM_free)+" MB]")
    printlog("DISK total["+str(DISK_total)+"], free["+str(DISK_free)+"], perc["+str(DISK_perc)+"]")
    printlog("## Update finished successfully ##")
    printlog("======================================================\n")

我有此错误:

dht22 = Adafruit_DHT.sensor(pi, sensor_pin_number) AttributeError: 'module' object has no attribute 'sensor'

///////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// /////////////////////////////////////////////////////

1 个答案:

答案 0 :(得分:2)

尝试一下:

expect(estring).to.satisfy(estring =>
  eventTag.split(",").some(b => estring.includes(b))
);