LED brightness ISSUE

hi…:heart_eyes:
i’m trying to control a led brightness with the slider in thinger dashboard
i’m writing the code as shown below , what’s wrong ??:sweat:
why the slider go back to zero !!
i’m using NodeMCU … v1.0

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define LED D0

#define SSID "*******************"
#define SSID_PASSWORD "****************"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << [](pson& in){
    if(in.is_empty()){
      in = LED;
    }
   else{
     LED= in;
    }
  };
}
void loop() {
  thing.handle();
}

THE ERROR IS :
assignment of read-only variable ‘D0’ :cry:

Hi @Hussam_Farhat, I think that your code tries to change a constant value that is unacceptable ( #define LED D0). Futhermore, in order to control the brightness of an LED you have to use PWM (Pulse Width Modulation). In your code you use D0 pin that, as I am aware, does not support PWM.

So, your code should look like the example below (I’ve used D1 pin):

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define LED D1;
byte brightness = 0;

#define SSID "*******************"
#define SSID_PASSWORD "****************"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << [](pson& in){
    if(in.is_empty()){
      in = brightness;
    }
   else {
     brightness = in;
    }
  };
}
void loop() {
  thing.handle();
  analogWrite(LED, brightness);
}

Set the slider widget from 0 to 255.

1 Like

Or:

#include <ESP8266WiFi.h>
#include <ThingerESP8266.h>

#define USERNAME "******************"
#define DEVICE_ID "***************"
#define DEVICE_CREDENTIAL "****************"

#define SSID "*******************"
#define SSID_PASSWORD "****************"

#define LED D1

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() 
{
  thing.add_wifi(SSID, SSID_PASSWORD);

  pinMode (LED,OUTPUT);

  thing["LED"] << analogPin(LED);
}

void loop() {
  thing.handle();
}
1 Like

OMG !!!:heart_eyes:
that’s amazing !!!
it’s working as well

thaaaaaaaaaaaaaaaaaaaank you so much
for both of you guys
:slight_smile:

what did work? Please write the solution in order to get it well documented for buddies with this issue in the future :wink:

It worked as long as my slider is set 0 to 1023. If my slider has a value of up to 255 it doesn’t work. Another question, why my slider is at maximum value, my LED turn off and when it is at minimum it turns on?

It should be because, for example, the nodemcu esp8266 has inverted logic for the builtin led (when its pin is LOW it turns ON, when its HIGH turns OFF).

Hi Hussam,

How I use this code for esp32 wroom?

Because i had a problem, no recognize the analogWrite and analogPin.

How I use this functions?What library I use?

How I use this code for esp32 wroom?

Because i had a problem, no recognize the analogWrite and analogPin.

How I use this

Hello @engclaudiorp,

It seems that the ESP32 way of managing PWM signals is a little bit different from the ESP8266, in that you need to set the frequency, the channel and the resolution, as well as using different functions.

With the latest libraries of ESP32 and Thinger this code seems to work:

#include <ThingerESP32.h>

#define USERNAME "*********"
#define DEVICE_ID "*********"
#define DEVICE_CREDENTIAL "*********"

#define SSID "*********"
#define SSID_PASSWORD "*********"

#define LED 16

const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;

ThingerESP32 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

void setup() {

  thing.add_wifi(SSID, SSID_PASSWORD);

  // configure LED PWM functionalitites
  ledcSetup(ledChannel, freq, resolution);

  // attach the channel to the GPIO to be controlled
  ledcAttachPin(LED, ledChannel);

  thing["led"] << [](pson& in){
    if(in.is_empty()){
      in = ledcRead(ledChannel);
    }
   else {
     ledcWrite(ledChannel,in);
    }
  };

}

void loop() {
  thing.handle();
}

I’ll leave a couple of references if you are interested in further investigating:

1 Like