Quantcast
Channel: myDevices Cayenne Community - Latest topics
Viewing all articles
Browse latest Browse all 4739

Turn on LED with one button

$
0
0

@dpozzi wrote:

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[] = "Mapear 2.4 GHz";
char wifiPassword[] = "03472456977";

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "5a3596e0-f12b-11e9-8221-599f77add412";
char password[] = "9a4f400584a42a4f47f840e3d782f06644668648";
char clientID[] = "28f53b50-f13d-11e9-b49d-5f4b6757b1bf";

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");
}

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4739

Latest Images

Trending Articles



Latest Images