Exemplo n.º 1
0
def main():
    """Connect to Notcard and run a transaction test."""
    print("Opening port...")
    try:
        if use_uart:
            port = busio.UART(board.TX, board.RX, baudrate=9600)
        else:
            port = busio.I2C(board.SCL, board.SDA)
    except Exception as exception:
        raise Exception("error opening port: " +
                        NotecardExceptionInfo(exception))

    print("Opening Notecard...")
    try:
        if use_uart:
            card = notecard.OpenSerial(port)
        else:
            card = notecard.OpenI2C(port, 0, 0)
    except Exception as exception:
        raise Exception("error opening notecard: " +
                        NotecardExceptionInfo(exception))

    # If success, do a transaction loop
    print("Performing Transactions...")
    while True:
        time.sleep(2)
        transactionTest(card)
Exemplo n.º 2
0
def main():

    # Initialize
    print("opening port")
    try:
        if sys.implementation.name == 'circuitpython':
            if use_uart:
                # https://circuitpython.readthedocs.io/en/2.x/shared-bindings/busio/UART.html
                port = busio.UART(board.TX, board.RX, baudrate=9600)
            else:
                # https://circuitpython.readthedocs.io/en/2.x/shared-bindings/busio/I2C.html
                port = busio.I2C(board.SCL, board.SDA)
        elif sys.implementation.name == 'micropython':
            if use_uart:
                # https://docs.micropython.org/en/latest/library/machine.UART.html
                # ESP32 IO2 RX:16 TX:17
                port = UART(2, 9600)
                port.init(9600, bits=8, parity=None, stop=1)
            else:
                # https://docs.micropython.org/en/latest/library/machine.I2C.html
                port = I2C()
        elif sys.implementation.name == 'cpython':
            if use_uart:
                # https://github.com/vsergeev/python-periphery#serial
                if sys.platform == "linux" or sys.platform == "linux2":
                    if use_periphery:
                        port = Serial("/dev/serial0", 9600)
                    else:
                        port = serial.Serial(port="/dev/serial0",
                                             baudrate=9600)
                elif sys.platform == "darwin":
                    port = serial.Serial(port="/dev/tty.usbmodemNOTE1",
                                         baudrate=9600)
                elif sys.platform == "win32":
                    port = serial.Serial(port="COM21",
                                         baudrate=9600)
            else:
                # https://github.com/vsergeev/python-periphery#i2c
                if use_periphery:
                    port = I2C("/dev/i2c-1")
                else:
                    raise Exception("I2C not supported on platform: "
                                    + sys.platform)
    except Exception as exception:
        raise Exception("error opening port: " + ExceptionInfo(exception))

    print("opening notecard")
    try:
        if use_uart:
            card = notecard.OpenSerial(port)
        else:
            card = notecard.OpenI2C(port, 0, 0)
    except Exception as exception:
        raise Exception("error opening notecard: " + ExceptionInfo(exception))

    # If success, do a transaction loop
    print("transaction loop")
    while True:
        time.sleep(2)
        transactionTest(card)
Exemplo n.º 3
0
def main():
    """Connect to Notcard and run a transaction test."""
    print("Opening port...")
    try:
        if use_uart:
            port = UART(2, 9600)
            port.init(9600, bits=8, parity=None, stop=1)
        else:
            port = I2C()
    except Exception as exception:
        raise Exception("error opening port: "
                        + NotecardExceptionInfo(exception))

    print("Opening Notecard...")
    try:
        if use_uart:
            card = notecard.OpenSerial(port)
        else:
            card = notecard.OpenI2C(port, 0, 0)
    except Exception as exception:
        raise Exception("error opening notecard: "
                        + NotecardExceptionInfo(exception))

    # If success, do a transaction loop
    print("Performing Transactions...")
    while True:
        time.sleep(2)
        transactionTest(card)
Exemplo n.º 4
0
    def connect_notecard(appConfig):
        """Connect to Notcard and run a transaction test."""
        print("Opening port...")
        use_uart = appConfig.PortType == config.PortType.UART or appConfig.PortType == config.PortType.USB
        try:
            if use_uart:
                uartMethodTimeoutMS = 10000
                port = UART(appConfig.PortID,
                            appConfig.PortBaudRate,
                            parity=None,
                            stop=1,
                            bits=8,
                            rx=Pin(17),
                            tx=Pin(16),
                            timeout=uartMethodTimeoutMS)

            else:
                port = I2C()
        except Exception as exception:
            raise Exception("error opening port: " +
                            NotecardExceptionInfo(exception))

        print("Opening Notecard...")
        try:
            if use_uart:
                card = notecard.OpenSerial(port, debug=appConfig.Debug)
            else:
                card = notecard.OpenI2C(port, 0, 0, debug=appConfig.Debug)
        except Exception as exception:
            raise Exception("error opening notecard: " +
                            NotecardExceptionInfo(exception))

        return card
Exemplo n.º 5
0
def test_debug_mode_on_i2c():
    periphery = Mock()  # noqa: F811
    port = periphery.I2C("dev/i2c-foo")
    port.try_lock.return_value = True

    nCard = notecard.OpenI2C(port, 0x17, 255, debug=True)

    assert nCard._debug
Exemplo n.º 6
0
def get_i2c_and_port():
    periphery = Mock()  # noqa: F811
    port = periphery.I2C("dev/i2c-foo")
    port.try_lock.return_value = True

    nCard = notecard.OpenI2C(port, 0x17, 255)

    return (nCard, port)
Exemplo n.º 7
0
def connectNotecard(config):
    if config.PortType == 'i2c':
        port = I2C(config.PortName)
        card = notecard.OpenI2C(port, 0, 0, debug=config.EnableDebug)
    else:
        port = serial.Serial(port=config.PortName, baudrate=config.BaudRate)
        card = notecard.OpenSerial(port, debug=config.EnableDebug)

    return card
Exemplo n.º 8
0
def connectToNotecard(debugFlag=defaultDebugFlag,
                      useSerial=defaultUseSerialFlag,
                      portName=defaultPortName,
                      baudRate=9600):
    if useSerial:
        port = serial.Serial(port=portName, baudrate=baudRate)
        card = notecard.OpenSerial(port, debug=debugFlag)
    else:
        port = I2C(portName)
        card = notecard.OpenI2C(port, 0, 0, debug=debugFlag)

    return card
Exemplo n.º 9
0
def main():
    """Connect to Notcard and run a transaction test."""
    print("Opening port...")
    try:
        port = I2C("/dev/i2c-1")
    except Exception as exception:
        raise Exception("error opening port: " +
                        NotecardExceptionInfo(exception))

    print("Opening Notecard...")
    try:
        card = notecard.OpenI2C(port, 0, 0)
    except Exception as exception:
        raise Exception("error opening notecard: " +
                        NotecardExceptionInfo(exception))

    # If success, do a transaction loop
    print("Performing Transactions...")
    while True:
        time.sleep(2)
        transactionTest(card)
Exemplo n.º 10
0
def main():
    """Connect to Notcard and run a transaction test."""
    print("Opening port...")
    try:
        port = I2C("/dev/i2c-1")
    except Exception as exception:
        raise Exception("error opening port: " +
                        NotecardExceptionInfo(exception))

    print("Opening Notecard...")
    try:
        card = notecard.OpenI2C(port, 0, 0)
    except Exception as exception:
        raise Exception("error opening notecard: " +
                        NotecardExceptionInfo(exception))

    # If success, do a transaction loop
    print("Performing Transactions...")
    counter = 0
    while True:
        time.sleep(2)
        print(counter)
        if counter < 1:
            transactionSet(card)
            counter = 1
        else:
            transactionSync(card)
            time.sleep(15)
            transactionSend(card)
            time.sleep(15)
            transactionTime(card)
            time.sleep(15)
            transactionTriangulate(card)
            time.sleep(15)
            transactionGPS(card)
            time.sleep(15)
            transactionSyncStatus(card)
            time.sleep(15)
Exemplo n.º 11
0
import os
from periphery import I2C
import base64
import math

import notecard
from notecard import note

import asyncio
import websockets

productUID = os.getenv('NC_PRODUCT_UID')

print("Connecting to Notecard...")
port = I2C("/dev/i2c-1")
card = notecard.OpenI2C(port, 0, 0, debug=True)


def base64ToBody(payload):
    bytesInHex = base64.b64decode(payload).hex()
    payloadBytes = bytearray.fromhex(bytesInHex)

    rawTemp = payloadBytes[0] + payloadBytes[1] * 256
    formattedTemp = sflt162f(rawTemp) * 100

    rawHu = payloadBytes[2] + payloadBytes[3] * 256
    formattedHumidity = sflt162f(rawHu) * 100

    return {"tempC": formattedTemp, "humidity": formattedHumidity}

def init_i2c():
    global port, nCard
    port = I2C("/dev/i2c-1")
    nCard = notecard.OpenI2C(port, 0, 0)
            "req": "note.add",
            "file": "bricklet.qos",
            "body": {
                "ch": channel,
                "v": voltage
            },
            "sync": True
        }
        rsp = nCard.Transaction(req)
        print(rsp)
    except Exception as e:
        print(e)


port = I2C("/dev/i2c-1")
nCard = notecard.OpenI2C(port, 0, 0)


def init_i2c():
    global port, nCard
    port = I2C("/dev/i2c-1")
    nCard = notecard.OpenI2C(port, 0, 0)


if __name__ == "__main__":

    setup_request = {
        "req": "hub.set",
        "product": "de.datacake.simon:datacaketest",
        "sn": "Datacake0003",
        "mode": "continuous",