Silicon ChipMake it with Micromite - May 2021 SILICON CHIP
  1. Outer Front Cover
  2. Contents
  3. Subscriptions: PE Subscription
  4. Subscriptions: PicoLog Cloud
  5. Back Issues: PICOLOG
  6. Publisher's Letter
  7. Feature: The Fox Report by Barry Fox
  8. Feature: Techno Talk by Mark Nelson
  9. Feature: Net Work by Alan Winstanley
  10. Project: 7-Band Mono or Stereo Equaliser by John Clarke
  11. Project: Touchscreen car altimeter by Peter Bennett
  12. Project: DIY Solder ReFLow Oven with PID Control by Phil Prosser
  13. Feature: Max’s Cool Beans by Max the Magnificent
  14. Feature: Make it with Micromite by Phil Boyce
  15. Feature: PICn’Mix by Mike Hibbett
  16. Feature: AUDIO OUT by Jake Rothman
  17. Feature: Circuit Surgery by Ian Bell
  18. Feature: Practically Speaking by Jake Rothman
  19. PCB Order Form
  20. Advertising Index: Max’s Cool Beans cunning coding tips and tricks

This is only a preview of the May 2021 issue of Practical Electronics.

You can view 0 of the 72 pages in the full issue.

Articles in this series:
  • (November 2020)
  • Techno Talk (December 2020)
  • Techno Talk (January 2021)
  • Techno Talk (February 2021)
  • Techno Talk (March 2021)
  • Techno Talk (April 2021)
  • Techno Talk (May 2021)
  • Techno Talk (June 2021)
  • Techno Talk (July 2021)
  • Techno Talk (August 2021)
  • Techno Talk (September 2021)
  • Techno Talk (October 2021)
  • Techno Talk (November 2021)
  • Techno Talk (December 2021)
  • Communing with nature (January 2022)
  • Should we be worried? (February 2022)
  • How resilient is your lifeline? (March 2022)
  • Go eco, get ethical! (April 2022)
  • From nano to bio (May 2022)
  • Positivity follows the gloom (June 2022)
  • Mixed menu (July 2022)
  • Time for a total rethink? (August 2022)
  • What’s in a name? (September 2022)
  • Forget leaves on the line! (October 2022)
  • Giant Boost for Batteries (December 2022)
  • Raudive Voices Revisited (January 2023)
  • A thousand words (February 2023)
  • It’s handover time (March 2023)
  • AI, Robots, Horticulture and Agriculture (April 2023)
  • Prophecy can be perplexing (May 2023)
  • Technology comes in different shapes and sizes (June 2023)
  • AI and robots – what could possibly go wrong? (July 2023)
  • How long until we’re all out of work? (August 2023)
  • We both have truths, are mine the same as yours? (September 2023)
  • Holy Spheres, Batman! (October 2023)
  • Where’s my pneumatic car? (November 2023)
  • Good grief! (December 2023)
  • Cheeky chiplets (January 2024)
  • Cheeky chiplets (February 2024)
  • The Wibbly-Wobbly World of Quantum (March 2024)
  • Techno Talk - Wait! What? Really? (April 2024)
  • Techno Talk - One step closer to a dystopian abyss? (May 2024)
  • Techno Talk - Program that! (June 2024)
  • Techno Talk (July 2024)
  • Techno Talk - That makes so much sense! (August 2024)
  • Techno Talk - I don’t want to be a Norbert... (September 2024)
  • Techno Talk - Sticking the landing (October 2024)
  • Techno Talk (November 2024)
  • Techno Talk (December 2024)
  • Techno Talk (January 2025)
  • Techno Talk (February 2025)
  • Techno Talk (March 2025)
  • Techno Talk (April 2025)
  • Techno Talk (May 2025)
  • Techno Talk (June 2025)
Make it with Micromite Phil Boyce – hands on with the mighty PIC-powered, BASIC microcontroller Part 28: Internet connection with an ESP32 Wi-Fi module – Part 1 O ver the next two months, we are going to show you how to connect your Micromite Keyring Computer (MKC) to the Internet. This is not so you can browse the Internet (you probably have enough devices for that already); instead, it’s so we can extract useful data that is available online. If you’re not already aware, there is a vast amount of accessible data on the Internet; for example, information about what is on TV and Radio, or data regarding the relative positions of the planets. Accessing and using this data with the MKC means it would be a simple task to make an MMBASIC program do some very impressive things. To put this data retrieval into practice, we will begin with something simple – how to extract the correct time from an Internet time server (often referred to as an NTP server). We will then demonstrate something much more fun – how to extract live weather data for anywhere in the world (thanks to openweathermap.org). To achieve data retrieval, the only hardware we need to attach to the MKC is some form of module that enables connection to the Internet. This may sound like a complex electronics project, but by using a dedicated Wi-Fi module built specifically for this task (and assuming you have access to a Wi-Fi network/router), then it’s actually quite easy. Thankfully, there are several devices available that allow us to achieve this, and here we have chosen a popular (and low-cost) ESP Wi-Fi module. ESP Wi-Fi modules A quick online search will reveal that there are two main types of ESP Wi-Fi module available: the ESP8266 and the ESP32. The ESP8266 was the first to appear back Micromite code The code in this article is available for download from the PE website. 46 Fig.1. Many different ESP modules are available. Shown here are (top left) ESP8266-01, (top centre) ESP8266-12e, (bottom left) ESP32-core back/front, and (right) ESP32 WROOM development board. in 2014, and this was followed two years later by the ESP32. The most noticeable difference between these two is that the ESP32 provides Bluetooth connectivity as well as Wi-Fi. However, the ESP32 also has additional Wi-Fi capability, along with improved Wi-Fi connectivity over the ESP8266. An important point to note here is that the ESP8266 and the ESP32 Wi-Fi modules both come in several different versions (see Fig.1), with each core module being manufactured by the Chinese company Espressif. There are numerous suppliers selling various ESP modules, with many suppliers even offering their own custom-built development board(s) incorporating a specific ESP module. Because the ESP modules have been available for several years, you may have already used one in a previous project, but if not, simply consider an ESP Wi-Fi module as a small PCB containing two main elements: a microcontroller and a Wi-Fi transceiver. The firmware that is loaded into the ESP’s microcontroller not only makes it possible to access some of the microcontroller’s I/O pins, but also enables the ESP to connect wirelessly to the ‘outside-world’. This could mean connecting to another ESP module (effectively creating two ESP microcontrollers that are wirelessly connected to each other) or what is much more common, allowing wireless connection to the Internet – what we will be doing. There are three main benefits of using ESP Wi-Fi modules: they are cheap (from below £5); readily available online; and the software in the ESP’s microcontroller can be reprogrammed to provide the required functionality. The benefits of low-cost and ready availability are obvious; however, Practical Electronics | May | 2021 this means that if you were to buy two ESP modules of the same hardware version, it does not necessarily mean they will both have the same firmware installed. So why is this important to understand? – remember, it is the firmware that determines the commands, features, and functionality available to the end user. Now let’s quickly talk about the ESP program code. A typical scenario is where an end-user uses a development environment designed for the Arduino. This allows the program code to be written on a computer, compiled, and then uploaded to the Fig.2. The MIKROE-3542 is a well-built ESP32 module ESP module for testing. This from MikroElektronika; it comes pre-loaded with firmware process is then repeated until that accepts a full set of AT commands. the code performs as required. For many people, this is simply a barrier as they don’t understand buyer-beware, as these two can be a problem the programming language used. – unfortunately, (very poor) copies exist. I have received some that look like they were soldered by a total novice, and other The ideal ESP module modules that had badly placed components, What we really want is a well-built ESP and even on a few occasions components module with a known version of pre-loaded were missing altogether. My advice is to firmware, which in turn has a complete buy a branded module – this has always set of commands (that are simple to use) proved to be safe and reliable. and that provide full functionality of all features. And if it were possible to send the commands (and get responses) via a ESP firmware and software simple hardware interface (such as a serial The ESP’s software complexity is often UART), then this would mean we could the reason why many people are put off totally bypass the need to write, compile using an ESP module in the first place (me and upload any complex software into the included!). To explain this further, think for ESP’s microcontroller. Instead, we could a moment about the Micromite. MMBASIC simply control the ESP module externally. is the firmware that is pre-installed inside This simple concept would allow us to the Micromite. It permanently resides in use a Micromite as the external controller, memory (or until you wish to upgrade it connected directly to the ESP via just a with a dedicated programmer such as a two-wire serial link. More importantly, PICkit or a MicroBridge). It is the firmware we can then easily send commands from that enables the Micromite to understand within our MMBASIC program code, and typed commands and hence make it do likewise, receive responses too. something useful. For example, you may Thankfully, ESP modules do have a serial write some code (your program), and RUN connection allowing external control; so it it to see the end result. You can EDIT the was really just a matter of finding a suitable program code at any point, but that doesn’t firmware. Several firmware versions claim affect the firmware in any way. Typing to support the ability to accept a simple LIST allows you to see the code; typing set of commands. However, in practice, NEW will remove the program code from only a few modules seem to support the Micromite. All of these commands all the commands required to reliably work with just the program code and leave talk to various online servers. So, after the firmware untouched. Even though the experimenting with several different ESP program code also remains in memory when modules from various suppliers that claim to the power is removed, it is editable via offer the above, we settled on an ESP Click commands, but the firmware is truly fixed. module made by MikroElektronika; or to be Now back to the ESP, it too has firmware more precise, the MikroElektronika Wi-Fi and program code. It is very important BLE Click ESP32-WROOM-32 Bluetooth here to understand that there are many Wi-Fi 2.4GHz MIKROE-3542 (see Fig.2). different versions of ESP firmware; from A question you may be asking is why we factory-versions supplied by Espressif, to decided to use a Click module based on custom-built versions of firmware written an ESP32 over one that uses a ESP8266. by suppliers and developers. Put simply, Practical Electronics | May | 2021 This comes down to a commercial project I was working on in which the requirement was to be able to reliably send data to a database that resided in the Cloud. It is too complex to explain fully, but in summary, the data was effectively being pushed to a secure web address beginning HTTPS (as opposed to HTTP). It turned out that unless the ESP8266 is reprogrammed with a custom-built version of firmware, it will only allow data to be sent to HTTP. Reprogramming the firmware resulted in several bricked ESP modules, so I gave up with that approach and got the ESP32 module working at the first attempt. It was then just a matter of looking for a readily available branded module that would make it far easier for PE readers to use – hence we’re taking the Mikroe-3542 route. Can I use an ESP8266? As an aside, it is worth noting that the ESP8266 module can be used as an alternative to the ESP32 if you have one lying around, but it will need to have a version of firmware that accepts the AT command set. I cannot guarantee that all the AT commands used in this article will be available to you as that depends on the firmware, as clearly outlined previously – but at least you can begin to experiment, and certain things may work. So, to be clear, the rest of this article will assume you are using the ESP32 module. AT commands You will likely come across AT commands whenever you use a communications module (ie, a modem). In fact, we discussed them very briefly back in Part 14 of this series (PE, March 2020) when we were using a Bluetooth module. The term ‘AT’ comes from the first two letters of the word ‘attention’. It can be regarded as one device saying to another, ‘Hey, I’m about to talk to you!’. In the early days of modem communication, AT was used to signify that what followed was a communication command, and it has pretty much been adopted ever since. Hence, every AT command begins with the letters ‘AT’ (or ‘at’), an example being AT+RST. The command is only sent to the module after sending a ‘Carriage Return’, ie, pressing the Enter key if sent from a computer. The communications module will then act upon the command it receives and send back an appropriate response. The simplest form of response is ‘OK’ and some commands will send just this. However, other commands will return one (or more) line(s) of data, followed with an ‘OK’ at the end. One point to note here is that it is good practice to use commands in upper case because this guarantees they will work correctly (I have several modules that do not respond to some commands when sent in lower case). 47 T X R X E N I O0 G N D E S P 3 2 N C N C R S T N C C S T X N C R X N C N C N C N C 3 V 3 N C G N D G N D Wifi BLE click 3 .3 V U S B - to- serial m odule 0 V T X R X We are not going to go into the fine detail of the ESP’s AT command set here, but if you’re interested, useful documents can be found at the manufacturer’s website: http://bit.ly/pe-may21-exp Connecting the ESP32 to the MKC Now that we have chosen our ESP module, we’ll start with a simple test. This will prove that we have a working ESP module, that it is powered correctly, and that the ESP firmware can indeed respond to commands externally sent to it. For this test you will need a USB-to-serial module such as a CP2102, or one based on an FTDI chip. You will likely have one already, but if not, then it is well worth getting a few to tuck in the draw as they always come in handy (simply check online, and you’ll find many available for around £1 plus delivery). 48 Fig.3. To test the ESP module, we connect it directly to a USB-to-serial module: a) (left) just four connections required; b) (right) no need for a breadboard, simply use jumper cables (female/female shown here). For this test, there are only four links required, as shown in Fig.3a. I tend to use male/ female jumper wires to hook the ESP module to the USB-toserial module rather than using a breadboard (see Fig.3b). One word of warning; the ESP32 Click module requires 3.3V, so take care when attaching the +V link (5V will damage the ESP module). With 3.3V and 0V connected, and the USB-to-serial module plugged into your computer, you should see the power LED illuminate on the ESP module. Once all four links are connected, launch your terminal application (eg, TeraTerm) and check that your settings match those in Fig.4a and Fig.4b: set the baud rate (115200) and receive/transmit characters (LF/CR+LF). Next, ensure the caps Lock is turned on (or hold down the shift key), and type AT followed by pressing Enter. As you type, you should see AT appear on the screen – if not, then check you have the Tx/ Rx connected correctly, and that you have the correct baud rate set in your terminal application. If everything was set up correctly you should see OK on the screen. If so, continue with another command: AT+GMR, which checks the version number of your ESP module. Fig.5 shows a screenshot of my result from sending these two commands to the ESP32. What’s the time? Now that we have confirmed that the ESP module is working, we can disconnect it from the USB-to-serial module and connect it directly to the MKC. This will then allow us to start using the MKC to externally control the ESP module; our first demonstration is to fetch the date and time from an Internetbased time server. Refer to Fig.6 to see the five connections required. In addition to the four links we used in the test above, we will also connect the CS pin on the ESP module to an I/O pin on the MKC (which is configured as an output). When the CS pin is at a high logic level, the ESP functions as normal, but taking the CS pin low will result in Fig.4. Ensure your terminal application settings match these parameters. Shown here are screenshots from using TeraTerm: a) (top) Setup/Terminal… settings; b) (bottom) Setup/Serial port… settings. Practical Electronics | May | 2021 Fig.5. After typing both test commands (AT and AT+GMR), you should see something like that shown here. the ESP module being reset. So go ahead and make these five connections, and then perform a quick visual check to ensure that everything is correct before switching on. Next, download the file NTP_TimeDemo.txt from the May 2021 page of the PE website and load it into the MKC. Before you run the program you will need to EDIT the first two lines of code for the ESP module to successfully connect to your wireless network. Simply enter your wireless network name (SSID$), and password (PWD$). Once this is done, RUN the program and you should see (on your terminal screen) the activity of the ESP module connecting to your network, fetching the data, and then displaying the date and time that was received from the NTP server (see Fig.7a). Depending on your location in the world, you may also need to edit lines 3-5 of the program to match your relevant NTP server (a quick Google search will highlight what these are – and up to three NTP addresses can be entered). The first time the program is run, the ESP will need to load your network log in parameters (the SSID$ and PWD$). On subsequent occasions when you RUN the program, the ESP will already be connected to your network so the screen activity will look slightly different (see Fig.7b). Line 6 of the code (Debug=1) can be changed to Debug=0, which will suppress all the AT command activity from being displayed on the terminal screen. Make this change and run the program again; you should now just see the date and time (see Fig.7c). So how does this all work? Well, that will have to wait for next month, but for those of you who want to explore things further, do take a look at the code, in particular the two ESP commands: AT+CIPSNTPCFG and AT+CIPSNTPTIME? T X R X E N I O0 G N D E S P 3 2 N C N C R S T N C Fig.7. Top to bottom: a) the first time you run the program, the ESP32 will be configured with your wireless network parameters; b) once set up, running the program again will get the NTP data quicker; c) the commands sent to the ESP (and response back) can be suppressed by setting Debug=0. C S T X 22 C OM 1 R x N C R X 21 C OM 1 T x N C N C Next month N C N C 3.3V 3 V 3 N C 0V G N D Now that we have our ESP module connected to the MKC and have been able to demonstrate that it is possible to extract the date and time from an NTP server, we will work through the program code and explain how it works. Then we will show you how to extract real-time weather data for any location Questions? Please email Phil at: in the world. Until contactus<at>micromite.org then, have FUN! 18 G N D Wifi BLE click Fig.6. Only five links are required to connect the ESP32 module to your MKC. Practical Electronics | May | 2021 49