Add Ota and mdns (#257)

* Add Sketch Update Library

* Add MDNS Library

* Add Arduino OTA Library

* add missing library file

* Add library files for Update

* Add missing headers

* fix ota command

* Add espota binary

* remove bad example

* PlatformIO does not auto forward declare methods like Arduino Builder
This commit is contained in:
Me No Dev
2017-03-11 07:15:44 +01:00
committed by GitHub
parent 2f5efed220
commit fa1716e73e
27 changed files with 2428 additions and 91 deletions

View File

@ -0,0 +1,80 @@
/*
ESP8266 mDNS-SD responder and query sample
This is an example of announcing and finding services.
Instructions:
- Update WiFi SSID and password as necessary.
- Flash the sketch to two ESP8266 boards
- The last one powered on should now find the other.
*/
#include <WiFi.h>
#include <ESPmDNS.h>
const char* ssid = "...";
const char* password = "...";
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
if (!MDNS.begin("ESP32_Browser")) {
Serial.println("Error setting up MDNS responder!");
while(1){
delay(1000);
}
}
}
void loop() {
browseService("http", "tcp");
delay(1000);
browseService("arduino", "tcp");
delay(1000);
browseService("workstation", "tcp");
delay(1000);
browseService("smb", "tcp");
delay(1000);
browseService("afpovertcp", "tcp");
delay(1000);
browseService("ftp", "tcp");
delay(1000);
browseService("ipp", "tcp");
delay(1000);
browseService("printer", "tcp");
delay(10000);
}
void browseService(const char * service, const char * proto){
Serial.printf("Browsing for service _%s._%s.local. ... ", service, proto);
int n = MDNS.queryService(service, proto);
if (n == 0) {
Serial.println("no services found");
} else {
Serial.print(n);
Serial.println(" service(s) found");
for (int i = 0; i < n; ++i) {
// Print details for each service found
Serial.print(" ");
Serial.print(i + 1);
Serial.print(": ");
Serial.print(MDNS.hostname(i));
Serial.print(" (");
Serial.print(MDNS.IP(i));
Serial.print(":");
Serial.print(MDNS.port(i));
Serial.println(")");
}
}
Serial.println();
}

View File

@ -0,0 +1,120 @@
/*
ESP32 mDNS responder sample
This is an example of an HTTP server that is accessible
via http://esp32.local URL thanks to mDNS responder.
Instructions:
- Update WiFi SSID and password as necessary.
- Flash the sketch to the ESP32 board
- Install host software:
- For Linux, install Avahi (http://avahi.org/).
- For Windows, install Bonjour (http://www.apple.com/support/bonjour/).
- For Mac OSX and iOS support is built in through Bonjour already.
- Point your browser to http://esp32.local, you should see a response.
*/
#include <WiFi.h>
#include <ESPmDNS.h>
#include <WiFiClient.h>
const char* ssid = "............";
const char* password = "..............";
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);
void setup(void)
{
Serial.begin(115200);
// Connect to WiFi network
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
// Set up mDNS responder:
// - first argument is the domain name, in this example
// the fully-qualified domain name is "esp8266.local"
// - second argument is the IP address to advertise
// we send our IP address on the WiFi network
if (!MDNS.begin("esp32")) {
Serial.println("Error setting up MDNS responder!");
while(1) {
delay(1000);
}
}
Serial.println("mDNS responder started");
// Start TCP (HTTP) server
server.begin();
Serial.println("TCP server started");
// Add service to MDNS-SD
MDNS.addService("http", "tcp", 80);
}
void loop(void)
{
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
Serial.println("");
Serial.println("New client");
// Wait for data from client to become available
while(client.connected() && !client.available()){
delay(1);
}
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
Serial.print("Invalid request: ");
Serial.println(req);
return;
}
req = req.substring(addr_start + 1, addr_end);
Serial.print("Request: ");
Serial.println(req);
client.flush();
String s;
if (req == "/")
{
IPAddress ip = WiFi.localIP();
String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>Hello from ESP32 at ";
s += ipStr;
s += "</html>\r\n\r\n";
Serial.println("Sending 200");
}
else
{
s = "HTTP/1.1 404 Not Found\r\n\r\n";
Serial.println("Sending 404");
}
client.print(s);
Serial.println("Done with client");
}

View File

@ -0,0 +1,25 @@
#######################################
# Syntax Coloring Map For Ultrasound
#######################################
#######################################
# Datatypes (KEYWORD1)
#######################################
ESPmDNS KEYWORD1
MDNS KEYWORD1
#######################################
# Methods and Functions (KEYWORD2)
#######################################
begin KEYWORD2
end KEYWORD2
addService KEYWORD2
enableArduino KEYWORD2
disableArduino KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

View File

@ -0,0 +1,9 @@
name=ESPmDNS
version=1.0
author=Hristo Gochkov, Ivan Grokhtkov
maintainer=Hristo Gochkov <hristo@espressif.com>
sentence=ESP32 mDNS Library
paragraph=
category=Communication
url=
architectures=esp32

View File

@ -0,0 +1,187 @@
/*
ESP8266 Multicast DNS (port of CC3000 Multicast DNS library)
Version 1.1
Copyright (c) 2013 Tony DiCola (tony@tonydicola.com)
ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com)
MDNS-SD Suport 2015 Hristo Gochkov (hristo@espressif.com)
Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com)
License (MIT license):
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.
*/
// Important RFC's for reference:
// - DNS request and response: http://www.ietf.org/rfc/rfc1035.txt
// - Multicast DNS: http://www.ietf.org/rfc/rfc6762.txt
// - MDNS-SD: https://tools.ietf.org/html/rfc6763
#ifndef LWIP_OPEN_SRC
#define LWIP_OPEN_SRC
#endif
#include "ESPmDNS.h"
#include <functional>
#include "esp_wifi.h"
MDNSResponder::MDNSResponder() : mdns(NULL), _if(TCPIP_ADAPTER_IF_STA) {}
MDNSResponder::~MDNSResponder() {
end();
}
bool MDNSResponder::begin(const char* hostName, tcpip_adapter_if_t tcpip_if, uint32_t ttl){
_if = tcpip_if;
if(!mdns && mdns_init(_if, &mdns)){
log_e("Failed starting MDNS");
return false;
}
_hostname = hostName;
if(mdns_set_hostname(mdns, hostName)) {
log_e("Failed setting MDNS hostname");
return false;
}
return true;
}
void MDNSResponder::end() {
if(!mdns){
return;
}
mdns_free(mdns);
mdns = NULL;
}
void MDNSResponder::setInstanceName(String name) {
if (name.length() > 63) return;
if(mdns_set_instance(mdns, name.c_str())){
log_e("Failed setting MDNS instance");
return;
}
}
void MDNSResponder::enableArduino(uint16_t port, bool auth){
const char * arduTxtData[4] = {
"board=" ARDUINO_BOARD,
"tcp_check=no",
"ssh_upload=no",
"auth_upload=no"
};
if(auth){
arduTxtData[3] = "auth_upload=yes";
}
if(mdns_service_add(mdns, "_arduino", "_tcp", port)) {
log_e("Failed adding Arduino service");
} else if(mdns_service_txt_set(mdns, "_arduino", "_tcp", 4, arduTxtData)) {
log_e("Failed setting Arduino service TXT");
}
}
void MDNSResponder::disableArduino(){
if(mdns_service_remove(mdns, "_arduino", "_tcp")) {
log_w("Failed removing Arduino service");
}
}
void MDNSResponder::enableWorkstation(){
char winstance[21+_hostname.length()];
uint8_t mac[6];
esp_wifi_get_mac((wifi_interface_t)_if, mac);
sprintf(winstance, "%s [%02x:%02x:%02x:%02x:%02x:%02x]", _hostname.c_str(), mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
if(mdns_service_add(mdns, "_workstation", "_tcp", 9)) {
log_e("Failed adding Workstation service");
} else if(mdns_service_instance_set(mdns, "_workstation", "_tcp", winstance)) {
log_e("Failed setting Workstation service instance name");
}
}
void MDNSResponder::disableWorkstation(){
if(mdns_service_remove(mdns, "_workstation", "_tcp")) {
log_w("Failed removing Workstation service");
}
}
void MDNSResponder::addService(char *name, char *proto, uint16_t port){
if(mdns_service_add(mdns, name, proto, port)) {
log_e("Failed adding service %s.%s.\n", name, proto);
}
}
bool MDNSResponder::addServiceTxt(char *name, char *proto, char *key, char *value){
//ToDo: implement it in IDF. This will set the TXT to one record currently
String txt = String(key) + "=" + String(value);
const char * txt_chr[1] = {txt.c_str()};
if(mdns_service_txt_set(mdns, name, proto, 1, txt_chr)) {
log_e("Failed setting service TXT");
return false;
}
return true;
}
int MDNSResponder::queryService(char *service, char *proto) {
mdns_result_free(mdns);
if(proto){
char srv[strlen(service)+2];
char prt[strlen(proto)+2];
sprintf(srv, "_%s", service);
sprintf(prt, "_%s", proto);
return mdns_query(mdns, srv, prt, 2000);
}
return mdns_query(mdns, service, NULL, 2000);
}
IPAddress MDNSResponder::queryHost(char *host){
mdns_result_free(mdns);
if(!mdns_query(mdns, host, NULL, 2000)){
return IPAddress();
}
return IP(0);
}
String MDNSResponder::hostname(int idx) {
const mdns_result_t * result = mdns_result_get(mdns, idx);
if(!result){
log_e("Result %d not found", idx);
return String();
}
return String(result->host);
}
IPAddress MDNSResponder::IP(int idx) {
const mdns_result_t * result = mdns_result_get(mdns, idx);
if(!result){
log_e("Result %d not found", idx);
return IPAddress();
}
return IPAddress(result->addr.addr);
}
uint16_t MDNSResponder::port(int idx) {
const mdns_result_t * result = mdns_result_get(mdns, idx);
if(!result){
log_e("Result %d not found", idx);
return 0;
}
return result->port;
}
MDNSResponder MDNS;

View File

@ -0,0 +1,117 @@
/*
ESP8266 Multicast DNS (port of CC3000 Multicast DNS library)
Version 1.1
Copyright (c) 2013 Tony DiCola (tony@tonydicola.com)
ESP8266 port (c) 2015 Ivan Grokhotkov (ivan@esp8266.com)
MDNS-SD Suport 2015 Hristo Gochkov (hristo@espressif.com)
Extended MDNS-SD support 2016 Lars Englund (lars.englund@gmail.com)
Rewritten for ESP32 by Hristo Gochkov (hristo@espressif.com)
This is a simple implementation of multicast DNS query support for an Arduino
running on ESP32 chip.
Usage:
- Include the ESP32 Multicast DNS library in the sketch.
- Call the begin method in the sketch's setup and provide a domain name (without
the '.local' suffix, i.e. just provide 'foo' to resolve 'foo.local'), and the
Adafruit CC3000 class instance. Optionally provide a time to live (in seconds)
for the DNS record--the default is 1 hour.
- Call the update method in each iteration of the sketch's loop function.
License (MIT license):
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.
*/
#ifndef ESP32MDNS_H
#define ESP32MDNS_H
#include "Arduino.h"
#include "mdns.h"
//this should be defined at build time
#ifndef ARDUINO_BOARD
#define ARDUINO_BOARD "esp32"
#endif
class MDNSResponder {
public:
MDNSResponder();
~MDNSResponder();
bool begin(const char* hostName, tcpip_adapter_if_t tcpip_if=TCPIP_ADAPTER_IF_STA, uint32_t ttl=120);
void end();
void setInstanceName(String name);
void setInstanceName(const char * name){
setInstanceName(String(name));
}
void setInstanceName(char * name){
setInstanceName(String(name));
}
void addService(char *service, char *proto, uint16_t port);
void addService(const char *service, const char *proto, uint16_t port){
addService((char *)service, (char *)proto, port);
}
void addService(String service, String proto, uint16_t port){
addService(service.c_str(), proto.c_str(), port);
}
bool addServiceTxt(char *name, char *proto, char * key, char * value);
void addServiceTxt(const char *name, const char *proto, const char *key,const char * value){
addServiceTxt((char *)name, (char *)proto, (char *)key, (char *)value);
}
void addServiceTxt(String name, String proto, String key, String value){
addServiceTxt(name.c_str(), proto.c_str(), key.c_str(), value.c_str());
}
void enableArduino(uint16_t port=3232, bool auth=false);
void disableArduino();
void enableWorkstation();
void disableWorkstation();
IPAddress queryHost(char *host);
IPAddress queryHost(const char *host){
return queryHost((char *)host);
}
IPAddress queryHost(String host){
return queryHost(host.c_str());
}
int queryService(char *service, char *proto);
int queryService(const char *service, const char *proto){
return queryService((char *)service, (char *)proto);
}
int queryService(String service, String proto){
return queryService(service.c_str(), proto.c_str());
}
String hostname(int idx);
IPAddress IP(int idx);
uint16_t port(int idx);
private:
mdns_server_t * mdns;
tcpip_adapter_if_t _if;
String _hostname;
};
extern MDNSResponder MDNS;
#endif //ESP32MDNS_H