2019-10-26, 04:55 PM
I finally succeeded, this is the code I used to send NMEA0183 sentences to SignalK. Now I'm able to display speed in instrument panel.This is just the beginning, now I have to do the same thing with the wind direction, but now I have good hopes
Code:
from gpiozero import Button
import socket, pynmea2
import time
import math
W_Direction = 35 # To be sostitute with analog...
count = 0 # Counts how many half-rotations
radius_cm = 7.0 # Radius of your anemometer
wind_interval = 1 # How often (secs) to report speed
store_speeds = [] # Empty list
ADJUSTMENT = 1.18
CM_IN_A_ML = 185200.0
SECS_IN_AN_HOUR = 3600
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# Every half-rotation, add 1 to count:
def spin():
global count
count = count + 1
# Calculate wind speed given the time interval:
def calc_speed(time_sec):
global count
circumference_cm = (2 * math.pi) * radius_cm
rotations = count / 2.0
# Calculate distance travelled by a cup in mile
dist_ml = (circumference_cm * rotations) / CM_IN_A_ML
# Speed = distance / time
ml_per_sec = dist_ml / time_sec
ml_per_hour = ml_per_sec * SECS_IN_AN_HOUR
# Calculate speed
final_speed = ml_per_hour * ADJUSTMENT
return final_speed
def reset_wind():
global count
count = 0
wind_speed_sensor = Button(5)
wind_speed_sensor.when_activated = spin
# Loop to measure wind speed and report at wind_interval:
while True:
start_time = time.time()
while time.time() - start_time <= wind_interval:
reset_wind()
time.sleep(wind_interval)
final_speed = calc_speed(wind_interval)
# NMEA0183 sentences to localhost 10110 to allow data read with Openplotter:
mwv = pynmea2.MWV('O$', 'MWV', (str(round(W_Direction,1)),'R',str(round(final_speed,1)),'N','A'))
mwv1=str(mwv)
mwv2=mwv1+"\r\n"
time.sleep(0.051)
sock.sendto(mwv2, ('localhost', 10110))