forked from tobyrobb/DMD_MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_DMD_display.ino
178 lines (117 loc) · 4.44 KB
/
mqtt_DMD_display.ino
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
08 jun 2014
some code lifted from the web
Toby Robb
This is a test sketch for the Arduino running the DMD led display
through the web and ethernet.
Write to the display via an MQTT server.
Publish message to the topic "DMD"
the dmd display connects to D9,10,11,12,13 and gnd
as per its subboard
there is also the ability to send commands to the UNO
NOTES:
Install yourself an MQTT server such as mosquitto for linux or pc
Plug in your Arduino DMD and ethernet combo
Publish messages on your MQTT server to the DMD topic
*/
// Includes
#include <Ethernet.h>
#include <PubSubClient.h>
#include <SPI.h> //SPI.h must be included as DMD is written by SPI (the IDE complains otherwise)
#include <DMD.h> // The Freetronics driver library
#include <TimerOne.h> //
#include "SystemFont5x7.h"
// Defines
#define mqttTopic "DMD" // choose the MQTT topic the DMD will listen to for messages
//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBE, 0xAF, 0xFE, 0x02 };
byte server[] = { 192,168,2,10 }; // This the address
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
char message_buff[100];
String message;
void setup(){
// Setup the serial
Serial.begin(9600); //Just for debugging
Serial.println("Beginning Setup"); //Just for debugging
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// try to congifure using IP address instead of DHCP:
}
if (client.connect("arduinoClient")) {
client.subscribe(mqttTopic);
}
Serial.print("server is at "); //Just for debugging
Serial.println(Ethernet.localIP());//Just for debugging
// setup the DMD required code
//initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 5000 ); //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
//clear/init the DMD pixels held in RAM
dmd.clearScreen( true ); //true is normal (all pixels off), false is negative (all pixels on)
dmd.selectFont(SystemFont5x7);
delay(2000); // Wait 2 seconds
message = "Waiting for message..."; //The first message
showMessage(); // Shows the message string on the DMD
delay(2000); // Wait 2 seconds
dmd.clearScreen( true );
}
void loop(){
client.loop(); //Waits for messages then calls callback function when one arrives
Serial.println(message); //Just for debugging
showMessage(); // Shows the message string on the DMD
delay(1000); //Wait for one second
dmd.clearScreen( true );
}
// all the functions begin here
void callback(char* topic, byte* payload, unsigned int length) {
int i = 0;
for(i=0; i<length; i++) {
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
Serial.println(msgString);
message = msgString;
checkCommand(msgString); // lets check if there was a command issued via message
}
void ScanDMD()
{
dmd.scanDisplayBySPI();
}
void showMessage(){
char marqueeString[256];
int myTextLength = message.length();
message.toCharArray(marqueeString, myTextLength+1);
dmd.drawMarquee(marqueeString,myTextLength,(32*DISPLAYS_ACROSS)-1,5);
long start=millis();
long timer=start;
boolean ret=false;
while(!ret){
if ((timer+30) < millis()) {
ret=dmd.stepMarquee(-1,0);
timer=millis();
}
}
}
void checkCommand(String inString){
// some examples of commands you could run
if (inString.equals("freetronics")){
Serial.println("www.freetronics.com for all your Adruino needs !"); //Just for debugging
message = "www.freetronics.com for all your Adruino needs !";
}
if (inString.equals("arduino")){
Serial.println("Programmed using Arduino V1.5 and a Freetronics DMD LED Display"); //Just for debugging
message = "Programmed using Arduino V1.5 and a Freetronics DMD LED Display";
}
if (inString.equals("relayOn")){
Serial.println("Turning on the relay"); //Just for debugging
message = "Turning on the relay";
//digitalWrite(relayPin, HIGH);
}
}