raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.62k stars 779 forks source link

[Bugfix] Refracture of not working `dht.c` Example #440

Open hasenradball opened 7 months ago

hasenradball commented 7 months ago

Refracture dht.c Example

This code is adapted to the Pico c files and was mainly taken from here: AM2302-sensor

Tested

Code is tesed on Arduino NANO, ESP8266, and also on Pi Pico

Screeshot

2023-11-29-223802_1920x1200_scrot

hasenradball commented 7 months ago

@lurch Hi, I provided an example fot a working dht-Sensor code

lurch commented 7 months ago

a) I personally don't have a DHT sensor, so I'm unable to test this b) It's not up to me to approve or merge PRs in this repo

However I do have a good eye for detail, so I sometimes (voluntarily) leave comments which I hope are helpful :wink:

It looks like there's a large chunk of code-duplication between await_state and read_sensor_data ?

hasenradball commented 7 months ago

@lurch Hi,

thanks for your comment. You are right It seems it looks duplicated, but it is slightly different. I checked this with different sensors against robustness on different mikrocontrollers. Using await_state()m function for the 80 us acknowledge sequence and the read_sensor_data() is the robustest one. Reading data is very time critical, especially for the logic zero. I also wanted to combine these steps but it is less robust. Therfore I decided to leave it as it is for now.

An Additional reason is I wanted to separate the acknowledge and the data reading sequence.

Frank

lurch commented 7 months ago

Cool. This demonstrates why testing on actual hardware is more important than somebody (e.g. me) just naively reading the code! :laughing:

hasenradball commented 7 months ago

@lurch Hi,

the most libraries of DHT Sensor don' t check the sensor status (timeout or checksum error). But if you check this you see how robust you code is. :-)

hasenradball commented 6 months ago

Added test for negative Temperatures...

20231211_16h31m16s_grim

peterharperuk commented 6 months ago

I did a quick test with a dht11 and it's reporting a bit hot...

Sensor-status: 0 Humidity = 870.4 % Temperature = 563.2 degC (1045.8 F)

hasenradball commented 6 months ago

@peterharperuk Hi Peter,

I tested everything with a AM2302 (aka DHT22 Sensor). I haven't had a DHT11. I will check the Data sheet for the DHT11.

hasenradball commented 6 months ago

@peterharperuk Hi Peter,

could you make a short test, please? I have only a AM2302-Sensor (DHT22).

In Line 82:

replace sleep_us(1200U) by sleep_us(18000U).

This seems the only difference between DHT22 and DHT11 regarding data reading.

image

I this works for the DHT11 I will check with the DHT22 variant.

Question: Did you place an PullUp resistor between Data and Vcc line?

Thanks

Frank :-)

peterharperuk commented 6 months ago

Yes, DHT22 woks nicely!

Sensor-status: 0 Humidity = 39.8 % Temperature = 22.3 degC (72.1 F)

peterharperuk commented 6 months ago

Changing the sleep didn't seem to make any difference unfortunately. I have a same resistor fitted I used for DHT22. I will say that your code at least works - unlike the existing code in the repository, so it's an improvement.

hasenradball commented 6 months ago

@peterharperuk Hi Peter, I will check the DH11 Datasheet again, and check how we can combine these Sensors in one code.

Maybee I needed to look for an DHT11 Sensor, I thought these type of sensor are out of date and most replaced by an DHT22.

So I concentrated on the DHT22 one.

peterharperuk commented 6 months ago

No worries. It might be better to remove support if they're hard to get. Any way, a quick look at the data sheet suggests to me that we should be dividing by 256 rather than 10? If I do that the numbers look better

hasenradball commented 6 months ago

Can you put the sheet you have into this chat?

If it is as you said. 1) There is a need to detect the sensor type. or 2) Make two examples one for DHT11 one for DHT22

hasenradball commented 6 months ago

@peterharperuk Found this Datasheet for the DHT11: DHT11 datasheet But it is not clear for me now how to calc the decimal value out of the recieved integer data for DHT11. The example uses only the HIGH byte for the humidity and temperature.

And for the DHT22: DHT22 datasheet

Ahhh now I got it.

High byte is Integer data LOW byte is decimal data So dividing by 256 seems correct then.

peterharperuk commented 6 months ago

This is what I was looking at https://www.mouser.com/datasheet/2/758/DHT11-Technical-Data-Sheet-Translated-Version-1143054.pdf

hasenradball commented 6 months ago

@peterharperuk Hi Peter I had a look into the DHT11 datasheet which says that the Vcc of the Sensor is defined from

3.5…5.5 V

which makes the sensor not suitable for the Pi pico (3.3 V). So I would sugest to stay with the AM2302 Sensor which voltage range is 3.3…5.5 V.

hasenradball commented 6 months ago

@peterharperuk Hi Peter it seems some datasheets differ. I found some other where it says that the Vcc for the DHT11 starts at 3 V. Mhhhh

But in fact I am wondering that you got some data response with the code for the DHT22. Because with the start sequence by the mcu sleep_us(1200) I expected to get no response from the DHT11 sensor.

hasenradball commented 6 months ago

@peterharperuk Hi Peter, how to proceed now with the PR?

My proposal is we merge this code with respect it works only for AM2302. And if needed I will work on a mor sophisticated solution after christmas with can deal both sensors.

peterharperuk commented 6 months ago

Hi, yes this change is fine as it is. I might push a minor change to build for DHT11 as it seems to work for me at 3v. But you can leave me to do that. Thanks for your work!

hasenradball commented 6 months ago

@peterharperuk Thanks. So I won' t buy then an DHT11.

I checked the data sheet. For the DHT11 I only see two diffs compared to DHT22. 1) Start sequence pull low at least 18 ms instead 1 ms 2) Division factor for data it is 256 instead of 10.

You could make a check in the setup if a dht11 or a dht22 is connected. Or similar way…

have a nice christmas Frank

hasenradball commented 6 months ago

@peterharperuk By the way do you know if it is possible with the sdk to create a webserver code , but to save the html, css and js file in the flash, and load it from there when the webpage is accessed?

peterharperuk commented 6 months ago

I'm sure it's possible. LWIP has a webserver which stores its webpages in the binary itself (which of course lives in flash). I put an example together awhile ago https://github.com/peterharperuk/pico-examples/tree/add_httpd However it requires a native tool to convert the web pages into a source file that gets built into the binary. A more sophisticated version might store the files in a file system. But I doubt it would be trivial to implement.

hasenradball commented 6 months ago

@peterharperuk Thanks I will look into the example. :-) I meant to store the html, css, js itself in the flash. And not covert into a binary. I do it so in the ESP8266. It is easier for maintainment, just load up the same files you would use on a regular webserver.

lurch commented 6 months ago

I meant to store the html, css, js itself in the flash.

See https://github.com/raspberrypi/pico-sdk/issues/531 and https://github.com/raspberrypi/pico-sdk/issues/758

hasenradball commented 6 months ago

@lurch thanks for your hint.

But one question, if such things like fileystem and so on will be added. Isn't it necessary to switch to cpp an object oriented programming technics? To make things easier…

lurch commented 6 months ago

The Raspberry Pi Pico SDK already supports C++ https://www.raspberrypi.com/documentation/pico-sdk/index_doxygen.html

hasenradball commented 6 months ago

@lurch so if I would I could directly switch to C++, right? If I would have know this I had written the dht example with c++ clases.

lurch commented 6 months ago

@hasenradball This one also needs to be targeted against develop rather than master (sorry for not spotting that earlier!)

hasenradball commented 6 months ago

@lurch already switched.

Just a question, what I have to do to setup my own projects folder for the pico? Just make a new folder and copy the CMakeList.txt file?

lurch commented 6 months ago

Just a question, what I have to do to setup my own projects folder for the pico?

See Chapter 8 of https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf

hasenradball commented 6 months ago

@lurch one question,

Is there a SW Docu available for the Pico compared to this one? ESP82666 Docu

Frank

lurch commented 6 months ago

Is there a SW Docu available for the Pico compared to this one? ESP82666 Docu

The documentation for the Pico and RP2040 can be found at https://www.raspberrypi.com/documentation/microcontrollers/rp2040.html#documentation

The documentation for the C/C++ SDK can be found at https://www.raspberrypi.com/documentation/pico-sdk/

The documentation for the MicroPython port can be found at https://docs.micropython.org/en/latest/library/rp2.html

However the page that you linked to was for Arduino documentation, and that isn't something that Raspberry Pi officially supports, so you'll have to look elsewhere for that.

hasenradball commented 6 months ago

@lurch Thanks That what you send was what I searched for. Kind of function descriptions, which are available in the sdk.

hasenradball commented 5 months ago

@peterharperuk Thanks. So I won' t buy then an DHT11.

I checked the data sheet. For the DHT11 I only see two diffs compared to DHT22.

1. Start sequence pull low at least 18 ms instead 1 ms

2. Division factor for data it is 256 instead of 10.

You could make a check in the setup if a dht11 or a dht22 is connected. Or similar way…

have a nice christmas Frank @peterharperuk Hi Peter,

did you already have the addition to the dht for the DHT11 sensor? So I can test it, also.

Best regards Frank

hasenradball commented 2 months ago

@peterharperuk Reopened this PR to have an working dht-example.

hasenradball commented 4 days ago

@peterharperuk Hi Peter,

just a short question this example is for the Pico. If I want to use it on the Pico_w, does the gpio_put changed to cyw43_arch_gpio_put?

lurch commented 3 days ago

For controlling "regular" GPIO pins you should continue to use gpio_put. You only need to use cyw43_arch_gpio_put for controlling the PicoW's on-board LED (because on the PicoW the on-board LED is connected to the CYW43 chip, but on the Pico it's connected to the RP2040 chip). Compare https://github.com/raspberrypi/pico-examples/tree/master/blink and https://github.com/raspberrypi/pico-examples/tree/master/pico_w/wifi/blink .

hasenradball commented 3 days ago

@lurch Thank you for explanation. I saw the difference in the examples for the blink.

Is there any hint if there is the plan or interest on the PR? It is opened since a long time and only activity comming from my side it seems.

peterharperuk commented 3 days ago

Yes, we're interested in improved examples. It just needs time from someone to test and review it.