-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet-control.ino
174 lines (142 loc) · 4.39 KB
/
net-control.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
String ipAddr;
String dnsAddr;
const unsigned maxWifiWaitSeconds = 60;
const int maxMqttRetry = 5;
bool mqttConnected = false;
//const char* mqttXXX = "/" MQTTDEVICEID "/XXX";
// note: delays mainly to keep tft text shortly readable
int setupWifi() {
DEBUG_PRINTLN();
DEBUG_PRINTLN("Connecting to wifi");
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);
tft.setTextFont(2);
tft.setCursor(20, 60);
tft.println("Connecting to WiFi");
unsigned retry_counter = 0;
WiFi.begin(wifi_ssid, wifi_pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
DEBUG_PRINT(".");
tft.print(".");
// display.drawXbm(46, 30, 8, 8, retry_counter % 3 == 0 ? activeSymbole : inactiveSymbole);
// display.drawXbm(60, 30, 8, 8, retry_counter % 3 == 1 ? activeSymbole : inactiveSymbole);
// display.drawXbm(74, 30, 8, 8, retry_counter % 3 == 2 ? activeSymbole : inactiveSymbole);
retry_counter++;
if (retry_counter > maxWifiWaitSeconds) {
DEBUG_PRINTLN(" TIMEOUT!");
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 60);
tft.println("Wifi TIMEOUT");
return 1;
}
}
ipAddr = WiFi.localIP().toString();
dnsAddr = WiFi.dnsIP().toString();
DEBUG_PRINTLN("");
DEBUG_PRINTLN("WiFi connected");
DEBUG_PRINTLN("IP address: ");
DEBUG_PRINTLN(ipAddr);
DEBUG_PRINTLN("DNS address: ");
DEBUG_PRINTLN(dnsAddr);
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 60);
tft.println("Wifi CONNECTED");
tft.print("IP Addr: "); tft.println(ipAddr);
delay(3000);
tft.fillScreen(TFT_BLACK);
return 0;
}
/******************************************************************************
* MQTT
*/
int mqttConnect(bool updateDisplay)
{
DEBUG_PRINT("Attempting MQTT connection...");
String connect_msg = "CONNECTED ";
int rc = 0;
connect_msg += VERSION;
if (updateDisplay) {
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);
tft.setTextFont(2); // 2, 4,
tft.setCursor(20, 60);
tft.println("Connecting to MQTT Server");
delay(1000);
}
// Attempt to connect
//mqttClient.setKeepAlive(0); // "fix" timeouts
if (mqttClient.connect(MQTTDEVICEID,
mqtt_user, mqtt_pass, mqttState, 1, 1, "OFFLINE")) {
DEBUG_PRINTLN("connected");
if (updateDisplay) {
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 60);
tft.println("MQTT CONNECTED");
}
// Once connected, publish an announcement...
mqttClient.publish(mqttState, connect_msg.c_str(), true);
// topic subscriptions ...
mqttClient.subscribe(mqttSetMode);
mqttClient.subscribe(mqttButton);
mqttClient.subscribe(mqttSetConfig);
// mqttClient.subscribe();
}
else {
DEBUG_PRINT("failed, mqttClient.state = ");
DEBUG_PRINTLN(mqttClient.state());
DEBUG_PRINT_MQTTSTATE(mqttClient.state());
if (updateDisplay) {
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 60);
tft.println("MQTT connection FAILED");
}
rc = 1;
}
delay(1000);
if (updateDisplay) tft.fillScreen(TFT_BLACK);
return rc;
}
void mqttCallback(char* topic, byte* payload, unsigned int length)
{
DEBUG_PRINT("Message arrived [");
DEBUG_PRINT(topic);
DEBUG_PRINT("] ");
char value[length+1];
memcpy(value, payload, length);
value[length] = '\0';
DEBUG_PRINTLN(value);
if (0 == strcmp(mqttSetMode, topic)) {
DEBUG_PRINTLN("setting operating mode via mqtt");
// handle set command ...
if (0 == memcmp("traffic_auto", payload, 12)) {
opMode = TRAFFIC_AUTO;
} else if (0 == memcmp("traffic_manual", payload, 14)) {
opMode = TRAFFIC_MANUAL;
} else if (0 == memcmp("traffic", payload, 7)) { // check after other "traffic" commands since they would match as well
opMode = TRAFFIC;
} else if (0 == memcmp("disco", payload, 5)) {
opMode = DISCO;
} else if (0 == memcmp("mood", payload, 4)) {
opMode = MOOD;
} else if (0 == memcmp("clock", payload, 5)) {
opMode = CLOCK;
showTimeNow = true;
}
drawModeText(opMode);
if (isMqttAvailable) mqttClient.publish(mqttOpmode, mode2str(opMode), true);
}
if (0 == strcmp(mqttSetConfig, topic)) {
DEBUG_PRINTLN("set config via mqtt");
}
if (0 == strcmp(mqttButton, topic)) {
DEBUG_PRINTLN("button press via mqtt");
if (0 == memcmp("1", payload, 1)) {
b1();
} else if (0 == memcmp("2", payload, 1)) {
b2();
} else if (0 == memcmp("3", payload, 1)) {
b3();
}
}
}