Skip to content

Commit 6b0690c

Browse files
committed
Rework MDNS code to run with WiFi101
1 parent c9fb5ea commit 6b0690c

File tree

4 files changed

+30
-55
lines changed

4 files changed

+30
-55
lines changed

examples/MDNS_WiFiWebServer/MDNS_WiFiWebServer.ino

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
MDNS WiFi Web Server
33
44
A simple web server that shows the value of the analog input pins,
5-
and exposes itself on the MDNS name 'winc1500.local'.
5+
and exposes itself on the MDNS name 'wifi101.local'.
66
77
On Linux (like Ubuntu 15.04) or OSX you can access the web page
8-
on the device in a browser at 'http://winc1500.local/'.
8+
on the device in a browser at 'http://wifi101.local/'.
99
1010
On Windows you'll first need to install the Bonjour Printer Services
1111
from:
1212
https://support.apple.com/kb/dl999?locale=en_US
13-
Then you can access the device in a browser at 'http://winc1500.local/'.
13+
Then you can access the device in a browser at 'http://wifi101.local/'.
1414
1515
This example is written for a network using WPA encryption. For
1616
WEP or WPA, change the Wifi.begin() call accordingly.
@@ -29,51 +29,26 @@
2929
*/
3030

3131
#include <SPI.h>
32-
#include <Adafruit_WINC1500.h>
33-
#include <Adafruit_WINC1500MDNS.h>
34-
35-
// Define the MDNS name that the board will respond to:
36-
#define MDNS_NAME "winc1500"
37-
// Note that the actual MDNS name will have '.local' after
38-
// the name above, so "winc1500" will be accessible on
39-
// the MDNS name "winc1500.local".
40-
41-
// Define the WINC1500 board connections below.
42-
// If you're following the Adafruit WINC1500 board
43-
// guide you don't need to modify these:
44-
#define WINC_CS 8
45-
#define WINC_IRQ 7
46-
#define WINC_RST 4
47-
#define WINC_EN 2 // or, tie EN to VCC and comment this out
48-
// The SPI pins of the WINC1500 (SCK, MOSI, MISO) should be
49-
// connected to the hardware SPI port of the Arduino.
50-
// On an Uno or compatible these are SCK = #13, MISO = #12, MOSI = #11.
51-
// On an Arduino Zero use the 6-pin ICSP header, see:
52-
// https://www.arduino.cc/en/Reference/SPI
53-
54-
// Setup the WINC1500 connection with the pins above and the default hardware SPI.
55-
Adafruit_WINC1500 WiFi(WINC_CS, WINC_IRQ, WINC_RST);
56-
57-
// Or just use hardware SPI (SCK/MOSI/MISO) and defaults, SS -> #10, INT -> #7, RST -> #5, EN -> 3-5V
58-
//Adafruit_WINC1500 WiFi;
32+
#include <WiFi101.h>
33+
#include <WifiMdns.h>
5934

6035
char ssid[] = "yourNetwork"; // your network SSID (name)
6136
char pass[] = "secretPassword"; // your network password
6237
int keyIndex = 0; // your network key Index number (needed only for WEP)
6338

39+
char mdnsName[] = "wifi101"; // the MDNS name that the board will respond to
40+
// Note that the actual MDNS name will have '.local' after
41+
// the name above, so "wifi101" will be accessible on
42+
// the MDNS name "wifi101.local".
43+
6444
int status = WL_IDLE_STATUS;
6545

6646
// Create a MDNS responder to listen and respond to MDNS name requests.
67-
MDNSResponder mdns(&WiFi); // Need to pass in a reference to the WiFi class above.
47+
MDNSResponder mdns;
6848

69-
Adafruit_WINC1500Server server(80);
49+
WiFiServer server(80);
7050

7151
void setup() {
72-
#ifdef WINC_EN
73-
pinMode(WINC_EN, OUTPUT);
74-
digitalWrite(WINC_EN, HIGH);
75-
#endif
76-
7752
//Initialize serial and wait for port to open:
7853
Serial.begin(9600);
7954
while (!Serial) {
@@ -105,13 +80,13 @@ void setup() {
10580
// Setup the MDNS responder to listen to the configured name.
10681
// NOTE: You _must_ call this _after_ connecting to the WiFi network and
10782
// being assigned an IP address.
108-
if (!mdns.begin(MDNS_NAME)) {
83+
if (!mdns.begin(mdnsName)) {
10984
Serial.println("Failed to start MDNS responder!");
11085
while(1);
11186
}
11287

11388
Serial.print("Server listening at http://");
114-
Serial.print(MDNS_NAME);
89+
Serial.print(mdnsName);
11590
Serial.println(".local/");
11691
}
11792

@@ -122,7 +97,7 @@ void loop() {
12297
mdns.update();
12398

12499
// listen for incoming clients
125-
Adafruit_WINC1500Client client = server.available();
100+
WiFiClient client = server.available();
126101
if (client) {
127102
Serial.println("new client");
128103
// an http request ends with a blank line

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ getSocket KEYWORD2
4040
WiFiClient KEYWORD2
4141
WiFiServer KEYWORD2
4242
WiFiSSLClient KEYWORD2
43+
WifiMdns KEYWORD2
4344

4445
#######################################
4546
# Constants (LITERAL1)

src/Adafruit_WINC1500MDNS.cpp renamed to src/WiFiMdns.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
// You should have received a copy of the GNU Lesser General Public
2121
// License along with this library; if not, write to the Free Software
2222
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
2324
#include "Arduino.h"
24-
#include "Adafruit_WINC1500MDNS.h"
25+
#include "WiFiMdns.h"
2526

2627
// Important RFC's for reference:
2728
// - DNS request and response: http://www.ietf.org/rfc/rfc1035.txt
@@ -36,14 +37,13 @@
3637
#define IP_OFFSET 10
3738

3839

39-
MDNSResponder::MDNSResponder(Adafruit_WINC1500* wifi)
40+
MDNSResponder::MDNSResponder()
4041
: _expected(NULL)
4142
, _expectedLen(0)
43+
, _index(0)
4244
, _response(NULL)
4345
, _responseLen(0)
44-
, _index(0)
4546
, _mdnsSocket()
46-
, _wifi(wifi)
4747
{ }
4848

4949
MDNSResponder::~MDNSResponder() {
@@ -74,7 +74,7 @@ bool MDNSResponder::begin(const char* domain, uint32_t ttlSeconds)
7474
}
7575
_expected[0] = (uint8_t)n;
7676
// Copy in domain characters as lowercase
77-
for (int i = 0; i < n; ++i) {
77+
for (unsigned int i = 0; i < n; ++i) {
7878
_expected[1+i] = tolower(domain[i]);
7979
}
8080
// Values for:
@@ -134,15 +134,15 @@ bool MDNSResponder::begin(const char* domain, uint32_t ttlSeconds)
134134
memcpy(records + TTL_OFFSET, ttl, 4);
135135
memcpy(records + A_RECORD_SIZE + 2 + TTL_OFFSET, ttl, 4);
136136
// Add IP address to response
137-
uint32_t ipAddress = _wifi->localIP();
137+
uint32_t ipAddress = WiFi.localIP();
138138
records[IP_OFFSET] = (uint8_t) ipAddress;
139139
records[IP_OFFSET + 1] = (uint8_t)(ipAddress >> 8);
140140
records[IP_OFFSET + 2] = (uint8_t)(ipAddress >> 16);
141141
records[IP_OFFSET + 3] = (uint8_t)(ipAddress >> 24);
142142

143143
// Open the MDNS UDP listening socket on port 5353 with multicast address
144144
// 224.0.0.251 (0xE00000FB)
145-
if (!_mdnsSocket.begin(5353, 0xE00000FB)) {
145+
if (!_mdnsSocket.beginMulti(IPAddress(224, 0, 0, 251), 5353)) {
146146
return false;
147147
}
148148

src/Adafruit_WINC1500MDNS.h renamed to src/WiFiMdns.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,16 @@
2020
// You should have received a copy of the GNU Lesser General Public
2121
// License along with this library; if not, write to the Free Software
2222
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23-
#ifndef ADAFRUIT_WINC1500MDNS_H
24-
#define ADAFRUIT_WINC1500MDNS_H
2523

26-
#include "Adafruit_WINC1500.h"
27-
#include "Adafruit_WINC1500Udp.h"
24+
#ifndef WIFIMDNS_H
25+
#define WIFIMDNS_H
26+
27+
#include "WiFi101.h"
28+
#include "WiFiUdp.h"
2829

2930
class MDNSResponder {
3031
public:
31-
MDNSResponder(Adafruit_WINC1500* wifi);
32+
MDNSResponder();
3233
~MDNSResponder();
3334
bool begin(const char* domain, uint32_t ttlSeconds = 3600);
3435
void update();
@@ -46,9 +47,7 @@ class MDNSResponder {
4647
uint8_t* _response;
4748
int _responseLen;
4849
// UDP socket for receiving/sending MDNS data.
49-
Adafruit_WINC1500UDP _mdnsSocket;
50-
// Reference to WINC1500 wifi object, used to get IP address.
51-
Adafruit_WINC1500* _wifi;
50+
WiFiUDP _mdnsSocket;
5251
};
5352

5453
#endif

0 commit comments

Comments
 (0)