Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcrisci committed Aug 30, 2024
0 parents commit f305c72
Show file tree
Hide file tree
Showing 13 changed files with 740 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.pio
.vscode
include/config.h
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# TileController

## Config

Please add a ``config.h`` file with the following content:

````c++
#define WLAN_SSID "<Your-Wifi-SSID>"
#define WLAN_PASS "<Your-Wifi-PASSWORD>"

#define MQTT_BROKER "<IP-or-Domain-of-your-MQTT-Broker>"
#define MQTT_PORT <Port-of-your-MQTT-Broker>

#define NANOLEAF_BASE_URL "<Nanoleaf-Base-Url>"
#define NANOLEAF_AUTH_TOKEN "<Nanoleaf-Auth-Token>"

#define FRIEND_ID "<Friend-UUID>"
#define DEVICE_ID "<Device-UUID>"
````
25 changes: 25 additions & 0 deletions include/ColorPaletteAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef COLORPALETTEADAPTER_H
#define COLORPALETTEADAPTER_H

#include "TopicAdapter.h"
#include "NanoleafApiWrapper.h"

class ColorPaletteAdapter final : public TopicAdapter {
public:
explicit ColorPaletteAdapter(NanoleafApiWrapper &nanoleaf): nanoleaf(nanoleaf), topic("color") {
}

[[nodiscard]] const char *getTopic() const override {
return topic;
}

void callback(char *topic, const JsonObject &payload, unsigned int length) override {
nanoleaf.setStaticColors(payload);
}

private:
NanoleafApiWrapper &nanoleaf;
const char *topic;
};

#endif
37 changes: 37 additions & 0 deletions include/MQTTClient.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef MQTTCLIENT_H
#define MQTTCLIENT_H

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <vector>
#include <ArduinoJson.h>
#include "TopicAdapter.h"

class MQTTClient {
public:
explicit MQTTClient(WiFiClient &wifiClient);

void setup(const char *mqttBroker, int mqttPort, const char *friendId, const char *deviceId);

void loop();

void publish(const char *topic, const JsonDocument &jsonPayload);

void addTopicAdapter(TopicAdapter *adapter);

private:
void reconnect();

char *buildTopic(const TopicAdapter *adapter) const;

void callback(char *topic, const byte *payload, unsigned int length) const;

static bool matches(const char *subscribedTopic, const char *receivedTopic);

PubSubClient client;
const char *friendId{};
const char *deviceId{};
static std::vector<TopicAdapter *> topicAdapters;
};

#endif
41 changes: 41 additions & 0 deletions include/NanoleafApiWrapper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef NanoleafApiWrapper_h
#define NanoleafApiWrapper_h

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoJson.h>
#include <vector>

class NanoleafApiWrapper {
public:
explicit NanoleafApiWrapper(const WiFiClient &wifiClient);

void setup(const char *nanoleafBaseUrl, const char *nanoleafAuthToken);

bool isConnected();

String generateToken();

bool identify();

std::vector<String> getPanelIds();

bool setPower(const bool &state);

bool setStaticColors(const JsonObject &doc);

private:
bool sendRequest(
const String &method,
const String &endpoint,
const JsonDocument *requestBody,
JsonDocument *responseBody,
bool useAuthToken
);

String nanoleafBaseUrl;
String nanoleafAuthToken;
WiFiClient client;
};

#endif
39 changes: 39 additions & 0 deletions include/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

This directory is intended for project header files.

A header file is a file containing C declarations and macro definitions
to be shared between several project source files. You request the use of a
header file in your project source file (C, C++, etc) located in `src` folder
by including it, with the C preprocessing directive `#include'.

```src/main.c

#include "header.h"

int main (void)
{
...
}
```

Including a header file produces the same results as copying the header file
into each source file that needs it. Such copying would be time-consuming
and error-prone. With a header file, the related declarations appear
in only one place. If they need to be changed, they can be changed in one
place, and programs that include the header file will automatically use the
new version when next recompiled. The header file eliminates the labor of
finding and changing all the copies as well as the risk that a failure to
find one copy will result in inconsistencies within a program.

In C, the usual convention is to give header files names that end with `.h'.
It is most portable to use only letters, digits, dashes, and underscores in
header file names, and at most one dot.

Read more about using header files in official GCC documentation:

* Include Syntax
* Include Operation
* Once-Only Headers
* Computed Includes

https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html
15 changes: 15 additions & 0 deletions include/TopicAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef TOPICADAPTER_H
#define TOPICADAPTER_H

#include <ArduinoJson.h>

class TopicAdapter {
public:
virtual ~TopicAdapter() = default;

[[nodiscard]] virtual const char *getTopic() const = 0;

virtual void callback(char *topic, const JsonObject &payload, unsigned int length) = 0;
};

#endif
46 changes: 46 additions & 0 deletions lib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@

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

The source code of each library should be placed in an own separate directory
("lib/your_library_name/[here are source files]").

For example, see a structure of the following two libraries `Foo` and `Bar`:

|--lib
| |
| |--Bar
| | |--docs
| | |--examples
| | |--src
| | |- Bar.c
| | |- Bar.h
| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html
| |
| |--Foo
| | |- Foo.c
| | |- Foo.h
| |
| |- README --> THIS FILE
|
|- platformio.ini
|--src
|- main.c

and a contents of `src/main.c`:
```
#include <Foo.h>
#include <Bar.h>

int main (void)
{
...
}

```

PlatformIO Library Dependency Finder will find automatically dependent
libraries scanning project source files.

More information about PlatformIO Library Dependency Finder
- https://docs.platformio.org/page/librarymanager/ldf.html
10 changes: 10 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[env:d1_mini]
platform = espressif8266
board = d1_mini
framework = arduino
monitor_speed = 115200
lib_deps =
knolleary/PubSubClient @ ^2.8
bblanchon/ArduinoJson @ ^7.0.4
tzapu/WiFiManager @ ^0.16.0
robtillaart/UUID @ ^0.1.6
Loading

0 comments on commit f305c72

Please sign in to comment.