User Tools

Site Tools


add_temperature_sensor_in_domoticz

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
add_temperature_sensor_in_domoticz [2016/04/03 21:54]
sgripon [Reference]
add_temperature_sensor_in_domoticz [2016/11/11 20:01] (current)
sgripon [Reference]
Line 17: Line 17:
 I modified a little bit the previous sketch in order to read from the serial line and send the temperature only when it is requested by client. I modified a little bit the previous sketch in order to read from the serial line and send the temperature only when it is requested by client.
  
-<code C++>+<file C++ SmartHomeBox_sgripon.ino>
 #include <​RFControl.h>​ #include <​RFControl.h>​
  
Line 110: Line 110:
   ​   ​
 } }
-</code>+</file>
  
 ==== The Auto-reset issue ==== ==== The Auto-reset issue ====
Line 132: Line 132:
 {{ ::​domoticz-hardware_list.png |}} {{ ::​domoticz-hardware_list.png |}}
  
-Then, in Setup/​Device list, not the ID of your virtual sensor.+Then, in Setup/​Device list, note the ID of your virtual sensor: it must be reused in the LUA script.
  
 ==== Update temperature with a script ==== ==== Update temperature with a script ====
  
-Domoticz ​provide ​a way to update its data through custom scripts written in LUA language. To use scripts, just create a file called //​script_time_HardwareName.lua//​ in domoticz installation path //​domoticz/​scripts/​lua//​.+Domoticz ​provides ​a way to update its data through custom scripts written in LUA language. To use scripts, just create a file called //​script_time_HardwareName.lua//​ in domoticz installation path //​domoticz/​scripts/​lua//​. The script will be automatically called every minute by Domoticz.
  
-This is the source code of my own script. The script ​read the temperature value by asking the arduino to write the value to the serial line. +This is the source code of my own script. The script ​reads the temperature value by asking the arduino to write the value to the serial line. The script ​use an external library to decode JSON data (see http://regex.info/blog/lua/json).
- +
-<code lua> +
--- demo time script +
--- script names have three name componentsscript_trigger_name.lua +
--- trigger can be '​time'​ or '​device',​ name can be any string +
--- domoticz will execute all time and device triggers when the relevant trigger occurs +
---  +
--- copy this script and change the "​name"​ part, all scripts named "​demo"​ are ignored.  +
--- +
--- Make sure the encoding is UTF8 of the file +
--- +
--- ingests tables: otherdevices,​otherdevices_svalues +
---  +
--- otherdevices and otherdevices_svalues are two item array for all devices:  +
---   ​otherdevices['​yourotherdevicename'​]="​On"​ +
--- otherdevices_svalues['​yourotherthermometer'​] = string of svalues +
--- +
--- Based on your logic, fill the commandArray with device commands. Device name is case sensitive.  +
--- +
--- Always, and I repeat ALWAYS start by checking for a state. +
--- If you would only specify commandArray['​AnotherDevice'​]='​On',​ every time trigger (e.g. every minutewill switch AnotherDevice on. +
--- +
--- The print command will output lua print statements to the domoticz log for debugging. +
--- List all otherdevices states for debugging:  +
---   for i, v in pairs(otherdevices) do print(i, v) end +
--- List all otherdevices svalues for debugging:  +
---   for i, v in pairs(otherdevices_svalues) do print(i, v) end+
  
 +<file lua script_time_HardwareName.lua>​
 -- Serial port must be configured before opening the file -- Serial port must be configured before opening the file
 -- stty -F /​dev/​ttyACM0 9600 -- stty -F /​dev/​ttyACM0 9600
Line 182: Line 156:
  
 -- Because Arduino auto-reset when a serial line is open, wait the end of reset before doing anything -- Because Arduino auto-reset when a serial line is open, wait the end of reset before doing anything
-os.execute("​sleep ​" .. tonumber(2))+os.execute("​sleep 2")
  
 -- First send a command to read the temperature. -- First send a command to read the temperature.
Line 197: Line 171:
  
 -- Ask a new read until a signal has been received from the sensor. It should be available within next 30 seconds with this sensor. -- Ask a new read until a signal has been received from the sensor. It should be available within next 30 seconds with this sensor.
 +local nbTries = 0;
 local json_value = JSON:​decode(read_value) local json_value = JSON:​decode(read_value)
-while json_value.channel==0 do+while json_value.channel==0 ​and nbTries < 16 do 
 + nbTries = nbTries + 1 
 + os.execute("​sleep 2")
  wserial:​write("​r"​)  wserial:​write("​r"​)
  wserial:​flush()  wserial:​flush()
Line 205: Line 182:
 end end
  
--- Update the sensor value with the new temperature +-- Update the sensor value with the new temperature ​if a temperature has been read 
-commandArray['​UpdateDevice'​]=deviceNum..'​|0|'​..json_value.temp/​10+if json_value.channel==1 then 
 + commandArray['​UpdateDevice'​]=deviceNum..'​|0|'​..json_value.temp/​10 
 + -- print("​[DEBUG] Temperature:​ "​..json_value.temp) 
 +end
  
 return commandArray return commandArray
-</code>+</file> 
 +===== The Temperature dashboard ===== 
 + 
 +{{ ::​domoticz-temperature.png |}} 
 + 
 +===== See also ===== 
 +  * [[add_temperature_and_humidity_sensor_dht11_in_domoticz|Add temperature and humidity sensor DHT11 in Domoticz]] 
 +  * [[record_temperature_from_lexibook_asm30_sensor|Record temperature from Lexibook ASM30 sensor with Arduino]] 
 ===== Reference ===== ===== Reference =====
  
   * Domoticz: https://​www.domoticz.com/​   * Domoticz: https://​www.domoticz.com/​
   * Arduino: Disabling Auto Reset On Serial Connection : http://​playground.arduino.cc/​Main/​DisablingAutoResetOnSerialConnection   * Arduino: Disabling Auto Reset On Serial Connection : http://​playground.arduino.cc/​Main/​DisablingAutoResetOnSerialConnection
 +  * JSON encoding/​decoding in LUA by Jeffrey Friedl: http://​regex.info/​blog/​lua/​json
  
 **Share this page:** **Share this page:**
  
 ~~socialite~~ ~~socialite~~
add_temperature_sensor_in_domoticz.1459713262.txt.gz · Last modified: 2016/04/03 21:54 by sgripon