Skip to content

Commit

Permalink
Added v2 usermods API
Browse files Browse the repository at this point in the history
  • Loading branch information
Aircoookie committed May 28, 2020
1 parent a4e0931 commit 696e438
Show file tree
Hide file tree
Showing 18 changed files with 443 additions and 38 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

### Development versions after 0.10.0 release

#### Build 2005280

- Added v2 usermod API
- Added v2 example usermod `usermod_v2_example` in the usermods folder as prelimary documentation
- Added DS18B20 Temperature usermod with Info page support
- Disabled MQTT on ESP01 build to make room in flash

#### Build 2005230

- Fixed TPM2
Expand Down
26 changes: 14 additions & 12 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ arduino_core_develop = https://github.com/platformio/platform-espressif8266#deve
arduino_core_git = https://github.com/platformio/platform-espressif8266#feature/stage

# Platform to use for ESP8266
platform_wled_default = ${common.arduino_core_2_7_1}
# We use 2.7.0+ on analog boards because of PWM flicker fix
platform_latest = ${common.arduino_core_2_7_1}

# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -180,51 +182,51 @@ lib_ignore =

[env:nodemcuv2]
board = nodemcuv2
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}

[env:esp01]
board = esp01
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_512k}
build_flags = ${common.build_flags_esp8266} -D WLED_DISABLE_OTA -D WLED_DISABLE_ALEXA -D WLED_DISABLE_BLYNK
-D WLED_DISABLE_CRONIXIE -D WLED_DISABLE_HUESYNC -D WLED_DISABLE_INFRARED
-D WLED_DISABLE_CRONIXIE -D WLED_DISABLE_HUESYNC -D WLED_DISABLE_INFRARED -D WLED_DISABLE_MQTT

[env:esp01_1m_ota]
board = esp01_1m
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_1m0m}
build_flags = ${common.build_flags_esp8266} -D WLED_DISABLE_ALEXA -D WLED_DISABLE_BLYNK -D WLED_DISABLE_CRONIXIE -D WLED_DISABLE_HUESYNC -D WLED_DISABLE_INFRARED
build_flags = ${common.build_flags_esp8266} -D WLED_DISABLE_ALEXA -D WLED_DISABLE_BLYNK -D WLED_DISABLE_CRONIXIE -D WLED_DISABLE_HUESYNC -D WLED_DISABLE_INFRARED -D WLED_DISABLE_MQTT

[env:esp01_1m_full]
board = esp01_1m
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_1m0m}
build_flags = ${common.build_flags_esp8266} -D WLED_DISABLE_OTA

[env:esp07]
board = esp07
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}

[env:d1_mini]
board = d1_mini
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
upload_speed = 921600
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}

[env:heltec_wifi_kit_8]
board = d1_mini
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}

[env:h803wf]
board = d1_mini
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266} -D LEDPIN=1 -D WLED_DISABLE_INFRARED

Expand Down Expand Up @@ -267,7 +269,7 @@ build_flags = ${common.build_flags_esp8266} -D WLED_USE_ANALOG_LEDS -D WLED_USE_
[env:d1_mini_debug]
board = d1_mini
build_type = debug
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266} ${common.debug_flags}

Expand All @@ -276,7 +278,7 @@ board = d1_mini
upload_protocol = espota
# exchange for your WLED IP
upload_port = "10.10.1.27"
platform = ${common.platform_latest}
platform = ${common.platform_wled_default}
board_build.ldscript = ${common.ldscript_4m1m}
build_flags = ${common.build_flags_esp8266}

Expand Down
10 changes: 10 additions & 0 deletions usermods/EXAMPLE_v2/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Usermods API v2 example usermod

In this usermod file you can find the documentation on how to take advantage of the new version 2 usermods!

## Installation

Copy `usermod_v2_example.h` to the wled00 directory.
Uncomment the corresponding lines in `usermods_list.h` and compile!
_(You shouldn't need to actually install this, it does nothing useful)_

119 changes: 119 additions & 0 deletions usermods/EXAMPLE_v2/usermod_v2_example.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#pragma once

#include "wled.h"

/*
* Usermods allow you to add own functionality to WLED more easily
* See: https://github.com/Aircoookie/WLED/wiki/Add-own-functionality
*
* This is an example for a v2 usermod.
* v2 usermods are class inheritance based and can (but don't have to) implement more functions, each of them is shown in this example.
* Multiple v2 usermods can be added to one compilation easily.
*
* Creating a usermod:
* This file serves as an example. If you want to create a usermod, it is recommended to use usermod_v2_empty.h from the usermods folder as a template.
* Please remember to rename the class and file to a descriptive name.
* You may also use multiple .h and .cpp files.
*
* Using a usermod:
* 1. Copy the usermod into the sketch folder (same folder as wled00.ino)
* 2. Register the usermod by adding #include "usermod_filename.h" in the top and registerUsermod(new MyUsermodClass()) in the bottom of usermods_list.cpp
*/

//class name. Use something descriptive and leave the ": public Usermod" part :)
class MyExampleUsermod : public Usermod {
private:
//Private class members. You can declare variables and functions only accessible to your usermod here
unsigned long lastTime = 0;
public:
//Functions called by WLED

/*
* setup() is called once at boot. WiFi is not yet connected at this point.
* You can use it to initialize variables, sensors or similar.
*/
void setup() {
//Serial.println("Hello from my usermod!");
}


/*
* connected() is called every time the WiFi is (re)connected
* Use it to initialize network interfaces
*/
void connected() {
//Serial.println("Connected to WiFi!");
}


/*
* loop() is called continuously. Here you can check for events, read sensors, etc.
*
* Tips:
* 1. You can use "if (WLED_CONNECTED)" to check for a successful network connection.
* Additionally, "if (WLED_MQTT_CONNECTED)" is available to check for a connection to an MQTT broker.
*
* 2. Try to avoid using the delay() function. NEVER use delays longer than 10 milliseconds.
* Instead, use a timer check as shown here.
*/
void loop() {
if (millis() - lastTime > 1000) {
//Serial.println("I'm alive!");
lastTime = millis();
}
}


/*
* addToJsonInfo() can be used to add custom entries to the /json/info part of the JSON API.
* Creating an "u" object allows you to add custom key/value pairs to the Info section of the WLED web UI.
* Below it is shown how this could be used for e.g. a light sensor
*/
/*
void addToJsonInfo(JsonObject& root)
{
int reading = 20;
//this code adds "u":{"Light":[20," lux"]} to the info object
JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u");
JsonArray lightArr = user.createNestedArray("Light"); //name
lightArr.add(reading); //value
lightArr.add(" lux"); //unit
}
*/


/*
* addToJsonState() can be used to add custom entries to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients
*/
void addToJsonState(JsonObject& root)
{
//root["user0"] = userVar0;
}


/*
* readFromJsonState() can be used to receive data clients send to the /json/state part of the JSON API (state object).
* Values in the state object may be modified by connected clients
*/
void readFromJsonState(JsonObject& root)
{
userVar0 = root["user0"] | userVar0; //if "user0" key exists in JSON, update, else keep old value
//if (root["bri"] == 255) Serial.println(F("Don't burn down your garage!"));
}


/*
* getId() allows you to optionally give your V2 usermod an unique ID (please define it in const.h!).
* This could be used in the future for the system to determine whether your usermod is installed.
*/
uint16_t getId()
{
return USERMOD_ID_EXAMPLE;
}

//More methods can be added in the future, this example will then be extended.
//Your usermod will remain compatible as it does not need to implement all methods from the Usermod base class!
};
40 changes: 40 additions & 0 deletions usermods/Temperature/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Temperature usermod

Based on the excellent `QuinLED_Dig_Uno_Temp_MQTT` by srg74!
This usermod will read from an attached DS18B20 temperature sensor (as available on the QuinLED Dig-Uno)
The temperature is displayed both in the Info section of the web UI as well as published to the `/temperature` MQTT topic if enabled.
This usermod will be expanded with support for different sensor types in the future.

## Installation

Copy `usermod_temperature.h` to the wled00 directory.
Uncomment the corresponding lines in `usermods_list.h` and compile!
If this is the only v2 usermod you plan to use, you can alternatively replace `usermods_list.h` in wled00 with the one in this folder.

## Project link

* [QuinLED-Dig-Uno](https://quinled.info/2018/09/15/quinled-dig-uno/) - Project link

### PlatformIO requirements

You might have to uncomment `DallasTemperature@~3.8.0`,`OneWire@~2.3.5 under` `[common]` section in `platformio.ini`:

```ini
# platformio.ini
...
[platformio]
...
; default_envs = esp07
default_envs = d1_mini
...
[common]
...
lib_deps_external =
...
#For use SSD1306 OLED display uncomment following
U8g2@~2.27.3
#For Dallas sensor uncomment following 2 lines
DallasTemperature@~3.8.0
OneWire@~2.3.5
...
```
79 changes: 79 additions & 0 deletions usermods/Temperature/usermod_temperature.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include "wled.h"

#include <DallasTemperature.h> //DS18B20

//Pin defaults for QuinLed Dig-Uno
#ifdef ARDUINO_ARCH_ESP32
#define TEMPERATURE_PIN 18
#else //ESP8266 boards
#define TEMPERATURE_PIN 14
#endif

#define TEMP_CELSIUS // Comment out for Fahrenheit

#define MEASUREMENT_INTERVAL 60000 //1 Minute

OneWire oneWire(TEMPERATURE_PIN);
DallasTemperature sensor(&oneWire);

class UsermodTemperature : public Usermod {
private:
//set last reading as "40 sec before boot", so first reading is taken after 20 sec
unsigned long lastMeasurement = UINT32_MAX - 40000;
float temperature = 0.0f;
public:
void getReading() {
sensor.requestTemperatures();
#ifdef TEMP_CELSIUS
temperature = sensor.getTempCByIndex(0);
#else
temperature = sensor.getTempFByIndex(0);
#endif
}

void setup() {
sensor.begin();
sensor.setResolution(9);
}

void loop() {
if (millis() - lastMeasurement > MEASUREMENT_INTERVAL)
{
getReading();

if (WLED_MQTT_CONNECTED) {
char subuf[38];
strcpy(subuf, mqttDeviceTopic);
strcat(subuf, "/temperature");
mqtt->publish(subuf, 0, true, String(temperature).c_str());
}
lastMeasurement = millis();
}
}

void addToJsonInfo(JsonObject& root) {
JsonObject user = root["u"];
if (user.isNull()) user = root.createNestedObject("u");

JsonArray temp = user.createNestedArray("Temperature");
if (temperature == DEVICE_DISCONNECTED_C) {
temp.add(0);
temp.add(" Sensor Error!");
return;
}

temp.add(temperature);
#ifdef TEMP_CELSIUS
temp.add("°C");
#else
temp.add("°F");
#endif
}

uint16_t getId()
{
return USERMOD_ID_TEMPERATURE;
}
};
25 changes: 25 additions & 0 deletions usermods/Temperature/usermods_list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "wled.h"
/*
* Register your v2 usermods here!
*/

/*
* Add/uncomment your usermod filename here (and once more below)
* || || ||
* \/ \/ \/
*/
//#include "usermod_v2_example.h"
#include "usermod_temperature.h"
//#include "usermod_v2_empty.h"

void registerUsermods()
{
/*
* Add your usermod class name here
* || || ||
* \/ \/ \/
*/
//usermods.add(new MyExampleUsermod());
usermods.add(new UsermodTemperature());
//usermods.add(new UsermodRenameMe());
}
Loading

0 comments on commit 696e438

Please sign in to comment.