Communicating two ESP8266 with “thinger.io device call” endpoint

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);

thing[“blink”] = {

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!

Thank you for writing this!
I suppose the resource on receptor device should be named “blink” as mentioned while creating the endpoint. Isn’t it?

Yes, you are right! thanks for the grade.

I would like to know if communication between two devices of the same user is possible or not. If yes, could you explain how to do it.

It is possible ussing the endpoint like we’ve done in this tutorial. If you want to send some data, only have to create a pson with the variables or the values before the call_endpoint intruction in the emmiter code, just like this:

[…]
pson myMessage;
myMessage[“pi”] = 3.141592;

thing.call_endpoint(“TutorialEndpoint”,myMessage );

Then, in the receptor code you have to change the Thinger resource to extract the input value:

thing[“blink”] << (pson& in){
reciebeData = in;
};

An other example for the STI:

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME " "
#define DEVICE_ID "deviceA"
#define DEVICE_CREDENTIAL "deviceA"

#define SSID " "
#define SSID_PASSWORD " "

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {
  pinMode(D0,OUTPUT);
  thing.add_wifi(SSID, SSID_PASSWORD);
  Serial.begin(115200);
  
  thing["led"] << [](pson & in) {
    int r = in;
    Serial.print("It's working");
    Serial.println(r);
    digitalWrite(D0, r?HIGH:LOW); 
  };

}
int anterior, estado = 1;

void loop() {
  thing.handle();
  

  pson dato;
  dato= estado;
   
  if (anterior == 1 && digitalRead(D3) == 0) {
    Serial.print ("send");
    Serial.println(estado);
    thing.call_device("deviceA", "led", dato);
    estado ? estado = 0 : estado = 1;
  }
  anterior = digitalRead(D3);





}

Hey there,
I am using ultrasonic sensor and want to get notified using email if the distance is less than 5cm.
I am beginner in fact this is my first project and having very less coding knowledge.

How should I add endpoint ?
Please help me.

#include<SPI.h>
#include<ESP8266WiFi.h>
#include <ThingerWifi.h>
#define USERNAME “"
#define DEVICE_ID "
"
#define DEVICE_CREDENTIAL "
****”
#define EMAIL_ENDPOINT “SmartBin”

#define TRIGGER_PIN 5 // D1
#define ECHO_PIN 4 //D2

//defining SSID and password for the router
#define SSID “swaptel”
#define SSID_PASSWORD “22334487”
ThingerWifi thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup()
{

thing.add_wifi(SSID, SSID_PASSWORD);

pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
thing[“SONIC”] >> (pson& out){
double duration, distance;
digitalWrite(TRIGGER_PIN, LOW); // Get Start
delayMicroseconds(2); // stable the line
digitalWrite(TRIGGER_PIN, HIGH); // sending 10 us pulse
delayMicroseconds(10); // delay
digitalWrite(TRIGGER_PIN, LOW); // after sending pulse wating to receive signals
duration = pulseIn(ECHO_PIN, HIGH); // calculating time
distance = (duration/2) / 29.1; // single path
out = distance;
};

}

void loop() {
thing.handle();

}

@SwapTel , notice that in this call_endpoint, I’m not sending any values to the endpoint but, in the example that I wrote on your topic, I’m sending the readed distance in a Pson.

I’m Trying to send data from one device to other on same account, but nothing is working.

my sender/emitter code of device1 is and this is working fine as it shows data of DHT11
thing[“DHT11”] >> [](pson& out){
if((err=dht11.read(humi, temp))==0)
{
out[“Humi”] = humi;
out[“Temp”] = temp;
}
};

}

void loop() {
thing.handle();
pson mydata;
mydata[“val1”]=humi;
mydata[“val2”]=temp;
thing.call_device(“device2”,“dht”,mydata);
}

my receiver code of device2 is
thing[“dht”] << [](pson& in){

h = in[“val1”];
t = in[“val2”];

};

}

void loop() {
thing.handle()
}