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