This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sketck_oracle_v3.ino
200 lines (159 loc) · 4.47 KB
/
sketck_oracle_v3.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
#include <Keypad.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>
#include <WiFi101.h>
#include <MQTT.h>
const char ssid[] = "InnovationLab24";
const char pass[] = "welcome1";
WiFiClient net;
MQTTClient client;
LiquidCrystal_I2C lcd(0x27, 16, 2);
int a = 0; // variabile per il publish
String gName = ""; // variabile globale per il payload del publish
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'D', 'C', 'B', 'A'},
{'#', '9', '6', '3'},
{'0', '8', '5', '2'},
{'*', '7', '4', '1'}
};
int portarossa = A3;
int portaverde = A2;
int portablu = A1;
byte rowPins[ROWS] = {2, 3, 4, 5};
byte colPins[COLS] = {6, 7, 8, 9};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void colore (unsigned char rosso, unsigned char verde, unsigned char blu) {
analogWrite(portarossa, rosso);
analogWrite(portablu, blu);
analogWrite(portaverde, verde);
}
unsigned long lastMillis = 0;
void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.print("\nconnecting...");
while (!client.connect("arduino", "try", "try")) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconnected!");
}
void stampa() {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - Welcome! - ");
lcd.setCursor(0, 1);
lcd.print(" Enter the code");
}
void setup() {
Serial.begin(9600);
WiFi.begin(ssid, pass);
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino.
// You need to set the IP address directly.
client.begin("192.168.1.198", net); //192.168.99.12
client.onMessage(messageReceived);
connect();
pinMode(portarossa, OUTPUT);
pinMode(portaverde, OUTPUT);
pinMode(portablu, OUTPUT);
colore(255, 0, 0);
lcd.init(); // inizializza il display
lcd.backlight(); //accende la retroilluminazione
stampa();
delay(3000);
}
String ctrlLen; // variabile per il controllo della lunghezza per la stampa del messaggio su display
String insertPin; // stringa che contiene il codice inseri
int f = 0; // variabile di controllo
int x = 0; // variabile di controllo lunghezza
void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
client.unsubscribe("answer");
gName = payload;
}
void loop() {
client.loop();
if (!client.connected()) {
connect();
}
colore(255, 0, 0);
char key = keypad.getKey();
if (key != NO_KEY && x < 6) {
if (f == 0) {
lcd.clear();
f = 1;
}
//Serial.print(key);
lcd.setCursor(0, 0);
lcd.print("Password:");
lcd.setCursor(x, 1);
lcd.print("*");
insertPin += key;
x++;
a++;
}
if(a == 5) { // inviato il messaggio
client.publish("code", insertPin);
insertPin = "";
client.subscribe("answer");
a = 7;
lcd.clear();
}
if(a == 7) { // ricevimento del messaggio e stampa sul display
lcd.setCursor(0, 0);
lcd.print("Recognition...");
if(gName.length() > 0) {
if(gName.equals("Err")) { // stampa errore sul display
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(" - Wrong code - ");
Serial.println(" - Wrong code - ");
for (int s = 0; s < 6; s++) {
colore(0, 0, 0);
delay(150);
colore(255, 0, 0);
delay(150);
}
delay(800);
lcd.clear();
stampa();
x = 0;
f = 0;
a = 0;
gName = "";
} else {
// Il pin viene aggiunto con la parola 'Welcome'
ctrlLen = "Welcome " + gName;
Serial.println(" - " + ctrlLen + " -");
// Se tutta la stringa è maggiore di 16 caratteri allora verrà stampata in due righe diverse
if (ctrlLen.length() > 16) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome");
lcd.setCursor(0, 1);
lcd.print(gName);
} else {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Welcome " + gName);
}
colore(255, 235, 59);
delay(1000);
colore(0, 255, 0);
delay(1000);
lcd.clear();
stampa();
x = 0;
f = 0;
a = 0;
gName = "";
}
}
}
}