Last active
August 29, 2015 14:00
-
-
Save chrishuan9/11396970 to your computer and use it in GitHub Desktop.
EthernetBonjour
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
#include <EthernetBonjour.h> | |
//#include <Dns.h> | |
#include <Ethernet.h> | |
//#include <EthernetClient.h> | |
//#include <EthernetServer.h> | |
//#include <util.h> | |
#include <SPI.h> | |
//Adruino Sketch to test out wifi | |
byte mac[] = { 0x90, 0xA2, 0xDDA, 0X0E, 0xF1, 0xA5}; | |
byte ip[] = { 192, 168, 1, 99}; | |
byte subnet[] = { 255, 255, 255, 0}; | |
byte dnsserver[] = { 192, 168, 1 , 1 }; | |
byte gateway[] = { 192, 168, 1, 1 }; | |
//declaration of the webserver running on port 80 | |
EthernetServer server(80); | |
//forward method defintion | |
const char* ip_to_str(const uint8_t*); | |
void nameFound(const char* name, const byte ipAddr[4]); | |
void setup(){ | |
//configuring ethernet manually avoiding bloat from dhcp | |
Ethernet.begin(mac,ip,dns,gateway); | |
server.begin(); | |
EthernetBonjour.begin("arduino"); | |
// We specify the function that the Bonjour library will call when it | |
// resolves a host name. In this case, we will call the function named | |
// "nameFound". | |
EthernetBonjour.setNameResolvedCallback(nameFound); | |
EthernetBonjour.addServiceRecord("Arduino Bonjour ._afpovertcp", | |
548, | |
MDNSServiceTCP); | |
EthernetBonjour.addServiceRecord("Arduino Bonjour Webserver Example._http", | |
80, | |
MDNSServiceTCP); | |
} | |
void loop(){ | |
// This actually runs the Bonjour module. YOU HAVE TO CALL THIS PERIODICALLY, | |
// OR NOTHING WILL WORK! | |
// Preferably, call it once per loop(). | |
EthernetBonjour.run(); | |
EthernetClient client = server.available(); | |
if (client) { | |
// an http request ends with a blank line | |
boolean current_line_is_blank = true; | |
while (client.connected()) { | |
if (client.available()) { | |
char c = client.read(); | |
// if we've gotten to the end of the line (received a newline | |
// character) and the line is blank, the http request has ended, | |
// so we can send a reply | |
if (c == '\n' && current_line_is_blank) { | |
// send a standard http response header | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: text/html"); | |
client.println(); | |
client.println("Hello from a Bonjour-enabled web-server running "); | |
client.println("on your Arduino board!"); | |
break; | |
} | |
if (c == '\n') { | |
// we're starting a new line | |
current_line_is_blank = true; | |
} else if (c != '\r') { | |
// we've gotten a character on the current line | |
current_line_is_blank = false; | |
} | |
} | |
} | |
// give the web browser time to receive the data | |
delay(1); | |
client.stop(); | |
} | |
} | |
// This function is called when a name is resolved via MDNS/Bonjour. We set | |
// this up in the setup() function above. The name you give to this callback | |
// function does not matter at all, but it must take exactly these arguments | |
// (a const char*, which is the hostName you wanted resolved, and a const | |
// byte[4], which contains the IP address of the host on success, or NULL if | |
// the name resolution timed out). | |
void nameFound(const char* name, const byte ipAddr[4]) | |
{ | |
if (NULL != ipAddr) { | |
Serial.print("The IP address for '"); | |
Serial.print(name); | |
Serial.print("' is "); | |
Serial.println(ip_to_str(ipAddr)); | |
} else { | |
Serial.print("Resolving '"); | |
Serial.print(name); | |
Serial.println("' timed out."); | |
} | |
} | |
// This is just a little utility function to format an IP address as a string. | |
const char* ip_to_str(const uint8_t* ipAddr) | |
{ | |
static char buf[16]; | |
sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]); | |
return buf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment