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

Commit

Permalink
Merge pull request #4 from qube-ai/simple
Browse files Browse the repository at this point in the history
v0.1.1
  • Loading branch information
mystogan99 authored Dec 7, 2022
2 parents 47ff089 + b7aecf2 commit e4c679d
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 60 deletions.
68 changes: 49 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tiny-webthing

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.
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.


## Installation (Platformio)
Expand Down Expand Up @@ -114,23 +114,6 @@ response :
}
```

## Architecture

![Architecture](https://img.shields.io/badge/Architecture-Tiny%20Things-blue.svg)

![Architecture](/docs/tiny-webthing-arch.png)

## TODO

- [x] Use StaticJsonDocument instead of DynamicJsonDocument
- [ ] Remove dependency on ArduinoJson
- [ ] Reduce size of TD
- [ ] Fixed size of messages received from server
- [ ] Change message schema in tunnel server and client




## Example

```C++
Expand All @@ -146,7 +129,10 @@ const int ledPin = LED_BUILTIN;
TinyAdapter *adapter;

void onOffChanged(ThingPropertyValue newValue);
ThingDevice led("tiny-thing-02");

// Define the types of the thing. This will be `@type` in the thing description.
const char *ledTypes[] = {"OnOffSwitch", "LightBrightnesControl", nullptr};
ThingDevice led("tiny-thing-02", ledTypes);
ThingProperty ledOn("on", BOOLEAN, "OnOffProperty", onOffChanged);
ThingProperty ledBrightness("brightness", NUMBER, "BrightnessProperty", nullptr);

Expand Down Expand Up @@ -201,3 +187,47 @@ void loop()


```
## Configuration
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:
```cpp
// By default, buffers are 256 for tiny, 512 bytes for small documents, 2048 for larger ones
// You can change these by defining them before including the library
#define LARGE_JSON_DOCUMENT_SIZE 2048
#define SMALL_JSON_DOCUMENT_SIZE 512
#define TINY_JSON_DOCUMENT_SIZE 256
#include <Thing.h>
#include <TinyAdapter.h>
```

- Enable debug mode by defining `TA_LOGGING` before including the library.

```cpp
#define TA_LOGGING 1

#include <Thing.h>
#include <TinyAdapter.h>

```


## Architecture

![Architecture](https://img.shields.io/badge/Architecture-Tiny%20Things-blue.svg)

![Architecture](/docs/tiny-webthing-arch.png)

## TODO

- [x] Use StaticJsonDocument instead of DynamicJsonDocument
- [ ] Remove dependency on ArduinoJson
- [ ] Reduce size of TD
- [ ] Fixed size of messages received from server
- [ ] Change message schema in tunnel server and client
- [ ] Add support for actions, events


5 changes: 4 additions & 1 deletion examples/simple/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const int ledPin = LED_BUILTIN;
TinyAdapter *adapter;

void onOffChanged(ThingPropertyValue newValue);
ThingDevice led("tiny-thing-02");

// Define the types of the thing. This will be `@type` in the thing description.
const char *ledTypes[] = {"OnOffSwitch", "LightBrightnesControl", nullptr};
ThingDevice led("tiny-thing-02", ledTypes);
ThingProperty ledOn("on", BOOLEAN, "OnOffProperty", onOffChanged);
ThingProperty ledBrightness("brightness", NUMBER, "BrightnessProperty", nullptr);

Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "tiny-webthing",
"description": "A library for creating Tiny Web Things. This library does not support actions and events.",
"keywords": "Communication",
"version": "0.1.0",
"version": "0.1.1",
"authors": {
"name": "Hrithik",
"email": "hrithiky328@gmail.com",
Expand Down
28 changes: 19 additions & 9 deletions src/ThingDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ class ThingDevice
{
public:
String id;
const char **type;
ThingDevice *next = nullptr;
ThingProperty *firstProperty = nullptr;

ThingDevice(const char *_id)
: id(_id) {}
ThingDevice(const char *_id, const char **_type)
: id(_id), type(_type) {}

/*
* @brief Find a property with the given id
* @param TinyProperty *property
* @param {String} id : property id
* @return TinyProperty *property
*/
ThingProperty *findProperty(const char *id)
Expand All @@ -33,18 +34,18 @@ class ThingDevice

/*
* @brief Add a property to the thing
* @param TinyProperty *property
* @param {TinyProperty} property
*/
void addProperty(ThingProperty *property)
{
property->next = firstProperty;
firstProperty = property;
}

/*
/*
* @brief Set a property value
* @param {String} id
* @param {JsonVariant} newValue
* @param {String} id : property id
* @param {JsonVariant} newValue : new value of the property (boolean, number, integer, string)
*/
void setProperty(const char *name, const JsonVariant &newValue)
{
Expand Down Expand Up @@ -94,13 +95,22 @@ class ThingDevice

/*
* @brief Serialize the thing to JSON
* @param {JsonObject} descr
* @param {String} tunnelUrl
* @param {JsonObject} descr : JSON object to serialize to
*/
void serialize(JsonObject descr)
{
descr["id"] = this->id;
ThingProperty *property = this->firstProperty;
descr["@context"] = "https://webthings.io/schemas";

JsonArray typeJson = descr.createNestedArray("@type");
const char **type = this->type;
while ((*type) != nullptr)
{
typeJson.add(*type);
type++;
}

if (property != nullptr)
{
JsonObject properties = descr.createNestedObject("properties");
Expand Down
14 changes: 6 additions & 8 deletions src/ThingProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ThingItem

/*
* @brief Set the value of the property
* @param TinyDataValue value
* @param TinyDataValue value : {boolean, number, integer, string}
*/
void setValue(ThingDataValue newValue)
{
Expand All @@ -53,7 +53,7 @@ class ThingItem

/*
* @brief Set the value of the property
* @param const char *s
* @param const char *s : string value
*/
void setValue(const char *s)
{
Expand Down Expand Up @@ -81,8 +81,7 @@ class ThingItem
/*
* @brief Serialize the property to JSON
* @param JsonObject &json
* @param String thingId
* @param String resourceType
* @param String deviceId
* @example
* {
* "id": "temperature",
Expand Down Expand Up @@ -173,9 +172,8 @@ class ThingProperty : public ThingItem

/*
* @brief Serialize the property to JSON
* @param JsonObject obj
* @param String thingId
* @param String resourceType
* @param JsonObject obj : JSON object to serialize to
* @param String deviceId : ID of the thing
*/
void serialize(JsonObject obj, String deviceId)
{
Expand All @@ -199,7 +197,7 @@ class ThingProperty : public ThingItem
/*
* @brief If the property has changed, call the callback function
* if it exists.
* @param TinyDataValue value
* @param TinyDataValue value : {boolean, number, integer, string}
*/
void changed(ThingPropertyValue newValue)
{
Expand Down
Loading

0 comments on commit e4c679d

Please sign in to comment.