One of the special features of Yoctopuce serial communication modules, such as the Yocto-I2C, the Yocto-RS232, and the Yocto-RS485-V2, is that they can autonomously execute small communication jobs, for example to query sensors. We have recently added the ability to synchronize these automatic exchanges to precise time constraints.
Until now, automated exchanges managed by jobs could be triggered by
- the arrival of an external message (reactive job, expect command)
- the expiry of a deadline (periodic job, wait command)
We have recently come across two circumstances where it is desirable to trigger communication not on the basis of a delay or relative periodicity, but on an absolute time basis, in relation to the actual time.
Reading cumulative counters
The first scenario concerns the reading of a sensor that measures an accumulation over time, such as an energy meter or a precipitation meter. If the accumulated value is to be converted into a quantity per hour or per day, it must be possible to ensure that the sensor is queried at specific times.
We have therefore added two functions that enable you to include absolute time constraints in expressions recognized by the job language: utctime() and utcdate(). The utctime() function returns the number of seconds elapsed since midnight UTC, while the utcdate() function returns the number of days elapsed since January 1, 1970. Used in an assert command, these functions ensure that a task only runs when a specific time condition is met. For example, the following task - which sends the time in text form to the serial port - is only executed at each full hour:
assert utctime() mod 3600 == 0 compute h = utctime() div 3600 writeVar "It is ($h:INT)h" writeLine ""
This technique can therefore be used to periodically read the accumulation counter before resetting it to zero, or alternatively to read the new value, compute the difference with the previous value, and store the new value. In the latter case, however, you must provide a mechanism which nevertheless periodically resets the counter to zero to avoid overflow problems.
Synchronization with measure transmission
The second situation requiring synchronization with a real-time clock is the transmission of data at fixed intervals. If, for example, you have configured a YoctoHub-Ethernet or your VirtualHub to transmit data to a web server via an HTTP callback every quarter-hour, you may want to read out recent measures just before the HTTP callback occurs. The synchronization method used previously is therefore also suitable for this case, with an expression of the type:
assert utctime() mod 900 == 890
A similar problem can arise when values are read several times a second and transmitted via the periodic callback mechanism - via MQTT, for example. In this case, it might be best to synchronize the reading of measures with the sending of periodic notifications, to ensure that each value read is transmitted. For this purpose, there is a new sync command, similar to the wait command, but which waits until the next multiple of the specified period in milliseconds has passed, before proceeding to the next command. For example, if genericSensor1 is configured to send 10 values per second, the following script ensures that each value read has the opportunity to be transmitted by periodic notification.
sync 100 writeLine "?" expect "($1:FLOAT)" sync 100 writeLine "?" expect "($1:FLOAT)" sync 100
Availability
These new functions are now available for all serial communication modules. Simply update your module's firmware to take advantage of them.