Telegram Controlled Led

We will be using an ESP32 running Micropython to talk to Telegram via its API and a bot.

Telegram is like SMS and email combined 

The ESP32 is a powerfull microcontroller with build in wifi

Setting up Telegram

First you’ll need to install Telegram https://telegram.org/


Search for the user BotFather

Type /newbot and follow the prompts

Take note of the API token it gives you

Get User Id

Search for the user userinfobot and take note of your user id

ESP32 Setup

Add a file called config.json and save it to the device

    "wifi": {
        "ssid": ""
        "password": ""
    "token": "",
    "recipient": ""

Add your wifi details, Telegram API token and id

Add a file called boot.py and save it to the device

# This file is executed on every boot (including wake-boot from deepsleep)
import esp

import network
import json

with open("config.json", 'r') as f:
    config = json.load(f)

sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
    print('connecting to network...')
    sta_if.connect(config["wifi"]["ssid"], config["wifi"]["password"])
while not sta_if.isconnected():

print('network config:', sta_if.ifconfig())

Add a file called telegram.py and save it to the device

    import urequests as requests #For MicroPython
except :
    import requests
import json

with open("config.json", 'r') as f:
    config = json.load(f)

offset = "0"
while True:
    #long poll for response
    querry = 'limit=1&offset={}&timeout=10'.format(offset)
        resp = requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "getUpdates", querry))
        json = resp.json()
        if json.get("result"):
            #get latest message
            offset = json.get("result")[0].get("update_id") + 1
            text = json["result"][0]["message"]["text"]
            #echo message
            querry = 'chat_id={}&text={}'.format(config["recipient"], text)
            requests.get(url='https://api.telegram.org/bot{}/{}?{}'.format(config["token"], "sendMessage", querry))
    except Exception as e:

Now run it, and send a message to the bot you created, it should echo what ever you sent.

If you type /on it’ll turn the led on. /off will turn it off.

Code can be found here

MicroPython with the ESP32

This article will show you how to setup Thonny, an editor that works well with the ESP32, the hardware you’ll need and how to flash Micropython onto the ESP32.

Installing Thonny

Thonny is a Python editor that works well with the ESP32 and works out of the box. Installation instructions can be found on the Thonny home page https://thonny.org/

Getting the Hardware

You will need an ESP32 WROOM-32 30 pin version and 2 breadboards

You can find them on Aliexpress.



Setting up the ESP32

First you’ll need to download the firmware fo the ESP32. It can be found here https://micropython.org/download/esp32/ . You’ll want the ESP-IDF v3.x, with support for BLE, LAN and PPP. You’ll want GENERIC unless you have external ram.

Now open Thonny and go to Tools > Manage plug-ins… in the menu. Search for “esptool” and install it.

Plug your ESP32 into you computer.

Now go to Tools > Options > Interpreter.

Select MicroPython(ESP32) from the first box.

Select the port.

Now click on “Install or update firmware” (above OK button).

Select the port. Then click browse to find the firmware you downloaded earlier.

Wait for it to install. Now your ESP32 is ready for its first program.

Blinkin’ Led

from machine import Pin
from time import sleep

led = Pin(2, Pin.OUT)

while True:

Click the green button with a triangle or go to Run->Run current script, you can also hit F5


You can either save a file locally or to the device

To save go to File->Save Copy… this will give option to save locally or to the device.

If the code you are running is saved to the device you’ll have to stop the script before saving.


If you are running files locally, if you import or open another file it won’t work. Those files need to be saved to the device. Red button or Ctrl+F2