# Process of provision device
1. **Connect user and websocket**
* `npm install -g wscat`
* `wscat -c wss://{solution name}.apps.exosite-staging.com/api:1/phone`
2. **Send message via websocket**
* `{"id": 1, "request": "login", "data": {"token": "{hamv_admin token}"}}`
* should get the response `{"status":"ok","response":"login","id":1}`

:warning: For getting hamv_admin token:
(1) Go to [https://{solution name}.apps.exosite-staging.com/#/device](https://)
(2) Click right button and choose `inspect`

(3) Click `Application` ono the tab bar and get token under the `storage` ➝ `local storage`

3. **Create device via api**
```
curl --request PUT \
--url 'https://bizapi.hosted.exosite-staging.com/api:1/service/{product id}/device2/identity/{random device id }106101733?locked=false' \
--header 'Authorization: token {murano token}' \
--data '{"locked":false}'
```
* Should see the whitelisted device show on your Product page

4. **Activate device and get the device token**
`host`={product id}.m2.exosite-staging.com
`sn`= `device id` you chose in **step 3-1**
```
def activate_device(self, host, sn):
device_token = {}
client = mqtt.Client(client_id="")
client.tls_set(
ca_certs=self.cert,
cert_reqs=ssl.CERT_NONE
)
client.tls_insecure_set(True)
def on_connect(client, userdata, flags, rc):
print("Connected with code {}".format(rc))
provision_str = "$provision/" + sn
client.publish(provision_str, None, qos=0)
def on_disconnect(client, userdata, rc):
print("Disconnected with code {}".format(rc))
def on_message(client, userdata, message):
device_token["token"] = message.payload.decode()
print("Activated, token: {}".format(device_token["token"]))
client.disconnect()
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.connect(host, 443)
client.loop_forever()
return device_token["token"]
```
* Then you will see the device from whitelisted state become provisioned state

* It should response the device token, and we will use in **step 6**
5. **Send message via websocket and get the provision token**
* `{"id": 1, "request": "provision_token", "data": {"expires_in": 2592000}}`
* Should get the response which contain `"status":"ok"`
* get the token in the response, we will use this in **step 7**

6. **Get the MQTT client**
`token`= the `device token` you get in **step 4**
```
def mqtt_connect(self, host, token=None):
client = mqtt.Client()
client.tls_set(
ca_certs=self.cert,
cert_reqs=ssl.CERT_NONE
)
client.tls_insecure_set(True)
client.username_pw_set("", token)
def on_connect(client, userdata, flags, rc):
print("Connected with code {}".format(rc))
def on_disconnect(client, userdata, rc):
if rc != 0:
print("Disconnected with code {}".format(rc))
client.disconnect()
def on_message(client, userdata, message):
topic = message.topic
data = message.payload.decode(encoding="UTF-8")
client.messages.append({"topic": topic, "message": data});
print("Received: {} from Topic: {}".format(data, topic))
client.on_connect = on_connect
client.on_message = on_message
client.on_disconnect = on_disconnect
client.messages = []
client.connect(host, 443)
self.clients.append(client)
thread = threading.Thread(target=client.loop_forever)
thread.start()
time.sleep(1)
return client
```
7. **Provision device**
`client`= MQTT client
`token`= provision token in **step 5**
`mac_address`= use the substring of device id
`model` and `firmware_version` are in random
```
def provision_device(self, client, token, mac_address, model, firmware_version):
esh = {"class": 0, "esh_version": "4.00", "device_id": 1, "brand": "exosite", "model": model}
model = {"firmware_version": firmware_version, "id": mac_address, "mac_address": mac_address, "local_ip":"192.168.0.13", "ssid":"Exosite-a83c" }
fields = ["H00", "H01", "H02", "H03", "H04", "H05", "H06", "H0B", "H0C", "H0E", "H11", "H14", "H17", "H1D", "H1E", "H1F", "H20", "H21", "H24", "H29"]
cert = {}
schedules = []
self.states = {"H00": 0, "H01": 0, "H02": 0, "H03": 0, "H04": 0, "H05": 0, "H06": 0, "H0B": 0, "H0C": 0, "H0E": 0, "H11": 0, "H14": 0, "H17": 0, "H1D": 0, "H1E": 0, "H1F": 0, "H20": 0, "H21": 0, "H24": 0, "H29": 0}
ota = {"state": "idle"}
self.mqtt_publish(client, "$resource/esh", json.dumps(esh))
self.mqtt_publish(client, "$resource/module", json.dumps(model))
self.mqtt_publish(client, "$resource/fields", json.dumps(fields))
self.mqtt_publish(client, "$resource/cert", json.dumps(cert))
self.mqtt_publish(client, "$resource/schedules", json.dumps(schedules))
self.mqtt_publish(client, "$resource/states", json.dumps(self.states))
self.mqtt_publish(client, "$resource/ota", json.dumps(ota))
self.mqtt_publish(client, "$resource/token", token)
ownerId = self._get_message(client, "$resource/owner", ".*")
self.mqtt_publish(client, "$resource/owner", ownerId)
```
8. should get response like
