setup()method. As well as you initialize your devices, set the input/output direction of a digital pin, or initialize the Serial port speed, you also need to initialize here your resources. This basically consists on configuring what values or resources you want to expose over the Internet.
loop()is the place to always call to the
thing.handle()method, so the thinger libraries can handle the connection with the platform. This is the place also for calling your endpoints, or streaming real-time data to a dashboard. Please, take into account to do not add any delay inside the
loop()except if you know what you are doing, like working with deep sleep modes or so in your device. Any other delay will condition the proper functioning of Thinger in your device. Also it can be bad to read a sensor value in every loop if the sensor takes too much time to complete a read. This will result in a device with a noticeable lag while attending to our commands.
thinginstance. Replace the your
deviceCredentialwith the values you have registered in the cloud. Note that credentials uses to be defined inside
setup()method of the Arduino sketch. This way the resources are configured at the beginning, but can be accessed later as necessary.
<<pointing to the resource name, and it uses a C++11 Lambda function to define the function.
psonthat is a variable type that can contain booleans, numbers, floats, strings, or even structured information like in a JSON document.
psontype can hold multiple data types, we can think that the
psonparameter of the input function is like a boolean.
setupfunction you can place a resource called
led(but you can use any other name), of input type (using the operator
<<), that takes a reference to a
psonparameter. This example will turn on/off the digital pin 10 using a ternary operator over the
psontype can hold multiple data types, we can still use the
psontype as an integer value.
psondata type can hold not only different data types, but also is fully compatible with JSON documents. So you can use the Pson data type to receive multiple values at the same time. This example will receive two different floats that are stored with the
>>pointing out of the resource name, and it uses a C++11 Lambda function to define the output function.
psontype that is a variable type that can contain booleans, numbers, floats, strings, or even structured information like in a JSON document.
>>. In the callback function we can fill the out value with any value we want, like in this case the output from a sensor reading.
=. In this case the function takes two different
psonparameters. One for input data and another one for output data. This example provides an altitude reading using the BMP180 Sensor. It takes the reference altitude as input, and provides the current altitude as output.
value2to provide the
sdLoggingvariable defined as a global variable.
deviceB, and we want to communicate both calling from
deviceBto a specific
deviceAinput resource. We can use "thing.call_device(,);" as shown in the example below:
deviceAdefines a resource like in the following example.
deviceBcan easily call this resource and send data to it by running the following command.
psonpayload in order to share data between devices. In this case, the
deviceAwill need to define a resource with some expected input
deviceBcan call this method providing the appropriated input by defining a
psontype that is filled with the same keys used on
resourceOnA, as shown in the code below:
deviceBcan also call this method by providing the information from an defined resource that generates the information, in this case, the call is similar as the previous example, but using the resource as the data source.
Thinger.io Device Call. Just register an endpoint of this type in the console, like in the following example.
call_endpointmethod over the
high_temp_emailthat contains some warning text about the temperature. For this case we do not want to check the temperature every millisecond, so we are introducing some variables to control the sensing and warning frequency. In this example, the temperature is checked every hour, and if it is above 30ºC, it will call the endpoint called
high_temp_emailwhich will send us an email with the predefined text. It is important here to do not add delays inside the loop method, as it will prevent the required execution of the
thing.handle()method, so we are using here a non-blocking delay based on the
call_endpointmethod, but in this case adding some information based on the
psondata format, which will be automatically converted to JSON. For example, if we want to report data to a third party service like Keen.io, we can create such kind of endpoints in the console. Once configured, we can call the endpoint with our readings, for example with humidity and temperature values from a DHT sensor.
"The actual level is %"
The actual level is 80.34%
Serialcommunication, as all the debugging information is displayed over Serial. So enable it in your sketch in the setup method.
set_state_listenerfunction in the
setup()method. For example, itis possible to define a listener that will receive the different connection states for the network, server, or authentication: