Skip to content

MexbaliaMX/ConnectIoT-SC

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConnectIoT

Arq,use

Mexbalia Mexbalia Mexbalia

Access services Smart Contract, which allows creating the connection between IoT devices and the Near Protocol Blockchain.

Arq,use


Features

  • Create a registry for a device group
  • Add device to registry
  • Set/Get device data
  • Set/Get device metadata
  • Set/Get device data parameters
  • Set/Get device metadata parameters

Prerequisites

  1. Install rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 
  • Restart the terminal

  • Add wasm target to your tool chain:

rustup target add wasm32-unknown-unknown

Visit https://www.rust-lang.org/tools/install for more info.

  1. Install node.js version>=12 (https://nodejs.org/es/download/package-manager/)

  2. Install Near-CLI

sudo npm i -g near-cli
  1. Create a Near testnet account in (https://wallet.testnet.near.org/)

  2. Account access

near login
  1. Install git
sudo apt install git
  1. Clone the project
git clone https://github.com/paul-cruz/ConnectIoT.git
  1. Go to the project directory
cd Connect-IoT
  1. Install libsqlite3-dev
sudo apt install libsqlite3-dev
  1. Install build-essential
sudo apt install build-essential
  1. Compile release version
cargo build --target wasm32-unknown-unknown --release

Deployment

You can check the contract ID in the .env file. To deploy this smart contract, run:

near dev-deploy ./target/wasm32-unknown-unknown/release/connect_iot.wasm

Trying ConnectIoT

Before using the smart contract, for a better and easy usage, you can set environment variables for CONTRACT_ACCOUNT_ID and OWNER_ACCOUNT_ID.

Environment variables

export CONTRACT_ACCOUNT_ID=<ID in .env file>

Your OWNER_ACCOUNT_ID is your testnet account

export OWNER_ACCOUNT_ID=<example.testnet> 

Create a registry for a device group.

near call $CONTRACT_ACCOUNT_ID create_registry '{"registry_name":"REGISTRY_NAME"}' --accountId $OWNER_ACCOUNT_ID

Add a device to an existing registry

near call $CONTRACT_ACCOUNT_ID add_device_to_registry '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","description":"DESCRIPTION"}' --accountId $OWNER_ACCOUNT_ID

Set device data

It is important to know that data is an Unordered Map, so you can input as many key/value items in it.

near call $CONTRACT_ACCOUNT_ID set_device_data '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","data":"{\"\":\"\"}"}' --accountId $OWNER_ACCOUNT_ID

Get device data

near call $CONTRACT_ACCOUNT_ID get_device_data '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME"}' --accountId $OWNER_ACCOUNT_ID

Set device data parameter

It is important to know that parameter and value are Strings, so you can only input a parameter & value per function call.

near call $CONTRACT_ACCOUNT_ID set_device_data_param '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","param":"DATA_PARAMETER","value":"VALUE"}' --accountId $OWNER_ACCOUNT_ID

Get device data parameter

near call $CONTRACT_ACCOUNT_ID get_device_data_param '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","param":"DATA_PARAMETER"}' --accountId $OWNER_ACCOUNT_ID

Set device metadata

It is important to know that metadata is an Unordered Map, so you can input as many key/value items.

near call $CONTRACT_ACCOUNT_ID set_device_metadata '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","metadata":{"{}":"{}"}}' --accountId $OWNER_ACCOUNT_ID

Get device metadata

near call $CONTRACT_ACCOUNT_ID get_device_metadata '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME"}' --accountId $OWNER_ACCOUNT_ID

Set device metadata parameter

It is important to know that parameter and value are Strings, so you can only input a parameter & value per function call.

near call $CONTRACT_ACCOUNT_ID set_device_metadata_param '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","param":"METADATA_PARAMETER","value":"VALUE"}' --accountId $OWNER_ACCOUNT_ID

Get device metadata parameter

near call $CONTRACT_ACCOUNT_ID get_device_data_param '{"registry_name":"REGISTRY_NAME","device_name":"DEVICE_NAME","param":"METADATA_PARAMETER"}' --accountId $OWNER_ACCOUNT_ID

Unit test

To run tests, run the following command

cargo test 

Or go to the test code in lib.rs and press Run test if using Visual Studio Code.


Examples

Creating a new registry

near call $CONTRACT_ACCOUNT_ID create_registry '{"registry name":"my_registry"}' --accountId $OWNER_ACCOUNT_ID

NewRegistry,1st function

You can check this transaction and its details at the Near Testnet Explorer. https://explorer.testnet.near.org/transactions/3tLWruvx3YGHBKuNVZBXspk1PBoryizMuQamqC3Ubq4s

Adding a new device to registry "GARDEN 2"

near call $CONTRACT_ACCOUNT_ID add_device_to_registry '{"registry_name":"my_registry","device_name":"my_device","description":"Temperature"}' --accountId $OWNER_ACCOUNT_ID

Add New Device, 2nd function

You can check this transaction and its details at the Near Testnet Explorer. https://explorer.testnet.near.org/transactions/24hztKUH7YW6Xpw8HiT55uqSyqfSzYaszS41A4WzWwso

Setting device data

near call $CONTRACT_ACCOUNT_ID set_device_data '{"registry_name":"my_registry","device_name":"my_device","data":"{\"sample1\": \"value1\", \"sample2\": \"value2\", \"sample3\": \"value3\"}"}' --accountId $OWNER_ACCOUNT_ID

Set Device Data,3rd function

You can check this transaction and its details at the Near Testnet Explorer. https://explorer.testnet.near.org/transactions/EjjP8UNGdQZYv6kKnS8MQ5QNNXPnEWsXdH2BVg5cdSze

Setting device metadata

near call $CONTRACT_ACCOUNT_ID set_device_metadata '{"registry_name":"my_registry","device_name":"my_device","metadata":"{\"battery\": \"87%\", \"date\": \"11/12/2019\", \"location\": \"orchard\"}"}' --accountId $OWNER_ACCOUNT_ID

Set Device Metadata, 4th function

You can check this transaction and its details at the Near Testnet Explorer. https://explorer.testnet.near.org/transactions/AVrMu5s4JtwbT8c2tj8FXCYmyFqsBCi4VhCWebYRACS6

Getting device data

near call $CONTRACT_ACCOUNT_ID get_device_data '{"registry_name":"my_registry","device_name":"my_device"}' --accountId $OWNER_ACCOUNT_ID

Get Device Data, 5th function

You can check this transaction and its details at the Near Testnet Explorer. https://explorer.testnet.near.org/transactions/DJMaTHFopcrGkWQW765Y8eceTbwPo6K58MFKBNyLhhfQ


Credits

Logo Mexbalia

Logo Near Foundation


Support

Reach out via website or send an email to info@mexbalia.com

About

New connect IoT project to the NEAR main net

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Rust 100.0%