forked from ardyesp/ESPWebDAV
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Simple.ino
156 lines (132 loc) · 4.76 KB
/
Simple.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright (c) 2018 Gurpreet Bal https://github.com/ardyesp/ESPWebDAV
Copyright (c) 2020 David Gauchard https://github.com/d-a-v/ESPWebDAV
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/
/* Using the WebDAV server
From windows file explorer,
\\ESPWebDAV.local\
or http://ESPWebDAV.local/
or Map Network Drive -> Connect to http://ESPWebDAV.local/
subst w: \\ESPWebDAV.local/DavWWWRoot
From macOS Finder > command-K > http://ESPWebDAV.local/
(do not select anonymous to have write access)
From macOS cmdline:
mkdir -p /tmp/esp; mount_webdav -S -i -v esp8266 http://ESPWebDAV.local/ /tmp/esp && echo OK
From linux:
use mount -t davs2 http://ESPWebDAV.local/ /mnt/
use gio/gvfs/nautilus/YourFileExplorer http://ESPWebDAV.local/
When running emulation on host (script ./run), use one of these URLs instead:
http://ESPWebDAV.local:9080/
http://127.0.0.1:9080/
http://local-ip-address:9080/
subst w: \\ESPWebDAV.local@9080/DavWWWRoot
*/
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <LittleFS.h>
#include <ESPWebDAV.h>
#define HOSTNAME "ESPWebDAV"
#ifndef STASSID
#define STASSID "ssid"
#define STAPSK "psk"
#endif
//FS& gfs = SPIFFS;
FS& gfs = LittleFS;
//FS& gfs = SDFS;
//WiFiServerSecure tcp(443);
WiFiServer tcp(80);
ESPWebDAV dav;
// ------------------------
void setup()
{
// ------------------------
WiFi.persistent(false);
WiFi.hostname(HOSTNAME);
WiFi.mode(WIFI_STA);
Serial.begin(115200);
WiFi.begin(STASSID, STAPSK);
Serial.println("Connecting to " STASSID " ...");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to "); Serial.println(STASSID);
Serial.print("IP address: "); Serial.println(WiFi.localIP());
Serial.print("RSSI: "); Serial.println(WiFi.RSSI());
Serial.print("Mode: "); Serial.println(WiFi.getPhyMode());
MDNS.begin(HOSTNAME);
gfs.begin();
tcp.begin();
dav.begin(&tcp, &gfs);
dav.setTransferStatusCallback([](const char* name, int percent, bool receive)
{
Serial.printf("%s: '%s': %d%%\n", receive ? "recv" : "send", name, percent);
});
Serial.println("WebDAV server started");
}
void help()
{
Serial.printf("interactive: F/ormat D/ir C/reateFile\n");
uint32_t freeHeap;
uint16_t maxBlock;
uint8_t fragmentation;
ESP.getHeapStats(&freeHeap, &maxBlock, &fragmentation);
Serial.printf("Heap stats: free heap: %u - max block: %u - fragmentation: %u%%\n",
freeHeap, maxBlock, fragmentation);
}
// ------------------------
void loop()
{
MDNS.update();
dav.handleClient();
int c = Serial.read();
if (c > 0)
{
if (c == 'F')
{
Serial.println("formatting...");
if (gfs.format())
Serial.println("Success");
else
Serial.println("Failure");
}
else if (c == 'D')
{
Serial.printf(">>>>>>>> dir /\n");
dav.dir("/", &Serial);
Serial.printf("<<<<<<<< dir\n");
}
else if (c == 'C')
{
auto f = gfs.open("readme.md", "w");
f.printf("hello\n");
f.close();
}
else
help();
}
// ------------------------
}