A few months ago, we showed you how to use a Yoctopuce serial interface module to automatically handle a serial sensor of type TTL, SPI, RS232, or RS485. This week, we enrich this feature to enable you to perform arithmetic computations directly in the Yoctopuce module, in order to obtain values that you can use straight away with an HTTP callback for example.

This new feature will appear in your Yoctopuce modules after upgrading the firmware to version 35770 and later. You will need a recent VirtualHub as well. As we have added a new type of command in the autonomous tasks, you find this new capability in the "jobs" definition, in the module configuration panel. If you are not sure to know the job definition mechanism, start by reading this previous post on the topic.

When you define a custom protocol (*Use a custom protocol* in the interface) to decode values, up till now you had to assign the decoded values directly to one of the nine *genericSensor* available on the module. For example, to read the data of Luminox oxygen sensors, we used the following string:

expect "O ($1:FLOAT) T ($2:FLOAT) P ($3:FLOAT) % ($4:FLOAT).*"

which directly assigned the four measures to the first four *genericSensor*s.

From now on, you can store the decoded values into intermediary variables, that you can then combine if needed to perform additional computations:

expect "O ($ppO2:FLOAT) T ($temp:FLOAT) P ($press:FLOAT) % ($o2:FLOAT).*"

For example, you could convert the temperature into Fahrenheit with the formula

compute "$2 = 1.8 * temp + 32"

### Recognized expressions

You can also use much more complex arithmetic expressions. All the usual mathematic operators are available, with the following order of precedence:

** | raise to the power |

~ + - not | complement, unary plus/minus, logical not |

* / % // | multiply, divide, integer modulo, integer divide |

+ - | add, subtract |

>> << | bitwise shift right and left |

& | bitwise AND |

| ^ | bitwise OR, XOR |

< <= >= > | compare |

== <> != | test if equal or different |

and | logical AND |

or | logical OR |

For convenience, some alternative operator symbols can be used:

div mod | can be used instead of / and % |

! && || | can be used instead of not, and, or |

Comparison and logical operators are meant to be used with the conditional evaluation operator:

compute "(temp > 0 ? log(temp) : -9999)"

Standard mathematics constants and functions are available as well:

pi e | the universal constants |

cos sin tan | trigonometric functions |

acos asin atan atan2 | inverse trigonometric functions |

cosh sinh tanh | hyperbolic functions |

exp log log10 pow sqr sqrt | power and logarithmic functions |

ceil floor round frac fmod | rounding functions |

fabs min max isnan isinf | ranging functions |

### Arbitrary functions

If need be, typically to apply temperature correction factors, you can even define your own function by linear interpolation between defined points. You can for example define an fcorr function the following way:

-40 : 1.3 -30 : 1.22 -20 : 1.15 -10 : 1.08 -5 : 1.06 -3 : 1.04 -2 : 1.02 -1 : 1.01 0.1 : 1 0 : 0.99 10 : 0.2 20 : 0.02 30 : 0.002 40 : 2e-4 50 : 1.4e-5 60 : 0

In your protocol, you can then use it in a compute command as follows:

compute "$3 = ppO2 - fcorr(temp)"

### Conclusion

Thanks to this new possibility, your Yoctopuce serial interface modules are now able to directly extract the measures of your most exotic sensors, without having to rework them by software later on. This enables you to store the final results on the module data logger, and also to use them directly with a YoctoHub-Ethernet or a YoctoHub-GSM-3G for example, with an HTTP callback, MQTT, InfluxDB, and so on...