Skip to content

Instantly share code, notes, and snippets.

@mdobson
Last active August 29, 2015 13:56
Show Gist options
  • Save mdobson/9076492 to your computer and use it in GitHub Desktop.
Save mdobson/9076492 to your computer and use it in GitHub Desktop.
Arduino Yun + Bonjour Service Discovery
/*********************
Elroy Service. Basic.
We'll be discovering where this is on the network with bonjour/mdns.
Then we'll interact with it.
Posting to the /on endpoint will turn pin 13 on.
Posting to the /off endpoint or any other endpoint will turn pin 13 off.
*********************/
#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>
YunServer server;
//Setup our yun server.
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
digitalWrite(13,LOW);
Bridge.begin();
digitalWrite(13, HIGH);
server.listenOnLocalhost();
server.begin();
}
void loop() {
// put your main code here, to run repeatedly:
YunClient client = server.accept();
if(client) {
process(client);
client.stop();
}
delay(50);
}
void process(YunClient client) {
String command = client.readStringUntil('/');
if(strcmp(command.substring(0, command.length() - 2).c_str(), "on") == 0) {
on(client);
}
if( strcmp(command.substring(0, command.length() - 2).c_str(), "off") == 0){
off(client);
}
}
void on(YunClient client) {
client.println(F("Turning on LED"));
digitalWrite(13, HIGH);
}
void off(YunClient client) {
client.println(F("Turning off LED"));
digitalWrite(13, LOW);
}
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
<name replace-wildcards="yes">%h</name>
<service>
<type>_elroy._tcp</type>
<port>5555</port>
<txt-record>elroy=awesome</txt-record>
<txt-record>elroy_version=0.1</txt-record>
</service>
</service-group>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment