Andrei
Published © Apache-2.0

NFC Controller for OSC

Control OSC-enabled software with an NFC-enabled NodeMCU-ESP32.

BeginnerFull instructions provided3 hours8,061
NFC Controller for OSC

Things used in this project

Hardware components

NodeMcu ESP32
×1
RFID- RC522
×1
Jumper wires (generic)
Jumper wires (generic)
×1
NFC Tags
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

NodeMCU ESP32S - RC522

Code

NFC Controller for OSC

Arduino
Arduino Code for ESP32 NodeMcu to send OSC commands on NFC Tag Detection
#include <SPI.h>
#include <MFRC522.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOSC.h>


WiFiUDP udp;
ArduinoOSC<WiFiUDP> osc;

////////////////set here the IP and the port of the receiving computer////////////
///////////////The port is usually indicated in the software you want to controll////
//////////////The ip can be left as it is if you use AP mode, but if not make sure you input the computer IP////////////////

const int recv_port = 10000;
const int send_port = 7000;
const char* host = "192.168.0.2";



///////////////////Set the ID for the NFC Tags Here/////////////////
const char* tag1 = "4 68 167 194";
const char* tag2 = "4 134 167 194";
const char* tag3 = "4 101 167 194";
const char* tag4 = "4 168 167 194";
const char* tag5 = "4 135 167 194";
const char* tag6 = "4 236 167 194";

String previousTag = "";

////////////////Set OSC Comands here//////////////////

const char* comand1 =  "/composition/columns/1/connect";
const char* comand2 =  "/composition/columns/2/connect";
const char* comand3 =  "/composition/columns/3/connect";
const char* comand4 =  "/composition/columns/4/connect";
const char* comand5 =  "/composition/columns/5/connect";
const char* comand6 =  "/composition/columns/6/connect";
////Failsafe - if an unregistred tag is detected will send this command///
const char* other =  "/composition/columns/7/connect";



//////////////////Define your SSID and Password - if on AP mode this will be used as settings for AP if in Wifi mode this will be used to connect to an existing WiFi network//////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

const char* ssid = "YOUR_SSID";
const char* password =  "YOUR_PASSWORD";
 


////Wifi stuff
WiFiServer server(80);
IPAddress IP(192,168,4,15);
IPAddress mask = (255, 255, 255, 0);

///nfc stuff
const int resetPin = 22; // Reset pin
const int ssPin = 21;    // Slave select pin
MFRC522 rfid = MFRC522(ssPin, resetPin); // Create instance
int code[] = {69,141,8,136}; //This is the stored UID
int codeRead = 0;
String uidString;
int selectedChannel = 0;



void setup() {

  ///serial setup
  Serial.begin(115200);
 
  SPI.begin();   
 
  rfid.PCD_Init();
  rfid.PCD_DumpVersionToSerial();  



//////////////Use this code if you want to connect the ESP32 to an existing WIFI/////////////
////////////////////////////////////////////////////////////////////////////////////////////


  /////Wifi connect

   WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
   
    delay(500);
    Serial.println("Connecting to WiFi..");
 
  }
 
  Serial.println("Connected to the WiFi network");


//////////////Use this code if you want to enable ESP32 AP mode and create a WiFI network/////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////

 // WiFi.mode(WIFI_AP);
 // WiFi.softAP(ssid, password);
 // WiFi.softAPConfig(IP, IP, mask);
//  server.begin();

//  Serial.println();
//  Serial.println("Server started.");
//  Serial.print("IP: ");     Serial.println(WiFi.softAPIP());
//  Serial.print("MAC:");     Serial.println(WiFi.softAPmacAddress());



/////osc init
osc.begin(udp, recv_port);
}

void loop() {
//////Uncomment next line if you use the AP mode
//runWifi();
runRfid();

}



void runWifi(){

  WiFiClient client = server.available();
  if (!client) {return;}
  
  String request = client.readStringUntil('\r');
  client.flush();

}

void runRfid(){

 // Look for new cards
  if ( ! rfid.PICC_IsNewCardPresent()) {
    return;
  }

  // Select one of the cards
  if ( ! rfid.PICC_ReadCardSerial()) {
    return;
  }

  // Dump debug info about the card. PICC_HaltA() is automatically called.
   uidString = String(rfid.uid.uidByte[0])+" "+String(rfid.uid.uidByte[1])+" "+String(rfid.uid.uidByte[2])+ " "+String(rfid.uid.uidByte[3]);

Serial.println("Detected:");
Serial.println(uidString);

///////You can use this code if all you need is to just send the next osc command when a new NFC is detected////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//if(uidString != previousTag){
//
//
//  previousTag = uidString;
//  if(selectedChannel == 6){
//
//    selectedChannel = 1;
//  }
//  else{
//
//  selectedChannel++;
//  }
//  sendOscSignal(selectedChannel);
//  
//}

///////////////////////////Use this code if you want to control the sent OSC Command When a certain NFC Tag is detected/////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

if(uidString == tag1){

  if(selectedChannel != 1){
  selectedChannel =  1;
  sendOscSignal(selectedChannel);
  }
}

else if(uidString == tag2){

  if(selectedChannel != 2){
  selectedChannel =  2;
  sendOscSignal(selectedChannel);
  }
}

else if(uidString == tag3){

  if(selectedChannel != 3){
  selectedChannel =  3;
  sendOscSignal(selectedChannel);
  }
}

else if(uidString == tag4){

  if(selectedChannel != 4){
  selectedChannel =  4;
  sendOscSignal(selectedChannel);
  }
}

else if(uidString == tag5){

  if(selectedChannel != 5){
  selectedChannel =  5;
  sendOscSignal(selectedChannel);
  }
}

else if(uidString == tag6){

  if(selectedChannel != 6){
  selectedChannel =  6;
  sendOscSignal(selectedChannel);
  }
}
else{
  if(selectedChannel != 7){
  selectedChannel =  7;
  sendOscSignal(selectedChannel);
  }
  
}


 delay(500); 

  
}


void sendOscSignal(int v){
Serial.println("Preparing OSC:");
 OSCMessage msg;
    msg.beginMessage(host, send_port);

if(v == 1){
Serial.println("sending v1:");
 msg.setOSCAddress(comand1);
 msg.addArgInt32(1);
  Serial.println("Sent v1:");
}
else if(v == 2){
Serial.println("sending v2:");
 msg.setOSCAddress(comand2);
 msg.addArgInt32(1);
  Serial.println("Sent v2:");
}
else if(v == 3){
Serial.println("sending v3:");
 msg.setOSCAddress(comand3);
 msg.addArgInt32(1);
  Serial.println("Sent v3:");
}
else if(v == 4){
Serial.println("sending v4:");
 msg.setOSCAddress(comand4);
 msg.addArgInt32(1);
  Serial.println("Sent v4:");
}
else if(v == 5){
Serial.println("sending v5:");
 msg.setOSCAddress(comand5);
 msg.addArgInt32(1);
  Serial.println("Sent v5:");
}
else if(v == 6){
Serial.println("sending v6:");
 msg.setOSCAddress(comand6);
 msg.addArgInt32(1);
  Serial.println("Sent v6:");
}
else{

  Serial.println("sending other:");
 msg.setOSCAddress(other);
 msg.addArgInt32(1);
  Serial.println("Sent other:");
}

osc.send(msg);
}

Credits

Andrei

Andrei

1 project • 0 followers

Comments