NodeMCU WebThings Dimmable LED

Hi Guy's

I'm starting out trying to get a NodeMCU working with WebThings.

The example code doesn't work for me. Webthings will detect the thing (nodemcu), but won' switch the led On or control the brightness.

I've done quite a bit of searching and found an exaample that did work, but only an On/Off.

I've had an attempt in trying to modifiy the On/Off example, but struggling with it.

Here's what I currently have;

#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"
#include <ESP8266WiFi.h>

// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = "*****";
const char *password = "*****";

#if defined(LED_BUILTIN)
const int lampPin = LED_BUILTIN;
#else
const int lampPin = 16; // manually configure LED pin
#endif

WebThingAdapter *adapter;

const char *lampTypes[] = {"OnOffSwitch", "Light", "Level", nullptr};

//const int lampPin = LED_BUILTIN;

ThingDevice lamp("lamp", "My Lamp", "lampLevel", nullptr);
ThingProperty lampOn("on", "Whether the lamp is turned on", BOOLEAN);
ThingProperty lampLevel("level", "The level of light from 0-100", NUMBER);

void setup() {
  pinMode(lampPin, OUTPUT);
  digitalWrite(lampPin, HIGH);
  analogWriteRange(255);

  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());

  lamp.addProperty(&lampOn);
  lamp.addProperty(&lampLevel);
  adapter.addDevice(&lamp);
  adapter.begin();
  Serial.println("HTTP server started");
}

void loop() {
  adapter.update();
  if (lampOn.getValue().boolean) {
    int level = map(lampLevel.getValue().number, 0, 100, 255, 0);
    analogWrite(lampPin, level);
  } else {
    analogWrite(lampPin, 255);
  }
}

Here is the On/Off example that did work;

#include <Arduino.h>
#include "Thing.h"
#include "WebThingAdapter.h"

// TODO: Hardcode your wifi credentials here (and keep it private)
const char *ssid = "*****";
const char *password = "*****";

#if defined(LED_BUILTIN)
const int ledPin = LED_BUILTIN;
#else
const int ledPin = 16; // manually configure LED pin
#endif

WebThingAdapter *adapter;

const char *ledTypes[] = {"OnOffSwitch", "Light", nullptr};
ThingDevice led("led", "Built-in LED", ledTypes);
ThingProperty ledOn("on", "", BOOLEAN, "OnOffProperty");

bool lastOn = false;

void setup(void) {
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  Serial.begin(115200);
  Serial.println("");
  Serial.print("Connecting to \"");
  Serial.print(ssid);
  Serial.println("\"");
#if defined(ESP8266) || defined(ESP32)
  WiFi.mode(WIFI_STA);
#endif
  WiFi.begin(ssid, password);
  Serial.println("");

  // Wait for connection
  bool blink = true;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    digitalWrite(ledPin, blink ? LOW : HIGH); // active low led
    blink = !blink;
  }
  digitalWrite(ledPin, HIGH); // active low led

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  adapter = new WebThingAdapter("w25", WiFi.localIP());

  led.addProperty(&ledOn);
  adapter->addDevice(&led);
  adapter->begin();
  Serial.println("HTTP server started");
  Serial.print("http://");
  Serial.print(WiFi.localIP());
  Serial.print("/things/");
  Serial.println(led.id);
}

void loop(void) {
  adapter->update();
  bool on = ledOn.getValue().boolean;
  digitalWrite(ledPin, on ? LOW : HIGH); // active low led
  if (on != lastOn) {
    Serial.print(led.id);
    Serial.print(": ");
    Serial.println(on);
  }
  lastOn = on;
}

Can someone please offer some advice. I don't know why the webthing-arduino LEDLamp didn't work?

Hi Guy's,

Here I've tried the example sketch again from the Arduino webthing-arduino-LEDLamp. Changed the led pin to 16 on the nodemcu and input my ssid and password.
Why does this not work?

I'd be really gratefull for some help with this.

Hi Guy's,

Has no one got an inkling to why the LED On/OFF will work, but not the LED Lamp with brightness.

I've also bought so D1 mini's and they have the same problem.

Can someone please offer some assisstance, it's quite frustrating.

https://microcontrollerslab.com/esp8266-nodemcu-pwm-slider-web-server-control-led-brightness/#google_vignette)
I got this one working, but only after --

  1. referring to the pin ["led_pin"] used by its "Dx" name (not the GPIO),
  2. and using only the specified PWM pins.

The author states, "In ESP8266, PWM is supported through all input output GPIO pins. So choose any GPIO pin (GPIO0-GPIO16) to connect with the anode pin of the LED." Such, though, was not my experience. I tried using the on-board LEDs (D0/16, D4/2) and No Joy.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.