IOTHUB SOFTWARE · 04
Python for IoT Helper Servers
Python is useful as the middle layer between ESP devices, MQTT, notifications, and AI processing. It should be isolated, logged, and reproducible.
Python runs the middle server between ESP32 and AI. It is a programming language that reads almost like plain language. Nothing scary. Install it and move on.
Download
Go to python.org/downloads and click the yellow Download Python 3.x.x button. Any current version above 3.8 is fine.
Windows Install
- Run the downloaded
.exe. - Most important step: check
Add Python to PATHon the first screen. If you miss this, thepythoncommand may not work later. - Click
Install Now. It takes about two minutes. - Press
Windows key + R, typecmd, then runpython --version. - If
Python 3.x.xappears, it worked.
Mac Install
- Run the downloaded
.pkg. Continue→Install.- Press
Command + Space, typeTerminal, then runpython3 --version. - If
Python 3.x.xappears, it worked.
Mac often usespython3instead ofpython. In this series, readpythonaspython3on Mac when needed.
What Is pip?
pip installs Python packages. Think of it like an app store for Python.
pip install package-nameFor example, pip install flask installs Flask, a small web server package used later.
What Is the Terminal?
- Windows: press
Windows key, typecmd, open Command Prompt. - Mac: press
Command + Space, typeTerminal, open it.
It is just a place where you type commands instead of clicking buttons. Do not be scared. We mostly use these three:
python --version pip install flask python server.py
Checklist
- □ Terminal opens.
- □
python --versionorpython3 --versionprintsPython 3.x.x. - □
pip --versionprints a version.
When It Does Not Work
| Problem | Fix |
|---|---|
python command not found | On Windows, reinstall Python and check Add Python to PATH. |
pip command not found | Try python -m pip --version. |
Mac needs python3 | Normal on Mac. Use python3 instead. |
| Microsoft Store opens | Turn off Python app execution aliases in Windows settings. |
Python is operations code
Firmware should read sensors and publish data. Python is better suited for collecting device states, parsing MQTT messages, sending Telegram alerts, storing logs, and connecting to AI helpers.
Add logs from the beginning
At startup, log configuration path, service port, broker address, and token loading status. During runtime, log MQTT connection, parsing failures, notification results, and tracebacks.
Use a virtual environment
Installing packages globally makes projects interfere with each other. A venv keeps dependencies local to one IoT service and makes migration easier.
python -m venv venv source venv/bin/activate pip install -r requirements.txt
Runtime checks
- Confirm python and pip point to the venv.
- Print configuration path and service port at startup.
- Log MQTT connection, Telegram response, and sensor message parsing errors.