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?