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

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

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

hasenradball
Copy link

@hasenradball hasenradball commented Nov 29, 2023

Refracture dht.c Example

  • improve functionality
  • add constants for:
    • Timeout Error
    • Checksum Error
    • Read OK

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

- refracture dht.c example
- improve functionality
- fix for negative temperature detection
@hasenradball
Copy link
Author

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

@lurch
Copy link
Contributor

lurch commented Dec 9, 2023

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 😉

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

@hasenradball
Copy link
Author

hasenradball commented Dec 9, 2023

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

lurch commented Dec 9, 2023

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

- correction into c style initialisation (line 118)
@hasenradball
Copy link
Author

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

Added test for negative Temperatures...

20231211_16h31m16s_grim

@peterharperuk
Copy link
Contributor

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

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

hasenradball commented Dec 15, 2023

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

Yes, DHT22 woks nicely!

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

@peterharperuk
Copy link
Contributor

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

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

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

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

hasenradball commented Dec 15, 2023

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

@hasenradball
Copy link
Author

hasenradball commented Dec 15, 2023

@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.

- add power up time for sensor to reach stable state
@hasenradball
Copy link
Author

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

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

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

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

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

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

hasenradball commented Dec 18, 2023

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

lurch commented Dec 18, 2023

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

See raspberrypi/pico-sdk#531 and raspberrypi/pico-sdk#758

@hasenradball
Copy link
Author

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

lurch commented Dec 18, 2023

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

@hasenradball
Copy link
Author

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

lurch commented Dec 19, 2023

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

@hasenradball hasenradball changed the base branch from master to develop December 19, 2023 08:05
@hasenradball
Copy link
Author

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

lurch commented Dec 19, 2023

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

@lurch one question,

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

Frank

@lurch
Copy link
Contributor

lurch commented Dec 28, 2023

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

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

@hasenradball
Copy link
Author

@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 hasenradball reopened this Apr 25, 2024
@hasenradball
Copy link
Author

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

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

Successfully merging this pull request may close these issues.

None yet

3 participants