Created
September 29, 2015 04:56
-
-
Save Neurogami/088fb292454f9be9fa92 to your computer and use it in GitHub Desktop.
Simple OSC lop using ESP8266 and Arduino IDE
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
#include <OSCMessage.h> | |
#include <WiFiUdp.h> | |
WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP | |
const IPAddress outIp(192,168,0,99); // remote IP of your computer | |
const unsigned int outPort = 9900; // remote port to receive OSC | |
const unsigned int localPort = 9901; // local port to listen for OSC packets (actually not used for sending) | |
#include <ESP8266WiFi.h> | |
int counter = 0; | |
const char* ssid = "YOUR_SSID"; | |
const char* password = "YOUR_SSID_PASSWORD"; | |
const char* host = "192.168.0.58"; // IP address for HTTP request | |
const int httpPort = 8090; // Port address for HTTP request | |
const char* ver = "0.5.0"; | |
WiFiClient client; | |
void keepTryingToConnect() { | |
Serial.println("**********************************************************************************"); | |
Serial.println("************ KEEP TRYING TO CONNECT **********************"); | |
Serial.println("**********************************************************************************"); | |
int connected = 0; | |
while (connected == 0 ) { | |
yield(); | |
Serial.println(WiFi.localIP()); | |
Serial.print("Wifi status: "); | |
Serial.println(WiFi.status()); | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
if (!client.connect(host, httpPort)) { | |
Serial.print("connection to " ); | |
Serial.print(host); | |
Serial.print(":" ); | |
Serial.print(httpPort); | |
Serial.println(" failed"); | |
Serial.print("client:status() = "); | |
Serial.println(client.status()); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
delay(5000); | |
} else { | |
// We now create a URI for the request | |
String url = "/ESP/data?ver="; | |
url += ver; | |
url += "¬ification=OscHacking"; | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
connected = 1; | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
keepTryingToConnect(); | |
sendNotification("started"); | |
Serial.println("Starting UDP"); | |
Udp.begin(localPort); | |
Serial.print("Local port: "); | |
Serial.println(Udp.localPort()); | |
} | |
void sendNotificationInt(int i) { | |
String url = "/ESP/data?ver="; | |
url += ver; | |
url += "¬ification="; | |
url += i; | |
url += "&milli="; | |
url += millis(); | |
if (!sendNotifcationURL(url )) { | |
keepTryingToConnect(); | |
} | |
} | |
void sendNotification(char* s) { | |
oscMessage(); | |
// We now create a URI for the request | |
String url = "/ESP/data?ver="; | |
url += ver; | |
url += "¬ification="; | |
url += s; | |
url += "&milli="; | |
url += millis(); | |
if (!sendNotifcationURL(url )) { | |
keepTryingToConnect(); | |
} | |
} | |
void oscMessage() { | |
Serial.print("oscMessage()"); | |
OSCMessage msg("/esp"); | |
int ts = millis(); // To cast it. | |
msg.add("hello, osc.").add(ts); | |
Udp.beginPacket(outIp, outPort); | |
msg.send(Udp); | |
Udp.endPacket(); | |
msg.empty(); | |
delay(50); | |
Serial.println(" is done."); | |
} | |
bool sendNotifcationURL(String url ) { | |
Serial.print("connecting to "); | |
Serial.println(host); | |
if (!client.connect(host, httpPort)) { | |
Serial.print("connection to " ); | |
Serial.print(host); | |
Serial.print(":" ); | |
Serial.print(httpPort); | |
Serial.println(" failed"); | |
return false; | |
} | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
Serial.println("--------------------"); | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println("--------------------"); | |
Serial.println("closing connection"); | |
return true; | |
} | |
void loop() { | |
delay(1000); | |
sendNotification("DEMO"); | |
oscMessage(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment