Controling the brightness of a lamp

Good, so at this point you want to step through your code and find where it’s not getting. Use Serial.print to help you follow the execution.

one of the best thing out there.

Sorry for asking help again, but I tried that for many hours and no positive result.

This is the code that is working without cayenne, meaning I write the dimming level wanted in the Arduino IDE serial monitor and it works perfectly (lamp changes brightness):

#include β€œhw_timer.h”
const byte zcPin = 12;
const byte pwmPin = 13;

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;
byte zcState = 0; // 0 = ready; 1 = processing;
void ICACHE_RAM_ATTR zcDetectISR ();
void ICACHE_RAM_ATTR dimTimerISR ();
void setup() {
Serial.begin(115200);
pinMode(zcPin, INPUT_PULLUP);
pinMode(pwmPin, OUTPUT);
attachInterrupt(zcPin, zcDetectISR, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
hw_timer_init(NMI_SOURCE, 0);
hw_timer_set_func(dimTimerISR);
}

void loop() {
// put your main code here, to run repeatedly:
if (Serial.available()){
int val = Serial.parseInt();
if (val>0){
tarBrightness =val;
Serial.println(tarBrightness);
}
}
}

void dimTimerISR() {
if (fade == 1) {
if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0)) {
–curBrightness;
}
else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
++curBrightness;
}
}
else {
if (state == 1) {
curBrightness = tarBrightness;
}
else {
curBrightness = 0;
}
}

if (curBrightness == 0) {
  state = 0;
  digitalWrite(pwmPin, 0);
}
else if (curBrightness == 255) {
  state = 1;
  digitalWrite(pwmPin, 1);
}
else {
  digitalWrite(pwmPin, 1);
}

zcState = 0;

}

void zcDetectISR() {
if (zcState == 0) {
zcState = 1;

if (curBrightness < 255 && curBrightness > 0) {
  digitalWrite(pwmPin, 0);
  
  int dimDelay = 30 * (255 - curBrightness) + 400;//400
  hw_timer_arm(dimDelay);
}

}
}

The only thing I need from cayenne, is to send the val variable to the ESP (present on the void loop) instead of writing it in the ArduinoIDE serial monitor, nothing more.
Is there any alternative way or viable way to do that?

The folder with main code and .h and .c files (working with cayenne slider widget only a few seconds):

I’ll look at it later today. Certainly it is possible.

Thank you. I will be waiting

this is the edited code, i have just added the cayenne code.

/*
  This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.

  The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

  Steps:
  1. If you have not already installed the ESP8266 Board Package install it using the instructions here: https://github.com/esp8266/Arduino#installing-with-boards-manager.
  2. Select your ESP8266 board from the Tools menu.
  3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  4. Set the network name and password.
  5. Compile and upload the sketch.
  6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
*/

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

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

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

#include "hw_timer.h"
const byte zcPin = 12;
const byte pwmPin = 13;

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;
byte zcState = 0; // 0 = ready; 1 = processing;
void ICACHE_RAM_ATTR zcDetectISR ();
void ICACHE_RAM_ATTR dimTimerISR ();

void setup() {
  Serial.begin(9600);
  pinMode(zcPin, INPUT_PULLUP);
  pinMode(pwmPin, OUTPUT);
  attachInterrupt(zcPin, zcDetectISR, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
  hw_timer_init(NMI_SOURCE, 0);
  hw_timer_set_func(dimTimerISR);
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

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

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  // Some examples of other functions you can use to send data.
  //Cayenne.celsiusWrite(1, 22.0);
  //Cayenne.luxWrite(2, 700);
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// 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(2)
{
  int tarBrightness = getValue.asInt();
}

void dimTimerISR() {
  if (fade == 1) {
    if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0))
    {
      --curBrightness;
    }
    else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
      ++curBrightness;
    }
  }
  else {
    if (state == 1) {
      curBrightness = tarBrightness;
    }
    else {
      curBrightness = 0;
    }
  }

  if (curBrightness == 0) {
    state = 0;
    digitalWrite(pwmPin, 0);
  }
  else if (curBrightness == 255) {
  
    state = 1;
    digitalWrite(pwmPin, 1);
  }
  else {
    digitalWrite(pwmPin, 1);
  }

  zcState = 0;
}

void zcDetectISR() {
  if (zcState == 0) {
    zcState = 1;

    if (curBrightness < 255 && curBrightness > 0) {
          Serial.println("ISR");
      digitalWrite(pwmPin, 0);

      int dimDelay = 30 * (255 - curBrightness) + 400;//400
      hw_timer_arm(dimDelay);
    }
  }
}

Hello,

The code uploaded successfully to ESP8266, and it is connected to cayenne. Sadly, the slider widget is not controlling the lamp, it is fully bright always.
I added the following line to void loop:

Serial.println(tarBrightness);

The serial monitor shows:

16:19:48.315 β†’ 255
16:19:49.326 β†’ 255
16:19:50.345 β†’ 255
16:19:51.322 β†’ 255
16:19:52.331 β†’ 255
16:19:53.340 β†’ 255
16:19:54.350 β†’ 255
16:19:55.359 β†’ 255
16:19:56.369 β†’ 255
16:19:57.379 β†’ 255
16:19:58.361 β†’ 255
16:19:59.382 β†’ 255
16:20:00.395 β†’ 255
16:20:01.372 β†’ 255
16:20:02.382 β†’ 255
16:20:03.393 β†’ 255
16:20:04.404 β†’ 255
16:20:05.415 β†’ 255

please share the entire code.

The files attached (.h and .c) to this main code are the same, as sent in the link before:

/*
This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.

The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.

Steps:

  1. If you have not already installed the ESP8266 Board Package install it using the instructions here: GitHub - esp8266/Arduino: ESP8266 core for Arduino.
  2. Select your ESP8266 board from the Tools menu.
  3. Set the Cayenne authentication info to match the authentication info from the Dashboard.
  4. Set the network name and password.
  5. Compile and upload the sketch.
  6. A temporary widget will be automatically generated in the Cayenne Dashboard. To make the widget permanent click the plus sign on the widget.
    */

//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

char ssid = β€œβ€;
char wifiPassword = β€œβ€;

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username = β€œβ€;
char password = β€œβ€;
char clientID = β€œβ€;

#include β€œhw_timer.h”
const byte zcPin = 12;
const byte pwmPin = 13;

byte fade = 1;
byte state = 1;
byte tarBrightness = 255;
byte curBrightness = 0;
byte zcState = 0; // 0 = ready; 1 = processing;
void ICACHE_RAM_ATTR zcDetectISR ();
void ICACHE_RAM_ATTR dimTimerISR ();

void setup() {
Serial.begin(115200);
pinMode(zcPin, INPUT_PULLUP);
pinMode(pwmPin, OUTPUT);
attachInterrupt(zcPin, zcDetectISR, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
hw_timer_init(NMI_SOURCE, 0);
hw_timer_set_func(dimTimerISR);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
}

void loop() {
delay(100);
Cayenne.loop();
Serial.println(tarBrightness);
}

// 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(2)
{
int tarBrightness = getValue.asInt();
}

void dimTimerISR() {
if (fade == 1) {
if (curBrightness > tarBrightness || (state == 0 && curBrightness > 0))
{
–curBrightness;
}
else if (curBrightness < tarBrightness && state == 1 && curBrightness < 255) {
++curBrightness;
}
}
else {
if (state == 1) {
curBrightness = tarBrightness;
}
else {
curBrightness = 0;
}
}

if (curBrightness == 0) {
state = 0;
digitalWrite(pwmPin, 0);
}
else if (curBrightness == 255) {

state = 1;
digitalWrite(pwmPin, 1);

}
else {
digitalWrite(pwmPin, 1);
}

zcState = 0;
}

void zcDetectISR() {
if (zcState == 0) {
zcState = 1;

if (curBrightness < 255 && curBrightness > 0) {
      Serial.println("ISR");
  digitalWrite(pwmPin, 0);

  int dimDelay = 30 * (255 - curBrightness) + 400;//400
  hw_timer_arm(dimDelay);
}

}
}

can you put it CAYENNE_IN and see what you get.

I added some Serial.Print to label the values:

17:12:03.927 β†’ Loop_tarBrightness->255
17:12:04.569 β†’ Cayenne_tarBrightness->11
17:12:05.377 β†’ Loop_tarBrightness->255
17:12:06.487 β†’ Loop_tarBrightness->255
17:12:07.599 β†’ Loop_tarBrightness->255
17:12:08.709 β†’ Loop_tarBrightness->255
17:12:09.822 β†’ Loop_tarBrightness->255
17:12:10.901 β†’ Loop_tarBrightness->255
17:12:11.543 β†’ Cayenne_tarBrightness->6
17:12:12.892 β†’ Loop_tarBrightness->255
17:12:14.002 β†’ Loop_tarBrightness->255
17:12:15.115 β†’ Loop_tarBrightness->255
17:12:16.191 β†’ Loop_tarBrightness->255
17:12:17.300 β†’ Loop_tarBrightness->255
17:12:18.408 β†’ Loop_tarBrightness->255

my bad. change it to

CAYENNE_IN(2) { tarBrightness = getValue.asInt(); }

With that change, this is what serial monitor shows:

17:29:32.462 β†’ ISR
17:29:32.496 β†’ ISR
17:29:32.496 β†’ ISR
17:29:32.496 β†’ ISR
17:29:32.529 β†’ ISR
17:29:33.068 β†’ [4829] Connected
17:29:34.448 β†’ Loop_tarBrightness->255
17:29:35.563 β†’ Loop_tarBrightness->255
17:29:36.675 β†’ Loop_tarBrightness->255
17:29:37.755 β†’ Loop_tarBrightness->255
17:29:38.867 β†’ Loop_tarBrightness->255
17:29:39.979 β†’ Loop_tarBrightness->255
…
17:29:51.544 β†’ Cayenne_curBrightness->255
17:29:51.577 β†’ Cayenne_tarBrightness->5
17:29:52.285 β†’ Loop_tarBrightness->5
17:29:52.656 β†’ ISR
17:29:52.656 β†’ ISR
17:29:52.689 β†’ ISR
17:29:52.689 β†’ ISR
17:29:52.689 β†’ ISR
…
17:29:55.959 β†’
17:29:55.959 β†’ Exception (0):
17:29:55.959 β†’ epc1=0x40201018 epc2=0x00000000 epc3=0x400046e4 excvaddr=0x00000000 depc=0x00000000
17:29:55.959 β†’
17:29:55.959 β†’ >>>stack>>>
17:29:55.959 β†’
17:29:55.959 β†’ ctx: sys
17:29:55.959 β†’ sp: 3ffee250 end: 3fffffb0 offset: 01a0
17:29:55.959 β†’ 3ffee3f0: 4010594f 00000000 00000000 00000000
17:29:55.959 β†’ 3ffee400: 00000020 00000000 00000000 40000f68
17:29:55.959 β†’ 3ffee410: 00000004 00000000 40000f58 00000000
17:29:55.959 β†’ 3ffee420: 40104be7 3fffee80 00000000 00000000
17:29:55.992 β†’ 3ffee430: 00000001 04000001 04000001 3feffe00
17:29:55.992 β†’ 3ffee440: 00000100 60000200 00000001 fffffffe
…
17:30:15.625 β†’ 3fffff90: 3fffdad0 00000000 3ffee94c 4020339a
17:30:15.625 β†’ 3fffffa0: feefeffe feefeffe 3ffee94c 402058fc
17:30:15.625 β†’ <<<stack<<<
17:30:15.659 β†’
17:30:15.659 β†’ ets Jan 8 2013,rst cause:2, boot mode:(3,6)
17:30:15.659 β†’
17:30:15.659 β†’ load 0x4010f000, len 1384, room 16
17:30:15.659 β†’ tail 8
17:30:15.659 β†’ chksum 0x2d
17:30:15.659 β†’ csum 0x2d
17:30:15.659 β†’ vbc204a9b
17:30:15.659 β†’ ~ld

i really dont know what is the issue here for the reset. @adam do you have anything?

@shramik_salgaonkar and @adam thank you so much for the help and effort!

The prototype is finally working as desired! I can now control a lamp brightness/intensity trough the slider widget on cayenne dashboard :smiley: All that, trough a ESP8266 and an AC Dimmer, making the prototype fully wireless/disconnected from computers!
The solution was to downgrade the ESP8266 library to version 2.5.0., on boards manager of Arduino IDE.

Soon, I will share here (cayenne community) my full project of low-cost home automation that includes:
Measurement of energy (KWh) of electric circuits and display of that information on Cayenne dashboard (trough ESP8266).
Control of electric loads with an relay switch trough cayenne dashboard switch (trough ESP8266).
Usage of sensors, of luminosity and movement, and receiving their information on cayenne dashboard (trough ESP8266).

1 Like

glad to hear it is working now.

2 Likes