Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

listen to the space state #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ monitor_speed = 115200
upload_speed = 1000000
lib_deps =
tzapu/WifiManager

knolleary/PubSubClient
31 changes: 30 additions & 1 deletion scarlettscroller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include <Arduino.h>
#include <WiFiUdp.h>

#include <PubSubClient.h>

#include <ArduinoOTA.h>

#define print Serial.printf
Expand All @@ -35,6 +37,18 @@ static int scrolling = 0;
static char scroll_buf[1501];
static int scroll_pos = 0;

static WiFiClient wifiClient;
static PubSubClient mclient(wifiClient);

static bool enabled = false;

static void mcallback(char *topic, byte *payload, unsigned int length)
{
enabled = payload[0] == '1';
if (!enabled)
draw_clear();
}

static int do_pix(int argc, char *argv[])
{
if (argc < 4) {
Expand Down Expand Up @@ -230,6 +244,9 @@ void setup(void)
ArduinoOTA.onEnd([](){draw_clear();});
ArduinoOTA.begin();

mclient.setServer("mqtt.vm.nurd.space", 1883);
mclient.setCallback(mcallback);

led_enable();
}

Expand All @@ -240,8 +257,20 @@ void loop(void)

unsigned long ms = millis();

mclient.loop();
if (!mclient.connected()) {
Serial.println(F("Attempting MQTT connection... "));

if (mclient.connect("johansson")) {
mclient.subscribe("space/statedigit");
Serial.println(F("Connected to MQTT server"));
}
}

// handle currently scrolling text with priority
if (scrolling > 0) {
if (enabled == false) {
}
else if (scrolling > 0) {
int scroll_tick = millis() / 20;
if (scroll_tick != scroll_tick_last) {
scroll_tick_last = scroll_tick;
Expand Down