def get_wifi_info_from_user(): camera = CameraRGB() for i, frame in enumerate(camera.stream()): frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) stream_frame(frame) qrcodes = myzbarlight.qr_scan(frame) if len(qrcodes) > 0: qr_data = qrcodes[0] try: qr_data = json.loads(qr_data.decode('utf-8')) ssid = qr_data['s'] password = qr_data['p'] break except: # We got invalid data from the QR code, which is # fine, we'll just move on with life and try again. pass if (i % 100) == 99: # Every 100 frames, we'll check to see if WiFi magically came back. if wireless.current() is not None: ssid = None password = None break console.clear_image() camera.close() return ssid, password
def ensure_token(): token = STORE.get('DEVICE_TOKEN', None) if token is not None: # We have a token. All is well. return console.big_image('images/token_error.png') console.big_status('Ready to receive login token.') camera = CameraRGB() system_password = None for i, frame in enumerate(camera.stream()): frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) stream_frame(frame) qrcodes = myzbarlight.qr_scan(frame) if len(qrcodes) > 0: qr_data = qrcodes[0] try: qr_data = json.loads(qr_data.decode('utf-8')) token = qr_data['t'] if 'p' in qr_data: # This allows us to override the default system_password for special-purpose devices. # The default is just... a default. No matter what is set here, it can be changed later. system_password = qr_data['p'] break except: pass console.big_image('images/token_success.png') console.big_status('Success. Token: {}...'.format(token[:5])) camera.close() STORE.put('DEVICE_TOKEN', token) print_all("Stored Device token: {}...".format(token[:5])) jupyter_password = util.token_to_jupyter_password(token) STORE.put('DEVICE_JUPYTER_PASSWORD', jupyter_password) print_all("Stored Jupyter password: {}...".format(jupyter_password[:2])) if system_password is None: # If a particular default system_password was not specified, we will generate a good # default system_password from the token. This is a good thing, since it ensures that # each device is given a strong, unique default system_password. system_password = util.token_to_system_password(token) util.change_system_password(system_priv_user, system_password) print_all("Successfully changed {}'s password!".format(system_priv_user)) time.sleep(5) console.big_clear() console.clear_image()
def stream(frame, to_console=True, to_labs=False, verbose=False): """ Stream the given `frame` (a numpy ndarray) to your device's console _and_ (optionally) to your `labs` account to be shown in your browser. The `frame` parameter must be a numpy ndarray with one of the following shapes: - (h, w, 3) meaning a single 3-channel RGB image of size `w`x`h` - (h, w, 1) meaning a single 1-channel gray image of size `w`x`h` - (h, w) meaning a single 1-channel gray image of size `w`x`h` """ if frame is None: if to_console: console.clear_image() if to_labs: send_message_to_labs({'base64_img': ''}) return # Publish the uncompressed frame to the console UI. if to_console: if frame.ndim == 3: if frame.shape[2] == 3: pass # all good elif frame.shape[2] == 1: pass # all good else: raise Exception("invalid number of channels") elif frame.ndim == 2: frame = np.expand_dims(frame, axis=2) assert frame.ndim == 3 and frame.shape[2] == 1 else: raise Exception(f"invalid frame ndarray ndim: {frame.ndim}") height, width, channels = frame.shape aspect_ratio = width / height if aspect_ratio != OPTIMAL_ASPECT_RATIO: final_frame = _add_white_bars(frame) height, width, channels = final_frame.shape else: final_frame = frame shape = [width, height, channels] rect = [0, 0, 0, 0] console.stream_image(rect, shape, final_frame.tobytes()) # Convert the frame to a JPG buffer and publish to the network connection. if to_labs: base64_img = base64_encode_image(frame) send_message_to_labs({'base64_img': base64_img}) if verbose: h, w = frame.shape[:2] print_all("Streamed frame of size {}x{}.".format(w, h))
else: wireless.delete_connection(ssid) msg = 'Disconnected.\nPlease use another WiFi network.' log.info(msg) console.big_image('images/wifi_error.png') console.big_status(msg) time.sleep(2) else: msg = 'WiFi credentials did not work.\nDid you type them correctly?\nPlease try again.' log.info(msg) console.big_image('images/wifi_error.png') console.big_status(msg) else: log.info("Success! Connected to SSID: {}".format(ssid)) console.big_image('images/wifi_success.png') console.big_status('WiFi connection success!') time.sleep(5) print_connection_info() break console.big_clear() console.clear_image() update_and_reboot_if_no_token() else: # We have WiFi. # After WiFi, we care that we have a Token so that we can authenticate with the CDP. ensure_token() time.sleep(5)