How to control a connected object with a smart contract?
write the on/off state of the object in the smart contract
make the connected object repeatedly read its state from the smart contract
Indeed the object must be able to actively read the contract storage on a regular basis because the blockchain is passive by nature.
Werenode is a french startup that leverages the power of the Tezos blockchain in order to simplify and reduce the cost of the electric vehicle charging process. Werenode is currently developing a Tezos-based solution to switch on the electric vehicle supply equipment (EVSE).
The basic idea is to deploy a smart contract that pilots the state of the EVSE:
The smart contract should provide an entry point start
which sets the status on. Of course, this should be done if the sufficient amount of tezis is transferred to the contract for the EVSE owner to buy the electricity and pay the maintenance of the equipment.
Rather than a boolean state, the state should be materialized as a date: if this date is in the future the object is on; it is off otherwise.
Basically the start entry converts the transferred tezis to a duration (according to a rate) and the duration to a date in the future.
Say for example the rate is 1.2, meaning the EVSE is switched on for 1h12m for each tezis transferred.
The following code implements this switch:
archetype switchvariable owner : address = @tz1XZ7s6uStC2hZVpPQhXgcdXPwxifByF3Aovariable interruption : date = 2020-06-18variable rate : rational = 1.2 // in time_unit / tez_unitvariable time_unit : duration = 1hvariable tez_unit : tez = 1tzvariable user : option<address> = nonevariable read_interval : duration = 5s// UTILSfunction get_rate_in_s_by_utz () : rational {var d : int = time_unit;var t : int = tez_unit;return (rate * d / t)}function get_return_tz () : tez {var res : int = 1 / get_rate_in_s_by_utz() * (interruption - now);return (res * 1utz)}// ENTRIESentry start () {require {r1: now > interruption;}effect {var t : int = transferred;var dur : duration = (get_rate_in_s_by_utz() * t)*1s;if dur > read_interval then (interruption := now + dur + read_interval;user := some(caller))}}entry interrupt () {require {r2: caller = opt_get(user) and now < interruption}effect {transfer (get_return_tz()) to caller;interruption := now - read_interval;}}entry collect () {called by ownereffect {var keep = 0tz;if now < interruption thenkeep := get_return_tz();if balance - keep > 0tz thentransfer (balance - keep) to owner}}entry setunits (dunit : duration, tunit : tez) {called by ownereffect {time_unit := dunit;tez_unit := tunit;}}
The contract is available on the Carthage net to play with:
This connected online bulb listens to this contract ... Feel free to switch it on and off.