Skip to content

Commit 827bd6f

Browse files
authored
Update TapToEsp32.ino
1 parent 4e2610f commit 827bd6f

File tree

1 file changed

+20
-63
lines changed

1 file changed

+20
-63
lines changed

TapToEsp32.ino

Lines changed: 20 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,24 @@
55
#include <AudioFileSourceLittleFS.h>
66
#include <AudioOutputI2S.h>
77
#include <AudioGeneratorMP3.h>
8-
#include <ArduinoWebsockets.h>
9-
#include <ArduinoJson.h>
10-
#include <UUID.h>
11-
#include <atomic>
128
#include <LittleFS.h>
9+
#include <TapToLaunchApi.h>
1310
#include "TapToEsp32.hpp"
1411

15-
using namespace websockets;
16-
1712
//Config found in ReadTag.hpp
1813

1914
MFRC522 mfrc522(SS_PIN, RST_PIN);
2015
NfcAdapter nfc = NfcAdapter(&mfrc522);
21-
boolean requestSent = false;
16+
TapToLaunchApi client;
2217
AudioOutputI2S* out;
2318
boolean wifiEnabled = false;
2419

2520
void setup() {
2621
Serial.begin(115200);
27-
Serial.println("Started");
2822
setupPins();
2923
#ifndef SERIAL_ONLY
3024
initWiFi();
25+
client.url(tapToUrl);
3126
#endif
3227
SPI.begin(); // Init SPI bus
3328
mfrc522.PCD_Init(); // Init MFRC522
@@ -126,54 +121,13 @@ void playAudio(){
126121
#endif
127122
}
128123

129-
bool sendTapTo(String gamePath){
124+
bool sendTapTo(String& gamePath){
130125
if(!wifiEnabled) return true;
131-
WebsocketsClient client;
132-
std::atomic<bool> complete(false);
133-
std::atomic<bool> wasError(false);
134-
JsonDocument doc;
135-
UUID uuid;
136-
const char* id = uuid.toCharArray();
137-
doc["jsonrpc"]= "2.0";
138-
doc["method"]="launch";
139-
doc["id"]= uuid.toCharArray();
140-
doc["params"]["text"] = gamePath;
141-
doc.shrinkToFit();
142-
client.onMessage([&complete, &wasError, &id](WebsocketsMessage msg){
143-
if(complete.load()) return;
144-
JsonDocument result;
145-
DeserializationError error = deserializeJson(result, msg.data());
146-
if (error) {
147-
Serial.print("Failed to parse json");
148-
Serial.println(error.c_str());
149-
expressError(4);
150-
complete.store(true);
151-
wasError.store(true);
152-
return;
153-
}
154-
const char* resultId = result["id"];
155-
if(strcmp(id, resultId) != 0) return;
156-
complete.store(true);
157-
if(result.containsKey("result")){
158-
Serial.print("Error with game path");
159-
expressError(3);
160-
wasError.store(true);
161-
return;
162-
}
163-
});
164-
if(!client.connect(tapToUrl)){
165-
Serial.println("Unable to connect");
166-
expressError(2);
167-
return false;
168-
}
169-
String request;
170-
serializeJson(doc, request);
171-
client.send(request);
172-
while(!complete.load()){
173-
client.poll();
126+
int code = client.launch(gamePath);
127+
if(code > 0){
128+
expressError(code);
174129
}
175-
client.close();
176-
return !wasError.load();
130+
return code == 0;
177131
}
178132

179133
void initWiFi() {
@@ -208,16 +162,19 @@ void loop(void) {
208162
for (int i = 3; i < payloadLength; i++) {
209163
payloadAsString += (char)payload[i];
210164
}
211-
if(sendTapTo(payloadAsString)){
212-
Serial.print("SCAN\t" + payloadAsString + "\n");
213-
launchLedOn(0);
214-
motorOn(0);
215-
playAudio();
216-
motorOff(0);
217-
launchLedOff();
165+
if(!payloadAsString.equalsIgnoreCase("")){
166+
if(sendTapTo(payloadAsString)){
167+
Serial.print("SCAN\t" + payloadAsString + "\n");
168+
Serial.flush();
169+
launchLedOn(0);
170+
motorOn(0);
171+
playAudio();
172+
motorOff(0);
173+
launchLedOff();
174+
}
175+
nfc.haltTag();
176+
delay(1000);
218177
}
219-
nfc.haltTag();
220-
delay(1000);
221178
}
222179
}
223180
}

0 commit comments

Comments
 (0)