Skip to content

Commit

Permalink
Merge pull request #1 from v1605/api-v2
Browse files Browse the repository at this point in the history
Api v2
  • Loading branch information
v1605 authored Oct 15, 2024
2 parents a3799de + 827bd6f commit 7065021
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 5,725 deletions.
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,50 @@
# TapTo Esp32

The goal of this project is to launch a game via the [TapTo Service](https://github.com/TapToCommunity/tapto) on the a Mister over Wifi. It should work with any Ardunio compatible board but only Esp32 models are official supported.
The goal of this project is to launch games via the [TapTo Service](https://github.com/TapToCommunity/tapto) on the a Mister over Wifi. It also supports launching games via [Simple Serial](https://tapto.wiki/Reader_Drivers#Simple_Serial).

As of now, this code should be considered alpha, as it is not using the TapTo 2.0 api (instead using v1 endpoints). Once TapTo 2.0 is released I will rework the api calls.
The project is now supporting the intial release of the v2 api (which uses websockets). Further updates are required once the security layer is implemented in the api.

## Required Parts
## Hardare
* An Esp32
* A MFRC522
* (Optional) A MAX98357a board and speaker
* (Optional) PWM Rumble motor. You can use a preconstructed board or build your own using a transistor and motor.

## Required Libaries
* ArduinoJson (future TapTo 2.0)
* UrlEncode
## Required Libaries (Which may have their own dependencies)
* MFRC55
* ESP8266Audio
* [NDEF](https://github.com/don/NDEF/tree/master)
* [TapTo Esp32 Launch Api](https://github.com/v1605/tapto-esp32-launch-api)

## Setup
1. If you haven't already, downalod and install Ardunio Studio.
2. Install the above Libaries.
3. Clone/Download the repository and open Tap2Esp32.ino in Arunido Studio.
4. Select your board and port, located in the Tools menu. If you do not have options for an Esp32, follow these [instrustions](https://docs.sunfounder.com/projects/umsk/en/latest/03_esp32/esp32_start/03_install_esp32.html) for installing the board configurations.
5. Edit the ReadTag.hpp file to define your pins for the MFRC522, the Mister url (using the Remote port until TapTo 2.0, default 8182), and your Wifi credentials.
6. (Optional) If you are planning to use a battery to power the Esp32, it might help to extend battery life by decreasing the CPU frequency under tools.
6. (Optional) If you are planning to use a battery to power the Esp32, it might help to extend battery life by decreasing the CPU frequency under tools. If using the audio configuration, you need a min cpu of 160mhz.
7. (Optional) Enable any of the settings in the optional section of the config by uncommenting the line and setting the correct value for your setup. See "Optional Setup" for more info.
8. (Optional) If using audio options, make sure your "Partion Scheme" under tools is set to "Default xMB with spiffs". xMB will vary based on your board, but 4MB is common. After you upload the project, follow the instructons [here](https://randomnerdtutorials.com/arduino-ide-2-install-esp32-littlefs/) to upload a mp3 (test with [this gb sound effect](https://tuna.voicemod.net/sound/e4674ff7-386c-4932-9faf-e50c82d45099)).
9. Compile and upload the project your esp32.

## Optional Setup
These descriptions are for the optional config options found in the ReadTag.hpp file. Uncommment and edit the lines in the config if you want to enable them.
1. MOTOR_PIN: Sends a pulse to this pin when WiFi is connected, a game is launched, or some error has occurred (see Error Feedback). It is intended for a small vibration motor to provide haptic feedback.
2. WIFI_LED_PIN: This pin will enter a high state once a WiFi connection has been established.
3. LAUNCH_LED_PIN: This pin mimics the behaviors the MOTOR_PIN, with a slightly different pulse behavior more suitable for an LED.
4. EXTERNAL_POWER_LED: This pin will enter a High state as soon as the unit boots. This feature is useful if your enclousre blocks any built in LEDs.
5. I2S_*: These pins are for a I2S module (such as a max98357a) to produce a launch sound. You can replace the sound in the launchAudio.h file by converting a 8-bit unsigned pcm audio file to hex.
1. SERIAL_ONLY: Uncommenting this setting will disable wifi. Only Simple Serial will be supported in this mode.
2. MOTOR_PIN: Sends a pulse to this pin when WiFi is connected, a game is launched, or some error has occurred (see Error Feedback). It is intended for a small vibration motor to provide haptic feedback.
3. WIFI_LED_PIN: This pin will enter a high state once a WiFi connection has been established.
4. LAUNCH_LED_PIN: This pin mimics the behaviors the MOTOR_PIN, with a slightly different pulse behavior more suitable for an LED.
5. EXTERNAL_POWER_LED: This pin will enter a High state as soon as the unit boots. This feature is useful if your enclousre blocks any built in LEDs.
6. I2S_*: These pins are for a I2S module (such as a max98357a) to produce a launch sound. The audio file must be a mp3 file uploaded via LittleFs as described above.
7. launchAudio: The name of the mp3 file that plays when a tag is launched.
8. AUDIO_GAIN: How loud the speaker should be.

## Error Feedback
If you choose to enable the Motor or Launch pins in the config file, you can get additional feedback when scanning a card:
* 2 Pulses: Game failed to launch via TapTo.
* 2 Pulses: Could not connect to TapTo
* 3 Pulses: TapTo could not read the text, check your tag
* 4 Pulses: TapTo sent a payload that could not be parsed to JSON.

## Portable Options
It is possible to incorporate a battery to make a wireless NFC adapter. There are a two options to consider.
1. An Adadfruit Feather Esp32: it has all the required hardware to run off a lipo battery. The downside is that even with a power switch to ground the enable pin, you can still get battery drain. If you add a swich on the battery, you can only charge when its powered on.
2. Combine a lipo charging board, 5v buck converter, and a switch to power the board. This option require more assembly and reasearch but avoids the downsides of option one.
17 changes: 11 additions & 6 deletions TapToEsp32.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
//Reguired Configuration-------------------------------------------------

//The SS/SDA and Reset pins of the MFRC522
#define SS_PIN 12
#define RST_PIN 27
#define SS_PIN 5
#define RST_PIN 16

//Wifi and mister information
const char* ssid = "NetworkName";
const char* password = "Password";
const String tapToUrl = "http://mister.local:7497";
const String tapToUrl = "ws://mister.local:7497";

//Uncomment if serial only, wifi credentials will not be used
//#define SERIAL_ONLY

//-----------------------------------------------------------------------

Expand All @@ -20,18 +23,20 @@ const String tapToUrl = "http://mister.local:7497";
//#define MOTOR_PIN 25

//Uncomment if using a led to display wifi status
//#define WIFI_LED_PIN 16
//#define WIFI_LED_PIN 4

//Uncomment if using a led to display launch status
//#define LAUNCH_LED_PIN 16
//#define LAUNCH_LED_PIN 33

//Uncomment if using a led to display power indicator
//#define EXTERNAL_POWER_LED 16
//#define EXTERNAL_POWER_LED 22

//Uncomment if using a I2S module to produce audio
//#define I2S_DOUT 26
//#define I2S_BCLK 17
//#define I2S_LRC 21
//#define AUDIO_GAIN 1 //Value 0.1 - 4
//const char* launchAudio = "/gbc-startup.mp3";

//-----------------------------------------------------------------------
#endif
218 changes: 116 additions & 102 deletions TapToEsp32.ino
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <HTTPClient.h>
#include <UrlEncode.h>
#include "NfcAdapter.h"
#include "AudioFileSourcePROGMEM.h"
#include "AudioGeneratorWAV.h"
#include "AudioOutputI2S.h"
#include "launchAudio.h"
#include <NfcAdapter.h>
#include <AudioFileSourceLittleFS.h>
#include <AudioOutputI2S.h>
#include <AudioGeneratorMP3.h>
#include <LittleFS.h>
#include <TapToLaunchApi.h>
#include "TapToEsp32.hpp"

//Config found in ReadTag.hpp

MFRC522 mfrc522(SS_PIN, RST_PIN);
NfcAdapter nfc = NfcAdapter(&mfrc522);
TapToLaunchApi client;
AudioOutputI2S* out;
boolean wifiEnabled = false;

void setup(void) {
Serial.begin(9600);
void setup() {
Serial.begin(115200);
setupPins();
#ifndef SERIAL_ONLY
initWiFi();
client.url(tapToUrl);
#endif
SPI.begin(); // Init SPI bus
mfrc522.PCD_Init(); // Init MFRC522
nfc.begin();
Expand All @@ -42,125 +46,135 @@ void setupPins(){
#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 loop(void) {
if (nfc.tagPresent()) {
NfcTag tag = nfc.read();
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];
}
sendTapTo(payloadAsString);
nfc.haltTag();
delay(1000);
}
}
delay(200);
void motorOn(int predelay=0){
#ifdef MOTOR_PIN
delay(predelay);
analogWrite(MOTOR_PIN, 255);
#endif
}

void initWiFi() {
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.print("Connecting to WiFi ..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
triggerWifiLed(HIGH, 0);
delay(500);
triggerWifiLed(LOW, 500);
}
Serial.println(WiFi.localIP());
triggerWifiLed(HIGH, 0);
triggerMotor(250, 2, 100);
void motorOff(int predelay=0){
#ifdef MOTOR_PIN
delay(predelay);
analogWrite(MOTOR_PIN, 0);
#endif
}

void sendTapTo(String gamePath){
HTTPClient http;
http.begin(tapToUrl + "/api/v1/launch/" + urlEncode(gamePath));
int httpResponseCode = http.GET();
if (httpResponseCode == 200) {
Serial.println("Launched");
triggerLaunchLed(HIGH, 0);
triggerMotor(200, 1, 0);
playAudio();
triggerLaunchLed(LOW, 2000);
}else{
Serial.print("Error code: ");
Serial.println(httpResponseCode);
expressError(2);
}
http.end();
void launchLedOn(int predelay=0){
#ifdef LAUNCH_LED_PIN
delay(predelay);
digitalWrite(LAUNCH_LED_PIN, HIGH);
#endif
}

void triggerMotor(int time, int loopCount, int loopDelay){
#ifdef MOTOR_PIN
int last = loopCount - 1;
for(int i=0; i <= last; i++){
digitalWrite(MOTOR_PIN, HIGH);
delay(time);
digitalWrite(MOTOR_PIN, LOW);
if(loopDelay > 0 && i != last){
delay(loopDelay);
}
}
void launchLedOff(int predelay=0, int postDelay=0){
#ifdef LAUNCH_LED_PIN
delay(predelay);
digitalWrite(LAUNCH_LED_PIN, LOW);
delay(postDelay);
#endif
}

void triggerLaunchLed(int state, int preDelay){
#ifdef LAUNCH_LED_PIN
if(preDelay > 0){
delay(preDelay);
}
digitalWrite(LAUNCH_LED_PIN, state);
void wifiLedOn(){
#ifdef WIFI_LED_PIN
digitalWrite(WIFI_LED_PIN, HIGH);
#endif
}

void triggerWifiLed(int state, int preDelay){
void wifiLedOff(){
#ifdef WIFI_LED_PIN
if(preDelay > 0){
delay(preDelay);
}
digitalWrite(WIFI_LED_PIN, state);
digitalWrite(WIFI_LED_PIN, LOW);
#endif
}

void expressError(int code){
int motorPin = -1;
int launchPin= -1;
#ifdef MOTOR_PIN
motorPin = MOTOR_PIN;
#endif
#ifdef LAUNCH_LED_PIN
launchPin = LAUNCH_LED_PIN;
#endif
if(motorPin == -1 && launchPin == -1) return;
for(int i=0; i < code; i++){
if(motorPin >= 0) digitalWrite(motorPin, HIGH);
if(launchPin >= 0) digitalWrite(launchPin, HIGH);
delay(800);
if(motorPin >= 0) digitalWrite(motorPin, LOW);
if(launchPin >= 0) digitalWrite(launchPin, LOW);
delay(400);
motorOn();
launchLedOn();
motorOff(800);
launchLedOff(0, 400);
}
}

void playAudio(){
#ifdef I2S_DOUT
AudioFileSourcePROGMEM* file = new AudioFileSourcePROGMEM(launchAudio, sizeof(launchAudio));
AudioGeneratorWAV* wav = new AudioGeneratorWAV();
wav->begin(file, out);
delay(50); //No delay, no sound
if (wav->isRunning()) {
if (!wav->loop()) wav->stop();
}
AudioFileSourceLittleFS* file = new AudioFileSourceLittleFS(launchAudio);
AudioGeneratorMP3* mp3 = new AudioGeneratorMP3();
mp3->begin(file, out);
while(mp3->loop()){}
mp3->stop();
delete file;
delete wav;
delete mp3;
#else
delay(1000);
#endif
}

bool sendTapTo(String& gamePath){
if(!wifiEnabled) return true;
int code = client.launch(gamePath);
if(code > 0){
expressError(code);
}
return code == 0;
}

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();
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];
}
if(!payloadAsString.equalsIgnoreCase("")){
if(sendTapTo(payloadAsString)){
Serial.print("SCAN\t" + payloadAsString + "\n");
Serial.flush();
launchLedOn(0);
motorOn(0);
playAudio();
motorOff(0);
launchLedOff();
}
nfc.haltTag();
delay(1000);
}
}
}
}
Loading

0 comments on commit 7065021

Please sign in to comment.