ESP8266 reading ultrasonic sensor HC-SR04

hi :heart_eyes:
i’m trying to monitor distance using ultrasonic sensor (HC-SR04)
on my thinger.io console
i’m writing this code :

 #include<ESP8266Wii> .h>
#include <ThingerESP8266.h>
> #define USERNAME "***********"
> #define DEVICE_ID "*****************"
>#define DEVICE_CREDENTIAL "****************"

> #define TRIGGER_PIN  14 // D5
> #define ECHO_PIN     12 //D6 

> //defining SSID and password for the router
> #define SSID "**************"
> #define SSID_PASSWORD "**************"
> ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

> void setup() 
> {
>   
>   thing.add_wifi(SSID, SSID_PASSWORD);

>   pinMode(TRIGGER_PIN, OUTPUT);
>   pinMode(ECHO_PIN, INPUT);
> }

> void loop() {
>   thing.handle();

>   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 

>   thing["SONIC"] >> [] (pson& out){
>     out = distance;
>     };

>   delay(100);




>   
> }

but i’m getting this error

C:\Users\Hussam\Desktop\thinger.io - Copy\thinger.io\thinger.io.ino: In lambda function:

thinger.io:56: error: 'distance' is not captured

       in = distance;

            ^

thinger.io:59: error: 'distance' is not captured

      distance = in;

      ^

exit status 1
'distance' is not captured

what’s wrong???:joy:
help :cry:

Hi,

define the resource in the setup!, like:

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

Keep your loop as:

void loop(){
    thing.handle();
}

Did it work? :slight_smile:

Bests

I see that you don’t have clear about how thinger works, you must define on “void setup” the “things” that you want to handle (or see on thinger console, mobile app, api).

See carefully how is the example (I invoque that several times just to see and make a copy paste), how it is defined the “led” and “millis” on the sketch, and copy that way to define the “things”.

By other way, a few days before you made your post, I write a “how to” work with this sensor specifically, the “delay” function that you are using, stops the loop in the microcontroller (and all the functions that are executing, like the communication), and this makes an abnormal working behavior, so for that reason is not recommended to work with “delays”, please dig a little bit in the forum before asking for help dude!

1 Like

thaaaaaaaaaank you sooooo much

yuuuuuuuuuuuup
you r the best :slight_smile:

oooook
thank you so much

It should be
#include<ESP8266WiFi.h>