Example: statistics for meteorological station

In this example we will add statistics to the meteo station project. You must have completed that tutorial before starting this one.
We will create new Oficloud channels in which we will publish these statistics. We will make use of the Oficloud Server API features to build hourly,dayly,monthly and yearly statistics for all sensor data.
These statistics will be calculated locally, therefore we will not break the requirement to retain data as private.

STEPS

1. Create new channels
You have to create a new channel in your Oficloud account for every statistics you want to publish: a channel for hourly data, a channel for dayly data, a channel for monthly data and a channel for yearly data.

2. Add code to script
Edit the main.js script at your project folder and add the code for generating statistics. We will make use of the datalogger class to save data and build statistics automatically. See here documentation for the datalogger class. Initialize an object:
                                
var mylog= new datalogger("meteo",on_new_hour,on_new_day,on_new_month,on_new_year);

                                
                            
Then, add data to the logger when it is available:
                                
mylog.add(today0,{"date":today0,"temp":temperature,"hum":humidity,"rain":state});                                    
                                
                            
You will receive notifications when new data for a period is available. Use it to calculate mean and max values and send these new values to the appropiate Oficloud channel:
                                    
function on_new_hour(data_array)
{
    var t=0,h=0,r=0;
    var maxt=0,mint=9999;
    var maxh=0,minh=9999;
    var max_date=new Date("1900/01/01");
    for(var i=0;i<data_array.length;i++)
    {
        var date=new Date(data_array[i].date);
        if(date>max_date)
            max_date=date;
        if(data_array[i].temp>maxt)
            maxt=data_array[i].temp;
        if(data_array[i].hum>maxh)
            maxh=data_array[i].hum;
        if(data_array[i].temp<mint)
            mint=data_array[i].temp;
        if(data_array[i].hum<minh)
            minh=data_array[i].hum;
        t+=data_array[i].temp;
        h+=data_array[i].hum;
        if(data_array[i].rain=="rain")
            r+=1;
    }
    var mt=t/data_array.length;
    var mh=h/data_array.length;
    var mr=r/data_array.length;

    var today0 = new Date();
    var today=dateFormat(today0, "yyyy-mm-dd H:MM:ss");
    var s="{\"date\":\""+today+"\",\"mean_rain\":"+mr.toFixed(1)+",\"temperature\":"+mt.toFixed(1)+",\"humidity\":"+mh.toFixed(1)+"}";
    oficloud.send_post('p_day',s,function(){
        
    });

    return({"date":max_date,"temp":mt,"hum":mh,"rain":mr,"max_temp":maxt,"min_temp":mint,"max_hum":maxh,"min_hum":minh});
}
                               
                                    
                                

3.Download full project
You can clone a full project with mean and max values for temperature, humidity and rain state every hour, day, month and year.
$ git clone https://github.com/juanjo75es/oficloud-sdk-meteo-adv