-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt_scroll_cloud.ino
More file actions
145 lines (112 loc) · 3.83 KB
/
Copy pathmqtt_scroll_cloud.ino
File metadata and controls
145 lines (112 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// https://www.smarthome-tricks.de/software-iobroker/esp8266-max7219-mqtt-matrixdisplay-fuer-iobroker/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include "credentials.h"
// mapping of pins:
//DOT Matrix: ESP8266 NodeMCU:
//VCC 5V (VIN when using USB)
//GND GND
//DIN D7 (GPIO13)
//CS D8
//CLK D5 (GPIO14)
#define WAITxSEC (5000)
const byte pinCS = D8; // attach CS to this pin, DIN to MOSI and CLK to SCK (cf http://arduino.cc/en/Reference/SPI)
const byte numberOfHorizontalDisplays = 4; // number of modules horizontally
const byte numberOfVerticalDisplays = 1; // number of modules vertically
Max72xxPanel matrix = Max72xxPanel (pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);
unsigned int scrollwait = 25; // default delay in ms for scroll speed
byte intensity = 1; // default brightness of display (0 up to 15)
const byte spacer = 1; // width of a single space
const byte width = 5 + spacer; // character width
String MatrixText = "..."; // default ticker text
void MQTTCallback (const char topic[], byte* payload, unsigned int length)
{
String PayloadString = "";
for (byte i = 0; i < length; i++)
{
PayloadString = PayloadString + (char) payload[i];
}
Serial.print ("New message arrived in topic: ");
Serial.println (topic);
Serial.print ("Payload = ");
Serial.println (PayloadString);
if (strcmp (topic, mqtt_topic_t) == 0)
{
if (MatrixText != PayloadString)
{
MatrixText = PayloadString;
Serial.println ("Set new text.");
}
}
if (strcmp (topic, mqtt_topic_i) == 0)
{
if (intensity != PayloadString.toInt ())
{
intensity = PayloadString.toInt ();
matrix.setIntensity (intensity);
Serial.println ("Set new intensity.");
}
}
if (strcmp (topic, mqtt_topic_s) == 0)
{
if (scrollwait != PayloadString.toInt ())
{
scrollwait = PayloadString.toInt ();
Serial.println ("Set new scrollwait.");
}
}
}
void setup ()
{
matrix.fillScreen (LOW);
matrix.shutdown (true);
Serial.begin (115200);
Serial.println ("Boot display...");
for (byte matrixIndex = 0 ; matrixIndex < numberOfHorizontalDisplays ; matrixIndex++)
{
matrix.setRotation (matrixIndex, 1); // set rotation for DOT matrix display
}
matrix.setIntensity (intensity); // use a value between 0 and 15 for brightness
// connect to WiFi and MQTT for the first time
reconnect (true);
// register callback
mqttClient.setCallback (MQTTCallback);
}
void printMatrix ()
{
matrix.shutdown (false);
// https://github.com/markruys/arduino-Max72xxPanel/blob/master/examples/Ticker/Ticker.pde
for (int i = 0 ; i < width * MatrixText.length() + matrix.width() - 1 - spacer; i++)
{
matrix.fillScreen (LOW);
int letter = i / width;
int x = (matrix.width () - 1) - i % width;
int y = (matrix.height () - 8) / 2; // center the text vertically
while ((x + width - spacer >= 0) && (letter >= 0))
{
if (letter < MatrixText.length ())
{
matrix.drawChar (x, y, MatrixText[letter], HIGH, LOW, 1);
}
letter--;
x -= width;
}
matrix.write (); // send bitmap to display
delay (scrollwait);
}
}
void loop ()
{
if (!mqttClient.connected ())
{
// make sure we get reconnected when connection to MQTT broker is lost
reconnect (false);
// register callback function
mqttClient.setCallback (MQTTCallback);
}
mqttClient.loop ();
delay (500);
printMatrix ();
}
// end of code file