303 lines
7.4 KiB
Arduino
303 lines
7.4 KiB
Arduino
|
|
||
|
#include <SPI.h>
|
||
|
#include "Adafruit_Soundboard.h"
|
||
|
#include <WiFi.h>
|
||
|
#include <WiFiUdp.h>
|
||
|
#include <OSCMessage.h>
|
||
|
//#include <OSCBundle.h>
|
||
|
#include <OSCData.h>
|
||
|
|
||
|
#include "nRF24L01.h"
|
||
|
#include "RF24.h"
|
||
|
#include "printf.h"
|
||
|
|
||
|
#define SFX_RST 21
|
||
|
#define SFX_ACT 26 // A0
|
||
|
#define RELE 13
|
||
|
|
||
|
#define RADIO_SEND true
|
||
|
#define CE_PIN 25 // A1
|
||
|
#define CSN_PIN 4
|
||
|
|
||
|
#define UDP_RX_PACKET_MAX_SIZE 4 // SUBIR EN CASO DE APURO
|
||
|
|
||
|
int radioId;
|
||
|
uint32_t CMD[ 8 ]; // CMD + Ch + 2
|
||
|
RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
|
||
|
const uint64_t pipeNode = 0xE8E8F0F0E1LL; // Define the transmit pipe
|
||
|
//int ch = 0x4C ; // 76 por defecto es el canal 76 (showroom)
|
||
|
|
||
|
Adafruit_Soundboard sfx = Adafruit_Soundboard(&Serial1, NULL, SFX_RST);
|
||
|
|
||
|
//char* ssid = "BRoomX-746"; // your network SSID (name)
|
||
|
//char* pass = "Broomx88"; // your network password
|
||
|
|
||
|
//char* ssid = "MID"; // your network SSID (name)
|
||
|
//char* pass = ""; // your network password
|
||
|
|
||
|
char* ssid = "knalito"; // your network SSID (name)
|
||
|
char* pass = "4yunu5jutyo4c"; // your network password
|
||
|
|
||
|
|
||
|
// A UDP instance to let us send and receive packets over UDP
|
||
|
WiFiUDP Udp;
|
||
|
|
||
|
//const IPAddress local_IP(10, 100, 50, 76); // local IP (if is need it)
|
||
|
//const IPAddress gateway(10, 100, 50, 1);
|
||
|
//const IPAddress local_IP(192, 168, 0, 150); // local IP (if is need it)
|
||
|
//const IPAddress gateway(192, 168, 0, 1);
|
||
|
//const IPAddress subnet(255, 255, 255, 0);
|
||
|
//const IPAddress outIp(192, 168, 0, 105); // remote IP (not needed for receive)
|
||
|
|
||
|
const IPAddress local_IP(172, 20, 10, 100); // local IP (if is need it)
|
||
|
const IPAddress gateway(172, 20, 10, 1);
|
||
|
const IPAddress subnet(255, 255, 255, 0);
|
||
|
const IPAddress outIp(172, 20, 10, 2); // remote IP (not needed for receive)
|
||
|
|
||
|
const unsigned int outPort = 9999; // remote port (not needed for receive)
|
||
|
const unsigned int localPort = 8888; // local port to listen for UDP packets (here's where we send the packets)
|
||
|
|
||
|
OSCErrorCode error;
|
||
|
unsigned int ledState = LOW; // LOW means led is *on*
|
||
|
|
||
|
unsigned long previousMillis = 0; // will store last time LED was updated
|
||
|
const long interval = 3000; // interval at which to blink (milliseconds)
|
||
|
|
||
|
|
||
|
#ifndef BUILTIN_LED
|
||
|
#ifdef LED_BUILTIN
|
||
|
#define BUILTIN_LED LED_BUILTIN
|
||
|
#else
|
||
|
#define BUILTIN_LED 13
|
||
|
#endif
|
||
|
#endif
|
||
|
|
||
|
String translateEncryptionType(wifi_auth_mode_t encryptionType) {
|
||
|
|
||
|
switch (encryptionType) {
|
||
|
case (WIFI_AUTH_OPEN):
|
||
|
return "Open";
|
||
|
case (WIFI_AUTH_WEP):
|
||
|
return "WEP";
|
||
|
case (WIFI_AUTH_WPA_PSK):
|
||
|
return "WPA_PSK";
|
||
|
case (WIFI_AUTH_WPA2_PSK):
|
||
|
return "WPA2_PSK";
|
||
|
case (WIFI_AUTH_WPA_WPA2_PSK):
|
||
|
return "WPA_WPA2_PSK";
|
||
|
case (WIFI_AUTH_WPA2_ENTERPRISE):
|
||
|
return "WPA2_ENTERPRISE";
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void scanNetworks() {
|
||
|
|
||
|
int numberOfNetworks = WiFi.scanNetworks();
|
||
|
|
||
|
Serial.print("Number of networks found: ");
|
||
|
Serial.println(numberOfNetworks);
|
||
|
|
||
|
for (int i = 0; i < numberOfNetworks; i++) {
|
||
|
|
||
|
Serial.print("Network name: ");
|
||
|
Serial.println(WiFi.SSID(i));
|
||
|
|
||
|
Serial.print("Signal strength: ");
|
||
|
Serial.println(WiFi.RSSI(i));
|
||
|
|
||
|
Serial.print("MAC address: ");
|
||
|
Serial.println(WiFi.BSSIDstr(i));
|
||
|
|
||
|
Serial.print("Encryption type: ");
|
||
|
String encryptionTypeDescription = translateEncryptionType(WiFi.encryptionType(i));
|
||
|
Serial.println(encryptionTypeDescription);
|
||
|
Serial.println("-----------------------");
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
void flushInput() {
|
||
|
// Read all available serial input to flush pending data.
|
||
|
uint16_t timeoutloop = 0;
|
||
|
while (timeoutloop++ < 40) {
|
||
|
while(Serial1.available()) {
|
||
|
Serial1.read();
|
||
|
timeoutloop = 0; // If char was received reset the timer
|
||
|
}
|
||
|
delay(1);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void led(OSCMessage &msg)
|
||
|
{
|
||
|
ledState = (int)msg.getFloat(0);
|
||
|
//ledState = msg.getInt(0);
|
||
|
digitalWrite(BUILTIN_LED, ledState);
|
||
|
Serial.print("led: ");
|
||
|
Serial.println(ledState);
|
||
|
}
|
||
|
|
||
|
void radioCmd(OSCMessage &msg)
|
||
|
{
|
||
|
|
||
|
if (msg.isFloat(0)){
|
||
|
CMD[0] = msg.getFloat(0);
|
||
|
}
|
||
|
if (msg.isFloat(1)){
|
||
|
CMD[1] = msg.getFloat(1);
|
||
|
}
|
||
|
|
||
|
Serial.print("Radio: ");
|
||
|
Serial.print(CMD[0]); Serial.print("/"); Serial.println(CMD[1]);
|
||
|
|
||
|
radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
|
||
|
|
||
|
}
|
||
|
|
||
|
void safeStop() {
|
||
|
int isPlaying = digitalRead(SFX_ACT);
|
||
|
if (isPlaying == LOW) {
|
||
|
Serial.println("Stopping...");
|
||
|
sfx.stop();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void playtrack(OSCMessage &msg)
|
||
|
{
|
||
|
|
||
|
flushInput() ;
|
||
|
safeStop();
|
||
|
|
||
|
uint8_t track = (int)msg.getFloat(0);
|
||
|
Serial.print("\nPlaying track #"); Serial.println(track);
|
||
|
if (! sfx.playTrack((uint8_t)track) )
|
||
|
{
|
||
|
Serial.println("Failed to play track?");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void setup()
|
||
|
{
|
||
|
pinMode(BUILTIN_LED, OUTPUT);
|
||
|
digitalWrite(BUILTIN_LED, ledState); // turn *on* led
|
||
|
pinMode(SFX_ACT, INPUT);
|
||
|
|
||
|
Serial.begin(115200);
|
||
|
Serial1.begin(9600);
|
||
|
|
||
|
if (!sfx.reset()) {
|
||
|
Serial.println("Not found");
|
||
|
while (1);
|
||
|
}
|
||
|
Serial.println("SFX board found");
|
||
|
//scanNetworks();
|
||
|
|
||
|
// Configures static IP address
|
||
|
if (!WiFi.config(local_IP, gateway, subnet)) {
|
||
|
Serial.println("STA Failed to configure");
|
||
|
}
|
||
|
|
||
|
WiFi.setSleep(false); // IMPORTANTE PARA QUE EL OSC VAYA FINO!
|
||
|
|
||
|
Serial.println();
|
||
|
Serial.println();
|
||
|
Serial.print("Connecting to ");
|
||
|
Serial.println(ssid);
|
||
|
WiFi.begin(ssid, pass);
|
||
|
|
||
|
////////////////////////////////////////////////////////////////////////////
|
||
|
// METER AQUI UN TIMER DE SEGURIDAD PARA QUE AVANCE SI NO ENCUENTRA LA WIFI
|
||
|
////////////////////////////////////////////////////////////////////////////
|
||
|
while (WiFi.status() != WL_CONNECTED)
|
||
|
{
|
||
|
delay(500);
|
||
|
Serial.print(".");
|
||
|
}
|
||
|
Serial.println("");
|
||
|
////////////////////////////////////////////////////////////////////////////
|
||
|
|
||
|
Serial.println("WiFi connected");
|
||
|
Serial.println("IP address: ");
|
||
|
Serial.println(WiFi.localIP());
|
||
|
|
||
|
Serial.println("Starting UDP");
|
||
|
Udp.begin(localPort);
|
||
|
Serial.print("Local port: ");
|
||
|
Serial.println(localPort);
|
||
|
|
||
|
|
||
|
//radioId = 0; // MASTER
|
||
|
radio.begin();
|
||
|
radio.setDataRate( RF24_250KBPS );
|
||
|
radio.setPALevel( RF24_PA_MAX );
|
||
|
radio.setAutoAck(0);
|
||
|
radio.setPayloadSize(8);
|
||
|
radio.setChannel(76);
|
||
|
radio.openWritingPipe(pipeNode);
|
||
|
radio.printDetails();
|
||
|
printf_begin();
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void loop()
|
||
|
{
|
||
|
OSCMessage msg;
|
||
|
int size = Udp.parsePacket();
|
||
|
|
||
|
if (size > 0) {
|
||
|
while (size--) {
|
||
|
//msg.fill(Udp.read());
|
||
|
uint8_t packetBuffer[UDP_RX_PACKET_MAX_SIZE];
|
||
|
Udp.read(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
|
||
|
msg.fill(packetBuffer, UDP_RX_PACKET_MAX_SIZE);
|
||
|
}
|
||
|
if (!msg.hasError()) {
|
||
|
msg.dispatch("/1/led", led);
|
||
|
msg.dispatch("/1/track", playtrack);
|
||
|
msg.dispatch("/1/RF", radioCmd);
|
||
|
}
|
||
|
else {
|
||
|
error = msg.getError();
|
||
|
Serial.print("error: ");
|
||
|
Serial.println(error);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/*
|
||
|
////////////////////////////////////////////////////////////////////////////
|
||
|
// TEST CONTADOR
|
||
|
////////////////////////////////////////////////////////////////////////////
|
||
|
CMD[0]++;
|
||
|
if(CMD[0]>255)
|
||
|
CMD[0] = 0;
|
||
|
|
||
|
Serial.println(CMD[0]);
|
||
|
radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
|
||
|
|
||
|
delay(30);
|
||
|
*/
|
||
|
|
||
|
|
||
|
unsigned long currentMillis = millis();
|
||
|
|
||
|
if (currentMillis - previousMillis >= interval)
|
||
|
{
|
||
|
// save the last time you blinked the LED
|
||
|
previousMillis = currentMillis;
|
||
|
CMD[0] = 1; // comando escena
|
||
|
CMD[1]++;
|
||
|
if(CMD[1]>3)
|
||
|
CMD[1] = 0;
|
||
|
|
||
|
Serial.println(CMD[1]);
|
||
|
radio.write( CMD, sizeof(CMD) ); // STREAM SIN CONTROL DE LLEGADA
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|