clear all;
close all;
clc;
instrreset;
regulateToDayTemp = 22;
regulateToNightTemp = 20.5;
regulateTempHyst = 0.5; 
simstep = 0.01;
simwait = 0.05;
simHours = 48;
getTimeout = 1;
outdoorAVGTemp = 8;
sl_househeat_data;
s1 = serial('COM2', 'BaudRate', 14400);
fopen(s1);
sdata = [16;48;1;0;0];
sdata(6) = crcCheck(sdata);
fwrite(s1,sdata);
while(s1.BytesAvailable == 0)

end
rdata = fread(s1,6);
if crcCheck(rdata(1:5)) == rdata(6) && rdata(3) == 2 
sdata(1:5) = [16;48;15;32;11];
sdata(6) = crcCheck(sdata(1:5));
sdata(7) = 17;
sdata(8:9) = convertValueToData(regulateToDayTemp);
sdata(10) = 18;
sdata(11:12) = convertValueToData(regulateToNightTemp);
sdata(13) = 19;
sdata(14:15) = convertValueToData(regulateTempHyst);
sdata(16) = 27;
sdata(17) = 0;
sdata(18) = crcCheck(sdata(7:17));
    fwrite(s1,sdata);
    while(s1.BytesAvailable == 0)

    end
    rdata = fread(s1,6);
    if crcCheck(rdata(1:5)) == rdata(6) && rdata(3) == 2 
        fclose(s1);
        try   
            [~, ~, ~] = sim('sl_househeat');
        catch

        end

    end
end

sdata = [16;48;15;32;8];
sdata(6) = crcCheck(sdata);
sdata(7) = 31;
time = fix(clock);
timeMessage(1) = time(1) - 2000;
timeMessage(2:3) = time(2:3);
if weekday(datenum(time)) == 1
    timeMessage(4) = 7;
else
    timeMessage(4) = weekday(datenum(time)) - 1;
end
timeMessage(5:7) = time(4:6);
sdata(8:14) = timeMessage;
sdata(15) = crcCheck(sdata(7:14));
fopen(s1);
fwrite(s1,sdata);
[~] = fread(s1,6);
sdata = [16;48;10;0;0];
sdata(6) = crcCheck(sdata);
fwrite(s1,sdata);
[~] = fread(s1,6);
fclose(s1);
    