-- Serial port must be configured before opening the file -- stty -F /dev/ttyACM0 9600 local deviceNum = 1 local serialPort = "/dev/ttyACM0" JSON = (loadfile "/home/pi/bin/domoticz/scripts/lua/JSON.lua")() -- one-time load of the routines commandArray = {} -- Open seria port in read and write wserial=io.open(serialPort, "w") rserial=io.open(serialPort, "r") -- Because Arduino auto-reset when a serial line is open, wait the end of reset before doing anything os.execute("sleep 2") -- First send a command to read the temperature. rserial:flush() wserial:write("r") wserial:flush() rserial:read() rserial:flush() -- Do it another time in order to ensure we have a full frame wserial:write("r") wserial:flush() read_value = rserial:read() -- 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) while json_value.channel==0 and nbTries < 16 do nbTries = nbTries + 1 os.execute("sleep 2") wserial:write("r") wserial:flush() read_value = rserial:read() json_value = JSON:decode(read_value) end -- Update the sensor value with the new temperature if a temperature has been read if json_value.channel==1 then commandArray['UpdateDevice']=deviceNum..'|0|'..json_value.temp/10 -- print("[DEBUG] Temperature: "..json_value.temp) end return commandArray