Last active
August 29, 2015 13:56
-
-
Save mdobson/9076492 to your computer and use it in GitHub Desktop.
Arduino Yun + Bonjour Service Discovery
This file contains hidden or 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
/********************* | |
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); | |
} |
This file contains hidden or 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
<?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