Compare commits

..

No commits in common. "25b5891f98f89e9920c3845ab04158d013421828" and "fa888bcfab7defc6918697d22cc072efa65889f0" have entirely different histories.

9 changed files with 11 additions and 131 deletions

1
.gitignore vendored
View File

@ -3,4 +3,3 @@
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
include/config.h

View File

@ -22,16 +22,4 @@
#define MAX_MILLIAMPS 1000
#define WIFI_SSID "..."
#define WIFI_PASS "..."
#define MQTT_CLIENT_ID "espleaf"
#define MQTT_USER "..."
#define MQTT_PASS "..."
#define MQTT_SERVER "..."
#define MQTT_SERVER_PORT 1883
#define MQTT_TOPIC_STATE "espleaf/state"
#define MQTT_TOPIC_STATE_LONG "espleaf/state_long"
#define MQTT_TOPIC_COMMANDS "esplead/cmnd"
//#define TEST_MODE

View File

@ -1,10 +0,0 @@
#pragma once
#include "wifi.h"
#include "config.h"
#include <PubSubClient.h>
static PubSubClient mqtt(wifi);
void mqtt_setup();
void mqtt_loop();

View File

@ -21,8 +21,3 @@ enum AnimationMode {
AM_FIRST_NODE,
AM_FLASH
};
extern AnimationMode mode;
extern AnimationMode temp_mode;
extern unsigned long temp_mode_until;

View File

@ -1,8 +0,0 @@
#pragma once
#include <ESP8266WiFi.h>
#include "config.h"
static WiFiClient wifi;
void wifi_setup();

View File

@ -17,5 +17,4 @@ upload_speed = 921600
monitor_port = /dev/cu.wchusbserial*
monitor_speed = 74880
monitor_filters = default, time, send_on_enter, esp8266_exception_decoder
lib_deps = fastled/FastLED @ 3.4.0
PubSubClient
lib_deps = FastLED

View File

@ -7,7 +7,6 @@
#include "edge.h"
#include "corner.h"
#include "prototypes.h"
#include "mqtt.h"
std::vector<Node*> nodes;
std::list<Edge*> edges;
@ -16,8 +15,6 @@ std::vector<Corner*> corners;
CRGB leds[LED_COUNT];
AnimationMode mode = AM_CORNERS;
AnimationMode temp_mode;
unsigned long temp_mode_until;
unsigned long last_loop = 0;
@ -79,7 +76,7 @@ void setup_fastled() {
set_all_leds(CRGB::Black);
}
void setup_rng() {
void setup_wifi() {
LOGln("Starting WiFi scan for RNG initialization...");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
@ -100,15 +97,11 @@ void setup() {
setup_layout();
setup_fastled();
setup_wifi();
#ifdef TEST_MODE
LOGln("TEST_MODE is active!");
#else
setup_rng();
wifi_setup();
mqtt_setup();
#endif
}
void loop() {
@ -126,23 +119,13 @@ void loop() {
}
#else
// Normal mode
mqtt_loop();
EVERY_N_MILLISECONDS(20 / SPEEDUP) {
looping = false;
AnimationMode active_mode = mode;
if (temp_mode_until > 0) {
if (temp_mode_until>millis()) {
active_mode = temp_mode;
} else {
temp_mode_until = 0;
}
}
if (active_mode == AM_CORNERS || active_mode == AM_FIRST_CORNER) {
if (mode == AM_CORNERS || mode == AM_FIRST_CORNER) {
for(Corner* corner: corners) {
corner->step();
if (active_mode == AM_FIRST_CORNER) {
if (mode == AM_FIRST_CORNER) {
corner->infect(512, 512);
} else {
corner->infect(300, 600);
@ -152,13 +135,13 @@ void loop() {
}
if (random8(128)==0) {
if (active_mode == AM_FIRST_CORNER) {
if (mode == AM_FIRST_CORNER) {
corners[0]->blend_to(CHSV(random8(), 255, 255));
} else {
corners[random16(corners.size())]->blend_to(CHSV(random8(), 255, 255));
}
}
} else if (active_mode == AM_NODES || active_mode == AM_FIRST_NODE) {
} else if (mode == AM_NODES || mode == AM_FIRST_NODE) {
for(Node* node : nodes) {
node->step();
node->infect(512);
@ -166,13 +149,13 @@ void loop() {
}
if (random8(128)==0) {
if (active_mode == AM_FIRST_NODE) {
if (mode == AM_FIRST_NODE) {
nodes[0]->blend_to(CHSV(random8(), 255, 255));
} else {
nodes[random8(nodes.size())]->blend_to(CHSV(random8(), 255, 255));
}
}
} else if (active_mode == AM_FLASH) {
} else if (mode == AM_FLASH) {
for (Node* node : nodes) {
node->step();
node->infect(512);

View File

@ -1,53 +0,0 @@
#include "mqtt.h"
#include "tools.h"
#include "prototypes.h"
void connect() {
LOGln("Connecting to MQTT broker...");
if (mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS, MQTT_TOPIC_STATE, 0, true, "OFFLINE")) {
LOGln("Connected.");
mqtt.publish(MQTT_TOPIC_STATE, "ONLINE", true);
char buffer[40];
snprintf(buffer, 40, "ONLINE %s", wifi.localIP().toString().c_str());
mqtt.publish(MQTT_TOPIC_STATE_LONG, buffer, true);
mqtt.subscribe(MQTT_TOPIC_COMMANDS);
} else {
LOGln("Connection failed. Reason: %d", mqtt.state());
delay(1000);
}
}
void callback(char* topic, byte* pl, unsigned int length) {
pl[length] = 0;
String payload((char*)pl);
uint16_t duration = 0;
int cp = payload.indexOf(",");
if (cp != -1) {
duration = payload.substring(cp+1).toInt();
payload = payload.substring(0, cp);
}
AnimationMode am;
if (payload.equals("corners")) am = AM_CORNERS;
else if (payload.equals("nodes")) am = AM_NODES;
if (duration > 0) {
temp_mode = am;
temp_mode_until = millis() + duration*1000;
} else {
mode = am;
}
}
void mqtt_setup() {
mqtt.setServer(MQTT_SERVER, MQTT_SERVER_PORT);
mqtt.setCallback(callback);
mqtt.setSocketTimeout(1);
connect();
}
void mqtt_loop() {
if (!mqtt.connected()) {
connect();
}
mqtt.loop();
}

View File

@ -1,13 +0,0 @@
#include "wifi.h"
#include "tools.h"
void wifi_setup() {
LOG("Connecting to WiFi %s...", WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
while (WiFi.status() != WL_CONNECTED) {
LOG(".");
delay(300);
}
LOGln(" Connected as %s", WiFi.localIP().toString().c_str());
}