So, now that you have installed MicroPython and connected to it via PuTTY as explained in the previous post, it’s time to do some real work.
As with Arduino, the “Hello World” is represented by the blinking of a LED. Your ESP has a built-in led as pin 2. All you need to do now is to install some editor that is able to handle Python, and to connect your device and be able to put files to it. Some people like to do this using Eclipse (the most popular Java environment) and Ampy, a little file manager for MicroPython, but for all its quirks, I have come to like a Chinese product called uPyCraft. The u stands for μ and means “micro”.
Install it, and it will ask you to install a specific font when you start it the first time. Let it do so, otherwise it will nag you until Finland wins the Eurovision song contest for a second time, i.e. a very long time. If it wants to update itself, which also seems a fairly common occurrence, it is better to let it do so.
The English of the product is a little quaint and sometimes not very easy to understand, but you will get the hang of it soon enough. When you have started uPyCraft, and your ESP32 is connected via USB, look at the Tools / Serial menu. It should have COM1 or COM3, which are not what you want, but if you see a third option (in my laptop it is COM5), select that. The file browser on the top left should sport little triangles to show that you can open the Device part and the uPy_lib part, as well as the workSpace.
At this point, make sure you have the right type of board. See Tools / Board, and check that the board that is selected corresponds to your device. Then, also see that you have the three angle brackets >>> at the bottom. This is the serial monitor of your device, and you want to watch what happens there.
The good thing about the uPyCraft is that it has a large set of examples available. When you select File / Examples, you will see a set of files you can use right off the bat. In the Basic section, you will see Blink.py, which is what you want to use at this point, so go ahead and open it. It will be shown on the right side, and the code is color-coded as you can see.
This is essentiallly all there is to working with uPyCraft. Write your code, use the Edit / Syntax Check to chck for issues in your code, and when you are done, hit Ctrl+S and F5 to save your file and download and run it on your ESP32. So, let’s see the code.
import time from machine import Pin led=Pin(2,Pin.OUT) #create LED object from pin2, Set Pin2 to output while True: led.value(1) #Set led turn on time.sleep(0.5) led.value(0) #Set led turn off time.sleep(0.5)
This is very basic Python indeed. Import time brings in the sleep function from the time library, and Pin is imported from the machine library. Led = pin(2, Pin.OUT ) defines the Led object to refer to pin 2, and it is an output pin. Time.sleep(0.5) causes the device to wait for 500 milliseconds, and the led.value(1) and led.value (0) turn the led on and off, respectively. With everything under the condition while True, your code will cause the LED to light up and die off at a regular interval until you get tired of the light show and cut its electricity supply.
Let’s go ahead and hit the magic Ctrl+S and F5. If everything goes right, you will see the following in the serial monitor part of the window, bottom:
Ready to download this file,please wait! .... download ok exec(open('blink.py').read(),globals())
This means that your little Blink is moved to the device – the four dots form up as it moves. If you have a bigger program, you get more dots. Download OK means it arrived intact, and the last bit is the MicroPython installation on your device actually executing your first program. You may want to note the command, because it is useful later on. When you write a script called boot.py and download it to the device, and paste the exec command inside it just as you see it here, your device will start blinking as soon as it feels the invigorating influx of electricity, even if you plug it into a phone charger.
And of course, you can now enjoy the lovely Space Cadet glow as you watch your device blinking on and off at half second intervals. Now you can edit the times and see the effect, and if you want to see output on your monitor, add these lines to your code:
led.value(1) #Set led turn on time.sleep(0.5) print("LED ON") led.value(0) #Set led turn off time.sleep(0.5) print("LED OFF")
This leads to the following on the monitor:
Note the informative text at the bottom of the screen :P, but that’s how you read what the chip is cooking at any given time. If your chip should crash, Python offers information on it called a traceback, but this is probably the biggest problem with the uPyCraft: the display of the output is often truncated, and that makes it a little hard to understand at times. Incidentally, if you connect your ESP32 to PuTTY, you can see the output of the program on it too.
I must warn you that the uPyCraft has some other oddities, such as the sometimes occurring urge to ask you to overwrite your chip with a new file it wants to download from the Internet, or the inability to make contact with your device and claiming the COM port is occupied. But you’ll soon learn how to fix these by rebooting uPyCraft and disconnecting the USB to the chip etc.
This concludes the Part 3 of the transition from Arduino to ESP32. The next part will handle reading files on the Internet as well as writing to a file on the Web, using PHP.