Created
March 9, 2018 14:40
-
-
Save headphones81/14196461d47fabd7f496ddb280a5130b to your computer and use it in GitHub Desktop.
Multicasting spectrum bytes from MSGEQ7 to multiple clients
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Server part | |
//UDPSend | |
#include <WiFiUdp.h> | |
WiFiUDP Udp; | |
IPAddress ipMulti(239, 0, 0, 57); //Our multicast group | |
unsigned int localUdpPort = 4210; //Our port | |
char spectrumUDP[7]; //Just define it | |
//Setup part | |
Udp.begin(localUdpPort); | |
//Loop part | |
//When get given frequency from MSGEQ - just assign it to corresponding specUdp.. | |
spectrumValue[i] = analogRead(MSGEQ7_AUDIO_PIN); | |
spectrumUDP[i] = spectrumValue[i] / 4; | |
//For now you have filled all the udp array with specBytes will send them in one packet | |
// My advice is to keep some delay between packets - 50-150packets per seconds is reasonable (fps) | |
EVERY_N_MILLISECONDS(20){ | |
Udp.beginPacketMulticast(ipMulti, 4210, WiFi.localIP(),1); //1 is TTL - it is ok for local networks ! | |
Udp.write(spectrumUDP, 7); | |
//Serial.printf("%i | %i | %i | %i | %i | %i | %i \n",spectrumUDP[0],spectrumUDP[1],spectrumUDP[2],spectrumUDP[3],spectrumUDP[4],spectrumUDP[5],spectrumUDP[6]); | |
Udp.endPacket(); | |
} | |
//Receiver part | |
#include <WiFiUdp.h> | |
WiFiUDP Udp; | |
IPAddress ipMulti(239, 0, 0, 57); | |
unsigned int localUdpPort = 4210; | |
unsigned int spectrumUDP[7]; | |
#define BUFFER_LEN 1024 | |
char incomingPacket[BUFFER_LEN]; | |
//Setup part | |
Udp.beginMulticast(WiFi.localIP(), ipMulti, localUdpPort); | |
//Loop | |
void readAudio() { | |
int packetSize = Udp.parsePacket(); | |
if (packetSize) | |
{ | |
int len = Udp.read(incomingPacket, BUFFER_LEN); | |
if (len > 0) | |
{ | |
for (int i = 0; i < 7; i++) { | |
uint8_t reading=max((int)incomingPacket[i],1); | |
spectrumUDP[i] = reading; | |
spectrumValue[i] = map(spectrumUDP[i],1,255,0,1023); | |
} | |
}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment