{"id":1504,"date":"2018-05-22T15:36:17","date_gmt":"2018-05-22T15:36:17","guid":{"rendered":"http:\/\/www.sabulo.com\/sb\/?p=1504"},"modified":"2018-05-23T07:56:44","modified_gmt":"2018-05-23T07:56:44","slug":"esp32-arduino-on-steroids","status":"publish","type":"post","link":"https:\/\/www.sabulo.com\/sb\/arduino\/esp32-arduino-on-steroids\/","title":{"rendered":"ESP32 &#8211; Arduino on Steroids"},"content":{"rendered":"<figure id=\"attachment_1505\" aria-describedby=\"caption-attachment-1505\" style=\"width: 225px\" class=\"wp-caption alignright\"><a href=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-1505\" src=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347-225x300.jpg?resize=225%2C300\" alt=\"ESP32 development board\" width=\"225\" height=\"300\" srcset=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347.jpg?resize=225%2C300&amp;ssl=1 225w, https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347.jpg?resize=768%2C1024&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347.jpg?resize=113%2C150&amp;ssl=1 113w, https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/IMG_20180515_102347.jpg?w=1536&amp;ssl=1 1536w\" sizes=\"auto, (max-width: 225px) 100vw, 225px\" \/><\/a><figcaption id=\"caption-attachment-1505\" class=\"wp-caption-text\">ESP32 development board<\/figcaption><\/figure>\n<p>Hi again! This time I thought to write up a quick one on the <a href=\"https:\/\/www.espressif.com\/sites\/default\/files\/documentation\/esp32_datasheet_en.pdf\" target=\"_blank\" rel=\"noopener\">ESP32 development board.<\/a><\/p>\n<p>I was made aware of it by my <a href=\"http:\/\/terokarvinen.com\/\" target=\"_blank\" rel=\"noopener\">esteemed colleague Tero Karvinen.<\/a>\u00a0His knowledge of the world of Arduino and related stuff is second to none. He hinted that if I ever need connectivity, I might want to look at the ESP32 and its capabilities. After all, it has<\/p>\n<ul>\n<li>WLAN<\/li>\n<li>Bluetooth Low Energy<\/li>\n<li>32-bit processor<\/li>\n<li>Real Time clock capability<\/li>\n<li>temperature, humidity, Hall effect sensors on board<\/li>\n<li>motor controller<\/li>\n<li>more programmable pins that you can dream of<\/li>\n<\/ul>\n<p>etc. etc. etc. In a word, a real beauty of a development board. And the thing is, it can be programmed with the Arduino IDE, so if you know any of that, it&#8217;s easy to get ahead with the ESP32.<\/p>\n<p>The reason I was hunting for something like this was the <a href=\"http:\/\/www.sabulo.com\/sb\/3d-printing-2\/the-morse-moai-statue-project\/\" target=\"_blank\" rel=\"noopener\">Morse Moai head<\/a>. As it now works, it has 15 sentences advertising the joys of IoT, 3D design and 3D printing, from which it picks one at random when someone approaches it close enough. I happened to think that what if I were able to update the data on it to announce my schedule? For example, if I was coming to a meeting and could not make it in time, so I could change the text to that effect.<\/p>\n<p>I happened to buy a SIM card shield for Arduino for a student project. Its implementation produced more grey hairs than I can afford, and that shield would not fit in the Moai head&#8217;s cavity either. Even with the box I printed for it to stand on, space would be at a premium. So, I dropped that idea. I also had an Ethernet shield, which is smaller than the SIM shield, but still way bigger than was ideal. And Ethernet is not WLAN &#8211; it&#8217;d need a wire to the wall. I also have a Bluetooth module, but to stand within 10 meters of the Moai for updating its message kind of defeats the purpose of remote access to it.<\/p>\n<p>So, when Tero hinted at the ESP32, I jumped at it and got <a href=\"https:\/\/www.ebay.com\/itm\/272656534488\" target=\"_blank\" rel=\"noopener\">five at USD28 from eBay<\/a>. This seemed to solve all my issues. I bypassed the obvious point that I would still need a method of getting the ESP32 to actually read or write data remotely, but as I always do, I&#8217;d cross that bridge when I got to it. When the boards arrived, I was quick to test the Bluetooth side first. I don&#8217;t know if you have checked Instructables, but it is a simply wondrous site with easy to follow instructions for just about anything short of brain surgery.<\/p>\n<p>Then again, I haven&#8217;t searched for that. But anyway.<\/p>\n<p>I found this <a href=\"http:\/\/www.instructables.com\/id\/ESP32-BLE-Android-App-Arduino-IDE-AWESOME\/\" target=\"_blank\" rel=\"noopener\">absolutely fantastic piece on how to introduce the ESP32<\/a> into the Arduino IDE. It isn&#8217;t entirely without its stumbling blocks, but with that information, I was able to create my first <a href=\"http:\/\/www.thunkable.com\" target=\"_blank\" rel=\"noopener\">Thunkable<\/a> app for my Andoird phone, and turn on and off the LED on the ESP32. All this within an hour and a half. Needless to say, I was just about bouncing with joy.<\/p>\n<p>Then I stopped to think of how to arrange the remote access to the ESP32. The easiest way seemed to be to write a little program here on www.sabulo.com, because I could use PHP on it at no extra charge. Not knowing any PHP wasn&#8217;t news. So, I though a little and came to the conclusion that I could steal snippets of code, and have four edit boxes of 16 characters written into a text file. The ESP32 can easily be sent to a website to read a file&#8217;s contents, which I had found looking at the sample files supplied with the ESP32 &#8211; IDE connection.That way, changing the text file with PHP would cause the ESP32 contents to be updated at a given interval.<\/p>\n<p>The PHP looks like this:<\/p>\n<pre>&lt;html&gt;\r\n    &lt;body&gt;\r\n        Enter the text you want&lt;br\/&gt;\r\n        the Morse Moai to speak:&lt;br\/&gt;\r\n        &lt;form name=\"form\" method=\"post\"&gt;\r\n            &lt;input type=\"text\" name=\"text_box_1\" maxlength=\"16\"\/&gt;&lt;br\/&gt;\r\n            &lt;input type=\"text\" name=\"text_box_2\" maxlength=\"16\"\/&gt;&lt;br\/&gt;\r\n            &lt;input type=\"text\" name=\"text_box_3\" maxlength=\"16\"\/&gt;&lt;br\/&gt;\r\n            &lt;input type=\"text\" name=\"text_box_4\" maxlength=\"16\"\/&gt;&lt;br\/&gt; &lt;br\/&gt;      \r\n            &lt;input type=\"submit\" id=\"search-submit\" value=\"submit\" \/&gt;\r\n        &lt;\/form&gt;\r\n        If you want to erase the &lt;br\/&gt;\r\n        previous text, just hit Submit.&lt;br\/&gt;\r\n    &lt;\/body&gt;\r\n&lt;\/html&gt;\r\n&lt;?php\r\n    if(isset($_POST['text_box_1'])) { \/\/only do file operations when appropriate\r\n        $line1 = $_POST['text_box_1'];\r\n        $line2 = $_POST['text_box_2'];\r\n        $line3 = $_POST['text_box_3'];\r\n        $line4 = $_POST['text_box_4'];\r\n        $myFile = \"moai3.txt\";\r\n        $fh = fopen($myFile, 'w') or die(\"can't open file\");\r\n        fwrite($fh, $line1);\r\n        fwrite($fh, \"\\n\");\r\n        fwrite($fh, $line2);\r\n        fwrite($fh, \"\\n\");        \r\n        fwrite($fh, $line3);\r\n        fwrite($fh, \"\\n\");        \r\n        fwrite($fh, $line4);\r\n        fclose($fh);\r\n    }\r\n?&gt;<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/Capture.jpg\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"wp-image-1507 size-full\" src=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/Capture.jpg?resize=212%2C235\" alt=\"Code in browser\" width=\"212\" height=\"235\" srcset=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/Capture.jpg?w=212&amp;ssl=1 212w, https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/uploads\/2018\/05\/Capture.jpg?resize=135%2C150&amp;ssl=1 135w\" sizes=\"auto, (max-width: 212px) 100vw, 212px\" \/><\/a><\/p>\n<p>It couldn&#8217;t be simpler really. Edit box contents are written to lines in the text file by terminating them with the \\n character. The good part is this: if I want to erase the text file contents, I just click Submit without entering any text in the edit boxes. This is the same in a browser window:<\/p>\n<p>The ESP32 needs to be given some settings so that it can actually connect to the Internet over a WLAN. Of course you need to be within coverage of a WLAN network to run this, but since the system is running at Haaga-Helia University of Applied Sciences, we have both the school network but also Eduroam, the European network for scholars. When I found the code for connecting to a WLAN, I got a working connection within seconds really. The sample code also happened to look for a file on a server, so by altering that, I was able to get the text file contents from my own server.<\/p>\n<p>I&#8217;ll show you the contents of the WLAN connection file too:<\/p>\n<p>&nbsp;<\/p>\n<pre>\/*\r\nThis example shows how to use WPA2 enterprise\r\nWritten by: Jeroen Beemster\r\n12 July 2017\r\nVersion 1.00\r\n*\/\r\n\r\n#include \"esp_wpa2.h\"\r\n#include &lt;WiFi.h&gt;\r\n\r\nconst char* ssid = \"eduroam\"; \/\/ your ssid\r\n#define EAP_ID \"heikki.hietala@haaga-helia.fi\"\r\n#define EAP_USERNAME \"heikki.hietala@haaga-helia.fi\"\r\n#define EAP_PASSWORD \"************\"\r\n\r\nvoid setup() {\r\nSerial.begin(115200);\r\ndelay(10);\r\n\r\nSerial.println();\r\nSerial.print(\"Connecting to \");\r\nSerial.println(ssid);\r\n\r\n\/\/ WPA2 enterprise magic starts here\r\nWiFi.disconnect(true);\r\nesp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));\r\nesp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));\r\nesp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));\r\nesp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();\r\nesp_wifi_sta_wpa2_ent_enable(&amp;config);\r\n\r\n\/\/ WPA2 enterprise magic ends here\r\n\r\nWiFi.begin(ssid);\r\nwhile (WiFi.status() != WL_CONNECTED) {\r\ndelay(500);\r\nSerial.print(\".\");\r\n}\r\n\r\nSerial.println(\"\");\r\nSerial.println(\"WiFi connected\");\r\nSerial.println(\"IP address: \");\r\nSerial.println(WiFi.localIP());\r\n\r\n}\r\n\r\n\/*\r\neveryhting below this, in loop(), is just a standard request to a webserver and nothing else than an example to show that is works.\r\n*\/\r\n\r\nint value = 0;\r\nconst char* host = \"www.sabulo.com\";\r\nvoid loop() {\r\ndelay(5000);\r\n++value;\r\n\r\nSerial.print(\"connecting to \");\r\nSerial.println(host);\r\n\r\nWiFiClient client;\r\nif (!client.connect(host, 80)) {\r\nSerial.println(\"connection failed\");\r\nreturn;\r\n}\r\n\/\/ We now create a URI for the request\r\nString url = \"\/moai3.txt\";\r\nSerial.print(\"Requesting URL: \");\r\nSerial.println(url);\r\n\r\n\/\/ This will send the request to the server\r\nclient.print(String(\"GET \") + url + \" HTTP\/1.1\\r\\n\" +\r\n\"Host: \" + host + \"\\r\\n\" +\r\n\"Connection: close\\r\\n\\r\\n\");\r\nunsigned long timeout = millis();\r\nwhile (client.available() == 0) {\r\nif (millis() - timeout &gt; 5000) {\r\nSerial.println(\"&gt;&gt;&gt; Client Timeout !\");\r\nclient.stop();\r\nreturn;\r\n}\r\n}\r\n\r\n\/\/ Read all the lines of the reply from server and print them to Serial\r\nwhile (client.available()) {\r\nString line = client.readStringUntil('\\r');\r\nSerial.print(line);\r\n}\r\n\r\nSerial.println();\r\nSerial.println(\"closing connection\");\r\n}<\/pre>\n<p>So, all in all, it&#8217;s just a request to get everything from the server that is in that text file. When you have the Serial Monitor on, you will see how it looks when you get the stuff back from the server. It&#8217;s a long string, in which is embedded the actual text you want. At this point I can just display it on the Serial Monitor, but the only thing that remains is to strip the required content from the file, and tell the Morse Moai to use those strings as the display and morse strings, and if there is no content, use the fifteen stored strings.<\/p>\n<p>This video shows you what I mean (sorry for its less than perfect quality, but I was a little excited to see this work with such a minimal effort):<\/p>\n<p><iframe loading=\"lazy\" title=\"ESP32 development board\" width=\"825\" height=\"464\" src=\"https:\/\/www.youtube.com\/embed\/ttI9d2_s5ac?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<p>&nbsp;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1504\" class=\"pvc_stats all  \" data-element-id=\"1504\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/i0.wp.com\/www.sabulo.com\/sb\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif?resize=16%2C16&#038;ssl=1\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Hi again! This time I thought to write up a quick one on the ESP32 development board. I was made &hellip; <a href=\"https:\/\/www.sabulo.com\/sb\/arduino\/esp32-arduino-on-steroids\/\" class=\"more-link\">More <span class=\"screen-reader-text\">ESP32 &#8211; Arduino on Steroids<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_1504\" class=\"pvc_stats all  \" data-element-id=\"1504\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.sabulo.com\/sb\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[48,64],"tags":[],"class_list":["post-1504","post","type-post","status-publish","format-standard","hentry","category-arduino","category-esp32-development-board"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6vhqE-og","_links":{"self":[{"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/posts\/1504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/comments?post=1504"}],"version-history":[{"count":5,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/posts\/1504\/revisions"}],"predecessor-version":[{"id":1511,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/posts\/1504\/revisions\/1511"}],"wp:attachment":[{"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/media?parent=1504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/categories?post=1504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sabulo.com\/sb\/wp-json\/wp\/v2\/tags?post=1504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}