HTTP callback + VirtualHub for Web

HTTP callback + VirtualHub for Web

This week, we'll be talking about our PHP library, HTTP callbacks, and VirtualHub for Web. In particular, how to use the three together.

We've already written several articles on HTTP callbacks and the PHP library. As a reminder, this mode of operation makes it possible to create applications that can be accessed from anywhere on the Internet, freeing you from NAT problems. We have, for example, seen how to display the day's appointments on Yocto-MaxiDisplay or how to remotely control the heating of a chalet.

The HTTP callback mode and PHP library can be used to build almost any application, but this is not a turnkey solution. It's up to the customers to develop their own application.

A few months ago, we released a new tool, VirtualHub for Web, which enables remote access to modules. The functionalities are the same as for the native version of VirtualHub, with a few limitations. In concrete terms, VirtualHub for Web is an application we wrote using the PHP library and the HTTP callback mode.

Let's take a look at how to create a system that uses both VirtualHub for Web and a PHP application using an HTTP callback.

Using an HTTP callback and VirtualHub for Web


For technical reasons, YoctoHubs can only connect to a single HTTP callback. You therefore need to configure the YoctoHub to connect to the application's HTTP callback. Then use the YAPI::ForwardHTTPCallback function to hand over control to VirtualHub for Web.

The YAPI::ForwardHTTPCallback method is used to hand over control to VirtualHub for Web
The YAPI::ForwardHTTPCallback method is used to hand over control to VirtualHub for Web



The example in the post Using the HTTP callback mode in PHP, which switches a relay according to the ambient temperature, can be modified so that VirtualHub for Web can be used in parallel.

To do so, we need to add a call to the YAPI::ForwardHTTPCallback method, passing as argument the URL we would normally configure on the YoctoHub. For example, if our VirtualHub for Web is installed on http://www.example.com/VHu4Web/, the parameter is "http://www.example.com/VHu4Web/HTTPCallback".

Here's the HTTP callback code with the redirection to VirtualHub for Web.

<?php
include('Sources/yocto_api.php');
include('Sources/yocto_temperature.php');
include('Sources/yocto_relay.php');

YAPI::RegisterHub("callback");
YAPI::ForwardHTTPCallback("http://www.example.com/VHu4Web/HTTPCallback");

$t = YTemperature::FirstTemperature();
if (is_null($t)){
    die("No temperature sensor found");
}
$r = YRelay::FirstRelay();
if (is_null($r)) {
    die("No temperature sensor found");
}
$value = $t->get_currentValue();
if ($value&gt;25)
    $r-&gt;set_state(YRelay::STATE_B);
if ($value<24)
    $r->set_state(YRelay::STATE_A);

Print("temperature = $value\n");
YAPI::FreeAPI();
?&gt;



Note that the call to YAPI::ForwardHTTPCallback is executed before the part of the code that switches the relay according to the temperature. This means that the script code overrides any command sent by VirtualHub for Web. In this case, it is not possible to change the state of the relay via VirtualHub for Web.

If you want VirtualHub for Web to take priority, it must be called last:

<?php
include('Sources/yocto_api.php');
include('Sources/yocto_temperature.php');
include('Sources/yocto_relay.php');

YAPI::RegisterHub("callback");

$t = YTemperature::FirstTemperature();
if (is_null($t)){
    die("No temperature sensor found");
}
$r = YRelay::FirstRelay();
if (is_null($r)) {
    die("No temperature sensor found");
}
$value = $t->get_currentValue();
if ($value&gt;25)
    $r-&gt;set_state(YRelay::STATE_B);
if ($value<24)
    $r->set_state(YRelay::STATE_A);

Print("temperature = $value\n");
YAPI::ForwardHTTPCallback("http://www.example.com/VHu4Web/HTTPCallback");
YAPI::FreeAPI();



Both solutions are correct. Depending on the situation, you may prefer to give priority to VirtualHub for Web or to the PHP script.

MD5 signature


As the script retransmits the same data to VirtualHub for Web, the password used for the MD5 signature must be the same everywhere. YoctoHub, PHP script and VirtualHub for Web must all use the same MD5 signature.

The YoctoHub MD5 signature
The YoctoHub MD5 signature



....
YAPI::RegisterHub("md5:mypass@callback");
YAPI::ForwardHTTPCallback("http://www.example.com/VHu4Web/HTTPCallback");
...



The VirtualHub for Web MD5 signature must be identical
The VirtualHub for Web MD5 signature must be identical



HTTPS


The YAPI::ForwardHTTPCallback method supports the HTTPS protocol.

YAPI::ForwardHTTPCallback("https://www.example.com:443/VHu4Web/HTTPCallback");



However, PHP must allow outgoing connections and certificates must be configured. Depending on your installation and web server, you may need to modify your php.ini file.

Conclusion


As you've just seen, it's possible to use VirtualHub for Web alongside your PHP scripts. This is a very useful feature for modifying some module parameters remotely, without having to add code to the PHP script. In our example, this enables us to view the minimum and maximum temperatures without having to modify the code.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.