Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Created December 9, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dropmeaword/7c12aeda3af74fb0d51f99758fc1bb49 to your computer and use it in GitHub Desktop.
Save dropmeaword/7c12aeda3af74fb0d51f99758fc1bb49 to your computer and use it in GitHub Desktop.
// //////////////////////////////////////////////////////////////////////////
// this sketch reads out three sensors and sends their values over OSC
// it also accepts an incoming message to activate the relay
// //////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <easywifi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
#define WIFI_SSID "hslab_2.4GhHz"
#define WIFI_PASSWORD "enterenter"
int reading1, reading2, reading3;
int times_per_second = 200;
int refresh_rate = 1000 / times_per_second;
int threshold = 2000;
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
const IPAddress destination(192, 168, 50, 255);
const unsigned int rxport = 54321; // remote port to receive OSC
const unsigned int txport = 12345; // local port to listen for OSC packets (actually not used
void setup() {
// initialize serial communication
// over the USB wire back to our computer
Serial.begin( 9600 );
while(!Serial) ;
// define which pins in our sketch
// will be used as inputs and which as outputs
pinMode(A2, INPUT);
pinMode(A3, INPUT);
pinMode(A4, INPUT);
// connect to existing wifi access point as client
if( wifi_connect_as_client(WIFI_SSID, WIFI_PASSWORD) ) {
// print debugging information
wifi_print_mode();
wifi_print_ip(); // print our known ip address
} else {
Serial.print("Failed to connect to wifi ");
Serial.print( WIFI_SSID );
Serial.println();
}
Udp.begin(rxport);
Serial.print("Listening on port ");
Serial.println(rxport);
}
void osc_dispatch(String addr, int value) {
// these two lines craft the message (with an address and a parameter)
OSCMessage out( addr.c_str() );
out.add( value );
// these 4 lines that follow send out the message
Udp.beginPacket(destination, txport);
out.send(Udp);
//Udp.endPacket();
out.empty();
}
void on_relay(OSCMessage &msg, int offset) {
// Serial.println("I got the message for the relay, but I am doing nothing with it");
Serial.println( "Message received: on_relay" );
float val = msg.getFloat(0);
Serial.print( "value of message: " );
Serial.println( val );
if(val > 0.5f) {
Serial.println("RELAY ON");
digitalWrite(33, HIGH); // ON
} else {
Serial.println("RELAY OFF");
digitalWrite(33, LOW); // OFF
}
}
void osc_incoming_message_pump() {
// check if we have any incoming OSC in the queue
if( (size = Udp.parsePacket()) > 0) {
// read the entire package coming in
while( size-- ) {
in.fill( Udp.read() );
}
// check that OSC package doesn't contain any errors
if( !in.hasError() ) {
// this is the incoming message router
in.route("*", on_relay);
}
}
} // osc_incoming_message_pump()
void loop() {
OSCMessage in;
int size;
// read all of my sensor values and store them in a variable
reading1 = analogRead(A2);
reading2 = analogRead(A3);
reading3 = analogRead(A4);
// send out those readings wrapped as OSC messages onto the network
osc_dispatch("/sensor/one/luis", reading1);
osc_dispatch("/sensor/two/luis", reading2);
osc_dispatch("/sensor/three/luis", reading3);
osc_incoming_message_pump();
delay(refresh_rate); // this is our refresh rate // is in milliseconds 1 sec = 1000 ms (1000 / 200 = 5)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment