Skip to content

Instantly share code, notes, and snippets.

@ubidefeo
Created May 18, 2021 12:56
Show Gist options
  • Save ubidefeo/e77a590bbbb24a326695bc92618def69 to your computer and use it in GitHub Desktop.
Save ubidefeo/e77a590bbbb24a326695bc92618def69 to your computer and use it in GitHub Desktop.
OSC Message sender for Arduino Nano RP2040 Connect
/* This Sketch is tested on
* - Nano 33 RP2040 Connect
* - Nano 33 IoT
* - MKR WiFi 1010
To compile and use it requires
Libraries:
- CNMAT OSC Library (https://github.com/CNMAT/OSC) - Arduino Library Manager
- Arduino WiFiNINA (https://github.com/arduino-libraries/WiFiNINA) - Arduino Library Manager
- Arduino_ConnectionHandler (https://github.com/arduino-libraries/Arduino_ConnectionHandler) - Arduino Library Manager
OSC Server:
A Max/MSP patch is provided as a comment at the bottom of this Sketch
*/
#include <OSCMessage.h>
#include <Arduino_ConnectionHandler.h>
#include <WiFiNINA.h>
#include <WiFiUDP.h>
#include <OSCMessage.h>
#define ANALOG_SAMPLE_INTERVAL 10
#define ANALOG_SAMPLE_COUNT 10
unsigned int samples[ANALOG_SAMPLE_COUNT];
unsigned long lastSampleTime;
unsigned short sampleIndex = 0;
WiFiUDP Udp;
WiFiConnectionHandler conMan("My 2.4GHz WiFi Network", "My password");
//the Arduino board's IP (choose one which is on your network's pool and not reserved)
IPAddress ip(192, 168, 0, 99);
//destination IP (use the IP of the computer you have your OSC Server running)
IPAddress outIp(192, 168, 0, 33);
const unsigned int outPort = 9999;
// the MAC address is sort of arbitrary
// you can read more on MAC Physical Address online
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
bool wifiIsConnected = false;
void setup() {
Serial.begin(9600);
Udp.begin(8888);
conMan.addCallback(NetworkConnectionEvent::CONNECTED, onNetworkConnect);
conMan.addCallback(NetworkConnectionEvent::DISCONNECTED, onNetworkDisconnect);
fillSampleBuffer(0);
lastSampleTime = millis();
}
void loop() {
unsigned long msNow = millis();
conMan.check();
if(msNow - lastSampleTime > ANALOG_SAMPLE_INTERVAL){
samples[sampleIndex] = analogRead(A0);
sampleIndex++;
if(sampleIndex == ANALOG_SAMPLE_COUNT) sampleIndex = 0;
}
unsigned int potValue = getAverageSample();
Serial.println(potValue);
//the message wants an OSC address as first argument
if (wifiIsConnected) {
OSCMessage msg("/analog/0");
Serial.println("/analog/0");
msg.add((int32_t)potValue);
Udp.beginPacket(outIp, outPort);
msg.send(Udp); // send the bytes to the SLIP stream
Udp.endPacket(); // mark the end of the OSC Packet
msg.empty(); // free space occupied by message
}
}
void onNetworkConnect() {
Serial.println("Connection open");
wifiIsConnected = true;
}
void onNetworkDisconnect() {
Serial.println("Connection closed");
wifiIsConnected = false;
}
void fillSampleBuffer(unsigned int _value){
for(uint8_t s = 0; s < ANALOG_SAMPLE_COUNT; s++){
samples[s] = _value;
}
}
uint32_t getAverageSample(){
uint32_t returnValue= 0;
for(uint8_t s = 0; s < ANALOG_SAMPLE_COUNT; s++){
returnValue += samples[s];
}
return returnValue / ANALOG_SAMPLE_COUNT;
}
/* Max/MSP compressed patch
* Copy the following block from <pre> to </pre> included
* If you don't know Max/MSP but can run your own OSC Server feel free to do so
<pre><code>
----------begin_max5_patcher----------
3018.3oc6bz1ihab9y69qvBkOjHw4adl2mppJE09kH0pdpoU8CQQm7B9104.
ajwr81Fk+68YFa.CdLXVauPZBmNyxvXOyy6uN7y2e2jGx9R75IA+gfeH3t69
46u6N2P1Atq5y2MYYzWlsHZsaZSlksbYbZwjokeWQ7WJbi+OiWWD7gnhYOE7
0qiSmuNnHKXQ1rnEOkgey6d2e5a1dOoaVlsoXQbg6IRpFck8dSRe7i4wyJJ2
QflDRruXRifqUFkbZ.kPC4ZCSBDtRw4JAfCJDgZAPjLhhJ0.mpryLjD7iUO9
j4tsY1C+z6DxZ6jjzsaDvN1ub+81KS6H1HM9+fOwFHiMoqhl8Y+vKsc3Uxog
ZJQ.RMkqnTpYZ.iIBMTCQinACWR3bbPtJjfvGsF7UtBEurJt7YMIwRjJeyGR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I
QXvV.CRnlS3TM9BmWKSbvkB5BLdDCbmfQCxvRw3ZzXHE.y5lwT+bwdl4Pqko
S.IvdE.IJqIXHD.Bv19l1CtqOlUOSrILx5GL1Ey5aQD8akD80MOeAlbMcIxA
Q8xKOyMlSdGE.YKbCCQTLzt3lx1HC52JA81IOu+hPZ+EUwzMm7ZNy9BSjtPm
FBjWWVmgvYGnKtUMHKjt6KjetApwCMloTgnPnlPEBJPXFirEtAOyrufjpC5g
fSZLlQkgvw+3pw31rjqkbjikYygm8.F3G5aNy9BSxdK01hrHuyRs7AVpE5TT
KxgfIuKHulKTYRJiVs54370Uy1sFSVF8SkUNQO89xDZW9QWVOmjG+bx14Ctr
lNIJe1SIEwyJ1jWlA4uHK662I1rilmtIox+LD7v0zkraa1lWupJGttbhe+ub
++CvZPFEZ
-----------end_max5_patcher-----------
</code></pre>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment