Recently thinger.io counts with a new kind of endpoint that allow us to communicate two devices, it could be very useful for transmitting data, make requests between two devices and many other projects. This tutorial will explain how to do it!
Fist we should know what an Endpoint and a Device Token is, and how to use it:
An Endpoint is an interface to requests some service to another online host. Same Endpoint could be called from multiple devices.
The Device token is a secure identifier that can be used to reference a device from other hosts. It only identifies one device, so it can be used to specify the final destiny of the message.
To create a single communication, we are only going to need to make a Thinger.io endpoint to a device using his token (passive, receptor), and call that Endpoint with the other one (active, emitter).
Note that if we want to create a full communication, it’s needed to make two Token devices and two Endpoints.
First we will have to create the devices and make a Device token in one of them:
Then, create de “thinger.io device call endpoint” and include the exact identifier, resource and token of the receptor device.
Now it’s time to open Arduino IDE (I’m using ESP8266 boards), configure all the credentials, and include the codes:
The emitter code call the endpoint when the button is closed.
#include < ESP8266WiFi.h>
#include < ThingerWifi.h>#define USERNAME “your_user_name”
#define DEVICE_ID “emitterDevice”
#define DEVICE_CREDENTIAL “your_device_credential”
#define SSID “SSID”
#define SSID_PASSWORD “SSID_PASSWORD”
#define buttonPin 0;ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
Int state;
void setup() {
thing.add_wifi(SSID, SSID_PASSWORD);
pinMode(buttonPin, INPUT);
thing[“button”] = { //this will allow us to test the communication in the device API
thing.call_endpoint(“TutorialEndpoint”);
};}
void loop() {
thing.handle();
if(digitalRead(buttonPin ==1 && state==0) {thing.call_endpoint("TutorialEndpoint");
state=1;
}
If(button==0){
State=0;
}
}
Thinger.io server will handle the endpoint and send a Pson to the receptor device with the massage (in this case it’s empty).
The receptor code:
#include <ESP8266WiFi.h>
#include <ThingerWifi.h>#define USERNAME “your_user_name”
#define DEVICE_ID “receptorDevice”
#define DEVICE_CREDENTIAL “your_device_credential”
#define SSID “SSID”
#define SSID_PASSWORD “SSID_PASSWORD”
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);
void setup() {
pinMode(13, OUTPUT);thing.add_wifi(SSID, SSID_PASSWORD);
digitalWrite(13,1);
delay(1000);
digitalWrite(13,0);
};
}
void loop() {
thing.handle();
}
Receptor code will make a led blink by one second each time that it receives the Pson.
In this example, we send an empty message but, if you want to include some data, only have to fill the pson data before to send it in the emitter and make an input resource in the receptor.
hope you enjoy!