DHT11 sensor

Hello
I’m having a trouble reading temperature from DHT11 sensor, what i’'ve done so far:

Items named demo.items

Number Temp “Temperature [%.1f °C]” (1stuk) {exec="<[/usr/bin/python /etc/openhab2/scripts/TempTest.py:10000:REGEX((.*?))]"}

Script named: TempTest.py (found script for DHT22 and changed everywhere 22 to 11)
import pigpio
import DHT11
from time import sleep
pi = pigpio.pi ()
dht11 = DHT11.sensor(pi, 4)
sleepTime = 3
dht11.trigger()
sleep(sleepTime)
dht11.trigger()
sleep(sleepTime)
def readDHT11():
dht11.trigger()
temp = ‘%.2f’ % (dht11.temperature())
return(temp)
while True:
temperature = readDHT11()
print (temperature)
break

sitemap named: default.sitemap
sitemap demo label=“1stuk”
{
Frame {
Text item=Temp
}
}

+P.S. i do not have 10k ohm resisstor(tryed different script and it shows temp just fine )

What happens when you run the script in the command line?

You’re not giving it much time to sleep in between reading temp. Also, if another script works, why not use it?

Ok, when i try to run the script error showes up and i double checked script and it does start with the import piqpio.

image

Sorry, i’m new to this. How much time does it need? And the working script was lost or deleted, because i’m trying to set up this sensor for the last week.

@Jaka_Smode here’s the Adafruit version, change as needed for your setup.

#!/usr/bin/python

# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import Adafruit_DHT

# Sensor should be set to Adafruit_DHT.DHT11,
# Adafruit_DHT.DHT22, or Adafruit_DHT.AM2302.
sensor = Adafruit_DHT.DHT22

# Example using a Beaglebone Black with DHT sensor
# connected to pin P8_11.
pin = 'P8_11'

# Example using a Raspberry Pi with DHT sensor
# connected to GPIO23.
#pin = 23

# Try to grab a sensor reading.  Use the read_retry method which will retry up
# to 15 times to get a sensor reading (waiting 2 seconds between each retry).
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)

# Note that sometimes you won't get a reading and
# the results will be null (because Linux can't
# guarantee the timing of calls to read the sensor).
# If this happens try again!
if humidity is not None and temperature is not None:
    print('Temp={0:0.1f}*C  Humidity={1:0.1f}%'.format(temperature, humidity))
else:
    print('Failed to get reading. Try again!')

Ok, so i edited the code like this (picture) and its still doesn’t work

I don’t know, what the adafruit-script does… But this error message you got using your TempTest.py tells you, that your python-script uses a library called pigpio which you obviously didn’t install beforehand.

If this happens (regardless which module python tells you is missing) do a quick search on MODULENAME +python and it’ll show you what to do. In this case, just type in: pip install pigpio and it’ll install the desired module. But beware! - Accessing GPIO normally requires sudo power unless you configure your Pi to do otherwise. I bet, you got your python script somewhere - if the author did the right thing, he did include a “prerequisite” for his script and I guess you just missed installing the required modules…?