Turn on LED with one button

Hello friends, I am testing Cayenne, I started using an ESP8266-07 and a DHT11 sensor, I got it to work properly.
Now I added two buttons to light two LEDs, these are connected directly to pins 2 and 5 with a resistance to ground each as indicated by all examples.
The issue that if I connect the system, is Off Line. If I disconnect the LEDs before connecting the system and then connect them, everything works normally. That is, I have to start the system with the LEDs disconnected.
Is there something wrong with the code? Should I insert a transistor between the doors and the LED?
My sckecht:

// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. 

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 0
#define DHTTYPE DHT11
DHT_Unified dht(DHTPIN, DHTTYPE);

// WiFi network info.
char ssid[] = "xxx";
char wifiPassword[] = "xxx";

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

unsigned long lastMillis = 0;

void setup() {
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  pinMode(2, OUTPUT);
  digitalWrite(2, LOW);
  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). Change this value to publish at a different interval.
  if (millis() - lastMillis > 1000) {
    lastMillis = millis();
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    Cayenne.virtualWrite(0, lastMillis);

    sensors_event_t event;  
    dht.temperature().getEvent(&event);
    Cayenne.celsiusWrite(1, event.temperature);
    dht.humidity().getEvent(&event);
    Cayenne.virtualWrite(2, event.relative_humidity, "rel_hum", "p");
  }
}

CAYENNE_IN(6)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
    //do whatever you want when you turn on the button on cayenne dashboard
    digitalWrite(2, HIGH);
  }
  else
  {
    //do whatever you want when you turn off the button on cayenne dashboard
    digitalWrite(2, LOW);
  }
}
CAYENNE_IN(7)
{
  int currentValue = getValue.asInt();
  if (currentValue == 1)
  {
    //do whatever you want when you turn on the button on cayenne dashboard
    digitalWrite(5, HIGH);
  }
  else
  {
    //do whatever you want when you turn off the button on cayenne dashboard
    digitalWrite(5, LOW);
  }
}

//Default function for processing actuator commands from the Cayenne Dashboard.
//You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

how much wattage is your led? The maximum source current of GPIO is about 12mA.
You may check the serial monitor to see if there is any issue by adding #define CAYENNE_DEBUG

Also change if (millis() - lastMillis > 1000) { to if (millis() - lastMillis > 15000) {. You are publishing data at very rapid rate and will hit rate limit. Sending MQTT messages within rate limits

I solved it by connecting the LEDs to the GPIO4 and GPIO5. For some reason, he didn’t like it being connected to GPIO2. Thanks!

1 Like

but i hope you made this changes.

1 Like