# place here your wifi configuration wifi.link("SSID", wifi.WIFI_WPA2, "PSW") pkey = helpers.load_key('private.hex.key') device_conf = helpers.load_device_conf() publish_period = 5000 # choose an appropriate way to get a valid timestamp (may be available through hardware RTC) def get_timestamp(): user_agent = {"user-agent": "curl/7.56.0"} return json.loads( requests.get('http://now.httpbin.org', headers=user_agent).content)['now']['epoch'] # create a google cloud device instance, connect to mqtt broker, set config callback and start mqtt reception loop device = iot.Device(device_conf['project_id'], device_conf['cloud_region'], device_conf['registry_id'], device_conf['device_id'], pkey, get_timestamp) device.mqtt.connect() device.on_config(config_callback) device.mqtt.loop() while True: print('publish random sample...') device.publish_event(json.dumps({'asample': random(0, 10)})) sleep(publish_period)
wifi.link(device_conf['wifi_ssid'], wifi.WIFI_WPA2, device_conf['wifi_password']) print('> linked') sampling_period = 2000 ateccx08a.hwcrypto_init( I2C0, 2, i2c_addr=device_conf['i2caddr'], dev_type=helpers.conf2atecctype[device_conf['devtype']]) # create a google cloud device instance, connect to mqtt broker, set config callback and start mqtt reception loop device = iot.Device(device_conf['project_id'], device_conf['cloud_region'], device_conf['registry_id'], device_conf['device_id'], '', helpers.get_timestamp, custom_jwt=ateccx08a.encode_jwt) print('> mqtt connect') device.mqtt.connect() print('> connected') device.on_config(config_callback) while True: print('> publish sample.') rand_val = random(0, 10) device.publish_event({'rand': rand_val}) sleep(sampling_period)