-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathardrino.ino
208 lines (136 loc) · 4.6 KB
/
ardrino.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
#include <ArduinoJson.h>
#include <LittleFS.h>
#include <WiFiManager.h> // https://github.com/tzapu/WiFiManager
#include <PubSubClient.h>
WiFiManager wifiManager;
WiFiClient wclient;
PubSubClient client(wclient);
char mqttServer[32] = "10.208.30.11"; // default mqtt server
WiFiManagerParameter customMqttServer("server", "mqtt server", mqttServer, sizeof mqttServer);
char botMqttPrefix[32] = "GHBot/"; // prefix for mqtt topics
WiFiManagerParameter customBotMqttPrefix("prefix", "bot MQTT prefix", botMqttPrefix, sizeof botMqttPrefix);
char msgsInTopic[128] { 0 };
// assuming here that callback() is never called concurrently
char msgsOutTopic[128] { 0 };
char registerRequestTopic[128] { 0 };
char registerTopic[128] { 0 };
void announce() {
client.publish(registerTopic, "cmd=ardrino|descr=An ESP8266 as a bot plugin");
}
void callback(char *topic, byte *payload, unsigned int length) {
Serial.print(F("Received MQTT msg for topic: "));
Serial.println(topic);
if (strcmp(topic, registerRequestTopic) == 0 && strncmp((const char *)payload, "register", 8) == 0) {
announce();
return;
}
if (strncmp(topic, msgsInTopic, strlen(msgsInTopic) - 1 /* ignore '#' */) == 0 && length >= 8) {
if (strncmp(reinterpret_cast<const char *>(payload), "~ardrino", 8) == 0) {
const char *p_topic = &topic[strlen(botMqttPrefix)];
const char *p_channel = &p_topic[9]; // "from/irc/"
const char *p_channel_end = strchr(p_channel, '/');
if (p_channel_end) {
char temp[32] { 0 };
strncpy(temp, p_channel, std::min(31, p_channel_end - p_channel));
snprintf(msgsOutTopic, sizeof msgsOutTopic, "%sto/irc/%s/privmsg", botMqttPrefix, temp);
Serial.print(F("Publishing to: "));
Serial.println(msgsOutTopic);
// TODO: do something instead of:
client.publish(msgsOutTopic, "Hello, this is Ardrino!");
}
}
}
}
void pollMqtt() {
client.loop();
while (!client.connected()) {
Serial.println("Attempting MQTT connection... ");
if (client.connect("ardrino")) {
Serial.println(F("Connected"));
snprintf(msgsInTopic, sizeof msgsInTopic, "%sfrom/irc/#", botMqttPrefix);
client.subscribe(msgsInTopic);
snprintf(registerRequestTopic, sizeof registerRequestTopic, "%sfrom/bot/command", botMqttPrefix);
client.subscribe(registerRequestTopic);
snprintf(registerTopic, sizeof registerTopic, "%sto/bot/register", botMqttPrefix);
// in case this is the first connect since up
announce();
break;
}
delay(1000);
}
}
void reboot() {
Serial.println(F("Reboot"));
Serial.flush();
delay(100);
ESP.restart();
delay(1000);
}
void loadConfiguration() {
if (LittleFS.begin()) {
Serial.println(F("mounted file system"));
if (LittleFS.exists("/config.json")) {
// file exists, reading and loading
Serial.println(F("reading config file"));
File configFile = LittleFS.open("/config.json", "r");
if (configFile) {
size_t size = configFile.size();
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonDocument jsonBuffer(128);
DeserializationError error = deserializeJson(jsonBuffer, buf.get());
if (error == 0) {
serializeJson(jsonBuffer, Serial);
strcpy(mqttServer, jsonBuffer["mqttServer"]);
strcpy(botMqttPrefix, jsonBuffer["botMqttPrefix"]);
return;
}
Serial.println(F("failed to load json config"));
}
}
}
else {
Serial.println(F("failed to mount FS"));
}
return;
}
void saveConfigCallback() {
strcpy(mqttServer, customMqttServer.getValue());
strcpy(botMqttPrefix, customBotMqttPrefix.getValue());
Serial.println(F("saving config"));
DynamicJsonDocument jsonBuffer(128);
jsonBuffer["mqttServer"] = mqttServer;
jsonBuffer["botMqttPrefix"] = botMqttPrefix;
File configFile = LittleFS.open("/config.json", "w");
if (!configFile) {
Serial.println(F("failed to open config file for writing"));
return;
}
serializeJson(jsonBuffer, Serial);
serializeJson(jsonBuffer, configFile);
configFile.close();
}
void setupWifi() {
WiFi.begin();
wifiManager.setTimeout(300);
wifiManager.setSaveConfigCallback(saveConfigCallback);
wifiManager.addParameter(&customMqttServer);
wifiManager.addParameter(&customBotMqttPrefix);
if (!wifiManager.autoConnect("ardrino-demo"))
reboot();
}
void setup() {
Serial.begin(115200);
Serial.println(F("Init"));
loadConfiguration();
setupWifi();
Serial.print(F("MQTT server: "));
Serial.println(mqttServer);
client.setServer(mqttServer, 1883);
client.setCallback(callback);
Serial.println(F("Go!"));
}
void loop() {
pollMqtt();
// TODO: do something
}