Skip to content

Commit

Permalink
Fix build and add fans
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter committed Jan 24, 2018
1 parent 64159dd commit 3d9c446
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 6 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ script:
- platformio run -e livingroom
- platformio run -e dht-dallas-sensors
- platformio run -e switch-binarysensor
- platformio run -e fan-example


#
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ and the GPIO pin like this:
app.make_simple_gpio_binary_sensor("Cabinet Motion", binary_sensor::device_class::MOTION, 36);
```

#### Fan

Fans can be created by first calling `app.make_fan("Friendly Name")` and then using the return value
to set the output channels. See [`examples/fan-example.cpp`](examples/fan-example.cpp) for an example.

## Current Features

* Powerful core that allows for easy creation of new, custom components
Expand Down Expand Up @@ -266,6 +271,7 @@ app.make_simple_gpio_binary_sensor("Cabinet Motion", binary_sensor::device_class
* Refine default options
* Pulse Counter
* Status LED
* Covers
* **Suggestions?** Feel free to create an issue and tag it with feature-request.

## Advanced Options
Expand Down
38 changes: 38 additions & 0 deletions examples/fan-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//
// Created by Otto Winter on 24.01.18.
//

#include <esphomelib/application.h>

using namespace esphomelib;

Application app;

void setup() {
app.set_name("livingroom-fan");
app.init_log();

app.init_wifi("YOUR_SSID", "YOUR_PASSWORD");
app.init_mqtt("MQTT_HOST", "USERNAME", "PASSWORD");
app.init_ota();

auto binary_fan = app.make_fan("Binary Fan");
binary_fan.output->set_binary(app.make_gpio_binary_output(32));

auto speed_fan = app.make_fan("Speed Fan");
// 0.0 -> off speed
// 0.33 -> low speed
// 0.66 -> medium speed
// 1.0 -> high speed
speed_fan.output->set_speed(app.make_ledc_component(33), 0.0, 0.33, 0.66, 1.0);

auto oscillating_fan = app.make_fan("Oscillating Fan");
oscillating_fan.output->set_binary(app.make_gpio_binary_output(34));
oscillating_fan.output->set_oscillation(app.make_gpio_binary_output(35));

app.setup();
}

void loop() {
app.loop();
}
36 changes: 36 additions & 0 deletions lib/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

This directory is intended for the project specific (private) libraries.
PlatformIO will compile them to static libraries and link to executable file.

The source code of each library should be placed in separate directory, like
"lib/private_lib/[here are source files]".

For example, see how can be organized `Foo` and `Bar` libraries:

|--lib
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |- readme.txt --> THIS FILE
|- platformio.ini
|--src
|- main.c

Then in `src/main.c` you should use:

#include <Foo.h>
#include <Bar.h>

// rest H/C/CPP code

PlatformIO will find your libraries automatically, configure preprocessor's
include paths and build them.

More information about PlatformIO Library Dependency Finder
- http://docs.platformio.org/page/librarymanager/ldf.html
19 changes: 14 additions & 5 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

[platformio]
env_default = livingroom
lib_dir = .
src_dir = examples
src_dir = .
include_dir = src

[common]
lib_deps =
Expand All @@ -26,27 +26,36 @@ build_flags =
-DMQTT_MAX_PACKET_SIZE=512
-DCONFIG_ARDUHAL_LOG_COLORS=1
-DCORE_DEBUG_LEVEL=5
src_filter = +<src>

[env:livingroom]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
src_filter = +<livingroom.cpp>
src_filter = ${common.src_filter} +<examples/livingroom.cpp>

[env:dht-dallas-sensors]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
src_filter = +<dht-dallas-sensors.cpp>
src_filter = ${common.src_filter} +<examples/dht-dallas-sensors.cpp>

[env:switch-binarysensor]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
src_filter = +<switch-binarysensor.cpp>
src_filter = ${common.src_filter} +<examples/switch-binarysensor.cpp>

[env:fan-example]
platform = espressif32
board = nodemcu-32s
framework = arduino
lib_deps = ${common.lib_deps}
build_flags = ${common.build_flags}
src_filter = ${common.src_filter} +<examples/fan-example.cpp>
18 changes: 17 additions & 1 deletion src/esphomelib/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ using namespace esphomelib::binary_sensor;
using namespace esphomelib::sensor;
using namespace esphomelib::output;
using namespace esphomelib::light;
using namespace esphomelib::fan;
using namespace esphomelib::switch_platform;

static const char *TAG = "app";
Expand Down Expand Up @@ -258,7 +259,7 @@ input::DallasComponent *Application::make_dallas_component(uint8_t pin) {

Application::SimpleGPIOSwitchStruct Application::make_simple_gpio_switch(uint8_t pin,
const std::string &friendly_name) {
auto *binary_output = this->register_component(new GPIOBinaryOutputComponent(pin));
auto *binary_output = this->make_gpio_binary_output(pin);
auto *simple_switch = new SimpleSwitch(binary_output);
auto *mqtt = this->make_mqtt_switch_for(friendly_name, simple_switch);

Expand All @@ -272,6 +273,21 @@ const std::string &Application::get_name() const {
return this->name_;
}


Application::FanStruct Application::make_fan(const std::string &friendly_name) {
FanStruct s{};
s.state = new FanState();
s.mqtt = this->register_mqtt_component(new MQTTFanComponent(friendly_name));
s.output = this->register_component(new BasicFanComponent());
s.mqtt->set_state(s.state);
s.output->set_state(s.state);
return s;
}

output::GPIOBinaryOutputComponent *Application::make_gpio_binary_output(uint8_t pin, uint8_t mode) {
return this->register_component(new GPIOBinaryOutputComponent(pin, mode));
}

Application::Application() {
global_application = this;
}
Expand Down
21 changes: 21 additions & 0 deletions src/esphomelib/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include <DallasTemperature.h>
#include <esphomelib/input/dallas_component.h>
#include <esphomelib/switch_platform/simple_switch.h>
#include <src/esphomelib/fan/mqtt_fan_component.h>
#include <src/esphomelib/fan/basic_fan_component.h>
#include <src/esphomelib/output/gpio_binary_output_component.h>
#include "component.h"
#include "esphomelib/mqtt/mqtt_client_component.h"
#include "wifi_component.h"
Expand Down Expand Up @@ -277,6 +280,24 @@ class Application {
switch_platform::MQTTSwitchComponent *make_mqtt_switch_for(const std::string &friendly_name,
switch_platform::Switch *switch_);

// ======================= FAN =======================

struct FanStruct {
fan::BasicFanComponent *output;
fan::FanState *state;
fan::MQTTFanComponent *mqtt;
};

/// Create a GPIO binary output component on the specified pin and pinMode.
output::GPIOBinaryOutputComponent *make_gpio_binary_output(uint8_t pin, uint8_t mode = OUTPUT);

/** Create and connect a Fan with the specified friendly name.
*
* @param friendly_name The friendly name of the Fan to advertise.
* @return A FanStruct, use the output field to set your output channels.
*/
FanStruct make_fan(const std::string &friendly_name);

// ======================= FUNCTIONS =======================

template<class C>
Expand Down

0 comments on commit 3d9c446

Please sign in to comment.