helloOpenIot - dl3ebb/OpenIot GitHub Wiki
/**
* This is a Hello World example for OpenIot.
*
* To run this example connect a led to a I/O Pin and a pushbutton to another I/O Pin. The
* pushbutton should switch to ground and have a pullup resistor to vcc
*/
#define SWITCH_PIN 25
#define LED_PIN 32
#include <OpenIot.h>
#include <Connector/DigitalInConnector.h>
#include <Connector/DigitalOutConnector.h>
void setup() {
/**
* Create the Elements and register them to the connectors
*/
BoolElement *switch1 = new BoolElement("Switch1");
digitalInConnector->registerElement(switch1, SWITCH_PIN, true);
BoolElement *led1 = new BoolElement("Led1");
digitalOutConnector->registerElement(led1, LED_PIN);
eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
led1->setValue(event->newValue);
});
openIot.setup("HelloOpenIot", 1);
}
void loop() {
openIot.loop();
}
The code above demonstrates a very basic application for OpenIot but effectively highlights its core concepts.
OpenIot supports only three basic types: Bool
, String
, and Long
. Elements act as containers for values of these types. In addition to their value, every Element also has a unique identifier (ID).
The code declares the elements for the switch and the led :
BoolElement *switch1 = new BoolElement("Switch1");
...
BoolElement *led1 = new BoolElement("Led1");
Next, it connects the Elements to the outside world by registering them with DigitalIn
and DigitalOut
connectors.
digitalInConnector->registerElement(switch1, SWITCH_PIN, true);
digitalOutConnector->registerElement(led1, LED_PIN);
The true
parameter in the registration of the switch with the DigitalInConnector
indicates that the signal needs to be inverted. Since the switch connects the input to GND when pressed, it produces a logical LOW signal. Inversion changes this behavior to interpret it as a logical HIGH.
The DigitalInConnector
scans all registered elements during each loop. If it detects a change in an input, it triggers a NewBooleanValueEvent
in the Event Manager. To respond to this event, we need to subscribe to it:
eventManager.addListener<NewBoolValueEvent>(NewBoolValue, "Switch1", [led1](NewBoolValueEvent *event) {
led1->setValue(event->newValue);
});
This code registers an EventListener with the Event Manager. It is triggered when an event of the type NewBoolValueEvent
is fired by the element with the ID Switch1
. The lambda function within this EventListener sets the value of the element led1
to the new value. This, in turn, adjusts the corresponding output pin to either HIGH or LOW.
An important detail is the value NewBoolValue
used during registration. This value is a unique integer constant representing the NewBoolValueEvent
. Using an integer constant for event identification significantly improves performance during event processing, as it is much faster to compare integers than to compare class names.
When you upload this code to your device and press the button, the LED will toggle on or off.