john30 / ebusd

daemon for communication with eBUS heating systems
GNU General Public License v3.0
562 stars 130 forks source link

Ajax Request to poll data from ebusd #313

Closed joergensen70 closed 4 years ago

joergensen70 commented 4 years ago

Hi, I'm running an ebusd daemon successfully on my RaspberyPi Debian. The http request works fine and I can access RaspberryIP:8889/data successfully using http request. All available values show up. I don't want to change the index.html on my device but instead create my own small Web Page just showing the most important Values of my Heating System (Vaillant flexoTherm). The idea is to have a small Web page which is polling every second with an Ajax Request. I'm almost brand new in this topic and don't have too much background (yet) of Ajax, JavaScript and JSON - but I'm learning. I try to access the data by using this code. Can somebody help me how the correct request needs to look? I want to test my WebPage on my external Computer (So probably I have to poll the IP Address of the Raspberry) and later the page should run in the html folder of ebusd (I assume I have to use 'localhost' in this case):

var pollingId = window.setInterval(pollServer, 1000);
function pollServer() 
{
    getData();
}

pollServer();

function getData() 
{
    // var http_request = new XMLHttpRequest();
    var http_request = new XMLHttpRequest('/data/');
    if(http_request) 
    {
        http_request.onreadystatechange = statechange();
    } 
    else 
    {
        alert("XMLHttpRequest failed");
    }
    function statechange()
    {
        if(http_request.readyState == 4)
        {
            if(http_request.status == 200)
            {
                process( JSON.parse(http_request.responseText));
            }
        }
    }   
    // http_request.open("GET", "192.168.4.212:8889/data/700/HwcTempDesired", true);
    http_request.open("GET", "192.168.4.212:8889", true);
    http_request.send();
}

Above are examples I tried so far to read all JSON data first of all, or just the HwcTempDesired. I'm even not getting into the readyState == 4.

Ideally I would like later to write values using this web page but I think this is getting difficult or can I do that somehow (I read that write will just work with telnet). Your help would be appreciated. Joerg

joergensen70 commented 4 years ago

Hi, writing a comment to my own topic since I made very good progress to create my own little web-page to just show the essential Vaillant flexoTherm datapoints. Not sure if I can post the entire code (400 lines) for others who try similar.

Still struggling with some things:

1) The http request doesn't get new data for many datapoints and I didn't find a way to force that the values will come from the bus everytime. This is what I try with my request. I tried to use maxage but it doesn't seem to work.

function getData()
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "/data?maxage=1&required");
    xhr.responseType = 'json';
    xhr.send();
    xhr.onload = function()
    {
        var responseObj = xhr.response;
        displayData(responseObj);
    }
}

2) Many datapoints are still missing (e.g. statistic values) but will try next to get them somehow

If somebody has an idea to force reading the data from the bus through http request I would appreciate it. Another still open question is if there is really no way to use the POST command to set values through http.