Simple gate opener

I try to make a simple gate opener but it will not work. It is based (most parts only copy paste) on this note : https://mydevices.com/article/cayenne-mqtt-standalone-esp8266-esp-01/.
For the push button I made the same trigger as described in the note, but it doesn’t work. In the data I can see that the switch is toggling.
Also the relay connected with the NODEMCU gives no reaction.

My sketch :
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “xxxxxxxxxxx”;
char wifiPassword = “xxxxxxxxxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxxxxx”;
char password = “xxxxxxxxxxx”;
char clientID = “xxxxxxxx”;

// Sound Pin
int soundPin = 3;

// Relay Pin
int relayPin = 2;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(relayPin, OUTPUT); // Relay
digitalWrite(relayPin, LOW);
pinMode(soundPin, INPUT); // Sound
}

void loop() {
Cayenne.loop();
}

CAYENNE_IN(relayPin) {
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(relayPin, HIGH);
delay (1000);
digitalWrite(relayPin, LOW);

}
else {
digitalWrite(relayPin, LOW);
}
}

Thanks for the support.

Try this code and see what you get in the serial monitor when you turn ON/OFF the button.

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “xxxxxxxxxxx”;
char wifiPassword = “xxxxxxxxxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxxxxx”;
char password = “xxxxxxxxxxx”;
char clientID = “xxxxxxxx”;

// Sound Pin
int soundPin = 3;

// Relay Pin
int relayPin = 2;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(relayPin, OUTPUT); // Relay
digitalWrite(relayPin, LOW);
pinMode(soundPin, INPUT); // Sound
}

void loop() {
Cayenne.loop();
}

CAYENNE_IN(2) {
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(relayPin, HIGH);
delay (1000);
digitalWrite(relayPin, LOW);

}
else {
digitalWrite(relayPin, LOW);
}
}

CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
}

Thanks,

Sketch give a fault message now : stray ‘\342’ in program.

Don"t know what is going on.

in the code, delete all " and retype "

Found it. Was a copy past problem.
I can activate the output led now and the led is going of after 1 sec ( delay 1000).
Problem is still the trigger. I need a momentory switch. For that reason I made a trigger :
'If NODEMCU relay ON then NODEMCU relay OFF"
Doesn’t work.
Second problem : I can activate the relay from PC but not from my smartphone. Smartphone is on the same LAN as the PC

try something like this:

CAYENNE_IN(2) {
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(relayPin, HIGH);
delay (1000);
digitalWrite(relayPin, LOW);
send = true;
}
else {
digitalWrite(relayPin, LOW);
}
}

CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
if (send)
{
Cayenne.virtualWrite(3, 1, "digital_sensor", "d");
send = false;
}
}

this will create a two-state widget on the dashboard with channel 3. Add a trigger to this widget. if channel 3 widget = 1, then turn OFF relay button.

Receive fault message during compiling : ’ send’ was not declared in this scope.

inserted ‘int send’ in definitions. Sketch is compiling now but still ON/OFF

I found a solution in this topic :Push button ( physical and virtual )

my sketch now .

#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

// WiFi network info.
char ssid = “xxxxxxxxxx”;
char wifiPassword = “xxxxx”;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = “xxxxxxxxx”;
char password = “xxxxxxxxxxxxxxxxx”;
char clientID = “xxxxxxxxxxxxxx”;

// Sound Pin
int soundPin = 3;

// Relay Pin
int relayPin = 2;

void setup() {
Serial.begin(9600);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
pinMode(relayPin, OUTPUT); // Relay
digitalWrite(relayPin, LOW);
pinMode(soundPin, INPUT); // Sound
}

void loop() {
Cayenne.loop();
}

CAYENNE_IN(2) {
if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin
digitalWrite(relayPin, HIGH);
delay (1000);
digitalWrite(relayPin, LOW);

}
//else {
//digitalWrite(relayPin, LOW);
//}
else {
digitalWrite(relayPin, HIGH);
delay(1500);
digitalWrite(relayPin, LOW);
}
}

CAYENNE_OUT_DEFAULT()
{
// Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
Cayenne.virtualWrite(0, millis());

}

you need to initialise send. boolean send = false

what does your code do?

Code is working. Each time I push the button, the Nodemcu creates a puls to activate the gate.
I have still one problem. When the app is running and my phone switches over from 4G to wifi and vise versa, the system is not working. Than I have to restart the app.

guy

there is an issue it app, and it is advisable to use the web dashboard to use for now.