Python and IoT: Building Smart Devices with MicroPython

Python and IoT: Building Smart Devices with MicroPython

How to Build Smart IoT Devices Using MicroPython: A Practical Guide


The Internet of Things (IoT) is revolutionizing the way we interact with the world around us. From smart homes to industrial automation, IoT devices are becoming increasingly prevalent. In this blog post, we will explore how to use Python, specifically MicroPython, to build smart devices for IoT applications. MicroPython is a lean and efficient implementation of Python3 that is optimized to run on microcontrollers and other resource-constrained devices.

Setting Up MicroPython

Choosing a Microcontroller

Before diving into code, you need a microcontroller that supports MicroPython. Popular choices include:

  • ESP8266

  • ESP32

  • Pyboard

For this tutorial, we'll use the ESP32 due to its robust features and support for Wi-Fi and Bluetooth.

Installing MicroPython on ESP32

  1. Download the MicroPython Firmware:

    • Visit the MicroPython download page.

    • Download the latest stable release for ESP32.

  2. Flash the Firmware to the ESP32:

    • Install the using pip:

        pip install esptool
    • Erase the flash memory of the ESP32: --port /dev/ttyUSB0 erase_flash
    • Flash the MicroPython firmware to the ESP32: --chip esp32 --port /dev/ttyUSB0 --baud 460800 write_flash -z 0x1000 esp32-20220117-v1.18.bin

Writing Your First MicroPython Script

Connecting to the ESP32

To interact with your ESP32, you'll need a serial communication tool. You can use picocom, minicom, or the built-in REPL in the Thonny IDE. For this example, we'll use picocom:

picocom /dev/ttyUSB0 -b 115200

Hello World in MicroPython

Let's start with a simple "Hello, World!" script to ensure everything is set up correctly.

  1. Open the REPL by connecting to your ESP32.

  2. Type the following code:

     print("Hello, World!")

Building a Smart Device

Example 1: Controlling an LED

Connecting an LED to the ESP32
  1. Wiring the LED:

    • Connect the anode (long leg) of the LED to GPIO 2.

    • Connect the cathode (short leg) of the LED to a resistor (220 ohms).

    • Connect the other end of the resistor to GND.

Writing the LED Control Script
  1. Open the REPL.

  2. Enter the following code:

     from machine import Pin
     import time
     led = Pin(2, Pin.OUT)
     while True:

This script will blink the LED on and off every second.

Example 2: Reading a Temperature Sensor

Connecting a DHT22 Sensor
  1. Wiring the DHT22:

    • Connect the VCC pin to the 3.3V pin on the ESP32.

    • Connect the GND pin to the GND pin on the ESP32.

    • Connect the Data pin to GPIO 4.

Writing the Temperature Sensor Script
  1. Install the DHT library:

     import upip
  2. Enter the following code:

     import dht
     import machine
     import time
     sensor = dht.DHT22(machine.Pin(4))
     while True:
         temp = sensor.temperature()
         hum = sensor.humidity()
         print('Temperature: {}°C Humidity: {}%'.format(temp, hum))

    This script will read and print the temperature and humidity every two seconds.

Connecting to Wi-Fi

One of the powerful features of the ESP32 is its Wi-Fi capability. Let's connect our device to a Wi-Fi network.

import network

ssid = 'your_SSID'
password = 'your_password'

station = network.WLAN(network.STA_IF)
station.connect(ssid, password)

while station.isconnected() == False:

print('Connection successful')

Sending Data to the Cloud

Let's send sensor data to a cloud service. For this example, we'll use the Adafruit IO service.

  1. Install the Adafruit IO library:

     import upip
  2. Send Data to Adafruit IO:

     from umqtt.simple import MQTTClient
     import ubinascii
     import machine
     import micropython
     import time
     # Adafruit IO configuration
     ADAFRUIT_IO_URL = b''
     ADAFRUIT_USERNAME = b'your_username'
     ADAFRUIT_IO_KEY = b'your_aio_key'
     FEED_ID = b'temperature'
     # ESP32 unique ID
     CLIENT_ID = ubinascii.hexlify(machine.unique_id())
     def connect_to_adafruit():
         return client
     client = connect_to_adafruit()
     while True:
         temperature = 25  # Replace with sensor reading
         payload = bytes(str(temperature), 'utf-8')
         client.publish(b'{}/feeds/{}'.format(ADAFRUIT_USERNAME, FEED_ID), payload)
    The information provided in this blog is for educational purposes only. Ensure you follow all safety guidelines and legal regulations when working with electronic components and IoT devices. The author and publisher assume no responsibility for any errors or omissions or for any damages resulting from the use of the information contained herein.
MicroPython makes it incredibly easy to develop IoT applications using Python. In this tutorial, we covered the basics of setting up MicroPython on an ESP32, writing simple scripts, connecting to Wi-Fi, sending data to the cloud, and creating a web server. With these building blocks, you can start creating your own smart devices and contribute to the growing world of IoT.