Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Last active December 9, 2021 14:44
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/eea7912fb711ab7704e506ea35381064 to your computer and use it in GitHub Desktop.
Save dropmeaword/eea7912fb711ab7704e506ea35381064 to your computer and use it in GitHub Desktop.
// //////////////////////////////////////////////////////////////////////////
// this sketch reads out three sensors and sends out the reading of all three
// as OSC messages
// //////////////////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <easywifi.h>
#include <WiFiUdp.h>
#include <OSCMessage.h>
// wifi credentials
#define WIFI_SSID "hslab_2.4GhHz"
#define WIFI_PASSWORD "enterenter"
int times_per_second = 200;
int refresh_rate = 1000 / times_per_second;
int threshold = 2000;
int reading1, reading2, reading3;
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 access point as client
if( wifi_connect_as_client(WIFI_SSID, WIFI_PASSWORD) ) {
// print debugging information
wifi_print_mode();
wifi_print_ip(); // print our 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 debug_sensor_readings() {
Serial.print( reading1 );
Serial.print(", ");
Serial.print( reading2 );
Serial.print(", ");
Serial.print( reading3 );
Serial.print(", ");
Serial.print( threshold );
Serial.println();
}
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);
Udp.endPacket();
out.send(Udp);
out.empty();
}
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);
delay(refresh_rate); // this is our refresh rate 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