Controling the brightness of a lamp

Hello,

I’m trying to control a lamp brightness using the cayenne platform.
I found this tutorial on docs:
https://developers.mydevices.com/cayenne/docs/supported-hardware/#supported-hardware-actuators-light-luminosity

But how can I control the brightness of a 230V lamp trough the cayenne platform?

Thank you!

first of all does your 230v lamp capable of controlling the brightness,

Yes, it is

can you share which device it is.

better if you can provide a link to this device and if there is any tutorial online on how to control it.

I am trying to use this to control the lamp:

Wich code should I use to use this device to control the lamp trough cayenne?

which device are you using to connect to cayenne?
in the link you gave, i found this code https://github.com/RobotDynOfficial/RBDDimmer/blob/master/examples/SimplePotentiometer/SimplePotentiometer.ino which can be modified to work with cayenne.

I am using this device to control the brightness of the lamp:

https://opencircuit.shop/Product/AC-Light-Dimmer-Module.-1-Channel.-3.3V-5V

I am using this code, uploading from arduino IDE to my ESP8266 - CP2102 NodeMCU V3 Lua (controling the lamp brightness trough cayenne dashboard widget):

//===================================================================
// Author : Techs & Crafts
// Project : Dimmer Control / FAN Speed Control through cayenne (MQTT)
//
//
//==================================================================

#include <CayenneMQTTESP8266.h> // Include library file for MQTT

char ssid = “xxxxxxxxx”; // Your WIFI Name
char wifiPassword = “xxxxxxxxxxx”; // Your WIFI password
char username = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”; // Your MQTT cayenne username
char Password = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”; // Your MQTT cayenne Password
char clientID = “xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”; // Your MQTT cayenne clientID
int DIMMING_VALUES=0;
int DIMMING_TIME=0;
#define DEVICE 14
//=====================Basic Setup ============================

void setup(){
Serial.begin(9600); // Setup Debug uart port if you want ?
pinMode(DEVICE, OUTPUT); // Set AC Load pin as output
attachInterrupt(4, zero_crosss_int, RISING); // Choose the zero cross interrupt egde selection
Cayenne.begin(username, Password, clientID, ssid, wifiPassword); // Setup cayenne server for MQTT protocol
}

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

void zero_crosss_int() //function to be fired at the zero crossing to dim the light
{
DIMMING_TIME = (75 * DIMMING_VALUES); // For 60Hz =>65
delayMicroseconds(DIMMING_TIME); // Wait till firing the TRIAC
digitalWrite(DEVICE, HIGH); // Fire the TRIAC
delayMicroseconds(10); // triac On propogation delay
// (for 60Hz use 8.33) Some Triacs need a longer period
digitalWrite(DEVICE, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}

CAYENNE_IN(7)
{
int Dimm_Val=getValue.asInt();
DIMMING_VALUES = (120-Dimm_Val);
Serial.println(DIMMING_VALUES);
}
//==================================================================

But it’s not working, any suggestions on how to fix the problem?

from where did you get the code to control the dimmer?

A video in youtube, where the user shows a lamp brightness being controled by cayenne dashboard widget. He connected the ac dimmer to a ESP-8266. He shared this code in his video.

can you share the link to the code.

first of all the dimmer circuit is not at same.

What code should I use then?

i gave here.

Thank you.
I will try and test. I will comment here about the results

1 Like

Hello again,

I uploaded this code to my ESP8266 (from where I connected it to the AC dimmer):

#include <CayenneMQTTESP8266.h>

// 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_PRINT Serial
#define CAYENNE_DEBUG

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

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

/**************

  • RobotDyn
  • Dimmer Library

  • The following sketch is meant to to define dimming value through potentiometer,
  • The potentiometer values are changing in range from 0 to 1023
  • potentiometer values are converted through the map function to values from 0 to 100% and saved in dimmer.setPower(outVal);
  • Serial.begin is used to display dimming values
  • ---------------------- OUTPUT & INPUT Pin table ---------------------
  • ±--------------±------------------------±------------------------+
  • | Board | INPUT Pin | OUTPUT Pin |
  • | | Zero-Cross | |
  • ±--------------±------------------------±------------------------+
  • | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
  • ±--------------±------------------------±------------------------+
  • | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
  • ±--------------±------------------------±------------------------+
  • | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
  • ±--------------±------------------------±------------------------+
  • | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
  • | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
  • | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
  • | | | D8(IO15) |
  • ±--------------±------------------------±------------------------+
  • | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
  • | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
  • | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
  • | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
  • | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
  • | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
  • | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
  • | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
  • | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
  • | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
  • | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
  • | | 33(GPIO21), 35(GPIO1), | |
  • | | 36(GPIO22), 37(GPIO23), | |
  • ±--------------±------------------------±------------------------+
  • | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
  • | Arduino Zero | | |
  • ±--------------±------------------------±------------------------+
  • | Arduino Due | D0-D53 | D0-D53 |
  • ±--------------±------------------------±------------------------+
  • | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
  • | Black Pill | PC13-PC15 | PC13-PC15 |
  • | BluePill | | |
  • | Etc… | | |
  • ±--------------±------------------------±------------------------+
    */

#include <RBDdimmer.h>//
#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define outputPin 12

dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
//dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;

void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
pinMode(outputPin, OUTPUT);
Serial.begin(74880);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
outVal = map(analogRead(0), 1, 1024, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%);
digitalWrite(outputPin, outVal);
dimmer.setPower(outVal); // name.setPower(0%-100%)
}

But it’s not working. I’m not understanding the best way to write the code to create the widget with dimming values on cayenne dashboard and how to implement the virtual channel.

You need to add a new slider widget on your cayenne dashboard and add the below code to read data from the slider and write it to digital PIn.

CAYENNE_IN(1)
{
	int outvalue = getValue.asInt(); // 100 to 0
digitalWrite(outputPin, outVal);
dimmer.setPower(outVal); // name.setPower(0%-100%)

}

With this code:

#include <RBDdimmer.h>
#include <CayenneMQTTESP8266.h>

// 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_PRINT Serial
#define CAYENNE_DEBUG

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

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

/**************

  • RobotDyn
  • Dimmer Library

  • The following sketch is meant to to define dimming value through potentiometer,
  • The potentiometer values are changing in range from 0 to 1023
  • potentiometer values are converted through the map function to values from 0 to 100% and saved in dimmer.setPower(outVal);
  • Serial.begin is used to display dimming values
  • ---------------------- OUTPUT & INPUT Pin table ---------------------
  • ±--------------±------------------------±------------------------+
  • | Board | INPUT Pin | OUTPUT Pin |
  • | | Zero-Cross | |
  • ±--------------±------------------------±------------------------+
  • | Lenardo | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
  • ±--------------±------------------------±------------------------+
  • | Mega | D2 (NOT CHANGABLE) | D0-D1, D3-D70 |
  • ±--------------±------------------------±------------------------+
  • | Uno | D2 (NOT CHANGABLE) | D0-D1, D3-D20 |
  • ±--------------±------------------------±------------------------+
  • | ESP8266 | D1(IO5), D2(IO4), | D0(IO16), D1(IO5), |
  • | | D5(IO14), D6(IO12), | D2(IO4), D5(IO14), |
  • | | D7(IO13), D8(IO15), | D6(IO12), D7(IO13), |
  • | | | D8(IO15) |
  • ±--------------±------------------------±------------------------+
  • | ESP32 | 4(GPI36), 6(GPI34), | 8(GPO32), 9(GP033), |
  • | | 5(GPI39), 7(GPI35), | 10(GPIO25), 11(GPIO26), |
  • | | 8(GPO32), 9(GP033), | 12(GPIO27), 13(GPIO14), |
  • | | 10(GPI025), 11(GPIO26), | 14(GPIO12), 16(GPIO13), |
  • | | 12(GPIO27), 13(GPIO14), | 23(GPIO15), 24(GPIO2), |
  • | | 14(GPIO12), 16(GPIO13), | 25(GPIO0), 26(GPIO4), |
  • | | 21(GPIO7), 23(GPIO15), | 27(GPIO16), 28(GPIO17), |
  • | | 24(GPIO2), 25(GPIO0), | 29(GPIO5), 30(GPIO18), |
  • | | 26(GPIO4), 27(GPIO16), | 31(GPIO19), 33(GPIO21), |
  • | | 28(GPIO17), 29(GPIO5), | 34(GPIO3), 35(GPIO1), |
  • | | 30(GPIO18), 31(GPIO19), | 36(GPIO22), 37(GPIO23), |
  • | | 33(GPIO21), 35(GPIO1), | |
  • | | 36(GPIO22), 37(GPIO23), | |
  • ±--------------±------------------------±------------------------+
  • | Arduino M0 | D7 (NOT CHANGABLE) | D0-D6, D8-D13 |
  • | Arduino Zero | | |
  • ±--------------±------------------------±------------------------+
  • | Arduino Due | D0-D53 | D0-D53 |
  • ±--------------±------------------------±------------------------+
  • | STM32 | PA0-PA15,PB0-PB15 | PA0-PA15,PB0-PB15 |
  • | Black Pill | PC13-PC15 | PC13-PC15 |
  • | BluePill | | |
  • | Etc… | | |
  • ±--------------±------------------------±------------------------+
    */

#define USE_SERIAL SerialUSB //Serial for boards whith USB serial port
#define outputPin 12
#define zerocross 5 // for boards with CHANGEBLE input pins

dimmerLamp dimmer(outputPin, zerocross); //initialase port for dimmer for ESP8266, ESP32, Arduino due boards
//dimmerLamp dimmer(outputPin); //initialase port for dimmer for MEGA, Leonardo, UNO, Arduino M0, Arduino Zero

int outVal = 0;

void setup() {
USE_SERIAL.begin(9600);
dimmer.begin(NORMAL_MODE, ON); //dimmer initialisation: name.begin(MODE, STATE)
pinMode(outputPin, OUTPUT);
Serial.begin(74880);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop()
{
outVal = map(analogRead(0), 1, 1024, 100, 0); // analogRead(analog_pin), min_analog, max_analog, 100%, 0%);
digitalWrite(outputPin, outVal);
dimmer.setPower(outVal); // name.setPower(0%-100%)
}

I’m getting this error:
‘serialUSB’ was not declared in this scope

How can I solve this?