Difference between revisions of "Radio Thermostat"

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

Latest revision as of 14:58, 7 July 2016


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:

#193 off:

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

  1. 192 on:


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

  1. 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)

  1. 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)

  1. 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)

  1. 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

  1. 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

  1. 350 process incoming data:


conn_.Close()

  1. 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()

  1. 356 process release:


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