11 #ifndef OM2M_SERVER_BASE_H
12 #define OM2M_SERVER_BASE_H
18 #include <ESP8266mDNS.h>
20 #include <ESP8266HTTPUpdateServer.h>
26 #include "restHandler.h"
32 #define HEAP_TRACE_MS 5000 // define to 0 to disable
35 String mDNSName = MDNSNAME;
36 ESP8266WebServer om2msrv(8282);
38 ESP8266HTTPUpdateServer httpUpdater;
41 void user_initial_setup();
42 void user_final_setup();
50 log(
"----> NOT SERVED URI = %s\n", om2msrv.uri().c_str());
52 String uri = om2msrv.uri().c_str();
53 uri.replace(F(
"//"), F(
"/"));
54 if (uri.startsWith(String(F(
"/~/"))))
60 bool serveFile(String path)
62 if (path.endsWith(
"/"))
64 path += F(
"index.html");
66 String pathgz = path + F(
".gz");
67 if (SPIFFS.exists(pathgz))
71 if (SPIFFS.exists(path))
73 File file = SPIFFS.open(path,
"r");
74 om2msrv.streamFile(file, contentType(path));
81 #define printHttpCode(code) _printHttpCode(code, #code)
82 HTTPCode _printHttpCode(HTTPCode code,
const char* desc)
84 log(
"\n----> %s: request returned %d (%s)\n\n", desc, code, HTTPCode2Human(code).c_str());
92 Serial.printf(
"\n\nESP826/Arduino - %s\n", ESP.getFullVersion().c_str());
93 Serial.printf(
"host name = " MDNSNAME
"\n");
94 Serial.printf(
"mac address = %s\n", WiFi.macAddress().c_str());
95 Serial.printf(
"\n\n---- LOM2M server ----\n\n");
96 Serial.printf(
"Connecting to '%s'", mySSID);
97 WiFi.hostname(mDNSName);
98 WiFi.begin(mySSID, myPSK);
99 while (WiFi.status() != WL_CONNECTED)
105 log(
"\nConnected\n");
107 log(
"Try me at these addresses:");
108 log(
"(with 'telnet <addr> or 'nc -u <addr> 23')");
110 IP = WiFi.localIP().toString();
113 WiFi.macAddress(mac);
114 sprintf(ret,
"%02X%02X%02X%02X%02X%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
115 CSE_ID =
"mn-cse-" + String(ret);
116 CSE_NAME =
"mn-" + String(ret);
117 user_initial_setup();
120 log(
"IP=%s\n", IP.c_str());
122 for (
auto a : addrList)
124 log(
"IF='%s' IPv6=%d local=%d hostname='%s' addr= %s",
129 a.toString().c_str());
132 log(
" mask:%s / gw:%s",
133 a.netmask().toString().c_str(),
134 a.gw().toString().c_str());
139 MDNS.begin(mDNSName);
140 MDNS.addService(
"http",
"tcp",
"port", 8282);
143 const char* headers [] = {
"Content-Type",
"X-M2M-Origin",
"accept",
"X-M2M-RI" };
144 om2msrv.collectHeaders(headers,
sizeof headers /
sizeof headers[0]);
147 httpUpdater.setup(&om2msrv);
151 om2msrv.onNotFound([]()
153 if (!serveFile(om2msrv.uri()))
166 Millis heapLastMs = 0;
171 om2msrv.handleClient();
176 handleNotifications(3);
180 Millis nowMs = millis();
181 if (nowMs - heapLastMs > HEAP_TRACE_MS)
184 log(
"-- free heap: %d bytes", ESP.getFreeHeap());
187 #endif // HEAP_TRACE_MS
190 #endif // OM2M_SERVER_BASE_H