Friday, July 10, 2020

ESP8266 Programming : Uploading Program to ESP8266

Steps:

1) Install ampy with pip
2) Write MicroPython code
3) Upload the MicroPython on code on the microcontroller with ampy
4) Unplug and then power up the ESP8266 and watch the LED Blink

1) Install ampy with pip

Ampy is a Python package developed by Adafruit, used to push code stored on a computer onto a microcontroller running MicroPython. Ampy can be installed using the Anaconda Prompt. If you are using a virtual environment, active the virtual environment first then proceed with ampy package installation.
> conda activate micropython
(micropython) > pip install adafruit-ampy
(micropython) > ampy --help

2) Write MicroPython Code

Write the MicroPython code which you will uploaded on the microcontroller. The Adafruit Feather Huzzah ESP8266 microcontroller contains two main Python files: boot.py and main.py. But additional files can also be uploaded to the microcontroller. boot.py is the file that runs first when the board is powered up. First boot.py runs, then main.py runs. An additional .py file can be added to the board to provide main.py with a function to run.

Two different .py files will be constructed in this section. One BLINKLED.py file contains a function that Blinks a LED of ESP8266. A second main.py file calls the function in the first .py file.
# Function to blink LED

def blinkLED():
     import machine
     import time

     print("Hello!! This is Blink LED Program In blink LED")

     led=machine.Pin(2,machine.Pin.OUT)
     while True:
         led.on()
         time.sleep(0.5)
         led.off()
         time.sleep(1)
The complete main.py file is below.
# main.py import BlinkLED

print("Hello !! This is Blink LED Sample Program : In mains")

BlinkLED.blinkLED()

3) Upload MicroPython Code to the ESP8266 Microcontroller with ampy

Once the BLINKLED.py file and the main.py files are saved, both files can be uploaded on the ESP8266 microcontroller.

Ensure the microcontroller is connected with a USB cable, and be aware of which serial port the microcontroller is connected to.

Upload the BLINKLED.py file and the main.py file to the board using ampy. Make sure you are in the directory with the .py files and that you are working in a virtual environment that has ampy installed in it. In the example code below, the (micropython) virtual environment is active. The command ampy --port COM4 ls lists the files stored on the microcontroller.
(micropython) > ampy --port COM4 put MCP9808.py
(micropython) > ampy --port COM4 put main.py
(micropython) > ampy --port COM4 ls
boot.py
MCP9808.py
main.py

4) Unplug and then power up the ESP8266 and watch the LED BLINK :) :)


The ESP8266 needs to be restarted to run the main.py file uploaded with ampy.

To restart the board, unplug and then replug in the board's power (the USB cable). Once power is restored, the board will run through the boot.py script then start the main.py script. When the board runs the main.py script, the board will Blink the LED.

No comments:

Post a Comment

ESP8266 Programming : Uploading Program to ESP8266

Steps: 1) Install ampy with pip 2) Write MicroPython code 3) Upload the MicroPython on code on the microcontroller with ampy 4) Unplug ...