def _main():
    api = ConnectAPI()
    # calling start_notifications is required for getting/setting resource synchronously
    api.start_notifications()
    devices = api.list_connected_devices().data
    if not devices:
        raise Exception("No connected devices registered. Aborting")

    # Synchronously get the initial/current value of the resource
    while True:
        value = api.get_resource_value(devices[0].id, BUTTON_RESOURCE)
        _current_val(value)
        val1 = value.encode()
        e1 = f.encrypt(val1)

        os.system("mosquitto_pub -h localhost -t temp -m " + str(e1) +
                  " -u \"roy\" -P \"roy\"")
        value2 = api.get_resource_value(devices[0].id, BUTTON_RESOURCE2)
        _current_val2(value2)

        val2 = value2.encode()

        e2 = f.encrypt(val2)
        os.system("mosquitto_pub -h localhost -t heart -m " + str(e2) +
                  " -u \"roy\" -P \"roy\"")
        a = [[float(value), float(value2)]]
        predict_from_joblib = joblib.load('/home/roy/disco/predict.pkl')
        b = predict_from_joblib.predict(a)
        pre = b[0]
        print(pre)
        os.system("mosquitto_pub -h localhost -t predict -m " + str(pre))

    # Register a subscription for new values
    api.add_resource_subscription_async(devices[0].id, BUTTON_RESOURCE,
                                        _subscription_handler)
def _main():
    api = ConnectAPI()
    # calling start_notifications is required for getting/setting resource synchronously
    api.start_notifications()
    devices = api.list_connected_devices().data
    if not devices:
        raise Exception("No connected devices registered. Aborting")

    # Synchronously get the initial/current value of the resource
    value = api.get_resource_value(devices[0].id, BUTTON_RESOURCE)
    _current_val(value)

    # Register a subscription for new values
    api.add_resource_subscription_async(devices[0].id, BUTTON_RESOURCE, _subscription_handler)

    # Run forever
    while True:
        pass
Пример #3
0
def main(arguments):

    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument("api_key", help="Mbed Cloud API Key")
    parser.add_argument("device_id", help="Device ID")
    parser.add_argument("-H",
                        "--host",
                        help="Mbed Cloud API URL",
                        default="https://api.us-east-1.mbedcloud.com")

    args = parser.parse_args(arguments)
    connect_api = ConnectAPI({"api_key": args.api_key, "host": args.host})

    connect_api.start_notifications()
    connect_api.add_resource_subscription_async(args.device_id, "/3200/0/5501",
                                                _subscription_handler)

    while True:
        pass
def _main():
    api = ConnectAPI()
    api.start_notifications()
    devices = api.list_connected_devices().data
    if not devices:
        raise Exception("No connected devices registered. Aborting")
    device_id = "015e9a938ff100000000000100100019"
    api.delete_device_subscriptions(device_id)
    api.list_presubscriptions()

    api.add_resource_subscription_async(device_id, INCRIMENTAL_RESOURCE,
                                        _callback_fn)
    api.add_resource_subscription_async(device_id, VOLTAGE_RESOURCE,
                                        _callback_fn)
    api.add_resource_subscription_async(device_id, CURRENT_RESOURCE,
                                        _callback_fn)
    api.add_resource_subscription_async(device_id, POWER_RESOURCE,
                                        _callback_fn)
    while True:
        time.sleep(0.1)
from mbed_cloud import ConnectAPI

TOKEN = "YOUR_ACCESS_KEY"

# set up the Python SDK
config = {}
config['api_key'] = os.getenv('TOKEN') or TOKEN
config['host'] = 'https://api.us-east-1.mbedcloud.com'
api = ConnectAPI(config)
api.start_notifications()

devices = list(api.list_connected_devices(filters={'device_type': 'default'}))

print("Found %d devices" % (len(devices)), [c.id for c in devices])

for device in devices:

    def pir_callback(device_id, path, distance):
        print("Distance changed at %s, new value is %s" %
              (device_id, distance))

    api.add_resource_subscription_async(device.id, '/3330/0/5700',
                                        pir_callback)
    print("subscribed to resource")

    api.execute_resource(device.id, '/3201/0/5850', '1')
    print("blinked LED of %s" % device.id)

# Run forever
while True:
    pass