ESP8266 Control Gate

I’m brand new to all of this, I followed all the guides and have tried to write the code from scratch instead of copy/paste to learn it.

I have a NodeMCU ESP8266 with a DHT11 sensor and a relay. It all works through the dashboard but I need the relay to switch momentarily in order to trigger the gate motor. I have searched all posts on this site with reference to this and I still haven’t been able to get a working solution. My code below:

     #define CAYENNE_PRINT Serial
    #include <CayenneMQTTESP8266.h>
    #include <DHT.h>
    #include <DHT_U.h>
    #define DHTPIN 4
    #define DHTTYPE DHT11
    #define VIRTUAL_CHANNEL0 0
    #define VIRTUAL_CHANNEL1 1
    #define VIRTUAL_CHANNEL2 2
    #define VIRTUAL_CHANNEL3 3
    #define ACTUATOR_PIN1 1
    DHT_Unified dht(DHTPIN, DHTTYPE);

    // WiFi network info.
    char ssid[] = "";
    char wifiPassword[] = "";
    // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
    char username[] = "";
    char password[] = "";
    char clientID[] = "";

    unsigned long lastMillis = 0;

    void setup() {
      Serial.begin(9600);
      pinMode(ACTUATOR_PIN1, OUTPUT);
      Cayenne.begin(username, password, clientID, ssid, wifiPassword);
      dht.begin();
      sensor_t sensor;
    }
    void loop() {
      Cayenne.loop();
      //Publish data every 1 seconds (1000 milliseconds)
      if (millis() - lastMillis > 1000) {
        lastMillis = millis();
        Cayenne.virtualWrite(VIRTUAL_CHANNEL2, lastMillis);
        sensors_event_t event;  
        dht.temperature().getEvent(&event);
        Cayenne.celsiusWrite(VIRTUAL_CHANNEL0, event.temperature);
        dht.humidity().getEvent(&event);
        Cayenne.virtualWrite(VIRTUAL_CHANNEL1, event.relative_humidity, "rel_hum", "p");
      }
    }
    CAYENNE_IN(VIRTUAL_CHANNEL3)
      {
      if (getValue.asInt() == 0) {
     digitalWrite(ACTUATOR_PIN1, HIGH);
    }
      else {
      digitalWrite(ACTUATOR_PIN1, LOW);
     }
    }

you count the time elapsed and do it. or have another switch to read the closed or open location of you motor.

What code can I use to count? I tried Ticker.h but couldn’t make sense of the examples to use it correctly in my code?

just simple as millis can do the work millis() - Arduino Reference

Okay can, I’ve got it working, what code do I need so the button in the dashboard knows the relay state? Also I’m not seeing any serial data on my monitor, is my code wrong?

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define VIRTUAL_CHANNEL0 0
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3
#define ACTUATOR_PIN1 1
DHT_Unified dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid[] = "MaxRange";
char wifiPassword[] = "";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

void setup() {
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN1, OUTPUT);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  dht.begin();
  sensor_t sensor;
}

void loop() {
  Cayenne.loop();
  //Publish data every 1 seconds (1000 milliseconds)
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    Cayenne.virtualWrite(VIRTUAL_CHANNEL2, lastMillis);
    sensors_event_t event;  
    dht.temperature().getEvent(&event);
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL0, event.temperature);
    dht.humidity().getEvent(&event);
    Cayenne.virtualWrite(VIRTUAL_CHANNEL1, event.relative_humidity, "rel_hum", "p");
  }
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
  {
  if (getValue.asInt() == 0) {
 digitalWrite(ACTUATOR_PIN1, LOW);
}
  else {
  digitalWrite(ACTUATOR_PIN1, HIGH);
  delay (500);
  digitalWrite(ACTUATOR_PIN1, LOW);
 }
}

you can send the relay state to a two state widget.
Cayenne.virtualWrite(1, relay_state, "digital_sensor", "d");

Add the following line in your code:
#define CAYENNE_DEBUG

Thank you @shramik_salgaonkar. I have added the code but the button on the dashboard doesn’t update and still can’t see any data on the serial monitor, a few random characters after restart and thats it

check your baud rate.

Serial monitor set to 9600 and Serial.begin(9600) ?

can you try this code:

That code works.

I found it I //comment out the pinMode(ACTUATOR_PIN1, OUTPUT); the serial monitor is seeing all the data. When that line is back in it doesn’t work?

This is the current code. With the pinMode(ACTUATOR_PIN1, OUTPUT); where it is the port doesn’t seem to be working. If I move this a few lines down, the port works but i have no serial data?

#define CAYENNE_PRINT Serial
#define CAYENNE_DEBUG
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 4
#define DHTTYPE DHT11
#define VIRTUAL_CHANNEL0 0
#define VIRTUAL_CHANNEL1 1
#define VIRTUAL_CHANNEL2 2
#define VIRTUAL_CHANNEL3 3
#define ACTUATOR_PIN1 1
DHT_Unified dht(DHTPIN, DHTTYPE);


// WiFi network info.
char ssid[] = "MaxRange";
char wifiPassword[] = "";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

unsigned long lastMillis = 0;

void setup() {
  pinMode(ACTUATOR_PIN1, OUTPUT);
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  dht.begin();
  sensor_t sensor;
}

void loop() {
  Cayenne.loop();
  //Publish data every 1 seconds (1000 milliseconds)
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    Cayenne.virtualWrite(VIRTUAL_CHANNEL2, lastMillis);
    sensors_event_t event;  
    dht.temperature().getEvent(&event);
    Cayenne.celsiusWrite(VIRTUAL_CHANNEL0, event.temperature);
    dht.humidity().getEvent(&event);
    Cayenne.virtualWrite(VIRTUAL_CHANNEL1, event.relative_humidity, "rel_hum", "p");
    Cayenne.virtualWrite(VIRTUAL_CHANNEL3, ACTUATOR_PIN1, "digital_sensor", "d");
  }
}
CAYENNE_IN(VIRTUAL_CHANNEL3)
  {
  if (getValue.asInt() == 1) {
 digitalWrite(ACTUATOR_PIN1, LOW);
}
  else {
  digitalWrite(ACTUATOR_PIN1, HIGH);
  delay (500);
  digitalWrite(ACTUATOR_PIN1, LOW);
 }
}

Many thanks for your help @shramik_salgaonkar. I found the issue, the relay was using digital pin 1, found that you do not use digital pins 0 or 1 since those conflict with the use of Serial.

I’m still not able to get the button on the dashboard to show the correct state of the pin

yup, you have to avoid using those pins.

what does your serial monitor output when you press the button on the dashboard.

you are sending data at quite a rapid rate, make it to 15 seconds.
if (millis() - lastMillis > 1000) {
to
if (millis() - lastMillis > 15000) {

okay changed to 15 seconds thank you.

serial monitor shows this when i press dashboard button:
[20879] Message received: topic 2, channel 3
[20879] In: value 1, channel 3
[21379] publishState: topic 1 channel 3
[21379] Publish: topic 1, channel 3, value 1, subkey , key
[21381] Send response: zMyn1FLL1xvvtw8

that is the correct output and your relay should be working unless you have connected it wrong and faulty relay module.

Sorry misunderstanding. Relay is working when i press the button from the OFF to the ON position. I want the button to automatically go back to the OFF position. Is that possible, like a momentary button

try this when you want to make the button state to OFF:

Cayenne.virtualWrite(3, 0);