...
Open Sketch: (Appdir)/examples/DemoCharts01/DemoCharts01.ino in Arduino IDE
Info |
---|
Install OpenDevice library using Arduino Library Manager (Installing Arduino Library ) |
...
If something is wrong, check the logs (mouse right-click).
Also try to disconnect and connect again. You can also, show logs.
...
How does it all work?
Now we will give a brief explanation of how it all works.
...
that we have everything working, let's explain how it works.
This is code for your example:
Downloded: (Appdir)/examples/DemoCharts01/DemoCharts01.ino
Code Block | ||
---|---|---|
| ||
#include <OpenDevice.h>
int temperature = 0;
int humidity = 0;
int lumens = 0;
void setup() {
// ODev.clear(); // if you had problems try to clear EEPROM settings.
ODev.name("MyBoard");
ODev.addDevice("Led", 13, Device::DIGITAL);
ODev.addSensor("Temperature", readTemperature)->readInterval = 200;
ODev.addSensor("Humidity", readHumidity)->readInterval = 200;
ODev.addSensor("Lumens", readLumens)->readInterval = 200;
ODev.begin(Serial, 115200);
}
void loop() {
ODev.loop();
}
//==================================================
// Listeners / Commands
//==================================================
value_t readTemperature() {
// NOTE: Here you can substitute the code that actually reads any sensor.
if(temperature == 100) temperature = 0;
return temperature++;
}
value_t readHumidity() {
if(humidity == 100) humidity = 0;
return humidity++;
}
value_t readLumens() {
if(lumens == 100) lumens = 0;
return lumens++;
} |
As already mentioned, we are using the OpenDevice library, it allows communication between Arduino and Application in several ways, including: USB, Bluetooth, Ethernet, WiFi / MQTT.
This library has several ways to send and receive data from the application, in this example we will explain some.
In line 17: We specify that the connection used will be through Serial (USB), using ODev.begin(Serial, 115200);
In order for actions on the Application interface like (Click on the Led) and it to turn on the Led on the Arduino, we need the name of the Widget to be the same name that we defined in the arduino code using: ODev.addDevice("Led", 13, Device::DIGITAL); // LINE 11
Now to send information from the Arduino to the Application, one way is to define a sensor, using: ODev.addSensor("Name", ....)
.
Taking line 13 as an example. Note that in this example we are passing a function: readTemperature
, it will be responsible for the reading code. (That way any sensor can be read).
Yet in line 13, we define the interval of 200ms
that the sensor should be read.
Info |
---|
OpenDevice supports some types of sensors, and it is still possible read digital/analog sensors directly without having to pass a function, using: ODev.addSensor ("Switch", 11, Device :: DIGITAL); |
What's ahead?
Please check for more options to send and receive data: Use Cases
How communication work ?
When you add a button and click on it, a command is sent to the Device (Ex. Arduino).
This command follows a communication protocol.
See more details: How communication work
Status | ||||
---|---|---|---|---|
|