2 # Copyright (c) 2017, Tobias Mueller tm(at)tm3d.de
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the
15 # * All advertising materials mentioning features or use of this
16 # software must display the following acknowledgement: This product
17 # includes software developed by tm3d.de and its contributors.
18 # * Neither the name of tm3d.de nor the names of its contributors may
19 # be used to endorse or promote products derived from this software
20 # without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 devInfo= ["","temperature","pressure","illuminance","humidity","constant","voltage","current","VOC","counter","CO2","resistance"]
41 devUnit= ["","°C","hPa","lux","%","","V","mA","ppm","","ppm","kOhm"]
43 devController=["","Old code","Attiny84A","Attiny44","Atmega328"]
45 devChip=["","DS18B20","DS2438","DS2438","DS2438","DS2450","Thermoelement","SHT21","SHT25","DHT22","HIH9021","HDC1080","HIH4030","HIH5030","BMP280","MAX44009","CDM7160","MAX1164/TGS8100","TGS8100","DS2423","intern ADC","SHT35","SHT31"]
48 def calcValue(code,vn,V):
56 return V[vn]*0.2 + 700;
58 return exp(V[vn] / 160.0);
60 return V[vn]*62.5 + 55000;
64 return ((float(V[2]) / float(V[1]) - 0.16) / 0.0062) / (1.0546 - 0.00216*V[0]/256.0);
68 return V[vn] / 65535.0*5.1;
70 return V[vn] / 65535.0*2.55;
72 return V[vn] / 65535.0*1.1;
78 return (V[vn] - 32767.0) / 100.0;
80 return exp((V[vn]-32767.0)/1000.0);
84 return V[vn]*0.2441/1000;
91 def owcom(dev,send,rc):
94 f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)
96 l=subprocess.check_output("rmmod w1_therm",shell=True)
97 f=open("/sys/bus/w1/devices/%s/rw" %(dev),"r+b",0)
98 f.write("".join(map(chr, send)))
100 res=map(ord,f.read(rc))
115 lscrc=(lscrc>>1)^0x8c
122 oddparity = [ 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0 ]
128 cdata = (cdata ^ (crc16 & 0xff)) & 0xff;
130 if (oddparity[cdata & 0xf] ^ oddparity[cdata >> 4]):
131 crc16 = crc16 ^ 0xc001;
132 cdata = (cdata << 6)&0xffff;
133 crc16 = crc16 ^ cdata;
134 cdata = (cdata << 1)&0xffff;
137 return (crc16==0xB001)
140 def ow_fconvert(b1, b2):
141 tsht = b1 | (b2 << 8)
148 for c in s.lower()[:]:
149 if not((c) in ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','-']):
164 def owfind(famcodes):
165 l=subprocess.check_output("ls /sys/bus/w1/devices/", shell=True)
167 for g in (l.split("\n")):
170 if (g[0:2] in famcodes) or len(famcodes)==0:
178 values=[0.0,0.0,0.0,0.0]
180 config=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
181 def __init__(self, owid):
184 def setdefaultConfig(self):
185 raise NotImplementedError()
187 def readConfig(self):
188 self.config=owcom(self.owid,[0x85],26)
190 if self.config[0]==0xFF:
191 print("No Deviceconfig. Not a Device form tm3d.de. Set Default");
192 self.setdefaultConfig()
194 if self.config[25]==0xFF:
195 if crc8(self.config[0:24]):
196 print("CRC Error reading Deviceconfig. Set Default")
197 self.setdefaultConfig()
199 if not(crc16([0x85]+self.config)):
200 print("CRC Error reading Deviceconfig. Set Default")
201 self.setdefaultConfig()
203 def convertAll(self):
204 raise NotImplementedError()
209 l.append(devChip[self.config[i+9]])
215 l.append(devUnit[self.config[i*2]])
218 def getProperties(self):
221 l.append(devInfo[self.config[i*2]])
225 class owDS2450(owDevice):
226 def setdefaultConfig(self):
227 self.config=[6,9, 6,9, 6,9, 6,9, 0,5,5,5,5,0,0,0,0,0,0,0,0,0,0,0]
229 def convertAll(self):
230 ret=owcom(self.owid,[0x3C,0x0F,0x00],2)
231 if not(crc16([0x3C,0x0F,0x00]+ret)):
235 ret=owcom(self.owid,[0xAA,0x00,0x00],10)
236 if not(crc16([0xAA,0x00,0x00]+ret)):
240 self.raw[i]=ret[2*i]|(ret[2*i+1]<<8);
242 self.values[i]=calcValue(self.config[i*2+1],i,self.raw)
245 class owDS18B20(owDevice):
246 def setdefaultConfig(self):
247 self.config=[1,1, 0,0, 0,0, 0,0, 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
249 def convertAll(self):
250 owcom(self.owid,[0x44],0)
252 ret=owcom(self.owid,[0xBE],9)
256 self.raw[0]=ow_fconvert(ret[0],ret[1]);
257 self.values[0]=calcValue(self.config[0*2+1],0,self.raw)
262 class owDS2438(owDevice):
263 def setdefaultConfig(self):
264 self.config=[1,6, 6,8, 4,7, 6,17, 0,2,3,12,4,0,0,0,0,0,0,0,0,0,0,0]
266 def readScratchpad(self,page,recall):
268 owcom(self.owid,[0xB8,page],0)
269 ret=owcom(self.owid,[0xBE,page],9)
275 def setConfigByte(self, cb):
277 owcom(self.owid,[0x4E,0x00,cb],0)
278 sp=self.readScratchpad(0,False)
286 def convertAll(self):
287 owcom(self.owid,[0x44],0)
289 self.setConfigByte(0x08)
290 owcom(self.owid,[0xB4],0)
292 sp=self.readScratchpad(0,True)
295 temp=ow_fconvert(sp[1],sp[2]);
296 VDD=ow_fconvert(sp[3],sp[4]);
297 self.setConfigByte(0x00)
298 owcom(self.owid,[0xB4],0)
300 sp=self.readScratchpad(0,True);
303 I=ow_fconvert(sp[5],sp[6]);
304 VAD=ow_fconvert(sp[3],sp[4]);
310 self.values[i]=calcValue(self.config[i*2+1],i,self.raw)
312 class owDS2423(owDevice):
313 def setdefaultConfig(self):
314 self.config=[9,13, 9,13, 9,13, 9,13, 0,19,19,19,19,0,0,0,0,0,0,0,0,0,0,0]
316 def readCounter(self,page):
318 ret=owcom(self.owid,[0xA5,addr&0xFF,addr>>8],11)
319 if not(crc16([0xA5,addr&0xFF,addr>>8]+ret)):
329 def convertAll(self):
331 self.raw[i]=self.readCounter(i+12);
333 self.values[i]=calcValue(self.config[i*2+1],i,self.raw)