-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathZaparooEsp32.ino
218 lines (201 loc) · 4.58 KB
/
ZaparooEsp32.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
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <NfcAdapter.h>
#include <AudioFileSourceLittleFS.h>
#include <AudioOutputI2S.h>
#include <AudioGeneratorMP3.h>
#include <LittleFS.h>
#include <ZaparooLaunchApi.h>
#include "ZaparooEsp32.hpp"
//Config found in .hpp file
MFRC522 mfrc522(SS_PIN, RST_PIN);
NfcAdapter nfc = NfcAdapter(&mfrc522);
ZaparooLaunchApi client;
AudioOutputI2S* out;
boolean wifiEnabled = false;
String lastId="";
int idLoop = 0;
void setup() {
Serial.begin(115200);
setupPins();
#ifndef SERIAL_ONLY
initWiFi();
client.url(zaparooUrl);
#endif
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
nfc.begin();
}
void setupPins(){
#ifdef MOTOR_PIN
pinMode(MOTOR_PIN, OUTPUT);
#endif
#ifdef WIFI_LED_PIN
pinMode(WIFI_LED_PIN, OUTPUT);
#endif
#ifdef LAUNCH_LED_PIN
pinMode(LAUNCH_LED_PIN, OUTPUT);
#endif
#ifdef EXTERNAL_POWER_LED
pinMode(EXTERNAL_POWER_LED, OUTPUT);
digitalWrite(EXTERNAL_POWER_LED, HIGH);
#endif
#ifdef I2S_DOUT
out = new AudioOutputI2S();
out->SetPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
out->SetChannels(1);
out->SetGain(AUDIO_GAIN);
if (!LittleFS.begin(true)) {
Serial.println("An error has occurred while mounting LittleFS. No launch audio");
}
else if (!LittleFS.exists(launchAudio)) {
Serial.println("Launch audio file not found");
}
#endif
}
void motorOn(int predelay=0){
#ifdef MOTOR_PIN
delay(predelay);
analogWrite(MOTOR_PIN, 255);
#endif
}
void motorOff(int predelay=0){
#ifdef MOTOR_PIN
delay(predelay);
analogWrite(MOTOR_PIN, 0);
#endif
}
void launchLedOn(int predelay=0){
#ifdef LAUNCH_LED_PIN
delay(predelay);
digitalWrite(LAUNCH_LED_PIN, HIGH);
#endif
}
void launchLedOff(int predelay=0, int postDelay=0){
#ifdef LAUNCH_LED_PIN
delay(predelay);
digitalWrite(LAUNCH_LED_PIN, LOW);
delay(postDelay);
#endif
}
void wifiLedOn(){
#ifdef WIFI_LED_PIN
digitalWrite(WIFI_LED_PIN, HIGH);
#endif
}
void wifiLedOff(){
#ifdef WIFI_LED_PIN
digitalWrite(WIFI_LED_PIN, LOW);
#endif
}
void expressError(int code){
for(int i=0; i < code; i++){
motorOn();
launchLedOn();
motorOff(800);
launchLedOff(0, 400);
}
}
void playAudio(){
#ifdef I2S_DOUT
AudioFileSourceLittleFS* file = new AudioFileSourceLittleFS(launchAudio);
AudioGeneratorMP3* mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);
while(mp3->loop()){}
mp3->stop();
delete file;
delete mp3;
#else
delay(1000);
#endif
}
bool send(String& gamePath){
if(!wifiEnabled) return true;
int code = client.launch(gamePath);
if(code > 0){
expressError(code);
}
return code == 0;
}
bool sendUid(String& uid){
if(!wifiEnabled) return true;
int code = client.launchUid(uid);
if(code > 0){
expressError(code);
}
return code == 0;
}
void successActions(){
launchLedOn(0);
motorOn(0);
playAudio();
motorOff(0);
launchLedOff();
}
void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
wifiLedOn();
delay(500);
wifiLedOff();
}
wifiEnabled =true;
Serial.println(WiFi.localIP());
wifiLedOn();
motorOn();
motorOff(250);
motorOn(100);
motorOff(250);
}
void loop(void) {
if (nfc.tagPresent()) {
NfcTag tag = nfc.read();
String id = tag.getUidString();
if(id.equalsIgnoreCase(lastId)){
idLoop++;
}
lastId = id;
bool foundMessage = false;
if(tag.hasNdefMessage()){
NdefMessage message = tag.getNdefMessage();
int recordCount = message.getRecordCount();
NdefRecord record = message.getRecord(0);
int payloadLength = record.getPayloadLength();
const byte *payload = record.getPayload();
String payloadAsString = "";
for (int i = 3; i < payloadLength; i++) {
payloadAsString += (char)payload[i];
}
foundMessage = !payloadAsString.equalsIgnoreCase("");
if(foundMessage){
if(send(payloadAsString)){
Serial.print("SCAN\t" + payloadAsString + "\n");
Serial.flush();
successActions();
}
nfc.haltTag();
delay(900);
}
}
if(!foundMessage){
if(idLoop > 3){
String toSend = id;
toSend.replace(" ", "");
toSend.toLowerCase();
if(sendUid(toSend)){
Serial.print("SCAN\tuid=" + toSend+ "\n");
Serial.flush();
successActions();
idLoop = 0;
delay(900);
}
nfc.haltTag();
}
}
}
delay(100);
}