Controling the brightness of a lamp

the value which you are reading from here, read it from cayenne using

CAYENNE_IN()
{
 // get data
}

Thank you, will try and report results.

This is the code i’m using:

#include “hw_timer.h”
#include <CayenneMQTTESP8266.h>
#define CAYENNE_PRINT Serial

char ssid = “”;
char wifiPassword = “”;

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

unsigned long lastMillis = 0;

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);
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
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() {
Cayenne.loop();
// 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);
}

}
}

CAYENNE_IN(26)
{
CAYENNE_LOG(“Channel %d, pin %d, value %d”, tarBrightness);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
}

The ESP connects to cayenne, but when I create the slider widget in chanel 26, it doesn’t control the lamp. Suggestions?

why 26, you can use lower value. and i dont see the code to read the value from the slider.

The Cayenne MQTT example for using slider widget is:

CAYENNE_IN(VIRTUAL_CHANNEL)
{
int value = getValue.asInt(); // 0 to 255
CAYENNE_LOG(“Channel %d, pin %d, value %d”, VIRTUAL_CHANNEL, ACTUATOR_PIN, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(ACTUATOR_PIN, value);
}

I tried the code with this lines, but the slider widget doesn’t work:

CAYENNE_IN(1)
{
int value = getValue.asInt();
value = tarBrightness;
CAYENNE_LOG(“Channel %d, pin %d, value %d”, 1, pwmPin, value);
// Write the value received to the PWM pin. analogWrite accepts a value from 0 to 255.
analogWrite(pwmPin, value);
}

Suggestions?

what do you get in the serial monitor.

Hello,

I managed to control the lamp intensity trough the cayenne slider widget, but sadly after a few seconds the system “crashes” meaning the lamp turns off without my order, and it stays off, but the ESP stays connected to cayenne. This is the code i’m using and the text that appears on serial monitor:

#include "hw_timer.h"   
#include <CayenneMQTTESP8266.h>
#define CAYENNE_PRINT Serial

char ssid[] = "";
char wifiPassword[] = "";

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

unsigned long lastMillis = 0;

const byte zcPin = 12;
const byte pwmPin = 13;  

byte fade = 1;
byte state = 1;
byte curBrightness = 0;
byte tarBrightness = 255;
byte zcState = 0; // 0 = ready; 1 = processing;

void ICACHE_RAM_ATTR zcDetectISR (); //PROBLEMAS DO ESP
void ICACHE_RAM_ATTR dimTimerISR ();

void setup() {
  Serial.begin(115200);   
  Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  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() {
  delay(200);
  Cayenne.loop();
}

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

CAYENNE_IN(1)
{
  int dimminglevel = getValue.asInt();
      if (dimminglevel>0){
  tarBrightness=dimminglevel; 
      Serial.println(dimminglevel);
      }
      if (dimminglevel == NULL){
  tarBrightness=curBrightness; 
      Serial.println(dimminglevel);
      }
}

Serial monitor:

00:59:09.838 → 15
00:59:16.310 → 1
00:59:19.042 → 72
00:59:42.282 →
00:59:42.282 → Exception (0):
00:59:42.282 → epc1=0x40201040 epc2=0x00000000 epc3=0x40000f68 excvaddr=0x00000000 depc=0x00000000
00:59:42.282 →
00:59:42.282 → >>>stack>>>
00:59:42.282 →
00:59:42.282 → ctx: sys
00:59:42.282 → sp: 3fffeba0 end: 3fffffb0 offset: 01a0
00:59:42.316 → 3fffed40: 40100462 00000000 00000000 401004ec
00:59:42.316 → 3fffed50: c0037035 00000016 00000016 ffffff60
00:59:42.316 → 3fffed60: 00000a10 00000142 00000142 00000022
00:59:42.316 → 3fffed70: 3fffc200 40100428 3fffc258 4000050c
00:59:42.316 → 3fffed80: 400046ea 00000030 00000020 ffffffff
00:59:42.316 → 3fffed90: 40104ae3 00000000 00000000 00000001
00:59:42.316 → 3fffeda0: 04000001 04000001 3feffe00 00000100
00:59:42.316 → 3fffedb0: 60000200 00000001 fffffffe 00000008
00:59:42.350 → 3fffedc0: 00000000 00000000 60000600 00000030
00:59:42.350 → 3fffedd0: 3ffed28c 3fffee80 3ffed918 00000016
00:59:42.350 → 3fffede0: 4021f088 3fffee80 3ffed7c4 3ffed150
00:59:42.350 → 3fffedf0: 4021f0e9 3fffee80 3fffee80 00000008
00:59:42.350 → 3fffee00: 2d4f454d 00000000 0000000d 4010038d
00:59:42.350 → 3fffee10: 3ffe957c 0000000c 3ffee5f1 401001d8
00:59:42.350 → 3fffee20: 401004fd 00000080 00000000 401004ec
00:59:42.384 → 3fffee30: 00000000 00000000 0000001f 40100250
00:59:42.384 → 3fffee40: 3ffee000 3ffef0c4 3fffc228 40105315
00:59:42.384 → 3fffee50: 4000050c 40100428 3fffc258 4000050c
00:59:42.384 → 3fffee60: 40000f68 00000030 00000010 ffffffff
00:59:42.384 → 3fffee70: 40000f58 00000000 00000020 00000000
00:59:42.384 → 3fffee80: 4010670a 4022c16c 3ffedfd8 00000001

02:57:06.006 → 3fffff80: 40205839 000003e8 3ffee89c 3ffee89c
02:57:06.006 → 3fffff90: 3fffdad0 00000000 3ffee85c 4020303d
02:57:06.006 → 3fffffa0: feefeffe feefeffe 3ffee85c 40205394
02:57:06.006 → <<<stack<<<
02:57:06.040 →
02:57:06.040 → ets Jan 8 2013,rst cause:2, boot mode:(3,6)
02:57:06.040 →
02:57:06.040 → load 0x4010f000, len 1392, room 16
02:57:06.074 → tail 0
02:57:06.074 → chksum 0xd0
02:57:06.074 → csum 0xd0
02:57:06.074 → v3d128e5c
02:57:06.074 → ~ld

Any way to solve the problem?

Can anyone help me solve this issue?
It’s a problem of cayenne firmware, because I was able to use the equipments refered in this post to offline control the lamp.

if it was cayenne problem then no one would have used esp8266 to connect to cayenne.

can you link me how to add this?

The files requested:

I just downloaded those files and used Arduino IDE sketch → Add files, then uploaded the code I sent earlier in the post.

Is there a reason you don’t call your dimming functions from within CAYENNE_IN(1)? Unless I’m missing something you wouldn’t need the hw_timer library if you did it there.

How can I do that?
Sorry, I’m not very experienced in Arduino programming.
Thank you

Any suggestion?

Could you send me how to do that, or the code adapted to your suggestion?

Thank you

This is the full folder, with the main code and .h and .c files:
https://easyupload.io/sfmy4s

Looking at it now

Try this

//#include "hw_timer.h"   
#include <CayenneMQTTESP8266.h>
//#define CAYENNE_PRINT Serial

char ssid[] = "";
char wifiPassword[] = "";

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

//unsigned long lastMillis = 0;

const byte zcPin = 12;
const byte pwmPin = 13;  

byte fade = 1;
byte state = 1;
byte curBrightness = 0;
byte tarBrightness = 255;
//byte zcState = 0; // 0 = ready; 1 = processing;

//void ICACHE_RAM_ATTR zcDetectISR (); //PROBLEMAS DO ESP
//void ICACHE_RAM_ATTR dimTimerISR ();

void setup() {
	Serial.begin(115200);   
	Cayenne.begin(username, password, clientID, ssid, wifiPassword);
	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() {
	delay(200);
	Cayenne.loop();
}

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;
	if (curBrightness < 255 && curBrightness > 0) {
			digitalWrite(pwmPin, 0);

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

/*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);
		}
	}
}*/

CAYENNE_IN(1)
{
	int dimminglevel = getValue.asInt();
	if (dimminglevel>0){
		tarBrightness=dimminglevel; 
		dimTimerISR();
		Serial.println(dimminglevel);
	}
	delay(200);
}
1 Like

Hello Adam,

Thank you for the response, the following error appears in Arduino IDE as I try to upload the code:

exit status 1
‘hw_timer_arm’ was not declared in this scope

Comment the line hw_timer_arm(dimDelay)

Hello Adam,

With that change, the code uploads sucessufuly to the ESP.
The cayenne dashboard detects and has the ESP connected to it, but the lamp is off and the slider does not control the lamp luminosity.

Thank you.