2023-10-10 17:27:08 -04:00
NIP-107
======
Nostr Internet of Things
-----------------------------------
2023-10-24 08:43:50 -04:00
`draft` `optional` `author:benarc` `author:blackcoffeebtc` `author:motorina0`
2023-10-10 17:27:08 -04:00
2023-10-19 05:55:59 -04:00
## Rationale
2023-10-10 17:27:08 -04:00
2023-10-24 08:33:47 -04:00
The terms/conditions of IoT software/hardware is horrible. A user should be able to message a light or heating system and ask it to turn on/off. Nostr gives the simple, permissionless development environment IoT needs.
2023-10-10 17:27:08 -04:00
## Terms
2023-10-19 06:00:51 -04:00
- `user` - user operating IoT devices with NOSTR key-pair and a client made specifically for IoT
2023-10-10 17:27:08 -04:00
- `device` - device to operate over Nostr with NOSTR key-pair, using a microcontroller client like < a href = "https://github.com/lnbits/arduino-nostr" > nostr-arduino</ a >
## Nostr IoT Clients
### User
Where the `user` registers 'device'(s) and its keys, then updates the `device` (s).
### Device
2023-10-19 06:27:29 -04:00
The `device` uses a client like < a href = "https://github.com/lnbits/arduino-nostr" > nostr-arduino</ a > to receive commands from the `user` or another `device` .
2023-10-19 05:53:43 -04:00
The `device` can also push data such as sensor readings and updates.
2023-10-10 17:27:08 -04:00
2023-10-19 06:27:29 -04:00
## Events
2023-10-10 17:27:08 -04:00
2023-10-25 07:49:28 -04:00
A `device` can publish any of the events described in [Data Types ](https://github.com/arcbtc/nips/blob/107/107.md#data-types ).
2023-10-24 08:33:47 -04:00
A `user` can publish these event kinds:
2023-10-24 10:11:18 -04:00
| Kind | | Description | NIP |
|---------|----------|-------------------------------|-----------------------------------------------------------------------------------------|
| `30107` | `config` | Configure a device | [NIP-107 Configure Device Event ](https://github.com/arcbtc/nips/edit/nip_107/107.md#configure-device-event ) |
2023-10-25 07:49:28 -04:00
| `8000` | `intent` | Trigger an action on a device | [NIP-107 Intent Event ](https://github.com/arcbtc/nips/blob/107/107.md#intent-events-kind-8000 ) |
| `8001. | ` data` | Publish the state of a `device` | [NIP-91 Event Kinds ](https://github.com/nostr-protocol/nips/blob/iot/91.md#event-kinds ) |
2023-10-24 08:33:47 -04:00
2023-10-10 17:27:08 -04:00
2023-10-24 10:11:18 -04:00
The content of events can be transmitted in clear text (for public data) or as [NIP-59 Gift Wrap ](https://github.com/staab/nips/blob/NIP-59/59.md ).
2023-10-19 06:27:29 -04:00
2023-10-25 05:36:08 -04:00
2023-10-24 10:11:18 -04:00
### Configure Device Event (`kind: 30107`)
2023-10-25 05:36:08 -04:00
---
2023-10-24 10:11:18 -04:00
This message is sent by an admin `user` to a `device` . The `device` saves the config locally and then uses it.
2023-10-10 17:27:08 -04:00
**Event Content**:
```json
{
2023-10-24 02:29:59 -04:00
"name": < String ( optional ) , set a name for the device > ,
2023-10-10 17:27:08 -04:00
"description": < String ( optional ) , device description > ,
"categories":[ < String ( optional ) , device category , such as ' boiler ' > ],
2023-10-24 09:28:19 -04:00
"admin_pubkeys": [ [< String ( optional ) , admin user public-key > ]],
"publish_to_pubkeys":[ [< String ( optional ) , user public-key > ]],
2023-10-24 09:21:46 -04:00
"intents_from_pubkeys":[ [< String ( optional ) , user public-key > ]],
2023-10-24 09:28:19 -04:00
"publish_on_change": < Boolean ( optional , default `true` ), publish event each time a sensor value changes > ,
2023-10-25 05:32:49 -04:00
"publish_interval": < Integer ( optional ), publish the sensor value at regular intervals ( regardless if the value changes or not ). The value is in `millisecods` . >
2023-10-24 10:11:18 -04:00
"unix_time": < Integer ( optional ), set system time in `millisecods` . >
2023-10-10 17:27:08 -04:00
}
```
2023-10-24 08:54:21 -04:00
| Field | Description |
|---|---|
2023-10-25 05:29:17 -04:00
| `admin_pubkeys` | List of public keys that are allowed to configure this device.< br > A fresh/blank device will not have this value.< br /> The `admin_pubkeys` implicitly have the `actions_from_pubkeys` permissions. |
2023-10-24 09:28:19 -04:00
| `publish_to_pubkeys` | List of public keys to which events are published. |
2023-10-24 09:21:46 -04:00
| `actions_from_pubkeys` | List of public keys that are allowed to trigger an action on this device.< br /> The `admin_pubkeys` implicitly have the `actions_from_pubkeys` permissions. |
2023-10-10 17:27:08 -04:00
2023-10-24 10:11:18 -04:00
> [!IMPORTANT]
> The content of the `30107` event should be encrypted if the user chooses the "encrypted" mode
**Event Tags**:
2023-10-10 17:27:08 -04:00
```json
2023-10-24 10:11:18 -04:00
"tags": [["d", < String , pubkey of the configured device ] ]
2023-10-10 17:27:08 -04:00
```
2023-10-24 10:11:18 -04:00
- the `d` tag is required, its value MUST be the same as the pubkey of the `device` .
### Intent Events (`kind: 8000`)
2023-10-25 05:36:08 -04:00
---
2023-10-24 10:11:18 -04:00
Intent Events represent different actions that can be performed on a device. These actions can be triggered by a `user` or by another `device` .
2023-10-24 10:16:32 -04:00
The content of the event is a `JSON` array representing the data type and the data value as specified in [NIP-91 Event Kinds ](https://github.com/nostr-protocol/nips/blob/iot/91.md#event-kinds )
2023-10-24 10:11:18 -04:00
2023-10-24 10:16:32 -04:00
**Event Content**:
```json
2023-10-24 10:22:39 -04:00
[[< Integer , between 8001 and 8999 > , < Any Type > ]]
2023-10-24 10:16:32 -04:00
```
Example that turns on a device and sets the temperature to `20.9` degrees Celsius:
2023-10-24 10:11:18 -04:00
```json
[[8001, true], [8003, 20.9]]
```
**Event Tags**:
```json
2023-10-25 05:22:25 -04:00
"tags": [["p", < String , pubkey of the targeted device ( s ) ] ]
2023-10-24 10:11:18 -04:00
```
2023-10-25 05:22:25 -04:00
- the `p` tag is required, its value MUST be the same as the pubkey(s) of the target `device` (s).
2023-10-24 10:11:18 -04:00
2023-10-10 17:27:08 -04:00
2023-10-25 07:49:28 -04:00
### Data Events (`kind: 8001`)
2023-10-25 05:36:08 -04:00
---
2023-10-25 05:32:49 -04:00
Data Events represent data published by `device` (s). The `kind` of the event and the `content` must follow ths specification from [NIP-91 Event Kinds ](https://github.com/nostr-protocol/nips/blob/iot/91.md#event-kinds ).
> [!NOTE]
> If a device has more sensors attached (eg: temperature and humidity) then it will publish separate events for each measured value.
2023-10-25 05:22:25 -04:00
**Event Tags**:
```json
"tags": [["p", < String , pubkey of the listening device ( s ) ] ]
```
2023-10-25 05:29:17 -04:00
- the `p` tag is optional. If set then its value MUST be the same as the `publish_to_pubkeys` value from the [Configure Device Event ](https://github.com/arcbtc/nips/edit/nip_107/107.md#configure-device-event-kind-30107 )
> [!IMPORTANT]
> If the transmission mode is encrypted ([NIP-59 Gift Wrap](https://github.com/staab/nips/blob/NIP-59/59.md)) then an individual message must be published for each public key in `publish_to_pubkeys`.
2023-10-25 06:36:09 -04:00
>
>
2023-10-25 07:46:20 -04:00
## Data types
2023-10-25 06:36:09 -04:00
Below is a non exhaustive list of data types that may be used in the content of the event.
| name | type | unit | example | description |
| ----------------- | ------- | --------------- | -------------------- | --------------------------------------------------------------- |
| temperature | float | celsius | 20.9 | Measures the ambient temperature |
| pressure | float | pascals | 1013 | Measures the atmospheric pressure |
| location | array | decimal degrees | [51.5053, -0.08737] | Specifies coordinates in latitude and longitude |
2023-10-25 06:38:02 -04:00
| light | float | lux | 500 | Measures the intensity of light |
2023-10-25 06:36:09 -04:00
| humidity | float | percentage | 45.5 | Measures the moisture level in the air |
| rain | boolean | millimeters | true | Detects if it's raining |
| moisture | float | percentage | 32.2 | Measures the moisture content in materials |
| air_quality | float | AQI | 75 | Measures the overall quality of the air based on pollutants |
| gas_concentration | float | ppm | 400 | Measures the concentration of a specific gas in the air |
| uv_index | float | index | 5.5 | Measures the strength of sunburn-producing ultraviolet radiation |
| wind_speed | float | m/s | 10.2 | Measures the speed of wind |
2023-10-25 06:38:02 -04:00
| wind_direction | float | degrees | 30 | Indicates the direction from which the wind is coming |
2023-10-25 06:36:09 -04:00
| sound_intensity | float | decibels | 65.5 | Measures the loudness or amplitude of sound |