# ¶ CoFyBox Provisioning

Provisioning and registering a CoFyBox requires a number of steps:

- Downloading a base image
- Registering a new device with Balena and recording the device details
- Uploading these device details to the CoFyCloud service
- Flashing the image to an SD card and installing it in a RaspberryPi
- Connecting the Pi to a network

The CoFy Box images are stored on ASW S3 and are pre-loaded with the latest
version of the CoFy Box software.

## ¶ Requirements

### ¶ Hardware Requirements

- A Raspberry Pi 3 (Model B+)
- Micro USB charger to power the Cofybox
- A case for your Raspberry Pi (optional but recommended- we recommend the official Modular Raspberry Pi 3 Case)
- SD slot cover (optional- official cover for the above case)
- Ethernet cable

If provisioning your own SD card, you will need:

- A microSD card with at least 32 GB capacity
- A microSD card drive for your computer (built in or a USB one)

### ¶ Software Requirements

- Install Balena CLI (IMPORTANT: use the latest standalone version from Github)
- Install AWS S3 CLI (either package manager or AWS direct DL)
- Install jq (probably package manager)
- Install Balena Etcher

## ¶ Prerequisites

- A Balena Cloud account connected with the appropriate application.
- The base image or an AWS account with permissions to access the S3 bucket containing the images.
- Access to the CoFyCloud sandbox.

## ¶ Instructions

This folder contains the files and scripts used in HEMS provisioning which implement the provisioning flow.

### ¶ Download and register with Balena

> If you already have an image you can start at step 4.
> If creating a development device, download the image from Balena Dashboard using these steps instead (requires access to Cofyco Balena organisation).

1. Clone or download the Cofybox-balena project
2. Login to balena-cli with `sudo balena login` (if required)
3. Run 1_download_sdimg.sh to download latest image (this only needs to be done once in any session).
4. Run (with sudo) 2_prepare_sdimg.sh to create a new device on balena and generate an image for this device. The image will be named using the balena name e.g. happy-lion.img.
5. The script will record the device details in the `devices.log` file. These details are used to register the device with the CoFyCloud.
6. Open Balena Etcher and burn your new img from the `tmp/` folder to an SD card.
7. If the image burn was successful, cleanup by removing the image that you have just burned.
8. Repeat 4-6 if you need to burn more than one device.
9. Device data is appended to devices.log (for making labels etc.).

#### ¶ Raspberry Pi 3/4 and 32/64 bit

To select a different architecture edit `ARCH_NAME` in the `1_download_sdimg.sh` script (default is RPI3-32bit).

Possible choices are:

- rapsberrypi3
- rapsberrypi3-64
- rapsberrypi4-64

### ¶ Register with CoFyCloud

1. Log in to the Community Portal section of `https://app.cofybox.io`
2. Take the device details from the `devices.log` file generated above.
3. Either upload the file, or individually register a device.
4. Add the Balena device uuid (eg. `45b5050581d7489446cda2187058d33d`) as the Registration Token. Add the device name (eg. `bitter-lake`) as the reference.

### ¶ Connect the CoFyBox

1. Connect the CoFyBox to an internet connected router with an ethernet cable.

### ¶ Verify Provisioning

1. Log into Balena Cloud to verify that the device is running and available under the correct Balena app.
2. Log into the Community Portal section of `https://app.cofybox.io` and verify that the device status is a green tick.

### ¶ See Also

Installation and Commissioning