-
Notifications
You must be signed in to change notification settings - Fork 8
Description
I was frustrated by not easily finding Berry examples, and then found the discussion thread and hence to this repo.
So below is my first Berry script, which illustrates some of the simplest functions in use
Maybe enable the wiki on this repo, and encourage people to contribute there - easier than forking and PRing.
I found this example useful in understanding some of the basics.
###Things to highlight:
Repeatedly running code in the berry console is cumulative - especially and obviously adding tas rules :).
At least when developing, best to remove then add.... else you end up restarting a lot.
It's not obvious that the function added with add_cmd should end with a tas response to be sent (e.g. tasmota.resp_cmnd_done() ).
Else I got a cryptic error BRY: ERROR, bad json: OFF
# A simple Berry example.
# Operates an automatic pet feeder (an old CAT feeder)
# the purpose here is to respond to a commmand 'feed n'
# if n > 0, then a motor is started, and a counter set to n
# every time switch1 transitions to ON, a counter is decremented.
# when the counter reaches zero, the motor is turned off.
# in my case, I've used a spare hbridge to provide the motor drive, so setup relay1, relay2 as the hbridge controls.
# I've set switch1 as the switch operated by the feeder deliveing one portion of food.
# note the setting of swicth options from berry.
# note the REMOVAL of rules and cmnd before adding.
import json
import string
#import mqtt
#import persist
# define relays for motor hbridge
var forward = 0
var backward = 1
# a variable to count how many portions we have fed this time
var portion_counter = 0
def feed(val)
if val
# turn motor forwards if val != 0
tasmota.set_power(forward,true)
tasmota.set_power(backward,false)
portion_counter = val
print("feed", val)
else
# turn off motor
tasmota.set_power(forward,false)
tasmota.set_power(backward,false)
end
end
# this is called by using 'feed' in tas.
# note that it needs to respond... else you get a cryptic 'BRY: ERROR, bad json: OFF'
def feed_cmnd(cmd, idx, payload)
print("cmnd from tas:",cmd, idx, payload)
feed(int(payload))
tasmota.resp_cmnd_done()
end
# example of direct mqtt - we will just use a cmnd
# hence not tested.
#def feed_message(topic, idx, payload_s, payload_b)
# var feed_msg = json.load(payload_s)
#
# if feed_msg['state'] == "start"
# feed(4)
# else
# end
#end
#def subscribes()
# mqtt.subscribe("cmnd/feed/control",water_message)
#end
#tasmota.remove_rule("MQTT#Connected=1")
#tasmota.add_rule("MQTT#Connected=1", subscribes)
# this will be called from a rule in tas
def buttonTriggered()
portion_counter = portion_counter - 1
print("Switch activated")
if portion_counter <= 0
feed(0)
end
end
# set switch 1 to report state, not toggle
tasmota.cmd("SwitchMode1 1");
# disconnect switches from relays
tasmota.cmd("SetOption114 1");
# note:
# if using the tas berry console, best to remove the rules first.
# because the old ones remain, and berry gets called multiple times.
# call our function every time the switch report on
tasmota.remove_rule("Switch1#State=1")
tasmota.add_rule("Switch1#State=1", buttonTriggered)
# our feed command - use like "feed 4" to feed a 4 portion meal.
tasmota.remove_cmd('feed')
tasmota.add_cmd('feed', / cmd, idx, payload -> feed_cmnd(cmd, idx, payload))