Creating Email Endpoint

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 "swaptel"
#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();

 }

Hi @SwapTel ,

regarding endpoints you can find detailed documentation here.

Instead of pulseIn() you can use newPing library which is much more efficient. See how I have implemented ultrasonic sensors using this library and email endpoints in my EchoGarage project here.

Hello @SwapTel ! Welcome to Thinger.io community!

This is a nice project, and it is so simple but it requires to know some steps, so I will try to help you to understand how to do it correctly.

First you have to now is how do thinger.io sketch works: When we create a resource into the setup()routine, what we are really doing is building a function that will be called during the execution by thing.handle() routine in the loop(), but this will happen only when you have a Dashboard or a Device API opened on a web browser. This means that, if you don’t have any data consumer, the device will not execute the resource.
If you want that your functionality works every time (like it’s expected on a real time system), you have to include your code into the loop() section.

Ok, let’s talk about how to use Endpoints on Thinger.io platform. This is a process that requires to steps:
1st: Create the Endpoint manager on Thinger.io>menu>endpoints>new endpoint. Select “Email” type and fill the “endpoint identifier”, in your case “SmartBin” This will be the identifier that we will use on our source code to call the endpoint.

2nd: Call the endpoint on our source code using the instruction: thing.call_endpoint(); when it is appropriate to do it. Here you have to be careful to prevent that the call_endpoint executes constantly without any control (we only want to be advised one time per event), so it’s important to create a lock.
then, in your program, it’s only need to include a few code lines to make what you are trying:

 #include<SPI.h>
#include<ESP8266WiFi.h>
#include <ThingerWifi.h>

#define USERNAME "*****"
#define DEVICE_ID "********"
#define DEVICE_CREDENTIAL "swaptel"
#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);

int locked;  

double 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 
    float duration = pulseIn(ECHO_PIN, HIGH); // calculating time 

    return (duration/2) / 29.1; // returning the distance
}

void setup() 
{

  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode(TRIGGER_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
 
  thing["SONIC"] >> [] (pson& out){
    out = distance();
  };

 }

   void loop() {
 thing.handle();

    if(distance()<=5 && locked==0){
      thing.call_endpoint( EMAIL_ENDPOINT,"SONIC");
      locked=1;
    }else{
      locked = 0;
    }

 }

Remember that you can read more about Thinger.io functions on the documentation

2 Likes

Hey,
Thank you so much :slight_smile:
It helped me a lot but still facing an issue.:confused:
I am receiving emails frequently showing the distance 10 cm, 8 cm, 4 cm, etc. though the distance is greater and smaller than 5 cm.


I want email notification only when the distance is less than 5 cm.

goto thinger,io website see left-side of the screen there we can find end point
once we r filling the data it will ask which type we want there we can choose email type.
if we are choosing email point we can menstion the subject and our mail id.
once cmpltion of this step we can select the type of data format like json or normal text