Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

MicroPython based online weather station on ESP8266 and ESP32

By Nikhil Agnihotri January 19, 2023

MicroPython is one of the best microcontroller firmware that supports a variety of embedded platforms. The WiFi development boards like ESP8266 and ESP32 are among the MicroPython-supported ports. With MicroPython, it is extremely simple to implement top-notch IoT applications.  MicroPython has wide support for network programming along with implementing all basic hardware functions. It combines low-level hardware support with high-level programming features, which embedded C-based microcontroller ecosystems are usually void of. Plus, the Python syntax makes the embedded programming super simple, clean, and precise.

In this project, we will develop an online weather station configured over ESP8266/ESP32. The project utilizes ESP8266 as a microcontroller by getting sensor readings from DHT-11, while at the same time, it benefits from the network programming functions of MicroPython to connect and operate within an IoT network. This weather station is operational within the limits of a wireless LAN and broadcasts temperature and humidity as a working TCP server within the network.

Prerequisites
Before continuing, you must have set up uPyCraft IDE or Thonny IDE as a software development environment. Plus, you must have uploaded MicroPython firmware to ESP8266/ESP32. You can find a guide for both tasks from this link. For making this project, you must know how to connect ESP8266/ESP32 with a WiFi network using MicroPython. You also need to know how to configure ESP8266/ESP32 as a MicroPython TCP server.

Components required

  1. ESP8266/ESP32 x1
  2. USB cable to connect ESP with computer
  3. DHT11 x1
  4. Breadboard x1
  5. Connecting wires/Jumper wires
  6. Computer/Mobile x1

Circuit connections
In this project, ESP8266/ESP32 act as a TCP server hosting a simple HTML webpage. DHT11 sensor is interfaced with ESP8266/ESP32. The board reads temperature and humidity from DHT11 and publishes the readings over the hosted webpage. The circuit connections are required only to interface DHT11 with ESP8266/ESP32. DHT11 has the following pin diagram.

To learn more about the DHT11 sensor, check out this Arduino tutorial. For interfacing DHT11 with ESP8266, connect the VCC and GND pins of DHT11 with the 3V and GND of ESP8266. Connect the data pin of DHT11 with any GPIO of ESP8266. In this project, the data pin of DHT11 is connected to GPIO14 (D5) of ESP8266. The data pin is pulled high by connecting a 10K resistor between the data output and VCC.

If ESP32 is used, D14 is the GPIO14 in ESP32. ESP32 will have the following circuit connections.

After circuit connections, the ESP8266 TCP server would look as follows.

MicroPython Script

 

How project works
This ESP8266/ESP32-based online weather station reads temperature and humidity from the DHT11 sensor and publishes the readings on a webpage in real-time. The webpage is hosted by ESP8266/ESP32 itself, for which, the board is configured as a TCP server. The webpage can be accessed by any device, a computer or mobile that has a browser to access the internet over HTTP protocol. The webpage is hosted locally within wireless LAN, so only devices connected to the same WiFi connection can access the webpage.

The working of the project is straightforward. The board reads temperature and humidity from DHT11 using the built-in DHT module of MicroPython. The board connects with an available WiFi connection using MicroPython’s network module. For hosting a webpage and operating as a TCP server, the socket module of MicroPython is utilized. ESP8266/ESP32 reads temperature and humidity and embeds the readings within the webpage. The webpage is returned as an HTTP response to any device that accesses the localhost IP address.

The code
The code begins with importing a usocket module. In case a usocket module is not available, the default socket module is imported. Next, the network module, pin class from the machine module, and dht module are imported. The network module is used to connect with a WiFi connection. The pin class of the machine module is used to interact with the DHT11 sensor. The dht module is used to get readings from DHT11. The esp module is imported, and OS-level errors are suppressed by calling esp.osdebug(None) method. The gc module is imported, and garbage collection is enabled by calling gc.collect() method. This frees microcontroller resources (RAM) as soon as variables and objects are left unused.

The SSID and network key of available WiFi connection are stored in variables SSID and password. You will need to replace the SSID and WiFi password with the SSID and network key of your own WiFi connection in the following lines of code.

ssid = ‘SSID’
password = ‘PASSWORD’

An object network.WLAN() class is instantiated, configuring ESP8266/ESP32 as a WiFi station. The ESP8266/ESP32 as a WiFi station is activated by calling station.active() method. The board is connected to an available WiFi connection by calling station.connect() method. The connection with the WiFi network is verified by the calling station.isconnected() method. On successful connection with the WiFi network, a message is printed to the console, and IP-level configuration parameters are also printed to the console by the calling station.ifconfig() method. The very first IP-level parameter printed to the console is the IP address of the ESP8266/ESP32 station.

A variable sensor is declared to instantiate the object of the DHT class. The variables temp and hum are declared to store temperature and humidity readings. A user-defined function read_dht() is defined to get temperature and humidity readings from DHT11. The function calls the measure() method for getting readings from the DHT11 sensor. It uses temperature() and humidity() methods to get temperature and humidity values, respectively, as float or integer. The temperature and humidity values are returned in global variables temp and hum, respectively.

A user-defined function web_page() is defined to return an HTML page with temperature and humidity values stored in global variables temp and hum embedded within the HTML page. An object of socket class is instantiated and configured to use IPv4 address and TCP for network layer protocol. The object is bound to the localhost address using the bind() method and activated to listen from TCP clients by calling listen() method.

An infinite while loop is started. In the loop, the ESP8266/ESP32 TCP server is configured to accept HTTP requests from TCP clients from the same WiFi network by calling the s.connect() method. The HTTP requests from a connected TCP client are stored in a variable request by calling conn.recv() method. The message containing temperature and humidity readings is printed to the console, and the webpage containing the temperature and humidity values embedded is returned as the response by calling conn.send() and conn.sendall() methods. After returning the webpage as a response, the connection with the TCP client is explicitly closed by calling conn.close() method.

Result
The webpage with temperature and humidity readings from the DHT11 sensor is accessed by typing the IP address returned by the station.ifconfig() method in any browser’s address bar. The ESP8266/ESP32 TCP server operating as an online weather station returns a custom webpage as a response to an HTTP request from your computer/mobile’s browser. The following screenshot shows the webpage returned by the ESP8266/ESP32 online weather station.

Remember that the ESP8266/ESP32 weather station is hosting the webpage locally, so the webpage can only be accessed by devices connected to the same WiFi connection. ESP8266/ESP32 is using the localhost IP address as the IP address of the web server.

You may also like:


  • How to use MicroPython’s watchdog timer in ESP8266 and ESP32

  • MicroPython: How to program a real-time clock (RTC) in ESP8266…

  • ESP8266/ESP32-based WiFi access point using MicroPython

  • Getting started with MicroPython on ESP8266

  • What is MicroPython?

  • Getting started with ESP8266

Filed Under: Python, Tutorials
Tagged With: MicroPython, MicroPython ESP32, MicroPython ESP8266, MicroPython WiFi
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • High Side current sensing
  • How to simulate power electronics converter in PSpice?
  • Reducing "shoot-through" in offline Full Bridge SMPS?
  • Voltage mode pushpull is a nonsense SMPS?
  • Layout IRN reduction in Comparator

RSS Electro-Tech-Online.com Discussions

  • Back to the old BASIC days
  • Parts required for a personal project
  • PIC KIT 3 not able to program dsPIC
  • Failure of polypropylene motor-run capacitors
  • Siemens large industrial PLC parts

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • What is AWS IoT Core and when should you use it?
  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe