go-dht icon indicating copy to clipboard operation
go-dht copied to clipboard

Error during call C.dial_DHTxx_and_read(): failed to open pin 22 direction for writing on Raspberry PI3

Open hyzhak opened this issue 5 years ago • 7 comments

$ go version
go version go1.12.6 linux/arm

I've tried code from example1:

	sensorType := dht.AM2302
	pin := 22
	temperature, humidity, retried, err :=
		dht.ReadDHTxxWithRetry(sensorType, pin, false, 10)
	if err != nil {
		fmt.Println(err)
	}

	fmt.Println("Sensor = %v: Temperature = %v*C, Humidity = %v%% (retried %d times)",
		sensorType, temperature, humidity, retried)

compiled and run by:

GOARM=7 GOARCH=arm GOOS=linux go build -o main & ./main

got error:

Error during call C.dial_DHTxx_and_read(): failed to open pin 22 direction for writing

in the same time tried to use DHT from https://github.com/adafruit/Adafruit_Python_DHT (python lib) and it worked fine.

Also I've tried to connect to different pins, but got the same result -- works fine on python and doesn't work on golang lib :(.

hyzhak avatar Jun 28 '19 07:06 hyzhak

hm, sudo solved that problem:

sudo ./main

hyzhak avatar Jun 28 '19 09:06 hyzhak

Hi @hyzhak. Nice to hear that you found workaround! It is curious that without sudo it doesn't work... Suppose, https://github.com/adafruit/Adafruit_Python_DHT works, because, I guess, it use another approach (/dev/mem), but here I employ GPIO.

I can't recall why I did not face with this problem, but probably this can help: https://stackoverflow.com/questions/30938991/access-gpio-sys-class-gpio-as-non-root

d2r2 avatar Jun 28 '19 20:06 d2r2

oh! great it is very helpful, I'd try to avoid sudo as much as possible :)

hyzhak avatar Jun 28 '19 21:06 hyzhak

Hello, I got the same error and sudo seems to 'fix'. I dig a little more and find that the commit adding support for DHT12 introduce this behavior Here is a go.mod that works :

module dht

go 1.13

require (
        github.com/d2r2/go-dht v0.0.0-20181222061613-42fe873fbabb
        github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e // indirect
        github.com/d2r2/go-shell v0.0.0-20190508080434-6fd313082bbf // indirect
        github.com/davecgh/go-spew v1.1.1 // indirect
)

With the following one, I got the failed to open pin X direction for writing error :

module dht

go 1.13

require (
        github.com/d2r2/go-dht v0.0.0-20190315195559-bce0dc951e46
        github.com/d2r2/go-logger v0.0.0-20181221090742-9998a510495e // indirect
        github.com/d2r2/go-shell v0.0.0-20190508080434-6fd313082bbf // indirect
        github.com/davecgh/go-spew v1.1.1 // indirect
)

Dalvany avatar Oct 05 '19 19:10 Dalvany

I read the diff between versions and came accross this https://github.com/d2r2/go-dht/commit/bce0dc951e4612ca94a049a0319f60fc75d6fa6b#diff-a9f55f7fcb9cb447f152c9128faea88fL166

    // !!! Found in experimental way, that additional pause should exist
    // between export pin to work with and direction set up. Otherwise,
    // under the regular user mistake occures frequently !!!
    //
    // Sleep 150 milliseconds
    // sleep_usec(150*1000);

Uncommenting this sleep_usec fix the issue as stated in the comment.

Dalvany avatar Oct 06 '19 12:10 Dalvany

I have the same problem, but 'sudo' does not solve it:

I get the following on pin 29: 2023-07-04T15:56:12.905 [ dht] WARN Error during call C.dial_DHTxx_and_read(): failed to export pin 29

and on pin 35: 2023-07-04T15:48:14.201 [ dht] WARN Error during call C.dial_DHTxx_and_read(): failed to open pin 35 direction for writing

Using iono Pi max with Rpi 3 CM Lite

Needless to mention that all good with Adafruit python library.

ezplanet avatar Jul 04 '23 13:07 ezplanet

got the same issue, it doesn't work with sudo

pflanze@pflanzen:~/read_dht22 $ go build
pflanze@pflanzen:~/read_dht22 $ ./main 
2024-05-07T04:44:11.329 [    main] NOTICE  ***************************************************************************************************
2024-05-07T04:44:11.329 [    main] NOTICE  *** You can change verbosity of output, to modify logging level of module "dht"
2024-05-07T04:44:11.329 [    main] NOTICE  *** Uncomment/comment corresponding lines with call to ChangePackageLogLevel(...)
2024-05-07T04:44:11.329 [    main] NOTICE  ***************************************************************************************************
2024-05-07T04:44:11.330 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:12.831 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:14.332 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:15.833 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:17.334 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:18.835 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:20.336 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:21.838 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:23.339 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:24.840 [     dht] WARN  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
2024-05-07T04:44:26.341 [    main] FATAL  Error during call C.dial_DHTxx_and_read(): failed to export pin 2
pflanze@pflanzen:~/read_dht22 $ cat main.go 
package main
import (
        "github.com/d2r2/go-dht"
        logger "github.com/d2r2/go-logger"
)

var lg = logger.NewPackageLogger("main",
        logger.DebugLevel,
        // logger.InfoLevel,
)

func main() {
        defer logger.FinalizeLogger()

        lg.Notify("***************************************************************************************************")
        lg.Notify("*** You can change verbosity of output, to modify logging level of module \"dht\"")
        lg.Notify("*** Uncomment/comment corresponding lines with call to ChangePackageLogLevel(...)")
        lg.Notify("***************************************************************************************************")
        // Uncomment/comment next line to suppress/increase verbosity of output
        logger.ChangePackageLogLevel("dht", logger.InfoLevel)

        // sensorType := dht.DHT11
        sensorType := dht.AM2302
        // sensorType := dht.DHT12
        // Read DHT11 sensor data from specific pin, retrying 10 times in case of failure.
        pin := 2
        temperature, humidity, retried, err :=
                dht.ReadDHTxxWithRetry(sensorType, pin, false, 10)
        if err != nil {
                lg.Fatal(err)
        }
        // print temperature and humidity
        lg.Infof("Sensor = %v: Temperature = %v*C, Humidity = %v%% (retried %d times)",
                sensorType, temperature, humidity, retried)
}
pflanze@pflanzen:~/read_dht22 $ cat /proc/version 
Linux version 6.6.28+rpt-rpi-v8 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT Debian 1:6.6.28-1+rpt1 (2024-04-22)

is this project abandoned?

Merlinux-source avatar May 07 '24 02:05 Merlinux-source