我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用machine.unique_id()。
def get_eui(): id = ubinascii.hexlify(unique_id()).decode() return mac2eui(id)
def mqtt_send(values): """Send key/value pairs over mqtt.""" device_name = rom_to_hex(bytearray(machine.unique_id())) mqtt = MQTTClient(device_name, MQTT_SERVER) mqtt.connect() # do mqtt ping/pong to ensure communication with broker is ok for x in range(CONNECT_WAIT): mqtt.ping() mqtt.sock.setblocking(False) res = mqtt.sock.read(1) if res == b"\xd0": sz = mqtt.sock.read(1)[0] if sz == 0: break time.sleep(1) else: return False for name, raw_value in values.items(): if isinstance(raw_value, float): value = b"{:4.3f}".format(raw_value) elif isinstance(raw_value, int): value = b"{:d}".format(raw_value) elif isinstance(raw_value, str): value = bytes(raw_value, 'utf8') mqtt.publish(b"templog/{}/{}".format(device_name, name), value) mqtt.disconnect()
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, }
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 }
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.
def chipid(): _chipid = '' _uid = unique_id() for _v in _uid: _chipid += str(_v) return _chipid
def configure(self, config): self.config = self.default_config.copy() self.config.update(config["Gateway"]) self.devices = [] if self.config['id'] is None: self.config['id'] = ubinascii.hexlify(uhashlib.sha256(machine.unique_id()).digest()).decode() # if self.config['version'] is None: # import network # self.topic = "/devices/" +ubinascii.hexlify(network.WLAN().config('mac')).decode() + "/esp8266" # else: self.topic = "/devices/" + self.config['id'] self.client = MQTTClient(self.topic, self.config['MQTTServer']['server'], self.config['MQTTServer']['port'], self.config['MQTTServer']['username'], self.config['MQTTServer']['password'], self.config['MQTTServer']['keep_alive']) self.client.set_callback(self.do_message_callback) self.exit_topic = self.topic+"/gateway/exit" for device_config in config["Devices"]: device_config.setdefault("module", None) device_config.setdefault("id", None) device_config.setdefault("description", None) try: class_name = device_config["type"] print(class_name) module_name = "dg_mqtt" if device_config["module"] is not None: module_name = device_config["module"] print(module_name) module = __import__(module_name, class_name) #module = sys.import_module(module_name) print(module) klass = getattr(module, class_name) #klass = module[class_name] print(klass) device = klass(device_config) self.devices.append(device) except ImportError as e: Util.log(self,"error: '{}' config: '{}'".format(e,device_config))
def do_get(clisock, uri, content_length): clisock.write(b'HTTP/1.0 200 OK\r\n' b'Content-type: text/html; charset=utf-8\r\n' b'\r\n') clisock.write(b'<!DOCTYPE html><html><head><title>Current time</title></head>') clisock.write(b'<body>The current time is: ') timestr ='{}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}'.format(*time.localtime()) clisock.write(timestr.encode('ascii')) del timestr uptime_s = int(time.ticks_ms() / 1000) uptime_h = int(uptime_s / 3600) uptime_m = int(uptime_s / 60) uptime_m = uptime_m % 60 uptime_s = uptime_s % 60 clisock.write(b'<p>Uptime: {:02d}h {:02d}:{:02d}'.format( uptime_h, uptime_m, uptime_s)) clisock.write(b'<p>Flash ID: {:x}'.format(esp.flash_id())) clisock.write(b'<p>Flash size: {:d}'.format(esp.flash_size())) clisock.write(b'<p>Python: {:s} on {:s} '.format(str(sys.implementation), sys.platform)) clisock.write(b'<p>Unique ID: ') for b in machine.unique_id(): clisock.write(b'{:02x}'.format(b)) clisock.write(b'\n<h2>Network interfaces</h2>\n') clisock.write(b'\n<table><tr><th>mac<th>active</th><th>connected</th><th>IP</th><th>Gateway</th>') for i in network.STA_IF, network.AP_IF: wlan = network.WLAN(i) # Show MAC address. clisock.write(b'<tr>') clisock.write(b'<td>') for b in wlan.config('mac'): clisock.write(b'{:02X}'.format(b)) clisock.write(b'<td>{:}</td>'.format(wlan.active())) clisock.write(b'<td>{:}</td>'.format(wlan.isconnected())) ifconfig = wlan.ifconfig() #ip, netmask, gateway, dns ifconfig = (ifconfig[0], ifconfig[2]) # ip, gw for item in ifconfig: clisock.write(b'<td>{:}</td>'.format(item)) clisock.write(b'\n</table>\n')