diff --git a/README.md b/README.md index 8748097..cd12219 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ With `pip` via PyPi: pip install note-python ``` -or +or ```bash @@ -102,11 +102,11 @@ The documentation for this library can be found The [examples](examples/) directory contains examples for using this library with: -- [Serial](examples/serial-example.py) -- [I2C](examples/i2c-example.py) -- [RaspberryPi](examples/rpi-example.py) -- [CircuitPython](examples/cpy-example.py) -- [MicroPython](examples/mpy-example.py) +- [Serial](examples/notecard-basics/serial-example.py) +- [I2C](examples/notecard-basics/i2c-example.py) +- [RaspberryPi](examples/notecard-basics/rpi-example.py) +- [CircuitPython](examples/notecard-basics/cpy-example.py) +- [MicroPython](examples/notecard-basics/mpy-example.py) ## Contributing diff --git a/examples/cpy-example.py b/examples/notecard-basics/cpy-example.py similarity index 58% rename from examples/cpy-example.py rename to examples/notecard-basics/cpy-example.py index e024415..2ba5ae5 100644 --- a/examples/cpy-example.py +++ b/examples/notecard-basics/cpy-example.py @@ -7,6 +7,8 @@ import time import notecard +productUID = "com.your-company.your-project" + # Choose either UART or I2C for Notecard use_uart = True @@ -31,22 +33,48 @@ def NotecardExceptionInfo(exception): + ": " + ' '.join(map(str, exception.args)) -def transactionTest(card): +def configure_notecard(card): """Submit a simple JSON-based request to the Notecard. Args: card (object): An instance of the Notecard class """ - req = {"req": "card.status"} + req = {"req": "hub.set"} + req["product"] = productUID + req["mode"] = "continuous" try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) + + +def get_temp_and_voltage(card): + """Submit a simple JSON-based request to the Notecard. + + Args: + card (object): An instance of the Notecard class + + """ + temp = 0 + voltage = 0 + + try: + req = {"req": "card.temp"} + rsp = card.Transaction(req) + temp = rsp["value"] + + req = {"req": "card.voltage"} rsp = card.Transaction(req) - print(rsp) + voltage = rsp["value"] except Exception as exception: print("Transaction error: " + NotecardExceptionInfo(exception)) time.sleep(5) + return temp, voltage + def main(): """Connect to Notcard and run a transaction test.""" @@ -63,18 +91,26 @@ def main(): print("Opening Notecard...") try: if use_uart: - card = notecard.OpenSerial(port) + card = notecard.OpenSerial(port, debug=True) else: - card = notecard.OpenI2C(port, 0, 0) + card = notecard.OpenI2C(port, 0, 0, debug=True) 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) + # If success, configure the Notecard and send some data + configure_notecard(card) + temp, voltage = get_temp_and_voltage(card) + + req = {"req": "note.add"} + req["sync"] = True + req["body"] = {"temp": temp, "voltage": voltage} + + try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) main() diff --git a/examples/i2c-example.py b/examples/notecard-basics/i2c-example.py similarity index 58% rename from examples/i2c-example.py rename to examples/notecard-basics/i2c-example.py index 2326667..1f71731 100644 --- a/examples/i2c-example.py +++ b/examples/notecard-basics/i2c-example.py @@ -13,6 +13,8 @@ import notecard # noqa: E402 +productUID = "com.your-company.your-project" + if sys.implementation.name != 'cpython': raise Exception("Please run this example in a CPython environment.") @@ -33,22 +35,48 @@ def NotecardExceptionInfo(exception): return "line " + s1 + ": " + s2 + ": " + ' '.join(map(str, exception.args)) -def transactionTest(card): +def configure_notecard(card): """Submit a simple JSON-based request to the Notecard. Args: card (object): An instance of the Notecard class """ - req = {"req": "card.status"} + req = {"req": "hub.set"} + req["product"] = productUID + req["mode"] = "continuous" try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) + + +def get_temp_and_voltage(card): + """Submit a simple JSON-based request to the Notecard. + + Args: + card (object): An instance of the Notecard class + + """ + temp = 0 + voltage = 0 + + try: + req = {"req": "card.temp"} + rsp = card.Transaction(req) + temp = rsp["value"] + + req = {"req": "card.voltage"} rsp = card.Transaction(req) - print(rsp) + voltage = rsp["value"] except Exception as exception: print("Transaction error: " + NotecardExceptionInfo(exception)) time.sleep(5) + return temp, voltage + def main(): """Connect to Notcard and run a transaction test.""" @@ -61,16 +89,24 @@ def main(): print("Opening Notecard...") try: - card = notecard.OpenI2C(port, 0, 0) + card = notecard.OpenI2C(port, 0, 0, debug=True) 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) + # If success, configure the Notecard and send some data + configure_notecard(card) + temp, voltage = get_temp_and_voltage(card) + + req = {"req": "note.add"} + req["sync"] = True + req["body"] = {"temp": temp, "voltage": voltage} + + try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) main() diff --git a/examples/mpy-example.py b/examples/notecard-basics/mpy-example.py similarity index 59% rename from examples/mpy-example.py rename to examples/notecard-basics/mpy-example.py index 2422126..6bf1b72 100644 --- a/examples/mpy-example.py +++ b/examples/notecard-basics/mpy-example.py @@ -7,6 +7,8 @@ import time import notecard +productUID = "com.your-company.your-project" + # Choose either UART or I2C for Notecard use_uart = True @@ -31,22 +33,48 @@ def NotecardExceptionInfo(exception): + ' '.join(map(str, exception.args)) -def transactionTest(card): +def configure_notecard(card): """Submit a simple JSON-based request to the Notecard. Args: card (object): An instance of the Notecard class """ - req = {"req": "card.status"} + req = {"req": "hub.set"} + req["product"] = productUID + req["mode"] = "continuous" try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) + + +def get_temp_and_voltage(card): + """Submit a simple JSON-based request to the Notecard. + + Args: + card (object): An instance of the Notecard class + + """ + temp = 0 + voltage = 0 + + try: + req = {"req": "card.temp"} + rsp = card.Transaction(req) + temp = rsp["value"] + + req = {"req": "card.voltage"} rsp = card.Transaction(req) - print(rsp) + voltage = rsp["value"] except Exception as exception: print("Transaction error: " + NotecardExceptionInfo(exception)) time.sleep(5) + return temp, voltage + def main(): """Connect to Notcard and run a transaction test.""" @@ -65,18 +93,26 @@ def main(): print("Opening Notecard...") try: if use_uart: - card = notecard.OpenSerial(port) + card = notecard.OpenSerial(port, debug=True) else: - card = notecard.OpenI2C(port, 0, 0) + card = notecard.OpenI2C(port, 0, 0, debug=True) 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) + # If success, configure the Notecard and send some data + configure_notecard(card) + temp, voltage = get_temp_and_voltage(card) + + req = {"req": "note.add"} + req["sync"] = True + req["body"] = {"temp": temp, "voltage": voltage} + + try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) main() diff --git a/examples/rpi-example.py b/examples/notecard-basics/rpi-example.py similarity index 61% rename from examples/rpi-example.py rename to examples/notecard-basics/rpi-example.py index 80eb977..1bda7dc 100644 --- a/examples/rpi-example.py +++ b/examples/notecard-basics/rpi-example.py @@ -12,6 +12,8 @@ import notecard # noqa: E402 +productUID = "com.your-company.your-project" + # Choose either UART or I2C for Notecard use_uart = True @@ -37,22 +39,48 @@ def NotecardExceptionInfo(exception): return "line " + s1 + ": " + s2 + ": " + ' '.join(map(str, exception.args)) -def transactionTest(card): +def configure_notecard(card): """Submit a simple JSON-based request to the Notecard. Args: card (object): An instance of the Notecard class """ - req = {"req": "card.status"} + req = {"req": "hub.set"} + req["product"] = productUID + req["mode"] = "continuous" try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) + + +def get_temp_and_voltage(card): + """Submit a simple JSON-based request to the Notecard. + + Args: + card (object): An instance of the Notecard class + + """ + temp = 0 + voltage = 0 + + try: + req = {"req": "card.temp"} + rsp = card.Transaction(req) + temp = rsp["value"] + + req = {"req": "card.voltage"} rsp = card.Transaction(req) - print(rsp) + voltage = rsp["value"] except Exception as exception: print("Transaction error: " + NotecardExceptionInfo(exception)) time.sleep(5) + return temp, voltage + def main(): """Connect to Notcard and run a transaction test.""" @@ -69,18 +97,27 @@ def main(): print("Opening Notecard...") try: if use_uart: - card = notecard.OpenSerial(port) + card = notecard.OpenSerial(port, debug=True) else: - card = notecard.OpenI2C(port, 0, 0) + card = notecard.OpenI2C(port, 0, 0, debug=True) 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) + # If success, configure the Notecard and send some data + configure_notecard(card) + temp, voltage = get_temp_and_voltage(card) + + req = {"req": "note.add"} + req["sync"] = True + req["body"] = {"temp": temp, "voltage": voltage} + + try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) main() diff --git a/examples/serial-example.py b/examples/notecard-basics/serial-example.py similarity index 67% rename from examples/serial-example.py rename to examples/notecard-basics/serial-example.py index ccb076d..65e3214 100644 --- a/examples/serial-example.py +++ b/examples/notecard-basics/serial-example.py @@ -12,6 +12,8 @@ import notecard # noqa: E402 +productUID = "com.your-company.your-project" + # For UART and I2C IO use_periphery = False if sys.implementation.name != 'cpython': @@ -38,22 +40,48 @@ def NotecardExceptionInfo(exception): return "line " + s1 + ": " + s2 + ": " + ' '.join(map(str, exception.args)) -def transactionTest(card): +def configure_notecard(card): """Submit a simple JSON-based request to the Notecard. Args: card (object): An instance of the Notecard class """ - req = {"req": "card.status"} + req = {"req": "hub.set"} + req["product"] = productUID + req["mode"] = "continuous" try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) + + +def get_temp_and_voltage(card): + """Submit a simple JSON-based request to the Notecard. + + Args: + card (object): An instance of the Notecard class + + """ + temp = 0 + voltage = 0 + + try: + req = {"req": "card.temp"} + rsp = card.Transaction(req) + temp = rsp["value"] + + req = {"req": "card.voltage"} rsp = card.Transaction(req) - print(rsp) + voltage = rsp["value"] except Exception as exception: print("Transaction error: " + NotecardExceptionInfo(exception)) time.sleep(5) + return temp, voltage + def main(): """Connect to Notcard and run a transaction test.""" @@ -82,11 +110,19 @@ def main(): raise Exception("error opening notecard: " + NotecardExceptionInfo(exception)) - # If success, do a transaction loop - print("Performing Transactions...") - while True: - time.sleep(2) - transactionTest(card) + # If success, configure the Notecard and send some data + configure_notecard(card) + temp, voltage = get_temp_and_voltage(card) + + req = {"req": "note.add"} + req["sync"] = True + req["body"] = {"temp": temp, "voltage": voltage} + + try: + card.Transaction(req) + except Exception as exception: + print("Transaction error: " + NotecardExceptionInfo(exception)) + time.sleep(5) main() diff --git a/examples/sensor-tutorial/circuit-python/code.py b/examples/sensor-tutorial/circuit-python/code.py new file mode 100644 index 0000000..5c9e390 --- /dev/null +++ b/examples/sensor-tutorial/circuit-python/code.py @@ -0,0 +1,48 @@ +"""note-python Circuit Python Sensor example. + +This file contains a complete working sample for using the note-python +library with Serial or I2C in Circuit Python to read from a BME680 sensor +and send those values to the Notecard. +""" +import board +import busio +import time +import json +import adafruit_bme680 +import notecard + +productUID = "com.your-company.your-project" + +# Select Serial or I2C with this flag +use_uart = True +card = None + +# Configure the Adafruit BME680 +i2c = busio.I2C(board.SCL, board.SDA) +bmeSensor = adafruit_bme680.Adafruit_BME680_I2C(i2c) + +# Configure the serial connection to the Notecard +if use_uart: + serial = busio.UART(board.TX, board.RX, baudrate=9600, debug=True) + card = notecard.OpenSerial(serial) +else: + card = notecard.OpenI2C(i2c, 0, 0, debug=True) + +req = {"req": "hub.set"} +req["product"] = productUID +req["mode"] = "continuous" +card.Transaction(req) + +while True: + temp = bmeSensor.temperature + humidity = bmeSensor.humidity + print("\nTemperature: %0.1f C" % temp) + print("Humidity: %0.1f %%" % humidity) + + req = {"req": "note.add"} + req["file"] = "sensors.qo" + req["start"] = True + req["body"] = {"temp": temp, "humidity": humidity} + card.Transaction(req) + + time.sleep(15) diff --git a/examples/sensor-tutorial/raspberry-pi-python/sensors.py b/examples/sensor-tutorial/raspberry-pi-python/sensors.py new file mode 100644 index 0000000..0976832 --- /dev/null +++ b/examples/sensor-tutorial/raspberry-pi-python/sensors.py @@ -0,0 +1,55 @@ +"""note-python Python Sensor example. + +This file contains a complete working sample for using the note-python +library with Serial or I2C in Python to read from a BME680 sensor +and send those values to the Notecard. +""" +import json +import notecard +from periphery import I2C, Serial +import time +import bme680 + +bme_sensor = bme680.BME680(bme680.I2C_ADDR_PRIMARY) + +bme_sensor.set_humidity_oversample(bme680.OS_2X) +bme_sensor.set_temperature_oversample(bme680.OS_8X) + +bme_sensor.get_sensor_data() + +productUID = "com.[your-company].[your-product]" + +# Select Serial or I2C with this flag +use_uart = True +card = None + +# Configure the serial connection to the Notecard +if use_uart: + serial = Serial('/dev/ttyS0', 9600) + card = notecard.OpenSerial(serial, debug=True) +else: + port = I2C("/dev/i2c-1") + card = notecard.OpenI2C(port, 0, 0, debug=True) + + +req = {"req": "hub.set"} +req["product"] = productUID +req["mode"] = "continuous" +card.Transaction(req) + +while True: + bme_sensor.get_sensor_data() + + temp = bme_sensor.data.temperature + humidity = bme_sensor.data.humidity + + print('Temperature: {} degrees C'.format(temp)) + print('Humidity: {}%'.format(humidity)) + + req = {"req": "note.add"} + req["file"] = "sensors.qo" + req["start"] = True + req["body"] = {"temp": temp, "humidity": humidity} + card.Transaction(req) + + time.sleep(15) diff --git a/setup.py b/setup.py index 4ba0a8e..9b57225 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setuptools.setup( name="note-python", - version="1.3.4", + version="1.3.5", author="Blues Inc.", author_email="support@blues.com", description="Cross-platform Python Library for the Blues Wireless Notecard,",