LED blinking and DHT22 Sensor Codes

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

LED blinking and DHT22 Sensor Codes

Post by kpin404 » Mon Aug 02, 2021 5:13 am

I am learning Micropython codes on ESP32. I have learnt to code ESP32 how to blink an LED and measure Temperature and Humidity with DHT22 sensor with simple Micropython codes. Now i would like to combine these separate codes to blink an LED and measure sensor data at the same time within a code. I have tried to combine these code, but didn't work and it still runs one type of code at a time. Please assist further to learn to make it work at the same time. Thanking you.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 6:32 am

Using the code tags post the code you have. Are you trying to use uasyncio?

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: LED blinking and DHT22 Sensor Codes

Post by pythoncoder » Mon Aug 02, 2021 7:19 am

The best way to achieve concurrency is to use uasyncio. In addition to the official docs I have written a tutorial which you may find helpful. There is a significant learning curve, but it's well worthwhile.
Peter Hinch
Index to my micropython libraries.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Mon Aug 02, 2021 7:37 am

davef wrote:
Mon Aug 02, 2021 6:32 am
Using the code tags post the code you have. Are you trying to use uasyncio?
Thank you for keep supporting. The code i am using:

Code: Select all

from machine import Pin
from dht import DHT22
from time import sleep

led = Pin(2, Pin.OUT)
sensor = dht.DHT22(Pin(14)) 
while True:
	sensor.measure()
	print('Temperature = %.2f' % sensor.temperature())
	print('Humidity = %.2f' % sensor.humidity())
	sleep(3)
	
	led.on()
	sleep(0.5)
	led.off()
	sleep(0.5)
	
Sorry, i have no idea about uasyncio? Thanking you.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Mon Aug 02, 2021 7:55 am

pythoncoder wrote:
Mon Aug 02, 2021 7:19 am
The best way to achieve concurrency is to use uasyncio. In addition to the official docs I have written a tutorial which you may find helpful. There is a significant learning curve, but it's well worthwhile.
Thank you for keep assisting. Gone through your provided "uasyncio" tutorial and found to be quite different from Micropython programming codes. This seems to start-off to learn with a different from Micropython programming again? I am confused. Please let me clear about this. Thanks.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: LED blinking and DHT22 Sensor Codes

Post by pythoncoder » Mon Aug 02, 2021 8:03 am

It's all standard Python. A key design aim of Damien's rewrite of uasyncio is compatibility with Python 3. Asynchronous coding requires learning some additional Python syntax and understanding the concepts of coroutines and tasks.

I wrote the tutorial because I found most of the information on the internet confusing, not least because Python's asyncio has changed over the years. Aside from one or two specialist examples which I identify, the code samples in my tutorial will run on Python 3 or on MicroPython.
Peter Hinch
Index to my micropython libraries.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 8:33 am

I suggest to first try to understand why your simple bit of coding doesn't work.

Does anything get printed out in repl when you use rshell?
Does the LED blink?

Comment out the lines:

Code: Select all

	sensor.measure()
	print('Temperature = %.2f' % sensor.temperature())
	print('Humidity = %.2f' % sensor.humidity())
	sleep(3)
Does the LED blink?

Then move on to concurrency ... if that is really what you need.

What do you mean by "at the same time"?

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 9:02 am

These are usually quite useful.
https://randomnerdtutorials.com/esp32-e ... ty-sensor/
I think you are missing one or two statements, check out the code sample.

kpin404
Posts: 13
Joined: Mon Jun 21, 2021 11:56 am

Re: LED blinking and DHT22 Sensor Codes

Post by kpin404 » Mon Aug 02, 2021 9:03 am

davef wrote:
Mon Aug 02, 2021 8:33 am
I suggest to first try to understand why your simple bit of coding doesn't work.

Does anything get printed out in repl when you use rshell?
Does the LED blink?

Comment out the lines:

Code: Select all

	sensor.measure()
	print('Temperature = %.2f' % sensor.temperature())
	print('Humidity = %.2f' % sensor.humidity())
	sleep(3)
Does the LED blink?

Then move on to concurrency ... if that is really what you need.

What do you mean by "at the same time"?
Thanks for keep assisting.

LED doesn't blinking, but printed out temperature and humidity values in the repl window.
>>>
Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
Temperature = 25.20
Humidity = 86.80
Temperature = 25.20
Humidity = 87.30
I mean "at the same time" means that as the simple code to blink an LED runs on at a time and the only code for to measure the temperature and humidity runs one at a time , once put these code blocks together and execute they must run the same way as if they run when run once at a time separately i.e. blinking an LED and printing the measured temperature and humidity values together. I hope you got it. Thanking you.

davef
Posts: 811
Joined: Thu Apr 30, 2020 1:03 am
Location: Christchurch, NZ

Re: LED blinking and DHT22 Sensor Codes

Post by davef » Mon Aug 02, 2021 9:29 am

Firstly, sorry I see you do call sensor.temperature() and sensor.humidity()

It looks you have another file called main.py. Better post that as I don't understand:

Code: Select all

Ready to download this file,please wait!
...
download ok
exec(open('./main.py').read(),globals())
I understand that you want both of these to execute in the one loop, just one after another.

Post Reply