Wind Speed?

Showroom for MicroPython related hardware projects.
Target audience: Users wanting to show off their project!
Post Reply
ZooKeeper
Posts: 4
Joined: Sun Feb 21, 2021 3:44 am

Wind Speed?

Post by ZooKeeper » Sun Feb 21, 2021 4:17 am

Excuse the potentially ignorant first post, been lurking/reading for a couple months.

I have a working Python script for determining wind speed, my problem is that I cannot translate it into a working uPython script. I remember having similar problems when I began the Python project about 10 years ago, but ultimately was able to work it out. I have found examples of code that count button presses, but have not been able to translate that into anything useful.

If someone can point me in the right direction, I can to the lifting. Either modifying what I had or maybe measuring pulse time to get RPM (so far also unsuccessful)?

Below is the Python code that works on the Rpi, but not with MicroPython. Board is an ESP8266 (ESP-WROOM-02U).

Input is wired between 3v3 & Pin13 w/4k7 pull-down and the button is a momentary reed switch.

Descriptively: Pin13 is tied low via 4k7, until the reed switch closes the connection to 3v3 at which time I get a high pulse. I verified this and can toggle the value() between 1 & 0 with rotation.

TIA!

Code: Select all

#BEGIN - Wind Speed in MPH
#
#   DOES NOT WORK on uPython :(

from machine import Pin
from time import sleep
import math
calibration = 1.18 #0.765 #assumes WS.py is accurate and duplicates reading in MPH
count = 0
interval = 0.5	# How often to report speed 5 > 0.5
WS = 0
miles_per_hour = 0


def spin():
    global count
    count = count + 1

wind_speed_sensor = Pin(13) #Input on Pin#13
    
def main():
    sleep(interval)
    WS1 = calibration * (count/interval)
    WS2 = WS1 * 1.609344 
    #print "       Wind Speed            =    %.2f" % WS2, "/ %.2f" % WS1
    print("Wind Speed = %.2f") %WS1
main()
JohnG

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

Re: Wind Speed?

Post by pythoncoder » Sun Feb 21, 2021 8:49 am

Aside from the fact that this line is clearly wrong

Code: Select all

    print("Wind Speed = %.2f") %WS1
I can't see how this code is supposed to work. You have declared a pin

Code: Select all

wind_speed_sensor
but nowhere do you actually use it.

There are two issues you need to grasp to get this working, namely contact bounce and concurrency. The application needs two concurrent tasks. One waits on pulses, debounces the contact, and times the interval between closures. The second periodically reads the time interval and performs any calibration, averaging or other processing, reporting the result.

I suggest you read up on uasyncio which is the library which supports concurrency. Official docs are here. There is a tutorial here. The latter library includes a class for switch debouncing, a subject explained here.
Peter Hinch

ZooKeeper
Posts: 4
Joined: Sun Feb 21, 2021 3:44 am

Re: Wind Speed?

Post by ZooKeeper » Sun Feb 21, 2021 3:29 pm

Thanks!! Now off to do some reading....

Admittedly the code first posted will not work, I copied the wrong one (this was a trial on the micro), see below to the correct working code on the Rpi and apologies for the confusion. My coding prowess (read: infancy) is quite clearly on display :(

Code: Select all

#!/usr/bin/env python 2.7.9
#
#	BEGIN - Wind Speed in MPH
#
# 27-Aug-17 @ 14:10, works
# FINAL: 29-Dec-18 @ 17.18 = Import ready!
#
from gpiozero import DigitalInputDevice
from time import sleep
import math
calibration = 1.18 #0.765 #assumes WS.py is accurate and duplicates reading in MPH
count = 0
interval = 0.5	# How often to report speed 5 > 0.5
WS = 0
miles_per_hour = 0

def spin():
    global count
    count = count + 1

wind_speed_sensor = DigitalInputDevice(21) #Input on Digital Pin#21
wind_speed_sensor.when_activated = spin   
    
def main():
    sleep(interval)
    WS1 = calibration * (count/interval)
    WS2 = WS1 * 1.609344 
    print "       Wind Speed            =    %.2f" % WS2, "/ %.2f" % WS1

#print spacing adjusted for file export 29-Dec18 @ 16:30
#if __name__ == "__main__":
main()

ZooKeeper
Posts: 4
Joined: Sun Feb 21, 2021 3:44 am

Re: Wind Speed?

Post by ZooKeeper » Mon Feb 22, 2021 5:00 pm

pythoncoder wrote:
Sun Feb 21, 2021 8:49 am
I suggest you read up on uasyncio which is the library which supports concurrency. Official docs are here. There is a tutorial here. The latter library includes a class for switch debouncing, a subject explained here.
Reading complete, understanding not. I did have debounce problems with the Rpi code at one point, but not with that posted immediately above. Am aware it might become an issue I need to correct, thus far it is not.

Obviously my code is very crude, the confusing part for this n00b is how to bridge the differences between the two.

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

Re: Wind Speed?

Post by pythoncoder » Mon Feb 22, 2021 6:11 pm

I had something like this in mind. This assumes you've installed the Switch primitive as per the docs:

Code: Select all

from machine import Pin
import uasyncio as asyncio
from primitives.switch import Switch
from time import ticks_diff, ticks_ms

tstart = None
delta = None

# Callback on debounced switch closure
def timeit():
    global tstart, delta
    if tstart is not None:
        delta = ticks_diff(ticks_ms(), tstart)
    tstart = ticks_ms()

async def report():
    while True:
    	if delta is not None:
    	    # apply scaling
    	    print(delta)  # To test, just print ms
    	await asyncio.sleep(1)  # Report once per second

pin = Pin('X1', Pin.IN, Pin.PULL_UP)  # Hardware: assumes switch to gnd
sw = Switch(pin)
sw.close_func(timeit())  # Associate callback with switch contact closure
try:
    asyncio.run(report())  # Run main application code
finally:
    asyncio.new_event_loop()
This is off the top of my head and untested. It will need adapting for your hardware, and after testing for scaling.

The case of zero wind speed also needs some thought, whatever solution you adopt. This quick hack will keep reporting the last nonzero speed it encountered...
Peter Hinch

ZooKeeper
Posts: 4
Joined: Sun Feb 21, 2021 3:44 am

Re: Wind Speed?

Post by ZooKeeper » Tue Feb 23, 2021 7:35 pm

pythoncoder wrote:
Mon Feb 22, 2021 6:11 pm
I had something like this in mind. This assumes you've installed the Switch primitive as per the docs:

This is off the top of my head and untested. It will need adapting for your hardware, and after testing for scaling.

The case of zero wind speed also needs some thought, whatever solution you adopt. This quick hack will keep reporting the last nonzero speed it encountered...
Thanks! More reading :D I found a heartbeat monitor sample code, did not get that working either, apparently I have a longer learning path that initially thought :(

Post Reply