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:47]
sgripon [Domoticz]
add_temperature_sensor_in_domoticz [2016/11/11 20:01] (current)
sgripon [Reference]
Line 11: Line 11:
 First, prepare the assembly from page [[record_temperature_from_lexibook_asm30_sensor|Record temperature from Lexibook ASM30 sensor with Arduino]]. Then, just plug the arduino to the raspberry pi USB. First, prepare the assembly from page [[record_temperature_from_lexibook_asm30_sensor|Record temperature from Lexibook ASM30 sensor with Arduino]]. Then, just plug the arduino to the raspberry pi USB.
  
-===== The Arduino sketch ​=====+===== Arduino ===== 
 + 
 +==== The Arduino sketch ====
  
 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 108: Line 110:
   ​   ​
 } }
-</code>+</file>
  
 +==== The Auto-reset issue ====
 +
 +The Arduino Uno automatically reset when the serial line is open by a client. This is due to the DTR signal used by the Arduino to reset. This signal cannot be disabled on the raspberry pi. The official Arduino website gives some hardware tricks to [[http://​playground.arduino.cc/​Main/​DisablingAutoResetOnSerialConnection|disable the auto-reset]]. However, I choose here a pure software method as a workaround: the reset is not disabled, but this method allows the serial communication:​
 +  - Open a serial connection on the client
 +  - Wait 2 seconds that the Arduino Uno reset
 +  - The serial connection must remain open
 +  - Communicate
 +
 +This workaround is used in the Domoticz LUA script after.
 ===== Domoticz ===== ===== Domoticz =====
  
Line 121: 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.+
  
-<code lua> +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).
--- demo time script +
--- script names have three name components: script_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 commandsDevice 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 minute) will 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_svaluesdo 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 171: 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 186: 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 194: 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
 +  * 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.1459712833.txt.gz · Last modified: 2016/04/03 21:47 by sgripon