Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hyzhak opened this issue Jun 28, 2019 · 7 comments

Comments

@hyzhak
Copy link

hyzhak commented Jun 28, 2019

$ 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
Copy link
Author

hyzhak commented Jun 28, 2019

hm, sudo solved that problem:

sudo ./main

@hyzhak hyzhak closed this as completed Jun 28, 2019
@d2r2
Copy link
Owner

d2r2 commented Jun 28, 2019

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

@hyzhak
Copy link
Author

hyzhak commented Jun 28, 2019

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

@Dalvany
Copy link

Dalvany commented Oct 5, 2019

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
Copy link

Dalvany commented Oct 6, 2019

I read the diff between versions and came accross this bce0dc9#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.

@ezplanet
Copy link

ezplanet commented Jul 4, 2023

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.

@Merlinux-source
Copy link

Merlinux-source commented May 7, 2024

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 (debian-kernel@lists.debian.org) (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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants