Last active
December 16, 2015 16:40
-
-
Save julioterra/5465129 to your computer and use it in GitHub Desktop.
This file contains 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
import spacebrew.*; | |
import processing.serial.*; | |
String server = "sandbox.spacebrew.cc"; | |
String name = "PhotoCell_Publisher_" + floor(random(1000)); | |
String description = "This is an example client which publishes the value of a photocell connected to an Arduino "; | |
int bright = 0; // the value of the phtocell we weill send over spacebrew | |
Spacebrew spacebrewConnection; // Spacebrew connection object | |
Serial myPort; // Serial port object | |
void setup() { | |
size(400, 200); | |
// instantiate the spacebrew object | |
spacebrewConnection = new Spacebrew( this ); | |
// add each thing you publish to | |
spacebrewConnection.addPublish( "brightness", bright ); | |
// connect to spacebrew | |
spacebrewConnection.connect(server, name, description ); | |
// print list of serial devices to console | |
println(Serial.list()); | |
myPort = new Serial(this, Serial.list()[0], 9600); // CONFIRM the port that your arduino is connect to | |
myPort.bufferUntil('\n'); | |
} | |
void draw() { | |
// set backgroun color based on brightness | |
background( bright / 4, bright / 4, bright / 4 ); | |
// if background is light then make text black | |
if (bright < 512) { fill(225, 225, 225); } | |
// otherwise make text white | |
else { fill(25, 25, 25); } | |
// set text alignment and font size | |
textAlign(CENTER); | |
textSize(16); | |
if (spacebrewConnection.connected()) { | |
// print client name to screen | |
text("Connected as: " + name, width/2, 25 ); | |
// print current bright value to screen | |
textSize(60); | |
text(bright, width/2, height/2 + 20); | |
} | |
else { | |
text("Not Connected to Spacebrew", width/2, 25 ); | |
} | |
} | |
void serialEvent (Serial myPort) { | |
// read data as an ASCII string: | |
String inString = myPort.readStringUntil('\n'); | |
if (inString != null) { | |
// trim off whitespace | |
inString = trim(inString); | |
// convert value from string to an integer | |
bright = int(inString); | |
// publish the value to spacebrew if app is connected to spacebrew | |
if (spacebrewConnection.connected()) { | |
spacebrewConnection.send( "brightness", bright ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment