Nood Question - ESP8266 + HC-SR04 + Relay, not working

Hi, I will try to not have much pain in the butt. So what I’m trying to do is simple except I can not figure out the best way to do it. Im working on an art project and i need to turn a relay on
I need help. I took a project that has me just a little confused on how to combine all the different functions I need to happen. I can make them all happen separately but combining. I want the button to activate the relay and the relay will turn itself off when the water is full.
Currently, it does not work.
Thank you for reading and helping.

#include "CayenneDefines.h"
#include "CayenneWiFi.h"
#include "CayenneWiFiClient.h"
#include <Ultrasonic.h>

// #define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#define RELAY_PIN 6 // RELAY PIN
#define VIRTUAL_PIN V2
#define TRIGGER 5
#define ECHO    4
int maximumRange = 150;
long duration;
int distance;


char token[] = "xxxxxx";
char ssid[] = "xxxxxxxxx";
char pwd[] = "xxxxxxxxx";
Ultrasonic ultrasonic(5, 4);
void setup() {
  Serial.begin(115200);
  Cayenne.begin(token, ssid, pwd);
  pinMode(RELAY_PIN, OUTPUT);
  pinMode (TRIGGER, OUTPUT);
  pinMode (ECHO, INPUT );
  pinMode(BUILTIN_LED, OUTPUT);

}

void loop() {
    digitalWrite(TRIGGER,LOW);
    delayMicroseconds(2);   
    digitalWrite(TRIGGER,HIGH);
    delayMicroseconds(10);
    duration=pulseIn (ECHO,HIGH);
    distance= duration/58.2;
    Serial.println(distance);
    Serial.println("cm");
    delay (1);
  Cayenne.run();
}

CAYENNE_OUT(V2)
{
    Cayenne.virtualWrite(V2, distance);
}

CAYENNE_IN(V1)
{
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open




  if (distance >= 55 ){
   digitalWrite (RELAY_PIN,LOW);// connect to relay(motor)
   delay(100);
 }
 
   else if (distance <=10) {
   digitalWrite (RELAY_PIN,HIGH); // connect to relay(motor)
   delay(100);
   
 }

}

try this. You had it almost correct but you were using a library to do the “ping” but then also trying to do it manually. Let the library do the work :slight_smile:. I also changed a library for the ESP. I commented out te parrts that are not needed. Also took out your delays as this could cause connection issues and didn’t look like they were needed. Let me know if this works for you.

#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266.h"
#include "CayenneWiFiClient.h"
#include <Ultrasonic.h>

// #define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#define RELAY_PIN 6 // RELAY PIN
// #define VIRTUAL_PIN V2
// #define TRIGGER 5
// #define ECHO    4
int maximumRange = 150;
// long duration;
int distance;


char token[] = "xxxxxx";
char ssid[] = "xxxxxxxxx";
char password[] = "xxxxxxxxx";

Ultrasonic ultrasonic(5, 4);

void setup() {
	Serial.begin(115200);
	Cayenne.begin(token, ssid, password);

	pinMode(RELAY_PIN, OUTPUT);
	// pinMode(TRIGGER, OUTPUT);
	// pinMode(ECHO, INPUT);
	pinMode(BUILTIN_LED, OUTPUT);

}

void loop()
{


	// digitalWrite(TRIGGER,LOW);
	// delayMicroseconds(2);
	// digitalWrite(TRIGGER,HIGH);
	// delayMicroseconds(10);
	// duration=pulseIn (ECHO,HIGH);

	distance = ultrasonic.distanceRead();
	Serial.println(distance);
	Serial.println("cm");
	delay (1);
	Cayenne.run();
}

CAYENNE_OUT(V2)
{
	Cayenne.virtualWrite(V2, distance);
}

CAYENNE_IN(V1)
{
	// get value sent from dashboard
	int currentValue = getValue.asInt(); // 0 to 1

	// assuming you wire your relay as normally open


	if (distance >= 55 )
		{
			digitalWrite (RELAY_PIN,LOW);// connect to relay(motor)
			// delay(100);
		}

	else if (distance <= 10)
		{
			digitalWrite (RELAY_PIN,HIGH); // connect to relay(motor)
			// delay(100);
		}
}
1 Like

Thanks @vapor83! Let us know how your project goes @giolangle. You’re in good hands :slight_smile:
-Benny

1 Like

Thanks @vapor83 and @bestes.
I realize it does not work.
Where did I go wrong? I came to ESP not too long, I was very surprised with it
I use NodeMCU

Wire connection

Relay
S / D6

  • / GND
  • / Vin (5V)

HC-SR04

GND / GND
Vcc / Vin (5v)
Triger / D5
Echo / D4

Thank you 2 very much. I’ll share it when it’s done.

so what is it doing when you say its not working? Is it not stopping when you need it to or doing nothing at all?

Does it upload to the board ok?

1 Like

I using NodeMCU CP2102.
I have tested on 2 circuits
Does my wire connection have errors?
Downloading the source code is complete without errors.

Does the cayenne dashboard say the deice is online?

1 Like

Cayenne dashboard say Pending Install.

click on it and get to the last step so on the right hand side it says “waiting for board to connect”. Make sure your token matches the one it says in your sketch and your wifi & password are correct for your network. Turn the device off and back on. If all goes well your dashboard will change & show the device online on the left. Then we can add our buttons

1 Like

Thanks @vapor83.
Does my wire connection have errors?
Everything is correct. I will check for errors

I tested the sample source code, it still works normally.
The router reports that the ESP is connected to the network. And the Auth code is correct. What is happening? My circuit was damaged, right?

I tested the sample source code, it still works normally.
> // #define CAYENNE_DEBUG
> // #define CAYENNE_PRINT Serial
> #include <ESP8266WiFi.h>
> #include “CayenneDefines.h”
> #include “BlynkSimpleEsp8266.h”
> #include “CayenneWiFiClient.h”
> #include “Ultrasonic.h”

> #define VIRTUAL_PIN V2

> #define TRIGGER 5
> #define ECHO    4




> // Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
> char token[] = "td8jdzzlfk";
> // Your network name and password.
> char ssid[] = "giolangle.com";
> char password[] = "18041990";

> Ultrasonic ultrasonic(5, 4);

> void setup() {
>   
>   Serial.begin (9600);
>   Cayenne.begin(token, ssid, password);
>   pinMode(TRIGGER, OUTPUT);
>   pinMode(ECHO, INPUT);
>   pinMode(BUILTIN_LED, OUTPUT);
>   
> }

> void loop() 
> {
>     Cayenne.run();
>   // do something
> }

> // This function is called when the Cayenne widget requests data for the Virtual Pin.
> CAYENNE_OUT(V2)

> {

>   long duration; 
>   int distance;
>   digitalWrite(TRIGGER, LOW);  
>   delayMicroseconds(2); 
>   
>   digitalWrite(TRIGGER, HIGH);
>   delayMicroseconds(10); 
>   
>   digitalWrite(TRIGGER, LOW);
>   duration = pulseIn(ECHO, HIGH);
>   distance = ((duration/2) / 29.1) *  0.393701;
>   
>   Serial.print(distance);
>   Serial.println("in");
>   delay(1);
>   Cayenne.virtualWrite(V2, distance);
> }

That should work. Try removing that device and adding a new one. Then reflash the device with the new token and see if that gets you past that screen.

1 Like

Thanks again for your generous help @vapor83
I deleted and reflash with the new auth code, but still can not connect.
Problem in source code. I will try to check

Were you able to get this working? If not also please send a screen shot of your serial output from arduino if you are getting anything there.