-
Notifications
You must be signed in to change notification settings - Fork 0
/
net-control.ino
141 lines (111 loc) · 3.38 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
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.c_str(),
mqtt_user, mqtt_pass, MQTT_TOPIC_STATE.c_str(), 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(MQTT_TOPIC_STATE.c_str(), connect_msg.c_str(), true);
// topic subscriptions ...
//mqttClient.subscribe(mqttSetMode);
mqttClient.subscribe(MQTT_TOPIC_SETCONFIG.c_str());
// 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(MQTT_TOPIC_SETCONFIG.c_str(), topic)) {
DEBUG_PRINTLN("set config via mqtt");
}
}