class I2C: DIGITAL = PyMata.DIGITAL ANALOG = PyMata.ANALOG def __init__(self, pin_type, clk_pin, data_pin, port="/dev/ttyACM0", debug=False): """ :param pin_type: DIGITAL 或者 ANALOG :param clk_pin: 时钟总线接入的针脚 :param data_pin: 数据总线接入的针脚 :param port: 虚谷连接I2C设备的COM口,默认为"/dev/ttyACM0" :param debug: 当为True的时候,会输出debug信息 """ self.board = PyMata(port, bluetooth=False, verbose=debug) # i2c设备初始化 self.i2c = self.board.i2c_config(0, pin_type, clk_pin, data_pin) def read(self, addr=0x48, register=0, read_byte=2): """ :param addr: i2c设备的一个地址 :param register: i2c设备某个地址的寄存器 :param read_byte: 一次读取的字节数量 :return: """ # 向i2c的一个地址发送一个信号 self.board.i2c_write(addr, PyMata.I2C_READ_CONTINUOUSLY) time.sleep(0.5) # 读取这个地址的寄存器中缓存的数据 self.board.i2c_read(addr, register, read_byte, PyMata.I2C_READ) time.sleep(0.5) # 获取该地址中的数据 data = self.board.i2c_get_read_data(addr) return data def write(self, value, addr=0x48, register=0): self.board.i2c_write(addr, addr, register, value)
signal.signal(signal.SIGINT, signal_handler) board.set_pin_mode(BOARD_LED, board.OUTPUT, board.DIGITAL) board.i2c_config(0, board.DIGITAL, 3, 2) # Setup MQTT Client client = mqtt.Client() client.username_pw_set("********", password="******") client.on_connect = on_connect client.connect("192.168.0.7", 1883, 60) client.loop_start() sensor_data = {"HUM": 0, "TEMP": 0} while 1: board.i2c_read(0x5c, 0, 5, board.I2C_READ) time.sleep(3) data = board.i2c_get_read_data(0x5c) calculated_sum = data[1] + data[2] + data[3] + data[4] if data[5] == calculated_sum: # Check for Checksum humidity = data[1] + float(data[2]) / 10 scaleValue = data[4] & 0xEF signValue = data[4] & 0x80 temperature = data[3] + float(scaleValue) / 10 if signValue: temperature = -temperature sensor_data["HUM"] = humidity
import time from PyMata.pymata import PyMata addr = 0x48 # Initialize Arduino using port name port = PyMata("/dev/cu.usbmodem621") # Configure I2C pin port.i2c_config(0, port.ANALOG, 4, 5) # one shot read asking peripheral to send 2 bytes port.i2c_read(addr, 0, 2, port.I2C_READ) # Wait for peripheral to send the data time.sleep(3) # Read from the peripheral data = port.i2c_get_read_data(addr) # Obtain temperature from received data TemperatureSum = (data[1] << 8 | data[2]) >> 4 celsius = TemperatureSum * 0.0625 print celsius fahrenheit = (1.8 * celsius) + 32 print fahrenheit firmata.close()
import time from PyMata.pymata import PyMata board = PyMata("/dev/ttyACM0") # for leonardo board.i2c_config(0, board.DIGITAL, 3, 2) # for uno # board.i2c_config(0, board.ANALOG, 4, 5) while True: try: board.i2c_write( 0x23, board.I2C_READ_CONTINUOUSLY) # same results with board.I2C_READ time.sleep(.2) board.i2c_read( 0x23, 0, 2, board.I2C_READ) # same results with board.I2C_READ_CONTINUOUSLY time.sleep(.3) data = board.i2c_get_read_data(0x23) print('Got read data: %s' % data) lux = (data[1] << 8 | data[2]) >> 4 lux /= 1.2 print(str(lux) + ' lux') except KeyboardInterrupt: board.close()
from PyMata.pymata import PyMata # The PyMata constructor will print status to the console and will return # when PyMata is ready to accept commands or will exit if unsuccessful firmata = PyMata("/dev/ttyACM0") #configure the I2C pins. This code is for the UNO firmata.i2c_config(0, firmata.ANALOG, 4, 5) # read i2c device at address 0x48, with no register specified. Expect 2 bytes to be returned # and the operation is a single shot read firmata.i2c_read(0x48, 0, 2, firmata.I2C_READ) # give the serial interface time to send a read, for the device to execute the read # and to get things back across the interface time.sleep(3) # retrieve the data sent from device data = firmata.i2c_get_read_data(0x48) # do some calculations on the raw data returned TemperatureSum = (data[1] << 8 | data[2]) >> 4 celsius = TemperatureSum * 0.0625 print celsius fahrenheit = (1.8 * celsius) + 32
# create a PyMata instance board = PyMata("/dev/ttyACM0") def signal_handler(sig, frame): print('You pressed Ctrl+C!!!!') if board is not None: board.reset() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) # configure firmata for i2c on an UNO board.i2c_config(0, board.ANALOG, 4, 5) # configure the I2C pins. This code is for the Leonardo #board.i2c_config(0, board.DIGITAL, 3, 2) # read i2c device at address 0x48, with no register specified. Expect 2 bytes to be returned # and the operation is a single shot read board.i2c_read(0x48, 0, 2, board.I2C_READ, temp_callback) # give the serial interface time to send a read, for the device to execute the read # and to get things back across the interface time.sleep(2) board.close()
if board is not None: board.reset() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) # configure firmata for i2c on an UNO board.i2c_config(0, board.ANALOG, 4, 5) # configure the I2C pins. This code is for the Leonardo # board.i2c_config(0, board.DIGITAL, 3, 2) # read i2c device at address 0x48, with no register specified. Expect 2 bytes to be returned # and the operation is a single shot read board.i2c_read(0x48, 0, 2, board.I2C_READ) # give the serial interface time to send a read, for the device to execute the read # and to get things back across the interface time.sleep(3) # retrieve the data sent from device data = board.i2c_get_read_data(0x48) # do some calculations on the raw data returned TemperatureSum = (data[1] << 8 | data[2]) >> 4 celsius = TemperatureSum * 0.0625 print(celsius) fahrenheit = (1.8 * celsius) + 32
# create a PyMata instance board = PyMata("/dev/ttyACM0") def signal_handler(sig, frame): print('You pressed Ctrl+C!!!!') if board is not None: board.reset() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) # configure firmata for i2c on an UNO board.i2c_config(0, board.ANALOG, 4, 5) # configure the I2C pins. This code is for the Leonardo # board.i2c_config(0, board.DIGITAL, 3, 2) # read i2c device at address 0x48, with no register specified. Expect 2 bytes to be returned # and the operation is a single shot read board.i2c_read(0x48, 0, 2, board.I2C_READ, temp_callback) # give the serial interface time to send a read, for the device to execute the read # and to get things back across the interface time.sleep(2) board.close()
def signal_handler(sig, frame): print('You pressed Ctrl+C!!!!') if board is not None: board.reset() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) def lux_callback(data): datax = data[2] print 'Got read data: %s' % data lux = (datax[1] << 8 | datax[2]) >> 4 lux /= 1.2 print str(lux) + ' lux' while True: try: board.i2c_write( 0x23, board.I2C_READ_CONTINUOUSLY) # same results with board.I2C_READ time.sleep(.3) board.i2c_read( 0x23, 0, 2, board.I2C_READ, lux_callback) # same results with board.I2C_READ_CONTINUOUSLY time.sleep(.3) except KeyboardInterrupt: board.close()
# This code is supporting material for the book # Python Programming for Arduino # by Pratik Desai # published by PACKT Publishing import time from PyMata.pymata import PyMata #Initializing Arduino using PyFirmata constructor port = PyMata("COM5") #Configure I2C pin port.i2c_config(0, port.ANALOG, 4, 5) # One shot read asking peripheral to send 2 bytes port.i2c_read(0x23, 0, 2, port.I2C_READ) # Wait for peripheral to send the data time.sleep(3) # Read from the peripheral data = port.i2c_get_read_data(0x23) # Obtain lux values from received data LuxSum = (data[1] << 8 | data[2]) >> 4 lux = LuxSum/1.2 print str(lux) + ' lux' port.close()
# import the API class import time from PyMata.pymata import PyMata # The PyMata constructor will print status to the console and will return # when PyMata is ready to accept commands or will exit if unsuccessful firmata = PyMata("/dev/ttyACM0") #configure the I2C pins. This code is for the UNO firmata.i2c_config(0, firmata.ANALOG, 4, 5) # read i2c device at address 0x48, with no register specified. Expect 2 bytes to be returned # and the operation is a single shot read firmata.i2c_read(0x48, 0, 2, firmata.I2C_READ) # give the serial interface time to send a read, for the device to execute the read # and to get things back across the interface time.sleep(3) # retrieve the data sent from device data = firmata.i2c_get_read_data(0x48) # do some calculations on the raw data returned TemperatureSum = (data[1] << 8 | data[2]) >> 4 celsius = TemperatureSum * 0.0625 print celsius fahrenheit = (1.8 * celsius) + 32
# for uno # board.i2c_config(0, board.ANALOG, 4, 5) def signal_handler(sig, frame): print('You pressed Ctrl+C!!!!') if board is not None: board.reset() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) def lux_callback(data): datax = data[2] print('Got read data: %s' % data) lux = (datax[1] << 8 | datax[2]) >> 4 lux /= 1.2 print(str(lux) + ' lux') while True: try: board.i2c_write(0x23, board.I2C_READ_CONTINUOUSLY) # same results with board.I2C_READ time.sleep(.3) board.i2c_read(0x23, 0, 2, board.I2C_READ, lux_callback) # same results with board.I2C_READ_CONTINUOUSLY time.sleep(.3) except KeyboardInterrupt: board.close()
# This code is supporting material for the book # Python Programming for Arduino # by Pratik Desai # published by PACKT Publishing import time from PyMata.pymata import PyMata #Initialize Arduino using port name port = PyMata("COM5") #Configure I2C pin port.i2c_config(0, port.ANALOG, 4, 5) # One shot read asking peripheral to send 2 bytes port.i2c_read(0x48, 0, 2, port.I2C_READ) # Wait for peripheral to send the data time.sleep(3) # Read from the peripheral data = port.i2c_get_read_data(0x48) # Obtain temperature from received data TemperatureSum = (data[1] << 8 | data[2]) >> 4 celsius = TemperatureSum * 0.0625 print celsius fahrenheit = (1.8 * celsius) + 32 print fahrenheit
# This code is supporting material for the book # Python Programming for Arduino # by Pratik Desai # published by PACKT Publishing import time from PyMata.pymata import PyMata #Initializing Arduino using PyFirmata constructor port = PyMata("COM5") #Configure I2C pin port.i2c_config(0, port.ANALOG, 4, 5) # One shot read asking peripheral to send 2 bytes port.i2c_read(0x23, 0, 2, port.I2C_READ) # Wait for peripheral to send the data time.sleep(3) # Read from the peripheral data = port.i2c_get_read_data(0x23) # Obtain lux values from received data LuxSum = (data[1] << 8 | data[2]) >> 4 lux = LuxSum / 1.2 print str(lux) + ' lux' port.close()