-
Notifications
You must be signed in to change notification settings - Fork 0
/
washing-machine.ino
219 lines (175 loc) · 4.68 KB
/
washing-machine.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
//Libraries
#include <SPI.h>//https://www.arduino.cc/en/reference/SPI
#include <MFRC522.h>//https://github.com/miguelbalboa/rfid
#include <elapsedMillis.h>
#include <WiFi.h>
#include <HTTPClient.h>
//Constants
#define SS_PIN 5
#define RST_PIN 15
int pin = 35;
const int delayBetweenMeasure = 600000;
// 10 minutes
const int measureDuration = 60000; // 1 minute
// mapping of a tag to a discord id
typedef struct {
byte tag[4];
String discordID;
} nfcTagToDiscordID;
const nfcTagToDiscordID tagsToDiscord[] {
{{115, 191, 129, 015}, "351460900370120707"}, // Tom
{{147, 177, 217, 18}, "252061771181850625"} // Luca
};
const char* ssid = "";
const char* password = "";
const char* webhookUrl = "https://discord.com/api/webhooks/950482190788083772/Vr1z0YAxqP6mapTyW3ihfcOSRROoiDNjX-JUsPBCtlUyv7L0k0GH7IThNH4yYCCEITdl";
HTTPClient http;
//Variables
byte nuidPICC[4] = {0, 0, 0, 0};
MFRC522::MIFARE_Key key;
MFRC522 rfid = MFRC522(SS_PIN, RST_PIN);
void setup() {
pinMode(pin, INPUT); //initializes sensor
//Init Serial USB
Serial.begin(115200);
Serial.println(F("Initialize System"));
//init rfid D8,D5,D6,D7
SPI.begin();
rfid.PCD_Init();
Serial.print(F("Reader :"));
rfid.PCD_DumpVersionToSerial();
connectToWifi();
sendDiscordMessage(
"{\
\"content\": null,\
\"embeds\": [\
{\
\"title\": \"Washing analyzer started up\",\
\"color\": 65280\
}\
]\
}"
);
}
void loop() {
readRFID();
}
void readRFID() { /* function readRFID */
// Scan for RFID card
for (byte i = 0; i < 6; i++) {
key.keyByte[i] = 0xFF;
}
// Return if no card is present
if (!rfid.PICC_IsNewCardPresent())
return;
// Verify if the NUID has been read
if (!rfid.PICC_ReadCardSerial())
return;
if (WiFi.status() != WL_CONNECTED) {
connectToWifi();
}
// Store NUID into nuidPICC array
for (byte i = 0; i < 4; i++) {
nuidPICC[i] = rfid.uid.uidByte[i];
}
Serial.print(F("Read the following tag: "));
printDec(rfid.uid.uidByte, rfid.uid.size);
Serial.println();
// Halt PICC
rfid.PICC_HaltA();
// Stop encryption on PCD
rfid.PCD_StopCrypto1();
washingAnalysisLoop();
}
void washingAnalysisLoop(){
boolean isRunning = true;
Serial.println("Washing loop startet");
sendDiscordMessage(
"{\
\"content\": \"<@!" + tagToDiscordID(nuidPICC) + ">\", \
\"embeds\": [\
{\
\"title\": \"Washing monitoring started\",\
\"color\": 65280\
}\
]\
}"
);
while(isRunning == true){
Serial.println("Waiting for " + String(delayBetweenMeasure) + " milliseconds");
delay(delayBetweenMeasure);
Serial.println("Starting measurement");
int measure = 0;
for (int i = 0; i < measureDuration; i++) {
measure += digitalRead(pin);
delay(1);
}
if (measure == 0){
isRunning = false;
sendMessage();
}
}
}
/**
Helper routine to dump a byte array as hex values to Serial.
*/
void printHex(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
Helper routine to dump a byte array as dec values to Serial.
*/
void printDec(byte *buffer, byte bufferSize) {
for (byte i = 0; i < bufferSize; i++) {
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], DEC);
}
}
void sendMessage(){
Serial.println("Washing machine finished!");
sendDiscordMessage(
"{\
\"content\": \"<@!" + tagToDiscordID(nuidPICC) + ">\", \
\"embeds\": [\
{\
\"title\": \"Washing machine finished!\",\
\"color\": 65280\
}\
]\
}"
);
}
void sendDiscordMessage(String content) {
http.begin(webhookUrl);
http.addHeader("Content-Type", "application/json");
http.POST(content);
}
String tagToDiscordID(byte tag[]) {
for (uint8_t i = 0; i < sizeof(tagsToDiscord); i++)
{
bool equals = true;
for (byte j = 0; j < 4; j++) {
if (tag[j] != tagsToDiscord[i].tag[j]){
equals = false;
}
}
if (equals) {
return tagsToDiscord[i].discordID;
}
}
return "";
}
void connectToWifi(){
WiFi.begin(ssid, password);
Serial.println("Establishing connection to WiFi");
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.print("\n");
Serial.println("Connected to network with IP:");
Serial.println(WiFi.localIP());
}