Hello,
I couldn’t find anything in the python timer examples in the library, so I’m posting my code here with my questions:
I am developing a car geofenced alarm system in which I’d like to use blynk timers tp handle the checking and tripping events. Basically if I toggle on the alarm through virtual pin write, a timer periodically checks if the newest vehicle location is X distance away from the saved vehicle location. If that that condition is met, then a new timer is started which streams GPS data online. The alarm does a number of other things, but I’ve removed those features from this code to keep this post simple. I am having trouble passing variables through to timer functions, as well as starting and stopping timers within other timers or functions.
Hardware/Software:
Raspberry Pi Zero W over Wifi - Blynk Local Server installed
Iphone 12 Pro
Latest Python Library (0.2.6)
Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.
### Alarm system handling ###
@blynk.handle_event('write V1')
def alarm(pin,value):
print('Alarm Status: {}'.format(int(value[0])))
if int(value[0]) == 1:
### gather/write GPS vehicle data ###
vehicle_lat = gather_gps_data()[0]
vehicle_lon = gather_gps_data()[1]
blynk.virtual_write(25, vehicle_lat)
blynk.virtual_write(26, vehicle_lon)
vehicle_location = (vehicle_lat, vehicle_lon)
### start geofence alarm ###
timer.start('run_geofence_alarm(vehicle_location)')
else:
timer.stop('run_geofence_alarm')
timer.stop('stream_gps')
### Alarm Timers ###
@timer.register(vpin_num = 103, interval = 10, stopped = True)
def run_geofence_alarm(vpin_num = 103, vehicle_location):
current_location = (gather_gps_data()[0], gather_gps_data()[0])
displacement = distance.distance(vehicle_location, current_location).km
alarm_trigger = .02
if displacement > alarm_trigger:
timer.start('stream_gps()')
timer.stop('run_geofence_alarm()')
@timer.register(vpin_num = 104, interval = 10, stopped = True)
def stream_gps(vpin_num = 104):
print('Streaming data to InitialState.com')
streamer.log("Location", "{lat},{lon}".format(lat=gather_gps_data()[0],lon=gather_gps_data()[1]))
streamer.log("speed",gather_gps_data()[3])
````Preformatted text`