-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.ino
392 lines (320 loc) · 10.3 KB
/
remote.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
/*
*************************************************************************
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF THE COPYRIGHT HOLDERS OR
CONTRIBUTORS ARE AWARE OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************
*/
#include <OneWire.h>
#include <DallasTemperature.h>
#include <avr/wdt.h>
#include <Pushbutton.h>
#include <DS3232RTC.h>
// Select your modem:
#define TINY_GSM_RX_BUFFER 512
#define TINY_GSM_MODEM_A6
//#include <SoftwareSerial.h>
//SoftwareSerial SerialAT(2, 3); // RX, TX
// Set serial for debug console (to the Serial Monitor, default speed 115200)
#define SerialMon Serial
// Set serial for AT commands (to the module)
// Use Hardware Serial on Mega, Leonardo, Micro
#define SerialAT Serial1
// See all AT commands, if wanted
//#define DUMP_AT_COMMANDS
// Define the serial console for debug prints, if needed
#define TINY_GSM_DEBUG SerialMon
// Range to attempt to autobaud
#define GSM_AUTOBAUD_MIN 9600
#define GSM_AUTOBAUD_MAX 115200
#include <TinyGsmClient.h>
#include <PubSubClient.h>
// Define how you're planning to connect to the internet
#define TINY_GSM_USE_GPRS true
#define TINY_GSM_USE_WIFI false
// set GSM PIN, if any
#define GSM_PIN ""
// Your GPRS credentials, if any
const char apn[] = "APN_Code";
const char gprsUser[] = "";
const char gprsPass[] = "";
// Your WiFi connection credentials, if applicable
//const char wifiSSID[] = "YourSSID";
//const char wifiPass[] = "YourWiFiPass";
// MQTT details
const char* broker = "server.broker";
//const char* broker = "test.mosquitto.org";
//const char* broker = "my_IP_local_broker";
//MQTT Topic String
const char* mqttSub = "remote_location/#";
const char* checkTemp01 = "remote_location/seguenze/check_temp01";
const char* checkTemp02 = "remote_location/seguenze/check_temp02";
const char* checkLevel01 = "remote_location/seguenze/check_level01";
const char* topicInit = "remote_location/init";
const char* inviaData = "remote_location/dispositivi/stato/tempus";
const char* comandoAttuatore_01 = "remote_location/comandi/dispositivi/att01";
const char* comandoAttuatore_02 = "remote_location/comandi/dispositivi/att02";
const char* analogValue01 = "remote_location/segnali/analogici/in01";
const char* temp01 = "remote_location/segnali/analogici/temp01";
const char* temp02 = "remote_location/segnali/analogici/temp02";
const char* digiValue01 = "remote_location/segnali/digitali/di01";
const char* alarmString = "remote_location/segnali/digitali/allarmi/intrusione";
const char* pwrString = "remote_location/segnali/digitali/allarmi/rete220";
const char* aux01String = "remote_location/segnali/digitali/allarmi/aux01";
const char* aux02String = "remote_location/segnali/digitali/allarmi/aux02";
const char* aux03String = "remote_location/segnali/digitali/allarmi/aux03";
const char* aux04String = "remote_location/segnali/digitali/allarmi/aux04";
const char* tempusString = "remote_location/segnali/timestamp/tempus";
// String Definition
char analValue[5];
char tmp01[5];
char tmp02[5];
int index = 0;
char tempus_buf[20];
#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif
TinyGsmClient client(modem);
PubSubClient mqtt(client);
//ATMEGA 2560 I/O definition
// Digital Output
#define ACT01_PIN 11
#define ACT02_PIN 12
// Digital I/O
#define ALARM_PIN 22
#define POWER_PIN 23
#define AUX01_PIN 26
#define AUX02_PIN 27
#define AUX03_PIN 28
#define AUX04_PIN 30
int act01Status = LOW;
int act02Status = LOW;
const int buttonPin = 2;
int buttonState = LOW;
//Temperature input
#define ONE_WIRE_TEMP01 3
#define ONE_WIRE_TEMP02 2
// Analog Input
int sensorPin = A0;
// Interface Object allocation
OneWire oneWire01(ONE_WIRE_TEMP01);
DallasTemperature sensors_temp01(&oneWire01);
OneWire oneWire02(ONE_WIRE_TEMP02);
DallasTemperature sensors_temp02(&oneWire02);
Pushbutton alm_button(ALARM_PIN);
Pushbutton pwr_button(POWER_PIN);
Pushbutton aux01_button(AUX01_PIN);
Pushbutton aux02_button(AUX02_PIN);
Pushbutton aux03_button(AUX03_PIN);
Pushbutton aux04_button(AUX04_PIN);
// RTC Time-Date
DS3232RTC gprs_RTC;
long lastReconnectAttempt = 0;
void mqttCallback(char* topic, byte* payload, unsigned int len) {
// Check temperature ch 1
if (String(topic) == checkTemp01) {
sensors_temp01.requestTemperatures();
dtostrf(sensors_temp01.getTempCByIndex(0), 4, 1,tmp01);
mqtt.publish(temp01, tmp01);
}
// Check temperature ch 2
if (String(topic) == checkTemp02) {
sensors_temp02.requestTemperatures();
dtostrf(sensors_temp02.getTempCByIndex(0), 4, 1,tmp02);
mqtt.publish(temp02, tmp02);
}
// Check analogic value from ch 01
if (String(topic) == checkLevel01) {
sprintf(analValue,"%d",analogRead(sensorPin));
mqtt.publish(analogValue01,analValue);
}
// Out 01 activation
if (String(topic) == comandoAttuatore_01) {
act01Status = !act01Status;
digitalWrite(ACT01_PIN, act01Status);
}
// Out 02 activation
if (String(topic) == comandoAttuatore_02) {
act02Status = !act02Status;
digitalWrite(ACT02_PIN, act02Status);
}
// Send data-time (for diagnostic)
if (String(topic) == inviaData) {
time_t t = gprs_RTC.get();
sprintf(tempus_buf, "%.2d\:%.2d\:%.2d %.2d %s %d",hour(t), minute(t), second(t), day(t), monthShortStr(month(t)), year(t));
mqtt.publish(tempusString,tempus_buf);
}
}
boolean mqttConnect() {
SerialMon.print("Connecting to ");
SerialMon.print(broker);
// Connect to MQTT Broker
//boolean status = mqtt.connect("Remote_location");
// Or, if you want to authenticate MQTT:
boolean status = mqtt.connect("Remote_location", "user", "pwd");
if (status == false) {
SerialMon.println(" fail");
return false;
}
SerialMon.println(" success");
mqtt.publish(topicInit, "Gprs Remote location started");
mqtt.subscribe(mqttSub);
return mqtt.connected();
}
void setup() {
// Set console baud rate
SerialMon.begin(115200);
delay(10);
pinMode(ACT01_PIN, OUTPUT);
pinMode(ACT02_PIN, OUTPUT);
pinMode(buttonPin, INPUT);
sensors_temp01.begin();
sensors_temp02.begin();
//display date-Time
gprs_RTC.begin();
time_t t = gprs_RTC.get();
sprintf(tempus_buf, "%.2d\:%.2d\:%.2d %.2d %s %d\n",
hour(t), minute(t), second(t), day(t), monthShortStr(month(t)), year(t));
SerialMon.println(tempus_buf);
SerialMon.println("Wait...");
// Set GSM module baud rate
TinyGsmAutoBaud(SerialAT,GSM_AUTOBAUD_MIN,GSM_AUTOBAUD_MAX);
//SerialAT.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
SerialMon.println("Initializing modem...");
modem.restart();
//modem.init();
wdt_enable(WDTO_8S);
String modemInfo = modem.getModemInfo();
SerialMon.print("Modem Info: ");
SerialMon.println(modemInfo);
//SerialMon.println(modem.getBattPercent());
#if TINY_GSM_USE_GPRS
// Unlock your SIM card with a PIN if needed
if ( GSM_PIN && modem.getSimStatus() != 3 ) {
modem.simUnlock(GSM_PIN);
}
#endif
SerialMon.println(" success");
//#endif
// wdt_reset();
SerialMon.print("Waiting for network...");
if (!modem.waitForNetwork()) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isNetworkConnected()) {
SerialMon.println("Network connected");
}
wdt_reset();
#if TINY_GSM_USE_GPRS
// GPRS connection parameters are usually set after network registration
SerialMon.print(F("Connecting to "));
SerialMon.print(apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
SerialMon.println(" fail");
delay(10000);
return;
}
SerialMon.println(" success");
if (modem.isGprsConnected()) {
SerialMon.println("GPRS connected");
}
#endif
wdt_reset();
// wdt_enable(WDTO_8S);
// MQTT Broker setup
mqtt.setServer(broker, 16198);
//mqtt.setServer(broker, 1883);
mqtt.setCallback(mqttCallback);
wdt_reset();
}
void loop() {
//SerialMon.println(modem.getLocalIP());
// digital input management
// Burglary alarm - Ready
if (alm_button.getSingleDebouncedPress())
{
mqtt.publish(alarmString, "Alert");
}
if (alm_button.getSingleDebouncedRelease())
{
mqtt.publish(alarmString, "Ready");
}
// 230 Vac test
if (pwr_button.getSingleDebouncedPress())
{
mqtt.publish(pwrString, "Inizio");
}
if (pwr_button.getSingleDebouncedRelease())
{
mqtt.publish(pwrString, "Rientro");
}
// Input AUX01
if (aux01_button.getSingleDebouncedPress())
{
mqtt.publish(aux01String, "Inizio");
}
if (aux01_button.getSingleDebouncedRelease())
{
mqtt.publish(aux01String, "Rientro");
}
// Input AUX02
if (aux02_button.getSingleDebouncedPress())
{
mqtt.publish(aux02String, "Inizio");
}
if (aux02_button.getSingleDebouncedRelease())
{
mqtt.publish(aux02String, "Rientro");
}
// Input AUX03
if (aux03_button.getSingleDebouncedPress())
{
mqtt.publish(aux03String, "Inizio");
}
if (aux03_button.getSingleDebouncedRelease())
{
mqtt.publish(aux03String, "Rientro");
}
// Input AUX04
if (aux04_button.getSingleDebouncedPress())
{
mqtt.publish(aux04String, "Inizio");
}
if (aux04_button.getSingleDebouncedRelease())
{
mqtt.publish(aux04String, "Rientro");
}
if (!mqtt.connected()) {
SerialMon.println("=== MQTT NOT CONNECTED ===");
unsigned long t = millis();
if (t - lastReconnectAttempt > 10000L) {
lastReconnectAttempt = t;
if (mqttConnect()) {
lastReconnectAttempt = 0;
}
}
delay(20);
return;
}
wdt_reset();
mqtt.loop();
}