Python machine.unique_id() Examples

The following are 15 code examples of machine.unique_id(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module machine , or try the search function .
Example #1
Source File: thingspeak.py    From upython-aq-monitor with MIT License 7 votes vote down vote up
def send_to_thingspeak(datapoints):
    mean_data = DataPoint.mean(datapoints)

    thingspeak_data = mean_data.to_thingspeak()
    print('sending data\n{}'.format(thingspeak_data))

    success = False
    number_of_retries = 3

    while not success and number_of_retries > 0:
        try:
            client_id = binascii.hexlify(machine.unique_id())
            client = MQTTClient(client_id, 'mqtt.thingspeak.com', user='wipy#1', password=MQTT_API_KEY, port=8883, ssl=True)
            client.connect()
            client.publish(topic='channels/379710/publish/{}'.format(MQTT_WRITE_API_KEY), msg=thingspeak_data)
            client.disconnect()
            success = True
        except OSError as e:
            print('network error: {}'.format(e.errno))
            number_of_retries -= 1
            pass

    return success 
Example #2
Source File: oled_test.py    From SX127x_driver_for_MicroPython_on_ESP8266 with GNU General Public License v3.0 5 votes vote down vote up
def get_eui():
    id = ubinascii.hexlify(unique_id()).decode()
    return mac2eui(id) 
Example #3
Source File: mqtt.py    From micropython-mqtt with MIT License 5 votes vote down vote up
def __init__(self, channel, config):
        self.channel = channel
        self.subscriptions = {}
        # Config defaults:
        # 4 repubs, delay of 10 secs between (response_time).
        # Initially clean session.
        config['subs_cb'] = self.subs_cb
        config['wifi_coro'] = self.wifi_han
        config['connect_coro'] = self.conn_han
        config['client_id'] = ubinascii.hexlify(unique_id())
        super().__init__(config)

    # Get NTP time or 0 on any error. 
Example #4
Source File: main.py    From uPyEcho with Apache License 2.0 5 votes vote down vote up
def __init__(
        self,
        listener,
        poller,
        port,
        root_url,
        server_version,
        persistent_uuid,
        other_headers=None,
        ip_address=None,
    ):
        self.listener = listener
        self.poller = poller
        self.port = port
        self.root_url = root_url
        self.server_version = server_version
        self.persistent_uuid = persistent_uuid
        self.uuid = machine.unique_id()
        self.other_headers = other_headers

        if ip_address:
            self.ip_address = ip_address
        else:
            self.ip_address = upnp_device.local_ip_address()

        self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.socket.bind((self.ip_address, self.port))
        self.socket.listen(5)
        if self.port == 0:
            self.port = self.socket.getsockname()[1]
        self.poller.add(self)
        self.client_sockets = {}
        self.listener.add_device(self) 
Example #5
Source File: __init__.py    From platypush with MIT License 5 votes vote down vote up
def unique_id(self, **kwargs) -> str:
        """
        Get the unique ID of the device.
        t will vary from a board/SoC instance to another, if underlying hardware allows. Length varies by hardware
        (so use substring of a full value if you expect a short ID). In some MicroPython ports, ID corresponds to
        the network MAC address..

        :param kwargs: Parameters to pass to :meth:`platypush.plugins.esp.EspPlugin.execute`.
        """
        code = '''
import machine
print(':'.join(['{:02x}'.format(b) for b in machine.unique_id()]))
'''

        return self.execute(code, **kwargs).output 
Example #6
Source File: stats_api.py    From esp8266 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_machine_stats(self):
        import machine
        import ubinascii
        id = "0x{}".format(ubinascii.hexlify(machine.unique_id()).decode().upper())
        return {
            'freq': machine.freq(),
            'unique_id': id
        } 
Example #7
Source File: api.py    From esp8266 with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def get_sys_stats(self):
        return {
            'machine_id': "0x{}".format(ubinascii.hexlify(machine.unique_id()).decode().upper()),
            'machine_freq': machine.freq(),
            'byteorder': sys.byteorder,
            'system': "{}-{}".format(
                sys.implementation[0],
                self.to_version_string(sys.implementation[1]),
            ),
            'maxsize': sys.maxsize,
            'modules': self.keys(sys.modules),
            'path': sys.path,
            'platform': sys.platform,
            'version': sys.version,
        } 
Example #8
Source File: sys_vars.py    From pysmartnode with MIT License 5 votes vote down vote up
def getDeviceID():
    if platform == "linux":
        from pysmartnode import config
        return config.DEVICE_NAME
    import machine
    return ubinascii.hexlify(machine.unique_id()).decode() 
Example #9
Source File: config_lora.py    From uPyLoRaWAN with Apache License 2.0 5 votes vote down vote up
def get_nodename():
    uuid = ubinascii.hexlify(machine.unique_id()).decode()
    node_name = "ESP_" + uuid
    return node_name 
Example #10
Source File: utils.py    From microhomie with MIT License 5 votes vote down vote up
def get_unique_id():
    if LINUX is False:
        return hexlify(unique_id()).decode()
    else:
        raise NotImplementedError(
            "Linux doesn't have a unique id. Provide the DEVICE_ID option in your settings.py."
        ) 
Example #11
Source File: mqtt_as.py    From microhomie with MIT License 5 votes vote down vote up
def unique_id():
        raise NotImplementedError("Linux doesn't have a unique id. Provide the argument client_id")


# Default "do little" coro for optional user replacement 
Example #12
Source File: mqtt_as.py    From microhomie with MIT License 5 votes vote down vote up
def __init__(self, client_id=None,
                 server=None,
                 port=0,
                 user='',
                 password='',
                 keepalive=60,
                 ping_interval=0,
                 ssl=False,
                 ssl_params={},
                 response_time=10,
                 clean_init=True,
                 clean=True,
                 max_repubs=4,
                 will=None,
                 subs_cb=lambda *_: None,
                 wifi_coro=None,
                 connect_coro=None,
                 ssid=None,
                 wifi_pw=None):
        client_id = client_id or hexlify(unique_id())
        wifi_coro = wifi_coro or eliza
        connect_coro = connect_coro or eliza
        super().__init__(client_id, server, port, user, password, keepalive, ping_interval,
                         ssl, ssl_params, response_time, clean_init, clean, max_repubs, will,
                         subs_cb, wifi_coro, connect_coro, ssid, wifi_pw)
        self._isconnected = False  # Current connection state
        keepalive = 1000 * self._keepalive  # ms
        self._ping_interval = keepalive // 4 if keepalive else 20000
        p_i = self.ping_interval * 1000  # Can specify shorter e.g. for subscribe-only
        if p_i and p_i < self._ping_interval:
            self._ping_interval = p_i
        self._in_connect = False
        self._has_connected = False  # Define 'Clean Session' value to use.
        if ESP8266:
            import esp
            esp.sleep_type(0)  # Improve connection integrity at cost of power consumption. 
Example #13
Source File: compat.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def monkeypatch_machine():

    from mock import Mock

    import uuid
    import machine

    # Some primitives.
    machine.enable_irq = Mock()
    machine.disable_irq = Mock()
    machine.unique_id = lambda: str(uuid.uuid4().fields[-1])[:5].encode()
    machine.freq = Mock(return_value=42000000)
    machine.idle = Mock()

    # Reset cause and wake reason.
    machine.PWRON_RESET = 0
    machine.HARD_RESET = 1
    machine.WDT_RESET = 2
    machine.DEEPSLEEP_RESET = 3
    machine.SOFT_RESET = 4
    machine.BROWN_OUT_RESET = 5

    machine.PWRON_WAKE = 0
    machine.GPIO_WAKE = 1
    machine.RTC_WAKE = 2
    machine.ULP_WAKE = 3

    machine.reset_cause = Mock(return_value=0)
    machine.wake_reason = wake_reason 
Example #14
Source File: util.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_device_id():
    """ 
    MAC address of device if supported.
    """
    import machine
    from ubinascii import hexlify
    return hexlify(machine.unique_id()).decode() 
Example #15
Source File: pycom.py    From terkin-datalogger with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_device_id():
    """ """
    import machine
    from ubinascii import hexlify
    return hexlify(machine.unique_id()).decode()