Skip to content
This repository was archived by the owner on Aug 5, 2024. It is now read-only.

Commit e4c679d

Browse files
authored
Merge pull request #4 from qube-ai/simple
v0.1.1
2 parents 47ff089 + b7aecf2 commit e4c679d

File tree

6 files changed

+115
-60
lines changed

6 files changed

+115
-60
lines changed

README.md

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# tiny-webthing
22

3-
A tiny Web Thing implementation for Arduino. This is library does not support events and actions. The library uses websocket for communication. So you have to create a websocket server to be able to communicate.
3+
A simple library for the ESP8266 that implements Mozilla's proposed Web of Things API. This library is a port of [webthing-arduino](https://github.com/WebThingsIO/webthing-arduino) but this library is much smaller and simpler. Instead of creating a server on the ESP8266 and communicate locally, this library uses websocket to tunnel the ESP8266/thing and communicate with it over the internet. So to use this library you need to write a tunnel server that will communicate with the ESP8266/thing over websocket with following schema as mentioned below.
44

55

66
## Installation (Platformio)
@@ -114,23 +114,6 @@ response :
114114
}
115115
```
116116

117-
## Architecture
118-
119-
![Architecture](https://img.shields.io/badge/Architecture-Tiny%20Things-blue.svg)
120-
121-
![Architecture](/docs/tiny-webthing-arch.png)
122-
123-
## TODO
124-
125-
- [x] Use StaticJsonDocument instead of DynamicJsonDocument
126-
- [ ] Remove dependency on ArduinoJson
127-
- [ ] Reduce size of TD
128-
- [ ] Fixed size of messages received from server
129-
- [ ] Change message schema in tunnel server and client
130-
131-
132-
133-
134117
## Example
135118

136119
```C++
@@ -146,7 +129,10 @@ const int ledPin = LED_BUILTIN;
146129
TinyAdapter *adapter;
147130

148131
void onOffChanged(ThingPropertyValue newValue);
149-
ThingDevice led("tiny-thing-02");
132+
133+
// Define the types of the thing. This will be `@type` in the thing description.
134+
const char *ledTypes[] = {"OnOffSwitch", "LightBrightnesControl", nullptr};
135+
ThingDevice led("tiny-thing-02", ledTypes);
150136
ThingProperty ledOn("on", BOOLEAN, "OnOffProperty", onOffChanged);
151137
ThingProperty ledBrightness("brightness", NUMBER, "BrightnessProperty", nullptr);
152138

@@ -201,3 +187,47 @@ void loop()
201187

202188

203189
```
190+
191+
192+
## Configuration
193+
If you have a complex device with large thing descriptions, you may need to increase the size of the JSON buffers. The buffer sizes are configurable as such:
194+
195+
```cpp
196+
// By default, buffers are 256 for tiny, 512 bytes for small documents, 2048 for larger ones
197+
// You can change these by defining them before including the library
198+
#define LARGE_JSON_DOCUMENT_SIZE 2048
199+
#define SMALL_JSON_DOCUMENT_SIZE 512
200+
#define TINY_JSON_DOCUMENT_SIZE 256
201+
202+
#include <Thing.h>
203+
#include <TinyAdapter.h>
204+
205+
```
206+
207+
- Enable debug mode by defining `TA_LOGGING` before including the library.
208+
209+
```cpp
210+
#define TA_LOGGING 1
211+
212+
#include <Thing.h>
213+
#include <TinyAdapter.h>
214+
215+
```
216+
217+
218+
## Architecture
219+
220+
![Architecture](https://img.shields.io/badge/Architecture-Tiny%20Things-blue.svg)
221+
222+
![Architecture](/docs/tiny-webthing-arch.png)
223+
224+
## TODO
225+
226+
- [x] Use StaticJsonDocument instead of DynamicJsonDocument
227+
- [ ] Remove dependency on ArduinoJson
228+
- [ ] Reduce size of TD
229+
- [ ] Fixed size of messages received from server
230+
- [ ] Change message schema in tunnel server and client
231+
- [ ] Add support for actions, events
232+
233+

examples/simple/src/main.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const int ledPin = LED_BUILTIN;
1010
TinyAdapter *adapter;
1111

1212
void onOffChanged(ThingPropertyValue newValue);
13-
ThingDevice led("tiny-thing-02");
13+
14+
// Define the types of the thing. This will be `@type` in the thing description.
15+
const char *ledTypes[] = {"OnOffSwitch", "LightBrightnesControl", nullptr};
16+
ThingDevice led("tiny-thing-02", ledTypes);
1417
ThingProperty ledOn("on", BOOLEAN, "OnOffProperty", onOffChanged);
1518
ThingProperty ledBrightness("brightness", NUMBER, "BrightnessProperty", nullptr);
1619

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tiny-webthing",
33
"description": "A library for creating Tiny Web Things. This library does not support actions and events.",
44
"keywords": "Communication",
5-
"version": "0.1.0",
5+
"version": "0.1.1",
66
"authors": {
77
"name": "Hrithik",
88
"email": "[email protected]",

src/ThingDevice.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@ class ThingDevice
88
{
99
public:
1010
String id;
11+
const char **type;
1112
ThingDevice *next = nullptr;
1213
ThingProperty *firstProperty = nullptr;
1314

14-
ThingDevice(const char *_id)
15-
: id(_id) {}
15+
ThingDevice(const char *_id, const char **_type)
16+
: id(_id), type(_type) {}
1617

1718
/*
1819
* @brief Find a property with the given id
19-
* @param TinyProperty *property
20+
* @param {String} id : property id
2021
* @return TinyProperty *property
2122
*/
2223
ThingProperty *findProperty(const char *id)
@@ -33,18 +34,18 @@ class ThingDevice
3334

3435
/*
3536
* @brief Add a property to the thing
36-
* @param TinyProperty *property
37+
* @param {TinyProperty} property
3738
*/
3839
void addProperty(ThingProperty *property)
3940
{
4041
property->next = firstProperty;
4142
firstProperty = property;
4243
}
4344

44-
/*
45+
/*
4546
* @brief Set a property value
46-
* @param {String} id
47-
* @param {JsonVariant} newValue
47+
* @param {String} id : property id
48+
* @param {JsonVariant} newValue : new value of the property (boolean, number, integer, string)
4849
*/
4950
void setProperty(const char *name, const JsonVariant &newValue)
5051
{
@@ -94,13 +95,22 @@ class ThingDevice
9495

9596
/*
9697
* @brief Serialize the thing to JSON
97-
* @param {JsonObject} descr
98-
* @param {String} tunnelUrl
98+
* @param {JsonObject} descr : JSON object to serialize to
9999
*/
100100
void serialize(JsonObject descr)
101101
{
102102
descr["id"] = this->id;
103103
ThingProperty *property = this->firstProperty;
104+
descr["@context"] = "https://webthings.io/schemas";
105+
106+
JsonArray typeJson = descr.createNestedArray("@type");
107+
const char **type = this->type;
108+
while ((*type) != nullptr)
109+
{
110+
typeJson.add(*type);
111+
type++;
112+
}
113+
104114
if (property != nullptr)
105115
{
106116
JsonObject properties = descr.createNestedObject("properties");

src/ThingProperty.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ThingItem
4343

4444
/*
4545
* @brief Set the value of the property
46-
* @param TinyDataValue value
46+
* @param TinyDataValue value : {boolean, number, integer, string}
4747
*/
4848
void setValue(ThingDataValue newValue)
4949
{
@@ -53,7 +53,7 @@ class ThingItem
5353

5454
/*
5555
* @brief Set the value of the property
56-
* @param const char *s
56+
* @param const char *s : string value
5757
*/
5858
void setValue(const char *s)
5959
{
@@ -81,8 +81,7 @@ class ThingItem
8181
/*
8282
* @brief Serialize the property to JSON
8383
* @param JsonObject &json
84-
* @param String thingId
85-
* @param String resourceType
84+
* @param String deviceId
8685
* @example
8786
* {
8887
* "id": "temperature",
@@ -173,9 +172,8 @@ class ThingProperty : public ThingItem
173172

174173
/*
175174
* @brief Serialize the property to JSON
176-
* @param JsonObject obj
177-
* @param String thingId
178-
* @param String resourceType
175+
* @param JsonObject obj : JSON object to serialize to
176+
* @param String deviceId : ID of the thing
179177
*/
180178
void serialize(JsonObject obj, String deviceId)
181179
{
@@ -199,7 +197,7 @@ class ThingProperty : public ThingItem
199197
/*
200198
* @brief If the property has changed, call the callback function
201199
* if it exists.
202-
* @param TinyDataValue value
200+
* @param TinyDataValue value : {boolean, number, integer, string}
203201
*/
204202
void changed(ThingPropertyValue newValue)
205203
{

0 commit comments

Comments
 (0)