forked from jmalovera10/SecurityTesting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridgeMQTT.ino
132 lines (113 loc) · 2.87 KB
/
bridgeMQTT.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
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
//DEFINES
#define TOPIC_SUBSCRIBE "yale.uniandes.ml337.key"
#define TOPIC_PUBLISH "yale.uniandes.ml337.alarm"
#define SIZE_BUFFER_DATA 50
//VARIABLES
const char* idDevice = "ISIS2503";
boolean stringComplete = false;
boolean init_flag = false;
String inputString = "";
char bufferData [SIZE_BUFFER_DATA];
// CLIENTE WIFI & MQTT
WiFiClient clientWIFI;
PubSubClient clientMQTT(clientWIFI);
// CONFIG WIFI
const char* ssid = "MiRedWifi";
const char* password = "123456";
// CONFIG MQTT
IPAddress serverMQTT (192,168,0,13);
const uint16_t portMQTT = 8083;
// const char* usernameMQTT = "admin";
// const char* passwordMQTT = "admin";
void connectWIFI() {
// Conectar a la red WiFi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
if(WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println(WiFi.localIP());
}
void reconnectWIFI() {
// Conectar a la red WiFi
if(WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, password);
}
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.write(payload, length);
Serial.println();
}
void setup() {
Serial.begin(9600);
inputString.reserve(100);
clientMQTT.setServer(serverMQTT, portMQTT);
clientMQTT.setCallback(callback);
connectWIFI();
delay(1000);
}
void processData() {
if (WiFi.status() == WL_CONNECTED) {
if(init_flag == false) {
init_flag = true;
boolean conectMQTT = false;
if (!clientMQTT.connected()) {
// if (!clientMQTT.connect(idDevice, usernameMQTT, passwordMQTT)) {
if (!clientMQTT.connect(idDevice)) {
conectMQTT = false;
}
conectMQTT = true;
}
else {
conectMQTT = true;
}
if(conectMQTT) {
if(clientMQTT.subscribe(TOPIC_SUBSCRIBE)) {
// Serial.println("Subscribe OK");
}
}
}
if (stringComplete && clientMQTT.connected()) {
if(clientMQTT.publish(TOPIC_PUBLISH, bufferData)) {
inputString = "";
stringComplete = false;
}
init_flag = false;
}
}
else {
connectWIFI();
init_flag = false;
}
clientMQTT.loop();
}
void receiveData() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
inputString.toCharArray(bufferData, SIZE_BUFFER_DATA);
stringComplete = true;
}
}
}
void loop() {
receiveData();
processData();
}