Last active
February 16, 2016 15:54
-
-
Save elbrockmeyer/8be3570901c4ebbcddf3 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 processing.net.*; | |
import java.util.Date; | |
import java.net.InetAddress; | |
String photonAccessToken = "bb8a68e41fe5760178312110e635abfebaf0f171"; // for your entire account | |
String photonDeviceID = "3d0029000a47343337373738"; // for the specific device | |
String photonIPAddress; | |
String photonSSID; | |
String photonDeviceName; | |
int port = 23; | |
Client client; | |
boolean isOnline; | |
int timeOut = 100; | |
String incomingData = ""; | |
String[] incomingPoints = new String[50]; | |
String[] cleanPoints = new String[500]; | |
String bin = ""; | |
void setup() { | |
println("Hello, World!"); | |
// setup the Processing window | |
size(1200, 800); | |
frameRate(60); | |
background(0); | |
// initialize and connect to the photon | |
initPhoton(); | |
pingAddress(); | |
connectPhoton(); | |
} | |
void initPhoton() { | |
// grabs the network info about the Photon | |
String photonInfo[] = new String[4]; | |
photonInfo = getPhotonInfo(photonDeviceID, photonAccessToken); | |
photonDeviceName = photonInfo[0]; | |
photonSSID = photonInfo[2]; | |
photonIPAddress = photonInfo[3]; | |
} | |
void pingAddress() { | |
// determines if the photon is online | |
isOnline = false; | |
try { | |
isOnline = InetAddress.getByName(photonIPAddress).isReachable(timeOut); | |
} catch (Exception E) { | |
isOnline = false; | |
} | |
} | |
void connectPhoton() { | |
// makes the TCP connection to the Photon | |
client = new Client(this, photonIPAddress, port); | |
println("Connected to " + photonDeviceName + " @ " + photonIPAddress); | |
} | |
String[] getPhotonInfo(String deviceID, String accessToken) { | |
JSONObject json; | |
String name = ""; | |
String id = ""; | |
String ipaddress = "192.168.x.x"; | |
String ssid = ""; | |
boolean connected = false; | |
String URL = ""; | |
String[] out = new String[4]; | |
try { | |
URL = "https://api.particle.io/v1/devices/" + deviceID + "/?access_token=" + accessToken; | |
json = loadJSONObject(URL); | |
name = json.getString("name"); | |
id = json.getString("id"); | |
connected = json.getBoolean("connected"); | |
println("Found " + name + "!"); | |
if (connected==true) { | |
URL = "https://api.particle.io/v1/devices/" + deviceID + "/ssid" + "/?access_token=" + accessToken; | |
json = loadJSONObject(URL); | |
ssid = json.getString("result"); | |
URL = "https://api.particle.io/v1/devices/" + deviceID + "/localip" + "/?access_token=" + accessToken; | |
json = loadJSONObject(URL); | |
ipaddress = json.getString("result"); | |
} | |
} catch(Exception E) { | |
//println("Couldn't find device"); | |
} | |
out[0] = name; | |
out[1] = id; | |
out[2] = ssid; | |
out[3] = ipaddress; | |
return out; | |
} | |
void getData() { | |
// receives the data from the Photon | |
if (isOnline == true) { | |
if (client.available() > 0) { | |
incomingData = client.readString(); | |
incomingPoints = (split(incomingData, '\n')); | |
for (int i = 0; i < incomingPoints.length; i++) { | |
bin = incomingPoints[i]; | |
bin = trim(bin); | |
cleanPoints = (split(bin, ' ')); | |
} | |
} | |
} | |
} | |
void displayData() { | |
// draws the data from the Photon | |
background(0); | |
for (int i = 0; i < cleanPoints.length; i++) { | |
if (cleanPoints[i] != null) { | |
rect(i * 4, height / 2, 4, -abs(int(cleanAllPoints[i]) * 5)); | |
} | |
} | |
} | |
void draw() { | |
background(0); | |
getData(); | |
displayData(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment