Note that the following code must be run at every boot for this to work, because of the repeated-start requirement:
sudo su -c 'echo "Y" > /sys/module/i2c_bcm2708/parameters/combined'
Note that the following code must be run at every boot for this to work, because of the repeated-start requirement:
sudo su -c 'echo "Y" > /sys/module/i2c_bcm2708/parameters/combined'
#!/usr/bin/env python3 | |
from adafruit.i2c import Adafruit_I2C | |
class Melexis: | |
def __init__(self, addr=0x5A, fahrenheit=False): | |
self._i2c = Adafruit_I2C(addr) | |
self.mode = fahrenheit | |
def readAmbient(self): | |
return self._readTemp(0x06) | |
def readObject(self): | |
return self._readTemp(0x07) | |
def readObject2(self): | |
return self._readTemp(0x08) | |
def getDifference(self): | |
"""Returns how much warmer the object is than the ambient | |
temperature.""" | |
return self.readObject() - sensor.readAmbient() | |
def _readTemp(self, reg): | |
temp = self._i2c.readS16(reg) | |
temp = temp * .02 - 273.15 | |
if self.mode: | |
return (temp * 9 / 5) + 32 | |
else: | |
return temp | |
if __name__ == "__main__": | |
sensor = Melexis(fahrenheit=True) | |
t = sensor.readObject() | |
print("Object: {}ºF ({}ºF warmer than the ambient temperature)".format( | |
round(t, 3), round(sensor.getDifference(), 3))) |
#!/usr/bin/env python3
import Adafruit_GPIO.I2C as I2C
class Melexis:
def __init__(self, address=0x5A, fahrenheit=False):
self._i2c = I2C.Device(address)
self._mode = fahrenheit
def readAmbient(self):
return self._readTemp(0x06)
def readObject1(self):
return self._readTemp(0x07)
def readObject2(self):
return self._readTemp(0x08)
def _readTemp(self, reg):
temp = self._i2c.readS16(reg)
temp = temp * .02 - 273.15
if self.mode:
return (temp * 9 / 5) + 32
else:
return temp
if name == "main":
sensor = Melexis(fahrenheit=True)
t = sensor.readObject1()
a = sensor.readAmbient()
print("Object: {}F ({}F warmer than the ambient temperature)".format(
round(t, 3), round(t - a, 3)))
In this code i am getting following error
pi@investigation ~ $ python3 /home/pi/Desktop/mouse_temp1.py
Traceback (most recent call last):
File "/home/pi/Desktop/mouse_temp1.py", line 30, in
sensor = Melexis(fahrenheit=True)
File "/home/pi/Desktop/mouse_temp1.py", line 9, in init
self._i2c = I2C.Device(address)
TypeError: init() takes at least 3 arguments (2 given)
Change the line self._i2c = I2C.Device(address)
to self._i2c = I2C.Device(address,busnum=1)
. There are a few other minor typos in your code, but it basically works.
Sorry, can you help me???When i try to run to code, I get the following error...
Traceback (most recent call last):
File "melexis90614.py", line 34, in
sensor = Melexis(fahrenheit=True)
File "melexis90614.py", line 8, in init
self._i2c = Adafruit_I2C(address,busnum=1)
NameError: global name 'Adafruit_I2C' is not defined
How can I fix this problem???Thank you so much
Thanks for the great help all.
This code works fine for me.
`
import Adafruit_GPIO.I2C as I2C
I2C.require_repeated_start()
class Melexis:
def __init__(self, address=0x5A):
self._i2c = I2C.Device(address,busnum=1)
def readAmbient(self):
return self._readTemp(0x06)
def readObject1(self):
return self._readTemp(0x07)
def readObject2(self):
return self._readTemp(0x08)
def _readTemp(self, reg):
temp = self._i2c.readS16(reg)
temp = temp * .02 - 273.15
return temp
#if name == "main":
sensor = Melexis()
t = sensor.readObject1()
a = sensor.readAmbient()
print("Object: {}C , Ambiant: {}C".format(round(t, 3), round(a, 3)))
`
As Adafruit_I2C library has been update these code are not running.... I tried a lot , if you can please help