Renvoie les températures des DS18B20
This commit is contained in:
		
							
								
								
									
										75
									
								
								server.lua
									
									
									
									
									
								
							
							
						
						
									
										75
									
								
								server.lua
									
									
									
									
									
								
							@@ -1,9 +1,70 @@
 | 
				
			|||||||
cs=coap.Server()
 | 
					-- vim : set autoindent expandtab tabstop=2 shiftwidth=2
 | 
				
			||||||
cs:listen(5683)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
function hello(payload)
 | 
					cs=coap.Server() -- Lance le serveur CoAP
 | 
				
			||||||
  print("hello called")
 | 
					cs:listen(5683)  -- sur le port 5683
 | 
				
			||||||
  respond = "hello"
 | 
					
 | 
				
			||||||
  return respond
 | 
					pin = 3
 | 
				
			||||||
 | 
					ow.setup(pin) -- DS18B20 sur le pin 3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function get_data(nbre)
 | 
				
			||||||
 | 
					  capteurs = {}
 | 
				
			||||||
 | 
					  temperatures = {}
 | 
				
			||||||
 | 
					  print("Cherche les capteurs : ")
 | 
				
			||||||
 | 
					  print(nbre)
 | 
				
			||||||
 | 
					  addr = ow.reset_search(pin)
 | 
				
			||||||
 | 
					  for ds = 1, nbre, 1 do
 | 
				
			||||||
 | 
					    addr = ow.search(pin)
 | 
				
			||||||
 | 
					    if addr == nil then
 | 
				
			||||||
 | 
					      pint("Pas de capteurs")
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					      print(addr:byte(1,8))
 | 
				
			||||||
 | 
					      local crc = ow.crc8(string.sub(addr,1,7))
 | 
				
			||||||
 | 
					      if crc == addr:byte(8) then
 | 
				
			||||||
 | 
					        if (addr:byte(1) == 0x10) or (addr:byte(1) == 0x28) then
 | 
				
			||||||
 | 
					          print("Device is a DS18S20 family device.")
 | 
				
			||||||
 | 
					          table.insert(capteurs, addr)
 | 
				
			||||||
 | 
					        end
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  ow.reset(pin)
 | 
				
			||||||
 | 
					  for _, addr in ipairs(capteurs) do
 | 
				
			||||||
 | 
					    tmr.create():alarm(1500, tmr.ALARM_SINGLE, startup)
 | 
				
			||||||
 | 
					    ow.reset(pin)
 | 
				
			||||||
 | 
					    ow.select(pin, addr)
 | 
				
			||||||
 | 
					    ow.write(pin, 0x44, 1)
 | 
				
			||||||
 | 
					    tmr.create():alarm(1500, tmr.ALARM_SINGLE, startup)
 | 
				
			||||||
 | 
					    ow.reset(pin)
 | 
				
			||||||
 | 
					    ow.select(pin, addr)
 | 
				
			||||||
 | 
					    ow.write(pin,0xBE,1)
 | 
				
			||||||
 | 
					    local data = ow.read_bytes(pin, 9)
 | 
				
			||||||
 | 
					    local crc = ow.crc8(string.sub(data,1,8))
 | 
				
			||||||
 | 
					    if crc == data:byte(9) then
 | 
				
			||||||
 | 
					      local t = (data:byte(1) + data:byte(2) * 256) * 625
 | 
				
			||||||
 | 
					      local sgn = t<0 and -1 or 1
 | 
				
			||||||
 | 
					      local tA = sgn*t
 | 
				
			||||||
 | 
					      local t1 = math.floor(tA / 10000)
 | 
				
			||||||
 | 
					      local t2 = tA % 10000
 | 
				
			||||||
 | 
					      temperature = (sgn<0 and "-" or "")..t1.."."..t2
 | 
				
			||||||
 | 
					      print("temperature ="..temperature.." C")
 | 
				
			||||||
 | 
					      local addr1 = addr:byte(1)
 | 
				
			||||||
 | 
					      local addr2 = addr:byte(2)
 | 
				
			||||||
 | 
					      local addr3 = addr:byte(3)
 | 
				
			||||||
 | 
					      local addr4 = addr:byte(4)
 | 
				
			||||||
 | 
					      local addr5 = addr:byte(5)
 | 
				
			||||||
 | 
					      local addr6 = addr:byte(6)
 | 
				
			||||||
 | 
					      local addr7 = addr:byte(7)
 | 
				
			||||||
 | 
					      local addr8 = addr:byte(8)
 | 
				
			||||||
 | 
					      local addr0 = string.format("%s:%s:%s:%s:%s:%s:%s:%s", addr1, addr2, addr3, addr4, addr5, addr6, addr7, addr8)
 | 
				
			||||||
 | 
					      result = addr0.."="..temperature
 | 
				
			||||||
 | 
					      table.insert(temperatures, result)
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					  data = table.concat(temperatures,";")
 | 
				
			||||||
 | 
					  print("data :")
 | 
				
			||||||
 | 
					  print(data)
 | 
				
			||||||
 | 
					  return data
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
cs:func("hello")
 | 
					
 | 
				
			||||||
 | 
					cs:func("get_data")
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user