forked from PaulStoffregen/Ethernet
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
-Add WebServerMDNS example -Update keywords
- Loading branch information
Showing
6 changed files
with
202 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "NativeEthernet.h" | ||
|
||
uint8_t mac[6]; | ||
void teensyMAC(uint8_t *mac) { | ||
for(uint8_t by=0; by<2; by++) mac[by]=(HW_OCOTP_MAC1 >> ((1-by)*8)) & 0xFF; | ||
for(uint8_t by=0; by<4; by++) mac[by+2]=(HW_OCOTP_MAC0 >> ((3-by)*8)) & 0xFF; | ||
Serial.printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); | ||
} | ||
|
||
EthernetServer server(80); | ||
|
||
void setup() { | ||
// put your setup code here, to run once: | ||
teensyMAC(mac); | ||
Ethernet.begin(mac); | ||
server.begin(); | ||
MDNS.begin("Teensy41"); | ||
MDNS.addService("_http._tcp", 80); | ||
Serial.print("IP address:"); | ||
Serial.println(Ethernet.localIP()); | ||
} | ||
|
||
void loop() { | ||
// listen for incoming clients | ||
EthernetClient client = server.available(); | ||
if (client) { | ||
Serial.println("new client"); | ||
// an http request ends with a blank line | ||
boolean currentLineIsBlank = true; | ||
while (client.connected()) { | ||
if (client.available()) { | ||
char c = client.read(); | ||
Serial.write(c); | ||
// if you've gotten to the end of the line (received a newline | ||
// character) and the line is blank, the http request has ended, | ||
// so you can send a reply | ||
if (c == '\n' && currentLineIsBlank) { | ||
// send a standard http response header | ||
client.println("HTTP/1.1 200 OK"); | ||
client.println("Content-Type: text/html"); | ||
client.println("Connection: close"); // the connection will be closed after completion of the response | ||
client.println("Refresh: 5"); // refresh the page automatically every 5 sec | ||
client.println(); | ||
client.println("<!DOCTYPE HTML>"); | ||
client.println("<html>"); | ||
// output the value of each analog input pin | ||
for (int analogChannel = 0; analogChannel < 6; analogChannel++) { | ||
int sensorReading = analogRead(analogChannel); | ||
client.print("analog input "); | ||
client.print(analogChannel); | ||
client.print(" is "); | ||
client.print(sensorReading); | ||
client.println("<br />"); | ||
} | ||
client.println("</html>"); | ||
break; | ||
} | ||
if (c == '\n') { | ||
// you're starting a new line | ||
currentLineIsBlank = true; | ||
} else if (c != '\r') { | ||
// you've gotten a character on the current line | ||
currentLineIsBlank = false; | ||
} | ||
} | ||
} | ||
// give the web browser time to receive the data | ||
delay(1); | ||
// close the connection: | ||
client.stop(); | ||
Serial.println("client disconnected"); | ||
} | ||
} |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* Copyright 2020 Tino Hernandez (vjmuzik) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
* software and associated documentation files (the "Software"), to deal in the Software | ||
* without restriction, including without limitation the rights to use, copy, modify, | ||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following | ||
* conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <Arduino.h> | ||
#include "NativeEthernet.h" | ||
#include "NativeMdns.h" | ||
|
||
void EthernetMDNS::begin(const char* host_name, uint8_t num_services){ | ||
fnet_mdns_params_t mdns_params = { | ||
.netif_desc = fnet_netif_get_default(), | ||
.addr_family = AF_INET, | ||
.rr_ttl = FNET_CFG_MDNS_RR_TTL, | ||
.name = host_name | ||
}; | ||
mdns_desc = fnet_mdns_init(&mdns_params); | ||
if(mdns_desc == 0){ | ||
Serial.println("Failed to begin MDNS"); | ||
return; | ||
} | ||
fnet_mdns_service_t mdns_service; | ||
mdns_service.name = (char*)host_name; | ||
fnet_mdns_service_update_name(mdns_desc, &mdns_service); | ||
service_desc = new fnet_mdns_service_desc_t[num_services]; | ||
num_service_desc = num_services; | ||
for(uint8_t i = 0; i < num_service_desc; i++){ | ||
service_desc[i] = 0; | ||
} | ||
} | ||
|
||
void EthernetMDNS::addService(const char* service_type, uint16_t service_port, const fnet_mdns_txt_key_t* (*service_get_txt)(void)){ | ||
fnet_mdns_service_t mdns_service; | ||
mdns_service.service_type = service_type; | ||
mdns_service.service_port = FNET_HTONS(service_port); | ||
mdns_service.service_get_txt = service_get_txt; | ||
for(uint8_t i = 0; i < num_service_desc; i++){ | ||
if(service_desc[i] == 0){ | ||
service_desc[i] = fnet_mdns_service_register(mdns_desc, &mdns_service); | ||
if(service_desc[i] == 0){ | ||
Serial.println("Failed to add MDNS Service"); | ||
} | ||
return; | ||
} | ||
} | ||
Serial.println("Unable to add more MDNS Services"); | ||
} | ||
|
||
void EthernetMDNS::removeService(const char* service_type){ | ||
fnet_mdns_service_desc_t srv_desc = fnet_mdns_service_get_by_type(mdns_desc, service_type); | ||
fnet_mdns_service_unregister(srv_desc); | ||
for(uint8_t i = 0; i < num_service_desc; i++){ | ||
if(service_desc[i] == srv_desc){ | ||
service_desc[i] = 0; | ||
} | ||
} | ||
} | ||
|
||
void EthernetMDNS::setServiceName(const char* service_name){ | ||
fnet_mdns_service_t mdns_service; | ||
mdns_service.name = (char*)service_name; | ||
fnet_mdns_service_update_name(mdns_desc, &mdns_service); | ||
} | ||
|
||
EthernetMDNS MDNS; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* Copyright 2020 Tino Hernandez (vjmuzik) | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
* software and associated documentation files (the "Software"), to deal in the Software | ||
* without restriction, including without limitation the rights to use, copy, modify, | ||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to the following | ||
* conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include "NativeEthernet.h" |