2018年5月22日 星期二

ble MESH : how to load provision data to serial example after restart (nordic pyaci supplement)

This article is a supplement of Interactive mesh provisioning and configuration on Nordic website


the introduction of Serial example of nRF52 ble MESH SDK
only teach how to provision a node.

However, the provision data is stored in .JSON outside the chip. We have to load the provisioned nodes information into the nRF52 to act as client after nRF52 Serial restart.

The following step shows how to :

for example, if I have provisioned a node "0577"
devicekey = 0
address key = 8



then after Serial example restart,  I use interactive_pyaci.py to load the provision data :

In [1]: db = MeshDB("database/example_database.json")

In [2]: p = Provisioner(device, db)

In [3]: 2018-05-17 15:37:26,908 - INFO - COM4: Success
2018-05-17 15:37:26,911 - INFO - COM4: Success
2018-05-17 15:37:26,916 - INFO - COM4: SubnetAdd: {'subnet_handle': 0}
2018-05-17 15:37:26,919 - INFO - COM4: AppkeyAdd: {'appkey_handle': 0}
2018-05-17 15:37:26,919 - INFO - COM4: AppkeyAdd: {'appkey_handle': 1}

In [3]: device.send(cmd.AddrPublicationAdd(db.nodes[0].unicast_address))

2018-05-17 15:38:09,309 - INFO - COM4: AddrPublicationAdd: {'address_handle': 0}
In [4]: sc = SimpleOnOffClient()

In [5]: device.model_add(sc)

In [6]: sc.publish_set(0, 0)

In [7]: sc.set(True)

and this sentence:

device.send(cmd.AddrPublicationAdd(db.nodes[0].unicast_address))

is how to load data of one provisioned node into the chip 


reference:
https://devzone.nordicsemi.com/f/nordic-q-a/34254/add-delete-nodes-set-sub-net-function-in-mesh-sdk-serial-example/132390#132390

ble MESH:how to delete a provisioned-node in Serial example (Pyaci)

In this April , Nordic Semiconductor release their Mesh SDK version 2.0.1
in Serial example, this example let:
- a nRF52 connect with a Windows or embedded system with UART
- a nRF52 (program serial example)act as provisioner and config client
- the provision and config records store in JSON database in  a Windows or embedded system
- the Windows or embedded system  control the nRF52 with python script
(named  interactive_pyaci.py)

for detail plz check in Nordic website:
serial example 


However the scripts doesn't have function to delete provisioned records.
That means if the node is reset, this serial example can not provision it again.
Or, if a node leave this Mesh network, the provision is not able to delete it.

In this article , I implement a script to delete the record.
name as delete_provisioned_node.py


#power by Agatha Kuan 05/16/2018
import json as JSON
import os
import sys

def check_provisioned_node(file_name):
    with open(file_name) as f:
        load_in = JSON.load(f)
        print(load_in["nodes"])


        for p in load_in["nodes"]:
            print("device name = "+p["name"])


def delete_node(undeleted_node,file_name):
    with open(file_name) as f:
        load_in = JSON.load(f)
        for p in load_in["nodes"]:
            print("list name = "+p["name"])

        for i in range(len(load_in["nodes"])):
            if load_in["nodes"][i]["name"] == undeleted_node:
                load_in["nodes"].pop(i)
                print("success delete ", undeleted_node)
                break

        open(file_name, "w").write(JSON.dumps(load_in, sort_keys=False, indent=4, separators=(',', ':')))


def main():
#sys.argv[0]  是這個腳本的檔名
    if("-c" == sys.argv[1]):
        print("check recent node")
        print("open file:", sys.argv[2])
        check_provisioned_node(sys.argv[2])

    elif("-d" == sys.argv[1]):
        print("delete device :",sys.argv[2])
        delete_node(sys.argv[2],sys.argv[3])

    elif("?" == sys.argv[1]):
        print("Functions:")
        print("1.for check provisioned node:")
        print("     -c FILE_NAME e.g -c example_database.json")
        print("2.after checking nodes, to select which to delete:")
        print("     -d DEVICE_NAME FILE_NAME e.g -d node_01 example_database.json")




if __name__ == "__main__":
    main()


Demostration:
I provision a node "0577"

in database will add a nodes record:

"nodes": [
    {
      "UUID": "0059ffff0000000009f5cf71a0100577",
      "appKeys": [
        0
      ],
      "cid": "0059",
      "configComplete": false,
      "crpl": 32,
      "deviceKey": "dd5baeb65d8ab9eb709c45397a285ab4",
      "elements": [
        {
          "index": 0,
          "location": "0000",
          "models": [
            {
              "modelId": "0000"
            },
            {
              "modelId": "0002"
            },
            {
              "bind": [
                0
              ],
              "modelId": "00590000"
            }
          ]
        }
      ],
      "features": {
        "friend": 2,
        "lowPower": 2,
        "proxy": 2,
        "relay": 0
      },
      "name": "0577",
      "netKeys": [
        0
      ],
      "pid": "0000",
      "security": "low",
      "unicastAddress": 16,
      "vid": "0000"
    }
]

Than put "delete_provisioned_node.py " in :
../script/interactive_pyaci/database


and run it in CMD :

$ database python delete_provisioned_node.py ?

it will show you operation command



So, if I want to delete "0577" , command

$ database python delete_provisioned_node.py -d 0577 example_database.json



reference:
http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.meshsdk.v2.0.1%2Fmd_scripts_interactive_pyaci_doc_demo_configuration.html&cp=4_1_0_1_1_3_2