{"id":620,"date":"2018-02-17T22:48:11","date_gmt":"2018-02-17T21:48:11","guid":{"rendered":"https:\/\/sharedinventions.com\/?p=620"},"modified":"2018-02-17T22:54:41","modified_gmt":"2018-02-17T21:54:41","slug":"esp32-with-bounded-resources","status":"publish","type":"post","link":"https:\/\/sharedinventions.com\/?p=620","title":{"rendered":"Esp32 server with bounded resources"},"content":{"rendered":"<p>If you have a web server set up in your ESP32 device, you might want to add resources like images or java-scripts, that are referenced from your web documents.<\/p>\n<p>I can imagine, that a quite few people does not know, that ESP32 (and ESP8266 as well) has a built in file storage area in the flash memory. For a web server context, the idea is that you can upload your binary content into this file area, where your web server can forward these files to the client requests.<\/p>\n<p>You can access files in this area with the SPIFFS library.<\/p>\n<p>This is an example as matters stands in February 2018.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;FS.h&gt;;\r\n#include &lt;SPIFFS.h&gt;;\r\n#include &lt;WiFi.h&gt;;\r\n#include &lt;WiFiClient.h&gt;;\r\n\r\n#define BUF_LEN 512\r\n\r\nconst char* ssid = &quot;Free WiFi&quot;;\r\nconst char* password = &quot;***for connecting***&quot;;\r\n\r\n\/\/ TCP server at port 80 will respond to HTTP requests\r\nWiFiServer server(80);\r\n\r\n\/\/ Our local FS will be the internal SPIFFS of the esp32\r\nfs::FS localFs = SPIFFS;\r\nstatic uint8_t buf&#x5B;BUF_LEN];\r\n\r\nvoid setup(void)\r\n{\r\n  Serial.begin(115200);\r\n\r\n  \/\/ Set up SPIFFS\r\n  if (!SPIFFS.begin())\r\n  {\r\n    Serial.println(&quot;SPIFFS Mount Failed&quot;);\r\n  }\r\n\r\n  setupWifi();\r\n\r\n  \/\/ Start TCP (HTTP) server\r\n  server.begin();\r\n  Serial.println(&quot;TCP server started&quot;);\r\n}\r\n\r\nvoid setupAP()\r\n{\r\n  \/\/ Set up access point\r\n  WiFi.softAP(ssid);\r\n\r\n  Serial.print(&quot;Created &quot;);\r\n  Serial.println(ssid);\r\n  Serial.print(&quot;IP address: &quot;);\r\n  Serial.println(WiFi.softAPIP());\r\n}\r\n\r\nvoid setupWifi()\r\n{\r\n  \/\/ Connect to WiFi network\r\n  WiFi.begin(ssid, password);\r\n\r\n  while (WiFi.status() != WL_CONNECTED)\r\n  {\r\n    delay(500);\r\n    Serial.print(&quot;.&quot;);\r\n  }\r\n\r\n  Serial.println();\r\n  Serial.println(&quot;WiFi connected.&quot;);\r\n  Serial.println(&quot;IP address: &quot;);\r\n  Serial.println(WiFi.localIP());\r\n}\r\n\r\nvoid loop(void)\r\n{\r\n  \/\/ Check if a client has connected\r\n  WiFiClient client = server.available();\r\n  if (!client)\r\n  {\r\n    return;\r\n  }\r\n  Serial.println(&quot;&quot;);\r\n  Serial.println(&quot;New client&quot;);\r\n\r\n  \/\/ Wait for data from client to become available\r\n  while (client.connected() &amp;amp;&amp;amp; !client.available())\r\n  {\r\n    delay(1);\r\n  }\r\n\r\n  \/\/ Read the first line of HTTP request\r\n  String req = client.readStringUntil('\\r');\r\n\r\n  \/\/ First line of HTTP request looks like &quot;GET \/path HTTP\/1.1&quot;\r\n  \/\/ Retrieve the &quot;\/path&quot; part by finding the spaces\r\n  int addr_start = req.indexOf(' ');\r\n  int addr_end = req.indexOf(' ', addr_start + 1);\r\n  if (addr_start == -1 || addr_end == -1)\r\n  {\r\n    Serial.print(&quot;Invalid request: &quot;);\r\n    Serial.println(req);\r\n    return;\r\n  }\r\n  req = req.substring(addr_start + 1, addr_end);\r\n  Serial.print(&quot;Request: &quot;);\r\n  Serial.println(req);\r\n  client.flush();\r\n\r\n  String s;\r\n  if (req == &quot;\/&quot;)\r\n  {\r\n    IPAddress ip = WiFi.softAPIP();\r\n    String ipStr = String(ip&#x5B;0]) + '.' + String(ip&#x5B;1]) + '.' + String(ip&#x5B;2]) + '.' + String(ip&#x5B;3]);\r\n    s = &quot;HTTP\/1.1 200 OK\\r\\n&quot;;\r\n    s += &quot;Content - Type: text \/ html\\r\\n&quot;;\r\n    s += &quot;\\r\\n&quot;; \/\/ End of header block\r\n    s += &quot;&lt;!DOCTYPE HTML&gt;\\r\\n&quot;;\r\n    s += &quot;&lt;html&gt;Hello from ESP32&quot;;\r\n    s += &quot;&lt;img src='\/sample.jpg'\/&gt;&quot;;\r\n    s += &quot;&lt;\/html&gt;\\r\\n\\r\\n&quot;;\r\n    Serial.println(&quot;Sending 200&quot;);\r\n  }\r\n  else\r\n  {\r\n    Serial.print(&quot;Sending &quot;);\r\n    Serial.println(req);\r\n\r\n    \/\/ Open file from FS\r\n    File file = localFs.open(req);\r\n    if(!file || file.isDirectory()){\r\n      \/\/ File not found\r\n      Serial.println(&quot;Sending 404&quot;);\r\n      s = &quot;HTTP \/ 1.1 404 Not Found\\r\\n&quot;;\r\n      s += &quot;\\r\\n&quot;; \/\/ End of header block\r\n      s += &quot;Resource not found\\r\\n&quot;;\r\n    }\r\n    else\r\n    {\r\n      int toSend = file.size();\r\n      Serial.print(&quot;Reading bytes &quot;);\r\n      Serial.println(toSend);\r\n      client.println(&quot;HTTP \/ 1.1 200 OK&quot;);\r\n      \/\/ TODO: Handle content type\r\n\/\/      client.println(&quot;Content - Type: image \/ jpeg&quot;);\r\n      client.print(&quot;Content - Length: &quot;);\r\n      client.println(toSend);\r\n      client.println(); \/\/ End of header block\r\n      \/\/ Copy file content to client via buffer\r\n      while(toSend &gt; 0)\r\n      {\r\n        size_t len = toSend &gt; BUF_LEN ? BUF_LEN : toSend;\r\n        file.read(buf, len);\r\n        client.write(buf, len);\r\n        toSend -= len;\r\n        Serial.print(&quot;.&quot;);\r\n      }\r\n      file.close();\r\n      s = &quot;&quot;; \/\/ Request already served no more lines to send.\r\n    }\r\n  }\r\n\r\n  \/\/ Send content to client\r\n  client.print(s);\r\n  Serial.println(&quot;Done with client&quot;);\r\n}\r\n<\/pre>\n<p>As you can see in the code we handle unidentified URLs as resources can be found on the file system.<\/p>\n<p>To upload files to this flash area you will need to use the &#8220;Arduino ESP32 filesystem uploader&#8221; from <a href=\"https:\/\/github.com\/me-no-dev\/arduino-esp32fs-plugin\">https:\/\/github.com\/me-no-dev\/arduino-esp32fs-plugin<\/a><\/p>\n<p><a href=\"https:\/\/sharedinventions.com\/?attachment_id=627\" rel=\"attachment wp-att-627\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-627\" src=\"https:\/\/sharedinventions.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-17-at-22.40.27.png\" alt=\"\" width=\"1056\" height=\"590\" \/><\/a><\/p>\n<p>The idea is that you have &#8220;data&#8221; folder inside your current sketch folder, and you can upload all files in this folder with the tool.<\/p>\n<p><a href=\"https:\/\/sharedinventions.com\/?attachment_id=626\" rel=\"attachment wp-att-626\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-626\" src=\"https:\/\/sharedinventions.com\/wp-content\/uploads\/2018\/02\/Screen-Shot-2018-02-17-at-22.43.20.png\" alt=\"\" width=\"802\" height=\"248\" \/><\/a><\/p>\n<p>I have personally faced with some issues around this tool, as some files was not uploaded correctly to the flash with this tool.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have a web server set up in your ESP32 device, you might want to add resources like images or java-scripts, that are referenced from your web documents. I can imagine, that a quite few people does not know, that ESP32 (and ESP8266 as well) has a built in file storage area in the\u2026 <span class=\"read-more\"><a href=\"https:\/\/sharedinventions.com\/?p=620\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":628,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,15,16],"tags":[],"class_list":["post-620","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-arduino","category-explained","category-tipps-and-tricks"],"_links":{"self":[{"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/posts\/620","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=620"}],"version-history":[{"count":11,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/posts\/620\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/posts\/620\/revisions\/634"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=\/wp\/v2\/media\/628"}],"wp:attachment":[{"href":"https:\/\/sharedinventions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=620"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=620"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sharedinventions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=620"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}