Skip to content

Commit

Permalink
Added mDNS browser example
Browse files Browse the repository at this point in the history
Bumped version
  • Loading branch information
PaulMartinsen committed Apr 19, 2018
1 parent 8d7efb7 commit 63224bf
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Version.xml
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<mlp-library>
<version>1.14</version>
<version>1.15</version>
</mlp-library>
165 changes: 165 additions & 0 deletions examples/Other/mDNSExample/mDNSExample.ino
@@ -0,0 +1,165 @@
/*
mDNSExample -
Based on example Transparent UART to Telnet Server for esp8266
See: https://www.megunolink.com/documentation/connecting/mdns-browser/
for more information
Copyright (c) 2018 Number Eight Innovation Limited.
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>

// Fill in your network information
const char* ssid = "ENTER SSID";
const char* password = "ENTER PASSWORD";

// Maximum number of clients that can connect to this device
#define MAX_SRV_CLIENTS 1
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];

String MakeMine(const char *Template)
{
uint16_t uChipId = ESP.getChipId();
String Result = String(Template) + String(uChipId, HEX);
return Result;
}

void ConnectToWiFi()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

Serial.print("Connecting to "); Serial.println(ssid);

uint8_t i = 0;
while (WiFi.status() != WL_CONNECTED)
{
Serial.print('.');
delay(500);

if ((++i % 16) == 0)
{
Serial.println(F(" still trying to connect"));
}
}

Serial.print(F("Connected. My IP address is: "));
Serial.println(WiFi.localIP());
}

void AdvertiseServices()
{
String MyName = MakeMine("WiFiSensor");
if (MDNS.begin(MyName.c_str()))
{
Serial.println(F("mDNS responder started"));
Serial.print(F("My name is: "));
Serial.println(MyName.c_str());

// Add service to MDNS-SD
MDNS.addService("n8i-mlp", "tcp", 23);
}
else
{
while (1)
{
Serial.println(F("Error setting up MDNS responder"));
delay(1000);
}
}
}

void setup()
{
Serial.begin(115200);
Serial.println(F("mDNS Test"));
Serial.println(F("---------"));

ConnectToWiFi();

AdvertiseServices();

// Start the TCP server
server.begin();
server.setNoDelay(true);

Serial.println("Ready.");
}

void loop()
{
uint8_t i;
// check if there are any new clients wanting to connect
if (server.hasClient())
{
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected())
{
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
Serial.print("New client: "); Serial.print(i);
break;
}
}
//no free/disconnected spot so reject
if ( i == MAX_SRV_CLIENTS)
{
WiFiClient serverClient = server.available();
serverClient.stop();
Serial.println("Connection rejected ");
}
}

// check clients for data. Any data that is received is sent
// out the local serial port.
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i] && serverClients[i].connected())
{
if (serverClients[i].available())
{
//get data from the telnet client and push it to the UART
while (serverClients[i].available())
{
Serial.write(serverClients[i].read());
}
}
}
}

// Check UART for data. Any serial data received is sent to
// connected TCP clients.
if(Serial.available())
{
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
//push UART data to all connected telnet clients
for(i = 0; i < MAX_SRV_CLIENTS; i++)
{
if (serverClients[i] && serverClients[i].connected()){
serverClients[i].write(sbuf, len);
delay(1);
}
}
}
}
Binary file added examples/Other/mDNSExample/mDNSExample.mlpz
Binary file not shown.
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=MegunoLink
version=1.14
version=1.15
author=Number Eight Innovation
maintainer=Number Eight Innovation <support@megunolink.com>
sentence=Library for storing data in the eeprom, processing serial messages and plotting data with MegunoLink.
Expand Down

0 comments on commit 63224bf

Please sign in to comment.