Micropython on Adafruit Huzzah esp8266: Difference between revisions

From UNamur InfoSec
Jump to navigation Jump to search
Line 43: Line 43:


== Control GPIO ==
== Control GPIO ==
The official document for Micropython GPIO Pins is available at: https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pins.html
The way to connect your board to the external world, and control other components, is through the GPIO pins. Not all pins are available to use, in most cases only pins 0, 2, 4, 5, 12, 13, 14, 15, and 16 can be used.
The pins are available in the machine module, so make sure you import that first.
import machine
Then you can create a pin using:
pin = machine.Pin(0)
Here, the “0” is the pin that you want to access. Usually you want to configure the pin to be input or output, and you do this when constructing it. To make an input pin use:
pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)
You can either use PULL_UP or None for the input pull-mode. If it’s not specified then it defaults to None, which is no pull resistor. You can read the value on the pin using:
pin.value()
The pin on your board may return 0 or 1 here, depending on what it’s connected to. To make an output pin use:
pin = machine.Pin(0, machine.Pin.OUT)
Then set its value using:
pin.value(0)
pin.value(1)
Or:
pin.off()
pin.on()
*The buildin red led on Adafruit Huzzah is on pin 0. And is reverse, we can turn it on with following code:
import macine
pin = machine.Pin(0, machine.Pin.OUT)
pin.off()


== Connect to WiFi ==  
== Connect to WiFi ==  

Revision as of 14:17, 18 December 2017

Installation

  • Download micropython firmware for esp8266

Go to http://micropython.org/download#esp8266 and download the stable firmware file of the board (for example: esp8266-20171101-v1.9.3.bin).

  • Install esptool

We need to install esptool to flash the firmware from the computer to the esp8266 board. We use the python pip to install the esptool with the following command:

pip install esptool 

Or

python -m pip install esptool
  • Using esptool to erase the flash on device

Once we install the esptool, we can use the esptool.py to erase the flash with the command:

esptool.py --port /dev/ttyUSB0 erase_flash

Or for Windows with com serial port: com + port number

esptool.py --port com4 erase_flash
  • Flashing the firmware

Open command line terminal. Change to directory that we store the downloaded firmware. Execute the following:

esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20171101-v1.9.3.bin

Or on Windows using com serial port:

esptool.py --port com4 --baud 460800 write_flash --flash_size=detect 0 esp8266-20171101-v1.9.3.bin

Micropython REPL

Putty com example sc.png

Micropython repl example sc.png

Note: to see which com port the device is on in windows. We can:

  1. Connect the device via usb cable
  2. Open device manager.
  3. Click on Ports (COM & LPT)
  4. The Device will be list with Name and port number

Com port device manager sc.png

Control GPIO

The official document for Micropython GPIO Pins is available at: https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/pins.html

The way to connect your board to the external world, and control other components, is through the GPIO pins. Not all pins are available to use, in most cases only pins 0, 2, 4, 5, 12, 13, 14, 15, and 16 can be used.

The pins are available in the machine module, so make sure you import that first.

import machine

Then you can create a pin using:

pin = machine.Pin(0)

Here, the “0” is the pin that you want to access. Usually you want to configure the pin to be input or output, and you do this when constructing it. To make an input pin use:

pin = machine.Pin(0, machine.Pin.IN, machine.Pin.PULL_UP)

You can either use PULL_UP or None for the input pull-mode. If it’s not specified then it defaults to None, which is no pull resistor. You can read the value on the pin using:

pin.value()

The pin on your board may return 0 or 1 here, depending on what it’s connected to. To make an output pin use:

pin = machine.Pin(0, machine.Pin.OUT)

Then set its value using:

pin.value(0)
pin.value(1)

Or:

pin.off()
pin.on()
  • The buildin red led on Adafruit Huzzah is on pin 0. And is reverse, we can turn it on with following code:
import macine
pin = machine.Pin(0, machine.Pin.OUT)
pin.off()

Connect to WiFi

MQTT

DHT Sensor

References

Getting started with MicroPython on the ESP8266: https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html