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 . 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);
}
}
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
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?
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