Tadeas Dvoracek
Published

Lighting control

Lighting control using the Cayenne Dashboard button and wall mount button.

IntermediateShowcase (no instructions)2 hours1,535
Lighting control

Things used in this project

Hardware components

Wall mount button
×1
NodeMCU ESP8266 Breakout Board
NodeMCU ESP8266 Breakout Board
×1
Resistor 10k ohm
Resistor 10k ohm
×1
LED (generic)
LED (generic)
×1
Resistor 221 ohm
Resistor 221 ohm
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE
Cayenne
myDevices Cayenne

Story

Read more

Schematics

Schematics

Code

Cayenne-MQTT-ESP8266-Light_Control.txt

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

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

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

unsigned long lastMillis = 0;

int led=LOW;

void setup() {
	Serial.begin(9600);
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  pinMode(4, INPUT);   // button
  pinMode(5, OUTPUT); // LED or relay
}

void loop() {
	Cayenne.loop();

	if (millis() - lastMillis > 100) {
		lastMillis = millis();
		Cayenne.virtualWrite(0, lastMillis);
		}

   if(digitalRead(4)==HIGH && led==LOW)
  {
    digitalWrite(5, HIGH);
    led=HIGH;
    Cayenne.virtualWrite(1,"1");
    while(digitalRead(4)==HIGH)
    {
      delay(10);
    }
  }
  
  if(digitalRead(4)==HIGH && led==HIGH)
  {
    digitalWrite(5, LOW);
    led=LOW;
    Cayenne.virtualWrite(1,"0");
    while(digitalRead(4)==HIGH)
    {
      delay(10);
    }
  }
}


CAYENNE_IN_DEFAULT()
{
	CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());

  if (request.channel = 1)
  {
    String value;
    value = (String)getValue.asString();

    if (value == "1")
    {
      digitalWrite(5, HIGH);
      led=HIGH;
    }
    else
    {
      digitalWrite(5, LOW);
      led=LOW;
    }
  }
}

Credits

Tadeas Dvoracek

Tadeas Dvoracek

10 projects • 41 followers

Comments