Difference between revisions of "Radio Thermostat"

From LinuxMCE
Jump to: navigation, search
Line 31: Line 31:
 
[[User:Techstyle|Techstyle]] #193 off:
 
[[User:Techstyle|Techstyle]] #193 off:
  
data = "{\"tmode\":" # Current mode of HVAC operation
+
data = "{\"tmode\":" # Current mode of HVAC operation
data+="0}" # Off
+
data+="0}" # Off
log("tmode set data: " + data)
+
log("tmode set data: " + data)
tstat_set(data)
+
tstat_set(data)
  
 
[[User:Techstyle|Techstyle]] #192 on:
 
[[User:Techstyle|Techstyle]] #192 on:
  
data+="3}" # Auto
+
data+="3}" # Auto
log("tmode set data: " + data)
+
log("tmode set data: " + data)
tstat_set(data)
+
tstat_set(data)
  
 
[[User:Techstyle|Techstyle]] #279 set fan:
 
[[User:Techstyle|Techstyle]] #279 set fan:
data = "{\"fmode\":"
+
data = "{\"fmode\":"
if onoff == "1"
+
if onoff == "1"
  data+="2}" # Fan is always on
+
  data+="2}" # Fan is always on
else
+
else
  data+="0}" # Auto
+
  data+="0}" # Auto
end
+
end
 
+
log("fmode set data: " + data)
+
log("fmode set data: " + data)
tstat_set(data)
+
tstat_set(data)
  
 
[[User:Techstyle|Techstyle]] #280 set heat/cool:
 
[[User:Techstyle|Techstyle]] #280 set heat/cool:
  
data = "{\"tmode\":"
+
data = "{\"tmode\":"
if onoff == "H"
+
if onoff == "H"
  data+="1" # Heat only
+
  data+="1" # Heat only
elsif onoff == "C"
+
elsif onoff == "C"
  data+="2" # Cool only
+
  data+="2" # Cool only
else
+
else
  data+="3" # Auto
+
  data+="3" # Auto
end
+
end
 
+
data+="}"
+
data+="}"
 
+
log("tmode set data: " + data)
+
log("tmode set data: " + data)
 
+
tstat_set(data)
+
tstat_set(data)
  
 
[[User:Techstyle|Techstyle]] # 278 set temperature:
 
[[User:Techstyle|Techstyle]] # 278 set temperature:
  
if ((value_to_assign[0,1]=="+") || (value_to_assign[0,1]=="-"))
+
if ((value_to_assign[0,1]=="+") || (value_to_assign[0,1]=="-"))
  # The new setpoint is relative to the current setpoint
+
  # The new setpoint is relative to the current setpoint
  new_temp = celcius_to_farhenheit(Float(@setpoint)).round + value_to_assign.to_i
+
  new_temp = celcius_to_farhenheit(Float(@setpoint)).round + value_to_assign.to_i
else
+
else
  # The new setpoint is absolute
+
  # The new setpoint is absolute
  new_temp = celcius_to_farhenheit(Float(value_to_assign.to_i)).round
+
  new_temp = celcius_to_farhenheit(Float(value_to_assign.to_i)).round
end
+
end
 
+
# to set the temp, we must know the mode
+
# to set the temp, we must know the mode
case $tstathash["tmode"].to_s
+
case $tstathash["tmode"].to_s
  when "1"
+
  when "1"
    data = "{\"t_heat\":" + new_temp.to_s  + "}"
+
    data = "{\"t_heat\":" + new_temp.to_s  + "}"
  when "2"
+
  when "2"
    data = "{\"t_cool\":" + new_temp.to_s  + "}"
+
    data = "{\"t_cool\":" + new_temp.to_s  + "}"
end
+
end
log("Set temp data: " + data)
+
log("Set temp data: " + data)
tstat_set(data)
+
tstat_set(data)
  
 
[[User:Techstyle|Techstyle]] #373 Private method listing:
 
[[User:Techstyle|Techstyle]] #373 Private method listing:
  
# Helper function for logging
+
# Helper function for logging
def log(word)
+
def log(word)
  $logFile.print(word + "\n")
+
  $logFile.print(word + "\n")
  $logFile.flush()
+
  $logFile.flush()
end
+
end
 
+
def get()
+
def get()
 
+
  # Reconnect to the device
+
  # Reconnect to the device
  conn_.Reconnect()
+
  conn_.Reconnect()
+
  # Send the request
+
  # Send the request
  s = "GET /tstat HTTP/1.1\r\n"
+
  s = "GET /tstat HTTP/1.1\r\n"
  s += "\r\n"
+
  s += "\r\n"
  conn_.Send(s)
+
  conn_.Send(s)
 
+
  # Wait for a reply
+
  # Wait for a reply
  recv = ""
+
  recv = ""
  while(true)
+
  while(true)
    buff=conn_.Recv(256, 5000)
+
    buff=conn_.Recv(256, 5000)
    if(buff.length() == 0)
+
    if(buff.length() == 0)
      break
+
      break
    end
+
    end
    recv = recv +  buff
+
    recv = recv +  buff
  end
+
  end
 
+
  # Check that we got a response and try to parse through it
+
  # Check that we got a response and try to parse through it
  if (recv=~ /^HTTP[^\r\n]+200\sOK.+?\r\n\r\n(.+)$/m)
+
  if (recv=~ /^HTTP[^\r\n]+200\sOK.+?\r\n\r\n(.+)$/m)
    #parse temp
+
    #parse temp
    $tstathash["temp"] = recv.to_s.scan(/\{\"temp\"Sad.*?),/)
+
    $tstathash["temp"] = recv.to_s.scan(/\{\"temp\"Sad.*?),/)
 
+
 
    #parse tmode
+
    #parse tmode
    $tstathash["tmode"] = recv.to_s.scan(/\"tmode\"Sad.*?),/)   
+
    $tstathash["tmode"] = recv.to_s.scan(/\"tmode\"Sad.*?),/)   
 
+
    #parse fmode
+
    #parse fmode
    $tstathash["fmode"] = recv.to_s.scan(/\"fmode\"Sad.*?),/)
+
    $tstathash["fmode"] = recv.to_s.scan(/\"fmode\"Sad.*?),/)
 
+
 
    #parse override
+
    #parse override
    $tstathash["override"] = recv.to_s.scan(/\"override\"Sad.*?),/)
+
    $tstathash["override"] = recv.to_s.scan(/\"override\"Sad.*?),/)
 
+
 
    #parse hold
+
    #parse hold
    $tstathash["hold"] = recv.to_s.scan(/\"hold\"Sad.*?),/)
+
    $tstathash["hold"] = recv.to_s.scan(/\"hold\"Sad.*?),/)
 
+
 
    #parse t_heat
+
    #parse t_heat
    $tstathash["t_heat"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/)
+
    $tstathash["t_heat"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/)
 
+
    #parse t_cool
+
    #parse t_cool
    $tstathash["t_cool"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/)
+
    $tstathash["t_cool"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/)
 
+
    #parse time
+
    #parse time
    $tstathash["day"] = recv.to_s.scan(/\"day\"Sad.*?),/)   
+
    $tstathash["day"] = recv.to_s.scan(/\"day\"Sad.*?),/)   
    $tstathash["hour"] = recv.to_s.scan(/\"hour\"Sad.*?),/)   
+
    $tstathash["hour"] = recv.to_s.scan(/\"hour\"Sad.*?),/)   
    $tstathash["minute"] = recv.to_s.scan(/\"minute\"Sad.*?)\}/)
+
    $tstathash["minute"] = recv.to_s.scan(/\"minute\"Sad.*?)\}/)
 
+
    case $tstathash["tmode"].to_s
+
    case $tstathash["tmode"].to_s
      when "1"
+
      when "1"
        $tstathash["setpoint"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/)
+
        $tstathash["setpoint"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/)
      when "2"
+
      when "2"
        $tstathash["setpoint"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/)
+
        $tstathash["setpoint"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/)
      when "3"
+
      when "3"
        $tstathash["setpoint"] = 32.0  #no setpoint for auto ??
+
        $tstathash["setpoint"] = 32.0  #no setpoint for auto ??
      else
+
      else
        log("Error getting setpoint! mode:" + $tstathash["tmode"].to_s + " 0 is OFF")
+
        log("Error getting setpoint! mode:" + $tstathash["tmode"].to_s + " 0 is OFF")
    end
+
    end
  end
+
  end
end
+
end
 
+
def tstat_set(data, update=true)
+
def tstat_set(data, update=true)
  # Reconnect to the device
+
  # Reconnect to the device
+
  conn_.Reconnect()
+
  conn_.Reconnect()
 
+
  s = "POST /tstat HTTP/1.1\r\n"
+
  s = "POST /tstat HTTP/1.1\r\n"
  s += "Content-type: application/x-www-form-urlencoded\r\n"
+
  s += "Content-type: application/x-www-form-urlencoded\r\n"
  s += "Content-Length: " + (data.length).to_s() + "\r\n"
+
  s += "Content-Length: " + (data.length).to_s() + "\r\n"
  s += "\r\n"
+
  s += "\r\n"
  s += data
+
  s += data
  s += "\r\n"
+
  s += "\r\n"
 
+
  conn_.Send(s)
+
  conn_.Send(s)
log("Update: " + data)
+
log("Update: " + data)
# Wait for a reply
+
# Wait for a reply
  recv = ""
+
  recv = ""
  while(true)
+
  while(true)
    buff=conn_.Recv(256, 5000)
+
    buff=conn_.Recv(256, 5000)
    if(buff.length() == 0)
+
    if(buff.length() == 0)
      break
+
      break
    end
+
    end
    recv = recv +  buff
+
    recv = recv +  buff
  end
+
  end
log(recv.to_s)
+
log(recv.to_s)
  # Update the state to reflect the new situation
+
  # Update the state to reflect the new situation
  if (update)
+
  if (update)
    update_state()
+
    update_state()
+
+
  end
+
  end
 
+
end
+
end
 
+
def update_state()
+
def update_state()
  # This function queries the thermostat to get the most recent state
+
  # This function queries the thermostat to get the most recent state
+
  log("Polling thermostat for updates at: " + Time.now.to_s)
+
  log("Polling thermostat for updates at: " + Time.now.to_s)
  get()
+
  get()
 
+
  # Get the new states
+
  # Get the new states
  new_tmode = $tstathash["tmode"]  # 0-Off, 1-Heat, 2-Cool, 3-Auto
+
  new_tmode = $tstathash["tmode"]  # 0-Off, 1-Heat, 2-Cool, 3-Auto
  #if ($tstathash["fmode"] == 2)
+
  #if ($tstathash["fmode"] == 2)
    # Always on
+
    # Always on
    #new_fmode = 2 # On
+
    #new_fmode = 2 # On
  #else
+
  #else
    # Auto/Schedule
+
    # Auto/Schedule
    #new_fmode = 1 # Auto
+
    #new_fmode = 1 # Auto
  #end
+
  #end
  new_fmode = $tstathash["fmode"]
+
  new_fmode = $tstathash["fmode"]
  new_setpoint = farhenheit_to_celcius(Float($tstathash["setpoint"].to_s))
+
  new_setpoint = farhenheit_to_celcius(Float($tstathash["setpoint"].to_s))
+
  new_temp = farhenheit_to_celcius(Float($tstathash["temp"].to_s))
+
  new_temp = farhenheit_to_celcius(Float($tstathash["temp"].to_s))
  #new_time = Time.at(results[4].to_i - Time.now.utc_offset) # Time.at creates a UTC time, wheras the thermostat reports the time in the local time zone
+
  #new_time = Time.at(results[4].to_i - Time.now.utc_offset) # Time.at creates a UTC time, wheras the thermostat reports the time in the local time zone
 
+
  # Update the state and send events when necessary
+
  # Update the state and send events when necessary
  if (new_tmode != @tmode)
+
  if (new_tmode != @tmode)
    log("  New mode detected : " + new_tmode.to_s)
+
    log("  New mode detected : " + new_tmode.to_s)
    if (@tmode == 0)
+
    if (@tmode == 0)
      # The old mode was "Off", so now the thermostat is on
+
      # The old mode was "Off", so now the thermostat is on
      cmd = Command.new(device_.devid_, -1001, 1, 2, 28) # On
+
      cmd = Command.new(device_.devid_, -1001, 1, 2, 28) # On
      SendCommand(cmd)
+
      SendCommand(cmd)
    end
+
    end
    if (new_tmode == 0)
+
    if (new_tmode == 0)
      # The new mode is "Off", so we send the Off event:
+
      # The new mode is "Off", so we send the Off event:
      cmd = Command.new(device_.devid_, -1001, 1, 2, 29) # Off
+
      cmd = Command.new(device_.devid_, -1001, 1, 2, 29) # Off
      SendCommand(cmd)
+
      SendCommand(cmd)
    else
+
    else
      # We send the Thermostat mode changed event:
+
      # We send the Thermostat mode changed event:
      cmd = Command.new(device_.devid_, -1001, 1, 2, 78)
+
      cmd = Command.new(device_.devid_, -1001, 1, 2, 78)
      cmd.params_[30] =  new_tmode.to_s #(new_tmode - 1).to_s
+
      cmd.params_[30] =  new_tmode.to_s #(new_tmode - 1).to_s
      SendCommand(cmd)
+
      SendCommand(cmd)
    end
+
    end
    @tmode = new_tmode
+
    @tmode = new_tmode
  end
+
  end
 
+
  if (new_fmode != @fmode)
+
  if (new_fmode != @fmode)
    @fmode = new_fmode
+
    @fmode = new_fmode
    log("  New fan mode detected : " + new_fmode.to_s)
+
    log("  New fan mode detected : " + new_fmode.to_s)
    cmd = Command.new(device_.devid_, -1001, 1, 2, 79) # Fan Mode Changed
+
    cmd = Command.new(device_.devid_, -1001, 1, 2, 79) # Fan Mode Changed
    cmd.params_[30] = new_fmode.to_s                  # Fan mode value
+
    cmd.params_[30] = new_fmode.to_s                  # Fan mode value
    SendCommand(cmd)
+
    SendCommand(cmd)
  end
+
  end
 
+
  if (new_setpoint != @setpoint)
+
  if (new_setpoint != @setpoint)
    @setpoint = new_setpoint
+
    @setpoint = new_setpoint
    log("  New temperature setpoint detected : " + new_setpoint.to_s)
+
    log("  New temperature setpoint detected : " + new_setpoint.to_s)
    cmd = Command.new(device_.devid_, -1001, 1, 2, 27) # Thermostat Set Point Chan
+
    cmd = Command.new(device_.devid_, -1001, 1, 2, 27) # Thermostat Set Point Chan
    cmd.params_[30] = new_setpoint.to_s                  # The new set point
+
    cmd.params_[30] = new_setpoint.to_s                  # The new set point
    SendCommand(cmd)
+
    SendCommand(cmd)
  end
+
  end
 
+
  if (new_temp != @temp)
+
  if (new_temp != @temp)
    @temp = new_temp
+
    @temp = new_temp
    log("  New temperature measurement detected : " + new_temp.to_s)
+
    log("  New temperature measurement detected : " + new_temp.to_s)
    cmd = Command.new(device_.devid_, -1001, 1, 2, 25) # Temperature changed event
+
    cmd = Command.new(device_.devid_, -1001, 1, 2, 25) # Temperature changed event
    cmd.params_[30] = new_temp.to_s        # The ambient temperature
+
    cmd.params_[30] = new_temp.to_s        # The ambient temperature
    SendCommand(cmd)
+
    SendCommand(cmd)
  end
+
  end
 
+
  # Check the latest system time
+
  # Check the latest system time
  #@time = new_time
+
  #@time = new_time
  #if ((@time-Time.now).abs > 600)
+
  #if ((@time-Time.now).abs > 600)
    # Time is out of sync for more than 10 minutes
+
    # Time is out of sync for more than 10 minutes
    #log("Thermostat's time ("+@time.to_s+") is out of sync with system time ("+Time.now.to_s+"), resynchronizing")
+
    #log("Thermostat's time ("+@time.to_s+") is out of sync with system time ("+Time.now.to_s+"), resynchronizing")
    #data="OID2.5.1=" # Current system time in seconds since Jan 1, 1970
+
    #data="OID2.5.1=" # Current system time in seconds since Jan 1, 1970
    #data+=(Time.now + Time.now.utc_offset).to_i.to_s # Time.to_i returns seconds since epoch in UTC, wheras the thermostat expects the time in seconds since epoch in the local time zone
+
    #data+=(Time.now + Time.now.utc_offset).to_i.to_s # Time.to_i returns seconds since epoch in UTC, wheras the thermostat expects the time in seconds since epoch in the local time zone
    #pdp_set(data, false)
+
    #pdp_set(data, false)
  #end
+
  #end
 
+
  # Record the last time the state was checked
+
  # Record the last time the state was checked
  @last_check = Time.now
+
  @last_check = Time.now
end
+
end
 
+
def farhenheit_to_celcius(cf)
+
def farhenheit_to_celcius(cf)
  # Converts a temperatue in deci-Farhenheit into deci-degrees Celcius
+
  # Converts a temperatue in deci-Farhenheit into deci-degrees Celcius
  power = 10.00
+
  power = 10.00
  return ((((cf - 32.0)*5.0)/9.0) * power).round / power
+
  return ((((cf - 32.0)*5.0)/9.0) * power).round / power
end
+
end
 
+
def celcius_to_farhenheit(c)
+
def celcius_to_farhenheit(c)
  # Converts a temperatue in degrees Celcius into deci-Farhenheit
+
  # Converts a temperatue in degrees Celcius into deci-Farhenheit
  power = 10.00
+
  power = 10.00
  return ((((c*9.0)/5.0)+32.0) * power).round / power
+
  return ((((c*9.0)/5.0)+32.0) * power).round / power
end
+
end
  
 
[[User:Techstyle|Techstyle]] # 351 process idle:
 
[[User:Techstyle|Techstyle]] # 351 process idle:
  
# Check how long it has been since we queried the thermostat
+
# Check how long it has been since we queried the thermostat
now = Time.now
+
now = Time.now
if ((@last_check + 300) < now)
+
if ((@last_check + 300) < now)
  # The last check was more than 5 minutes ago
+
  # The last check was more than 5 minutes ago
  update_state()
+
  update_state()
end
+
end
  
 
[[User:Techstyle|Techstyle]] # 350 process incoming data:
 
[[User:Techstyle|Techstyle]] # 350 process incoming data:
  
conn_.Close()
+
conn_.Close()
  
 
~~ # 355 process init:
 
~~ # 355 process init:
  
# Create Log File
+
# Create Log File
$logFile = File.new("/var/log/pluto/RadioThermostatWiFi.log", "w")
+
$logFile = File.new("/var/log/pluto/RadioThermostatWiFi.log", "w")
log("Starting RadioThermostatWiFi" + "\n")
+
log("Starting RadioThermostatWiFi" + "\n")
 
+
@tmode = 0
+
@tmode = 0
@fmode = 0
+
@fmode = 0
@temp = 0.0
+
@temp = 0.0
@setpoint = 0.0
+
@setpoint = 0.0
 
+
# Define Hashes to store our data
+
# Define Hashes to store our data
$tstathash = Hash.new
+
$tstathash = Hash.new
$tstathash["hour"] = 0
+
$tstathash["hour"] = 0
$tstathash["minute"] = 0
+
$tstathash["minute"] = 0
$tstathash["day"] = 0
+
$tstathash["day"] = 0
$tstathash["temp"] = 0.00
+
$tstathash["temp"] = 0.00
$tstathash["setpoint"] = 0.00
+
$tstathash["setpoint"] = 0.00
$tstathash["tmode"] = 0
+
$tstathash["tmode"] = 0
$tstathash["fmode"] = 0
+
$tstathash["fmode"] = 0
$tstathash["override"] = 0
+
$tstathash["override"] = 0
$tstathash["hold"] = 0
+
$tstathash["hold"] = 0
$tstathash["t_heat"] = 0.00
+
$tstathash["t_heat"] = 0.00
$tstathash["t_cool"] = 0.00
+
$tstathash["t_cool"] = 0.00
$tstathash["tstate"] = 0
+
$tstathash["tstate"] = 0
$tstathash["fstate"] = 0
+
$tstathash["fstate"] = 0
$tstathash["t_type_post"] = 0
+
$tstathash["t_type_post"] = 0
 
+
 
+
# Update the state
+
# Update the state
get()
+
get()
@tmode = $tstathash["tmode"]
+
@tmode = $tstathash["tmode"]
@fmode = $tstathash["fmode"]
+
@fmode = $tstathash["fmode"]
@temp = $tstathash["temp"]
+
@temp = $tstathash["temp"]
@setpoint = $tstathash["setpoint"]
+
@setpoint = $tstathash["setpoint"]
@last_check = Time.now
+
@last_check = Time.now
update_state()
+
update_state()
  
 
[[User:Techstyle|Techstyle]] 356 process release:
 
[[User:Techstyle|Techstyle]] 356 process release:
 
+
# Close the Log File
+
# Close the Log File
if ($logFile != nil) then
+
if ($logFile != nil) then
  $logFile.close
+
  $logFile.close
end
+
end

Revision as of 04:26, 3 May 2013


Radio Thermostat makes several thermostats that can communicate via various wireless protocols using USNAP modules. This is an attempt to bring some of the information for these thermostats into one place.

Current Models are:

 CT-22 (Beginning to be depreciated)
 CT-30 (Also known as Filtrete 3M-50 sold in Home Depot in the U.S. with a slightly different case)
 CT-80

The USNAP communication modules include Wi-Fi, Zigbee and Z-Wave. Store Front

There is a Windows program to control it via Wi-Fi

These thermostats will connect on their own via a Wi-Fi Access Point back to their hosted control service and allow you to control your thermostat from any browser.

The documentation for the Wi-Fi communication API was released.

I also found this unofficial Cloud API documentation.

Driver is complete and waiting for sqlCVS approval. -Aviator

[1]

The template is in process but is #2235. The code below is under development:

Techstyle #193 off:

data = "{\"tmode\":" # Current mode of HVAC operation data+="0}" # Off log("tmode set data: " + data) tstat_set(data)

Techstyle #192 on:

data+="3}" # Auto log("tmode set data: " + data) tstat_set(data)

Techstyle #279 set fan: data = "{\"fmode\":" if onoff == "1" data+="2}" # Fan is always on else data+="0}" # Auto end

log("fmode set data: " + data) tstat_set(data)

Techstyle #280 set heat/cool:

data = "{\"tmode\":" if onoff == "H" data+="1" # Heat only elsif onoff == "C" data+="2" # Cool only else data+="3" # Auto end

data+="}"

log("tmode set data: " + data)

tstat_set(data)

Techstyle # 278 set temperature:

if ((value_to_assign[0,1]=="+") || (value_to_assign[0,1]=="-")) # The new setpoint is relative to the current setpoint new_temp = celcius_to_farhenheit(Float(@setpoint)).round + value_to_assign.to_i else # The new setpoint is absolute new_temp = celcius_to_farhenheit(Float(value_to_assign.to_i)).round end

# to set the temp, we must know the mode case $tstathash["tmode"].to_s when "1" data = "{\"t_heat\":" + new_temp.to_s + "}" when "2" data = "{\"t_cool\":" + new_temp.to_s + "}" end log("Set temp data: " + data) tstat_set(data)

Techstyle #373 Private method listing:

# Helper function for logging def log(word) $logFile.print(word + "\n") $logFile.flush() end

def get()

# Reconnect to the device conn_.Reconnect()

# Send the request s = "GET /tstat HTTP/1.1\r\n" s += "\r\n" conn_.Send(s)

# Wait for a reply recv = "" while(true) buff=conn_.Recv(256, 5000) if(buff.length() == 0) break end recv = recv + buff end

# Check that we got a response and try to parse through it if (recv=~ /^HTTP[^\r\n]+200\sOK.+?\r\n\r\n(.+)$/m) #parse temp $tstathash["temp"] = recv.to_s.scan(/\{\"temp\"Sad.*?),/)

#parse tmode $tstathash["tmode"] = recv.to_s.scan(/\"tmode\"Sad.*?),/)

#parse fmode $tstathash["fmode"] = recv.to_s.scan(/\"fmode\"Sad.*?),/)

#parse override $tstathash["override"] = recv.to_s.scan(/\"override\"Sad.*?),/)

#parse hold $tstathash["hold"] = recv.to_s.scan(/\"hold\"Sad.*?),/)

#parse t_heat $tstathash["t_heat"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/)

#parse t_cool $tstathash["t_cool"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/)

#parse time $tstathash["day"] = recv.to_s.scan(/\"day\"Sad.*?),/) $tstathash["hour"] = recv.to_s.scan(/\"hour\"Sad.*?),/) $tstathash["minute"] = recv.to_s.scan(/\"minute\"Sad.*?)\}/)

case $tstathash["tmode"].to_s when "1" $tstathash["setpoint"] = recv.to_s.scan(/\"t_heat\"Sad.*?),/) when "2" $tstathash["setpoint"] = recv.to_s.scan(/\"t_cool\"Sad.*?),/) when "3" $tstathash["setpoint"] = 32.0 #no setpoint for auto ?? else log("Error getting setpoint! mode:" + $tstathash["tmode"].to_s + " 0 is OFF") end end end

def tstat_set(data, update=true) # Reconnect to the device

conn_.Reconnect()

s = "POST /tstat HTTP/1.1\r\n" s += "Content-type: application/x-www-form-urlencoded\r\n" s += "Content-Length: " + (data.length).to_s() + "\r\n" s += "\r\n" s += data s += "\r\n"

conn_.Send(s) log("Update: " + data) # Wait for a reply recv = "" while(true) buff=conn_.Recv(256, 5000) if(buff.length() == 0) break end recv = recv + buff end log(recv.to_s) # Update the state to reflect the new situation if (update) update_state()


end

end

def update_state() # This function queries the thermostat to get the most recent state

log("Polling thermostat for updates at: " + Time.now.to_s) get()

# Get the new states new_tmode = $tstathash["tmode"] # 0-Off, 1-Heat, 2-Cool, 3-Auto #if ($tstathash["fmode"] == 2) # Always on #new_fmode = 2 # On #else # Auto/Schedule #new_fmode = 1 # Auto #end new_fmode = $tstathash["fmode"] new_setpoint = farhenheit_to_celcius(Float($tstathash["setpoint"].to_s))

new_temp = farhenheit_to_celcius(Float($tstathash["temp"].to_s)) #new_time = Time.at(results[4].to_i - Time.now.utc_offset) # Time.at creates a UTC time, wheras the thermostat reports the time in the local time zone

# Update the state and send events when necessary if (new_tmode != @tmode) log(" New mode detected : " + new_tmode.to_s) if (@tmode == 0) # The old mode was "Off", so now the thermostat is on cmd = Command.new(device_.devid_, -1001, 1, 2, 28) # On SendCommand(cmd) end if (new_tmode == 0) # The new mode is "Off", so we send the Off event: cmd = Command.new(device_.devid_, -1001, 1, 2, 29) # Off SendCommand(cmd) else # We send the Thermostat mode changed event: cmd = Command.new(device_.devid_, -1001, 1, 2, 78) cmd.params_[30] = new_tmode.to_s #(new_tmode - 1).to_s SendCommand(cmd) end @tmode = new_tmode end

if (new_fmode != @fmode) @fmode = new_fmode log(" New fan mode detected : " + new_fmode.to_s) cmd = Command.new(device_.devid_, -1001, 1, 2, 79) # Fan Mode Changed cmd.params_[30] = new_fmode.to_s # Fan mode value SendCommand(cmd) end

if (new_setpoint != @setpoint) @setpoint = new_setpoint log(" New temperature setpoint detected : " + new_setpoint.to_s) cmd = Command.new(device_.devid_, -1001, 1, 2, 27) # Thermostat Set Point Chan cmd.params_[30] = new_setpoint.to_s # The new set point SendCommand(cmd) end

if (new_temp != @temp) @temp = new_temp log(" New temperature measurement detected : " + new_temp.to_s) cmd = Command.new(device_.devid_, -1001, 1, 2, 25) # Temperature changed event cmd.params_[30] = new_temp.to_s # The ambient temperature SendCommand(cmd) end

# Check the latest system time #@time = new_time #if ((@time-Time.now).abs > 600) # Time is out of sync for more than 10 minutes #log("Thermostat's time ("+@time.to_s+") is out of sync with system time ("+Time.now.to_s+"), resynchronizing") #data="OID2.5.1=" # Current system time in seconds since Jan 1, 1970 #data+=(Time.now + Time.now.utc_offset).to_i.to_s # Time.to_i returns seconds since epoch in UTC, wheras the thermostat expects the time in seconds since epoch in the local time zone #pdp_set(data, false) #end

# Record the last time the state was checked @last_check = Time.now end

def farhenheit_to_celcius(cf) # Converts a temperatue in deci-Farhenheit into deci-degrees Celcius power = 10.00 return ((((cf - 32.0)*5.0)/9.0) * power).round / power end

def celcius_to_farhenheit(c) # Converts a temperatue in degrees Celcius into deci-Farhenheit power = 10.00 return ((((c*9.0)/5.0)+32.0) * power).round / power end

Techstyle # 351 process idle:

# Check how long it has been since we queried the thermostat now = Time.now if ((@last_check + 300) < now) # The last check was more than 5 minutes ago update_state() end

Techstyle # 350 process incoming data:

conn_.Close()

~~ # 355 process init:

# Create Log File $logFile = File.new("/var/log/pluto/RadioThermostatWiFi.log", "w") log("Starting RadioThermostatWiFi" + "\n")

@tmode = 0 @fmode = 0 @temp = 0.0 @setpoint = 0.0

# Define Hashes to store our data $tstathash = Hash.new $tstathash["hour"] = 0 $tstathash["minute"] = 0 $tstathash["day"] = 0 $tstathash["temp"] = 0.00 $tstathash["setpoint"] = 0.00 $tstathash["tmode"] = 0 $tstathash["fmode"] = 0 $tstathash["override"] = 0 $tstathash["hold"] = 0 $tstathash["t_heat"] = 0.00 $tstathash["t_cool"] = 0.00 $tstathash["tstate"] = 0 $tstathash["fstate"] = 0 $tstathash["t_type_post"] = 0


# Update the state get() @tmode = $tstathash["tmode"] @fmode = $tstathash["fmode"] @temp = $tstathash["temp"] @setpoint = $tstathash["setpoint"] @last_check = Time.now update_state()

Techstyle 356 process release:

# Close the Log File if ($logFile != nil) then $logFile.close end