Skip to content

batovpasha/2smart-cloud-cpp-sdk

Repository files navigation

Homie

A class for controlling Homie entities and ensuring their interaction with the MQTT broker.

Platforms

This sdk currently support ESP32 platform.

Examples


API

  • Homie(MqttClient* mqtt_client)
  • void SetDevice(Device* device);
  • bool Init(String user_hash, String host, String broker_port, String token, std::function<void(char*, uint8_t*, unsigned int)> callback)
  • bool Publish(const Device& device, String attribute, String value, bool retained)
  • bool Publish(const Node& node, String attribute, String value, bool retained)
  • bool Publish(const Property& property, String attribute, String value, bool retained)
  • bool SubscribeTopic(const Property& property)
  • void HandleMessage(String topic, byte* payload, unsigned int length)
  • void HandleCurrentState()

Homie(MqttClient* mqtt_client)

Creates a homie object, accepts an MQTT client object


void SetDevice(Device* device)

Assigns a device object to the current homie object

  • device: the device object to be assigned.

bool Init(String user_hash, String host, String broker_port, String token, std::function<void(char*, uint8_t*, unsigned int)> callback)

Connects to MQTT broker and initiates the device initialization.

  • user_hash: user ID, generated based on the email.
  • host: the address where the MQTT broker is installed.
  • broker_port: the port on which the broker is located.
  • token: password for connecting to a broker, generated when creating a user.
  • callback: a function that will be called to accept new messages from the broker.

Returns the initialization status.


bool Publish(const Device& device, String attribute, String value, bool retained)

Publishes the value of a device attribute to the broker.

  • device: pointer to device object.
  • attribute: the attribute that is being published.
  • value: parameter value.
  • retained: flag responsible for storing messages in topics.

Returns the status of posting a message to a topic.


bool Publish(const Node& node, String attribute, String value, bool retained)

Publishes the value of a node's attribute to the broker.

  • node: pointer to node object.
  • attribute: the attribute that is being published.
  • value: parameter value.
  • retained: flag responsible for storing messages in topics.

Returns the status of posting a message to a topic.


bool Publish(const Property& property, String attribute, String value, bool retained)

Publishes the value of a property attribute to the broker.

  • property: pointer to property object.
  • attribute: the attribute that is being published.
  • value: parameter value.
  • retained: flag responsible for storing messages in topics.

Returns the status of posting a message to a topic.


bool SubscribeTopic(const Property& property)

Subscribes to /set topic of property value.

Example:

For a sensor that has a topic of value 73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2/sweet-home/device-id/node-id/sensor-id
when calling SubscribeTopic homie will subscribe to the topic 73062d872926c2a556f17b36f50e328ddf9bff9d403939bd14b6c3b7f5a33fc2/sweet-home/device-id/node-id/sensor-id/set
  • property: pointer to the property object.

Returns the subscription status.


void HandleMessage(String topic, byte* payload, unsigned int length)

Accepts a message and calls a handler on the required property

  • topic: topic where the message came from.
  • payload: message text.
  • length: the length of the message.

void HandleCurrentState()

Calls a loop on the MQTT client, and processes the current state of the device


To Do

  • ESP8266 platform support
  • TLS support

Read our docs about 2Smart Cloud protocol

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • CSS 67.1%
  • C++ 19.7%
  • HTML 7.9%
  • C 5.3%