clear all

%% load csv file
[FileName,PathName,FilterIndex] = uigetfile('*.csv','CSV data file','test.csv');
data = load(FileName);

%% discard first measures
[idx,dummy] = find(data == 65535);
start = max(idx)+1;


%% read raw measures
temperatureR = data(start:end,1);
humidityR = data(start:end,2);
activeRadiationR = data(start:end,3);
totalRadiationR = data(start:end,4);
internalTemperatureR = data(start:end,5);
internalVoltageR = data(start:end,6);

%% make human readable format (as from Moteiv faq)
temperature = -39.60 + 0.01*temperatureR;                                    % [°C]
humidity = -4 + 0.0405*humidityR + (-2.8 * 10^-6)*(humidityR.^2);            % [%] ?
humidity_true = (temperature - 25) .* (0.01 + 0.00008*humidityR) + humidity; % [%] ?
activeRadiation = 0.625 * 1e6 * (activeRadiationR/100000) * 1000;            % [lux]
totalRadiation = 0.769 * 1e5 * (totalRadiationR/100000) * 1000;              % [lux]
internalTemperature = (internalTemperatureR - 0.986)/0.00355;                % [°C]
internalVoltage = internalVoltageR/4096 * 3;                                 % [V]

%% plots
fh = 20;
figure(fh+1)
plot(temperature)
title('temperature')
figure(fh+2)
plot([humidity humidity_true])
title('humidity')
legend('humidity','humidity true')
figure(fh+3)
plot([activeRadiation totalRadiation])
title('radiation')
legend('active radiation','total radiation')
figure(fh+4)
plot(internalVoltage)
title('voltage')
