Skip to content

Instantly share code, notes, and snippets.

@dropmeaword
Last active March 14, 2022 19:20
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/3e1921050089cd6cabbfb55adc7abcff to your computer and use it in GitHub Desktop.
Save dropmeaword/3e1921050089cd6cabbfb55adc7abcff to your computer and use it in GitHub Desktop.
// //////////////////////////////////////////////////////////////////////////
// this sketch reads out three sensors and sends out the reading of the first
// as an OSC message, as an exercise complete this sketch by sending out the
// readings of all three sensors, not just one.
// //////////////////////////////////////////////////////////////////////////
#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 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 debug_sensor_readings() {
Serial.print( reading1 );
Serial.print(", ");
Serial.print( reading2 );
Serial.print(", ");
Serial.print( reading3 );
Serial.print(", ");
Serial.print( threshold );
Serial.println();
}
void loop() {
// read the values of the three analog sensors
reading1 = analogRead(A2);
reading2 = analogRead(A3);
reading3 = analogRead(A4);
// these two lines craft the message (with an address and a parameter)
OSCMessage out("/sensor/one/luis");
out.add( reading1 );
// these 4 lines that follow send out the message
Udp.beginPacket(destination, txport);
out.send(Udp);
Udp.endPacket();
out.empty();
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