Fehlendes Time Modul Python, Raspberry Pi 3

Probleme bei der Installation?
Antworten
Rupiert
User
Beiträge: 1
Registriert: Samstag 6. April 2019, 17:23

Das ist mein ganzer Code. Ich habe kein time modul und weiß nichtmehr weiter. Habe Python Neu Installiert... Hatte gedacht das es mit Python mitgeht bei der Installation :| :| ; Bitte Hilfe!!! Danke im Voraus für alles!!!

Fehler/Output
root@raspberrypi:~# python /home/programme/clima_sensor.py
<type 'datetime.time'>
Traceback (most recent call last):
File "/home/programme/clima_sensor.py", line 87, in <module>
loop()
File "/home/programme/clima_sensor.py", line 77, in loop
check(temperature, humidity)
File "/home/programme/clima_sensor.py", line 64, in check
time.sleep(sleeptime)
AttributeError: 'str' object has no attribute 'sleep'
root@raspberrypi:~#

Code: Alles auswählen

#!/usr/bin/python
# coding=utf-8
import I2C_LCD_driver
import Adafruit_DHT
import sys
import RPi.GPIO as GPIO
import subprocess
import os
from time import *
from datetime import *

sensor = Adafruit_DHT.DHT11
pin = 4
sleeptime = 1
print time
led_rot = 21
led_gelb = 20
led_gruen = 12
led_blau = 16
path = "bash /home/programme/pin.sh"

humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
mylcd = I2C_LCD_driver.lcd()

now = datetime.now()
date = now.strftime("%m/%d/%Y")
time = now.strftime("%H:%M:%S")


def check(temp, humidity):
    if 20 < temp < 25:
        os.system('%s %i 0' % (path, led_blau))
        os.system('%s %i 0' % (path, led_rot))
        os.system('%s %i 1' % (path, led_gruen))
    elif 20 > temp:
        os.system('%s %i 0' % (path, led_gruen))
        os.system('%s %i 0' % (path, led_rot))
        while True:
            if 20 > temp:
                time.sleep(sleeptime)
                os.system('%s %i 1' % (path, led_blau))
                time.sleep(sleeptime)
                os.system('%s %i 0' % (path, led_blau))
            else:
                break
    elif 25 < temp:
        os.system('%s %i 0' % (path, led_gruen))
        os.system('%s %i 0' % (path, led_blau))
        while True:
            if 25 < temp:
                time.sleep(sleeptime)
                os.system('%s %i 1' % (path, led_rot))
                time.sleep(sleeptime)
                os.system('%s %i 0' % (path, led_rot))
            else:
                break
    else:
        for x in range(0, 100):
            x = x + 1
            os.system('%s %i 1' % (path, led_rot))
            os.system('%s %i 1' % (path, led_blau))
            os.system('%s %i 1' % (path, led_gruen))
            time.sleep(sleeptime)
            os.system('%s %i 0' % (path, led_rot))
            os.system('%s %i 0' % (path, led_blau))
            os.system('%s %i 0' % (path, led_gruen))
            time.sleep(sleeptime)


def loop():
    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        temp = "Waerme: %i%s C" % (int(temperature), chr(223))
        humidity2show = "Naesse: %i " % (int(humidity))
        #os.system('%s %i 0' % (path, led_gruen))
        check(temperature, humidity)
        # mylcd.lcd_display_string("als",1,1)
        mylcd.lcd_display_string(temp, 1)
        mylcd.lcd_display_string(humidity2show, 2)
def destroy():
    GPIO.output(ledPin, GPIO.LOW)  # led off
    GPIO.cleanup()  # Release resource

while True:
    try:
        loop()
    except KeyboardInterrupt:  # When 'Ctrl+C' is pressed, the child program destroy() will be  executed.
        sys.exit("Programm exit")
__deets__
User
Beiträge: 14522
Registriert: Mittwoch 14. Oktober 2015, 14:29

Wenn du etwas anders an den Namen time bindest, dann wird das Modul dadurch nicht erreicht. Mit ein Grund, so endlos viele globale Variablen zu vermeiden.

Schau mal genau hin, wo etwas der Form

time = ...

steht.
Sirius3
User
Beiträge: 17737
Registriert: Sonntag 21. Oktober 2012, 17:20

Schon die zweite Zeile ist falsch. So versteht Python die Encodingangabe nicht.
Sternchenimporte sind schlecht, weil man nicht kontrollieren kann, welche Namen da tatsächlich in den eigenen Namensraum geladen werden.
`subprocess` wird importiert, aber nicht benutzt, nichts was aus `time` importiert wird, wird benutzt.
Konstanten schreibt man KOMPLETT_GROSS damit man schon am Namen erkennen kann, was eine Konstante ist.
`date` und `time` werden definiert, aber nirgends benutzt.

In `check` rufst Du ein externes Programm zum Pin-Schalten auf, obwohl Du doch GPIO importierst und das nutzen könntest.
Die while-Schleifen sind umständlich geschrieben, da die Abbruchbedingung nicht hinter `while` steht sondern als `if`.
Die Schleifen berechen aber nie ab, weil sich `temp` nicht ändert.

`destroy` wird nie aufgerufen, was aber passiern sollte. `LedPin` ist nicht definiert.
`sys.exit` erwartet eine Zahl als Argument, keinen String.
Entweder ist die Schleife in `loop` oder im Hauptprogramm überflüssig.

Ungetestet:

Code: Alles auswählen

#!/usr/bin/python
# -*- coding: utf-8 -*- 
import sys
import RPi.GPIO as gpio
from time import sleep
import I2C_LCD_driver
import Adafruit_DHT

SENSOR = Adafruit_DHT.DHT11
PIN = 4
SLEEPTIME = 1
LED_ROT = 21
LED_GELB = 20
LED_GRUEN = 12
LED_BLAU = 16

def check(temp, humidity):
    if temp < 20:
        gpio.output([LED_GRUEN, LED_ROT], 0)
        gpio.output([LED_BLAU], 1)
        sleep(SLEEPTIME)
        gpio.output([LED_BLAU], 0)
        sleep(SLEEPTIME)
    elif 20 < temp < 25:
        gpio.output([LED_BLAU, LED_ROT], 0)
        gpio.output([LED_GRUEN], 1)
    elif temp > 25:
        gpio.output([LED_GRUEN, LED_BLAU], 0)
        gpio.output([LED_ROT], 1)
        sleep(SLEEPTIME)
        gpio.output([LED_ROT], 0)
        sleep(SLEEPTIME)
    else:
        # exakt 20 oder 25
        gpio.output([LED_GRUEN, LED_BLAU, LED_ROT], 1)
        sleep(SLEEPTIME)
        gpio.output([LED_GRUEN, LED_BLAU, LED_ROT], 0)
        sleep(SLEEPTIME)

def destroy():
    gpio.cleanup()  # Release resource

def main():
    gpio.setup([LED_BLAU, LED_GELB, LED_GRUEN, LED_ROT], gpio.OUT)
    try:
        lcd = I2C_LCD_driver.lcd()
        while True:
            humidity, temperature = Adafruit_DHT.read_retry(SENSOR, PIN)
            lcd.lcd_display_string("Waerme: %i °C" % temperature, 1)
            lcd.lcd_display_string("Naesse: %i " % humidity, 2)
            check(temperature, humidity)
    except KeyboardInterrupt:
        pass
    finally:
        destroy()

if __name__ == '__main__':
    main()
Antworten