-
Notifications
You must be signed in to change notification settings - Fork 0
/
7segMQTT.ino
234 lines (193 loc) · 5.42 KB
/
7segMQTT.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
Based on "Basic MQTT example"
*/
//#include <avr/wdt.h> //watch do timer
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h> //MQTT
#define countdown 0
#define score 0
#define high_score 1
int top_score=0;
//GPIO declarations
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
byte segmentClock = 6;
byte segmentLatch = 5;
byte segmentData = 7;
int number = 0;
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
// Update these with values suitable for your network.
#if (countdown==1)
#define MAC_ADDRESS 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0x01
#define IPADDRESS 192, 168, 1, 80
#define CLIENT "countdown"
#endif
#if (score==1)
#define MAC_ADDRESS 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0x02
#define IPADDRESS 192, 168, 1, 81
#define CLIENT "score"
#endif
#if (high_score==1)
#define MAC_ADDRESS 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0x03
#define IPADDRESS 192, 168, 1, 82
#define CLIENT "high_score"
#endif
byte mac[] = {MAC_ADDRESS};
IPAddress ip(IPADDRESS);
IPAddress server(192, 168, 1, 239);
void callback(char* topic, byte* payload, unsigned int length) {
if ((String)topic == "start" && countdown==1)count_down();
if ((String)topic == "score" && score==1)score_point();
if ((String)topic == "start" && score==1){
number=0;
showNumber(number);
}
if ((String)topic == "score" && high_score==1)number++;
if ((String)topic == "stop" && high_score==1){
if (number>top_score)top_score=number;
number=0;
showNumber(top_score);
}
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(CLIENT)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic","hello world");
// ... and resubscribe
client.subscribe("start");
client.subscribe("score");
client.subscribe("stop");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup()
{
//wdt_enable(WDTO_8S);
Serial.begin(9600);
client.setServer(server, 1883);
client.setCallback(callback);
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
pinMode(segmentClock, OUTPUT);
pinMode(segmentData, OUTPUT);
pinMode(segmentLatch, OUTPUT);
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, LOW);
digitalWrite(segmentLatch, LOW);
showNumber(number);
}
int swap(int digit)
{
int first_digit = digit/10;
first_digit = floor(first_digit);
int second_digit = digit%10;
int swapped_number = (second_digit*10)+first_digit;
return swapped_number;
}
void count_down()
{
number = 31;
showNumber(number-1); //Test pattern
while (number!=0)
{
delay(980);//1 second - processing
showNumber(number-1);
if (number==18)showNumber(number-1);//to fix bizzar glich in displaying the number 17 as 87
number--;
Serial.println(number); //For debugging
}
//Reset x after 99
}
void score_point()
{
number++;
showNumber(number);
delay(150);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
client.loop();
//wdt_reset();
}
void showNumber(float value)
{
int number = abs(value); //Remove negative signs and any decimals
number = swap(number);
//Serial.print("number: ");
//Serial.println(number);
for (byte x = 0 ; x < 2 ; x++)
{
int remainder = number % 10;
postNumber(remainder, false);
number /= 10;
}
//Latch the current segment data
digitalWrite(segmentLatch, LOW);
digitalWrite(segmentLatch, HIGH); //Register moves storage register on the rising edge of RCK
}
//Given a number, or '-', shifts it out to the display
void postNumber(byte number, boolean decimal)
{
// - A
// / / F/B
// - G
// / / E/C
// -. D/DP
#define a 1<<0
#define b 1<<6
#define c 1<<5
#define d 1<<4
#define e 1<<3
#define f 1<<1
#define g 1<<2
#define dp 1<<7
byte segments;
switch (number)
{
case 1: segments = b | c; break;
case 2: segments = a | b | d | e | g; break;
case 3: segments = a | b | c | d | g; break;
case 4: segments = f | g | b | c; break;
case 5: segments = a | f | g | c | d; break;
case 6: segments = a | f | g | e | c | d; break;
case 7: segments = a | b | c; break;
case 8: segments = a | b | c | d | e | f | g; break;
case 9: segments = a | b | c | d | f | g; break;
case 0: segments = a | b | c | d | e | f; break;
case ' ': segments = 0; break;
case 'c': segments = g | e | d; break;
case '-': segments = g; break;
}
if (decimal) segments |= dp;
//Clock these bits out to the drivers
for (byte x = 0 ; x < 8 ; x++)
{
digitalWrite(segmentClock, LOW);
digitalWrite(segmentData, segments & 1 << (7 - x));
digitalWrite(segmentClock, HIGH); //Data transfers to the register on the rising edge of SRCK
}
}