ESP32 – Arduino on Steroids

ESP32 development board
ESP32 development board

Hi again! This time I thought to write up a quick one on the ESP32 development board.

I was made aware of it by my esteemed colleague Tero Karvinen. His 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

  • WLAN
  • Bluetooth Low Energy
  • 32-bit processor
  • Real Time clock capability
  • temperature, humidity, Hall effect sensors on board
  • motor controller
  • more programmable pins that you can dream of

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’s easy to get ahead with the ESP32.

The reason I was hunting for something like this was the Morse Moai head. 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.

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’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 – it’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.

So, when Tero hinted at the ESP32, I jumped at it and got five at USD28 from eBay. 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’d cross that bridge when I got to it. When the boards arrived, I was quick to test the Bluetooth side first. I don’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.

Then again, I haven’t searched for that. But anyway.

I found this absolutely fantastic piece on how to introduce the ESP32 into the Arduino IDE. It isn’t entirely without its stumbling blocks, but with that information, I was able to create my first Thunkable 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.

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’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’s contents, which I had found looking at the sample files supplied with the ESP32 – IDE connection.That way, changing the text file with PHP would cause the ESP32 contents to be updated at a given interval.

The PHP looks like this:

<html>
    <body>
        Enter the text you want<br/>
        the Morse Moai to speak:<br/>
        <form name="form" method="post">
            <input type="text" name="text_box_1" maxlength="16"/><br/>
            <input type="text" name="text_box_2" maxlength="16"/><br/>
            <input type="text" name="text_box_3" maxlength="16"/><br/>
            <input type="text" name="text_box_4" maxlength="16"/><br/> <br/>      
            <input type="submit" id="search-submit" value="submit" />
        </form>
        If you want to erase the <br/>
        previous text, just hit Submit.<br/>
    </body>
</html>
<?php
    if(isset($_POST['text_box_1'])) { //only do file operations when appropriate
        $line1 = $_POST['text_box_1'];
        $line2 = $_POST['text_box_2'];
        $line3 = $_POST['text_box_3'];
        $line4 = $_POST['text_box_4'];
        $myFile = "moai3.txt";
        $fh = fopen($myFile, 'w') or die("can't open file");
        fwrite($fh, $line1);
        fwrite($fh, "\n");
        fwrite($fh, $line2);
        fwrite($fh, "\n");        
        fwrite($fh, $line3);
        fwrite($fh, "\n");        
        fwrite($fh, $line4);
        fclose($fh);
    }
?>

Code in browser

It couldn’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:

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.

I’ll show you the contents of the WLAN connection file too:

 

/*
This example shows how to use WPA2 enterprise
Written by: Jeroen Beemster
12 July 2017
Version 1.00
*/

#include "esp_wpa2.h"
#include <WiFi.h>

const char* ssid = "eduroam"; // your ssid
#define EAP_ID "heikki.hietala@haaga-helia.fi"
#define EAP_USERNAME "heikki.hietala@haaga-helia.fi"
#define EAP_PASSWORD "************"

void setup() {
Serial.begin(115200);
delay(10);

Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);

// WPA2 enterprise magic starts here
WiFi.disconnect(true);
esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EAP_ID, strlen(EAP_ID));
esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EAP_USERNAME, strlen(EAP_USERNAME));
esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EAP_PASSWORD, strlen(EAP_PASSWORD));
esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&config);

// WPA2 enterprise magic ends here

WiFi.begin(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());

}

/*
everyhting below this, in loop(), is just a standard request to a webserver and nothing else than an example to show that is works.
*/

int value = 0;
const char* host = "www.sabulo.com";
void loop() {
delay(5000);
++value;

Serial.print("connecting to ");
Serial.println(host);

WiFiClient client;
if (!client.connect(host, 80)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/moai3.txt";
Serial.print("Requesting URL: ");
Serial.println(url);

// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}

// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}

Serial.println();
Serial.println("closing connection");
}

So, all in all, it’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’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.

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):

 

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.