Пример #1
0
def main():
    c.set_callback(sub_cb)
    if not wait_network():
        print('Cannot connect WiFi')
        raise Exception('Cannot connect WiFi')

    c.connect()
    if conf.data['orgID'] != 'quickstart':
        c.subscribe(ledCommandTopic)
        c.subscribe(irCommandTopic)
    print('Connected, waiting for event ({})'.format(conf.data['deviceID']))

    status = {'d': {'sine': {}}}
    count = 0
    try:
        while True:
            status['d']['sine'] = sineVal(-1.0, 1.0, 16, count)
            count += 1
            c.publish(statusTopic, json.dumps(status))
            time.sleep_ms(10000)
            #c.wait_msg()
            c.check_msg()
    finally:
        c.disconnect()
        print('Disonnected')
Пример #2
0
 def check_network(self):
     self.view_manager.open_status_box()
     self.view_manager.set_status_text('Wait for network')
     if not util.wait_network():
         self.view_manager.set_status_text('Cannot connect WiFi')
         raise Exception('Cannot connect WiFi')
     self.view_manager.set_status_text('Listing RPS games')
     self.view_manager.close_status_box()
Пример #3
0
    def init_iotf(self):
        # Show
        self.view_manager.open_status_box()

        # Check for network availability
        self.view_manager.set_status_text('Waiting for network')
        if not util.wait_network():
            self.view_manager.set_status_text('Cannot connect WiFi')
            raise Exception('Cannot connect WiFi')

        sta_if = network.WLAN(network.STA_IF)
        mac_addr = ''.join('{:02X}'.format(c) for c in sta_if.config('mac'))

        orgId = iotfcfg.orgId
        deviceType = iotfcfg.deviceType
        deviceId = iotfcfg.deviceId if hasattr(iotfcfg,
                                               'deviceId') else mac_addr
        user = '******'
        authToken = iotfcfg.authToken

        clientID = 'd:' + orgId + ':' + deviceType + ':' + deviceId
        broker = orgId + '.messaging.internetofthings.ibmcloud.com'

        # Check for device registration
        url = 'https://hongjs-nodered.mybluemix.net/api/badge2018/type/{}/register'.format(
            deviceType)
        payload = {
            'deviceId': deviceId,
            'authToken': authToken,
            'deviceInfo': {},
            'groups': [],
            'location': {},
            'metadata': {}
        }
        headers = {'token': 'helloiot'}
        r = urequests.post(url, json=payload, headers=headers)
        if r.status_code == 201:
            print('OK')
        elif r.status_code == 409:
            print('Already Exists')
        else:
            print(r.text)
            raise Exception(r.text)
        r.close()

        self.deviceId = deviceId

        self.mqtt = simple.MQTTClient(clientID,
                                      broker,
                                      user=user,
                                      password=authToken,
                                      ssl=True)
        self.mqtt.set_callback(self.sub_cb)
        self.mqtt.connect()
        self.mqtt.subscribe(self.COMMAND_TOPIC_RPS)

        # Close Popup
        self.view_manager.close_status_box()
Пример #4
0
    def init_iotf(self):
        # Check for network availability
        print('Waiting for network')
        if not util.wait_network(3):
            print('Cannot connect WiFi')
            raise Exception('Cannot connect WiFi')

        sta_if = network.WLAN(network.STA_IF)
        mac_addr = ''.join('{:02X}'.format(c) for c in sta_if.config('mac'))

        orgId = iotfcfg.orgId
        deviceType = iotfcfg.deviceType
        deviceId = iotfcfg.deviceId if hasattr(iotfcfg,
                                               'deviceId') else mac_addr
        user = '******'
        authToken = iotfcfg.authToken

        clientID = 'd:' + orgId + ':' + deviceType + ':' + deviceId
        broker = orgId + '.messaging.internetofthings.ibmcloud.com'

        self.deviceId = deviceId

        if orgId == 'quickstart':
            self.mqtt = simple.MQTTClient(clientID, broker)
        else:
            self.mqtt = simple.MQTTClient(clientID,
                                          broker,
                                          user=user,
                                          password=authToken,
                                          ssl=True)
        self.mqtt.set_callback(self.sub_cb)
        self.mqtt.connect()

        if orgId == 'quickstart':
            print(
                'https://quickstart.internetofthings.ibmcloud.com/?deviceId=test#/device/{}'
                .format(deviceId))
        else:
            self.mqtt.subscribe(self.COMMAND_TOPIC_LED)
            self.mqtt.subscribe(self.COMMAND_TOPIC_BUZZER)

        print('DeviceID is {}'.format(deviceId))
        print('IoT Ready')
Пример #5
0
    def list_apps(self):
        self.create_status_box()
        self.set_status('Waiting for network')
        if not util.wait_network():
            self.set_status('Cannot connect WiFi')
            raise Exception('Cannot connect WiFi')
        self.set_status('Downloading app list')
        apps = ota.download_json(self.LIST_URL)
        self.close_status_box()
        w = self.window
        ugfx.set_default_font('IBMPlexSans_Regular18')
        self.app_list = ugfx.List(5, 50, 150, w.height() - 60, parent = w)
        self.app_list.visible(False)
        self.apps = []
        for slug, app in apps.items():
            app['installed'] = False
            app['slug'] = slug
            app['upgrade'] = False
            app['ver_string'] = '{}'.format(app['version'])
            try:
                with open('/apps/{}/app.json'.format(slug)) as fp:
                    data = json.load(fp)
                app['installed'] = data['version']
                if app['version'] != app['installed']:
                    app['upgrade'] = True
                    app['ver_string'] = '{} -> {}'.format(
                        app['installed'], app['version'])
            except Exception as e:
                print(e)
            self.apps.append(app)
            self.app_list.add_item(app['name'] if 'name' in app else slug)
        self.app_list.visible(True)

        #ugfx.input_attach(ugfx.BTN_A, self.network_selected)
        ugfx.input_attach(ugfx.BTN_B, util.reboot)
        self.widgets.append(self.app_list)
Пример #6
0
 def check_network(self):
     print('Wait for network')
     if not util.wait_network():
         print('Cannot connect WiFi')
         raise Exception('Cannot connect WiFi')
     print('Ready')
Пример #7
0
 def check_network(self):
     self.set_status_text('Wait for network')
     if not util.wait_network():
         raise Exception('Cannot connect WiFi')
     self.set_status_text('Network is ready')