ESP32 Perfume Vending Machine with On-Device QR Payments and MQTT Telemetry

Adequate Infosoft has designed the Street Aroma, which is a perfume vending machine based on the ESP32 platform. It showcases the firm's expertise in various areas of technology, including IOT product development, custom PCB design, creation of embedded firmware, MQTT-based communication in machines, and backend creation. The Street Aroma incorporates such features of a modern vending machine as touchscreen interface, fragrance dispensing, and determining the price of the perfume.

1. Project Overview and Problem Statement

The Street Aroma vending machine sells perfumes in various locations, and it has become available to commercial use. Conventional vending machines can only accept cash bills or coins. However, in Street Aroma, the whole transaction process takes place digitally. The user chooses any of the four selected brands by just pressing the different buttons on the machine.

This selection generates a Payme QR code, which can be scanned by the user on his phone running the Payme app, after which he gets exactly two doses of the perfume.

Rather than a prototype, the solution represents a fully functional commercial entity comprising firmware, payment backend, remote price management, and hardware control.

One of the most important problems of engineering is a process reliability in the edge, i.e. the vending machine has no other options than to manage transactions and possible timeouts without relying on Wi-Fi connection and exposing secrets of the merchant.

The operations are conducted by Street Aroma via ESP32-based equipment operating on Python Flask server to validate the payment and capable to operate even in offline mode with NVS pricing system data.

Street Aroma ESP32 perfume vending machine

2. System Architecture and Data Flow

The Street Aroma system uses the device-broker-server architecture. Communication between the vending machine (ESP32) and Flask backend occurs via two separate communication channels: HTTPS for order creation and price synchronization, and MQTT for payment confirmation events.

The HiveMQ server acts as an MQTT broker (configurable as broker.hivemq.com), and topic namespace is limited to your merchant ID (payments/your_merchant_id). The Flask server acts as an intermediary between the device and Payme payments gateway.

It receives the JSON-RPC webhooks from Payme and converts payment events into MQTT messages to be consumed by the ESP32 vending machine.

The flow is the following: the customer clicks the product button; the ESP32 POSTs /create-order to the Flask server with product ID and its price; the server creates Payme checkout URL and publishes it using MQTT.

The ESP32 renders the URL on the TFT in a QR-code; the customer makes the payment, Payment sends a webhook to the server, the server publishes "confirmed" via MQTT, the ESP32 switches to the success page and gives two doses of the spray the customer clicks the spray button to dispense the dose, and the servo works once on each click until the counter runs out.

This approach guarantees that the ESP32 never stores the Payme credentials or checks the payment cryptographically. The device has to do nothing except displaying a URL, subscribing to a topic and responding to a boolean result.

ESP32 perfume vending system architecture

3. Hardware Design and Pin-Level Integration

The bill of materials is purposely kept simple: one ESP32 DevKit, one TFT ILI9488 320×480 screen, four SG90 servo motors, nine buttons, and four fragrance tanks with nozzles.

There are nine buttons in total that have been categorized into different types: four product selection buttons, four spray trigger buttons (one per fragrance), and one cancel button.

The pin mapping is clearly specified in the README file where the selection buttons are placed at GPIO pins 22, 23, 32, and 33, the spray buttons at GPIO pins 19, 21, 26, and 27, the cancel button at GPIO pin 25 and servos starting from GPIO pin 18 with three more spare pins.

A critical hardware–firmware coupling is the enablement of PSRAM. The QR code rendering pipeline allocates an RGB565 pixel buffer in PSRAM to generate the QR bitmap directly on the ESP32 without calling an external QR service.

This is not merely an optimization, it is what allows the device to operate as a standalone payment terminal rather than a thin client dependent on a third-party QR API.

4. Firmware Development: LVGL, State Machine, and Deferred Payment Handling

The firmware is comprised of five separate source files each addressing their individual responsibilities: main.cpp for the main loop, LVGL tick, state machine, and timeout functions, payment.cpp for Wi-Fi, MQTT, QR code generation, order flow, and price synchronization, buttons_control.cpp for selecting products, cancellation, and spray function, Servo_controll.cpp for dose distribution; and tft_draw.cpp for the LVGL flush function to TFT_eSPI.

Shared defines, pin assignments, and variables live in Globals.h, and the project is built with PlatformIO (a platformio.ini is present) using a custom partition table (partitions_custom.csv).

LVGL 8.3 is used to develop the user interface which includes seven dedicated screens meant to control the entire process of vending including a main menu showing four scents along with their respective prices which change from time to time, a QR code screen used to make payment, an MQTT error screen, a loading screen, a payment success screen and a payment cancellation screen among others.

The infinite loop executes the LVGL tick and increments a finite state machine that monitors the life cycle of an order from idle to product choice, order making, QR code showing, payment validation, dispensing, and reset. The discipline imposed by the state machine is what makes race conditions impossible between MQTT callbacks, button interrupts, and timeouts.

It can be seen that the payment timeout policy is especially interesting from the perspective of edge device development. The machine will show a countdown for nine seconds in case the payment is not confirmed during five minutes.

In case the countdown is interrupted by any button presses, it continues to run, however, in case the countdown reaches zero, the order is automatically cancelled.

Thus, customers have enough time to perform their actions, but the machine should be released for another user in case of necessity. The error recovery policy for MQTT connection errors is non-blocking.

Perfume vending QR payment and dispensing workflow

5. Backend Development: Flask, Payme Webhooks, and MQTT Publishing

The Flask app (app.py) is an efficient, yet fully functional payment orchestration server. There are four functions associated with the Flask application. First, there is a Payme JSON-RPC webhook that provides the full cycle of transactions: CheckPerform, Create, Perform, and Cancel. The Flask application is the ultimate authority about the payment. The ESP32 will never decide about the payment success.

Second, it publishes created, confirmed, and cancelled events to the MQTT broker, which the ESP32 subscribes to. Third, it provides GET and POST /api/prices endpoints for remote price management. Fourth, it persists every order in orders.json, providing a lightweight audit trail without requiring a database.

The configuration of the server is done using environment variables which are loaded using the python-dotenv package, ensuring the security of these credentials is not compromised both within the firmware and source code repository. The dependencies for the server are flask, paho-mqtt, and python-dotenv. This minimalist setup can run on a cheap VPS or even a Raspberry Pi.

The most production-friendly aspect of the Street Aroma project is its price synchronization loop. Prices are fetched every 30 seconds by the ESP32, and they are stored in NVS (Non-Volatile Storage) with the help of the Arduino Preferences library.

Should the server become unavailable for some reason, the device will continue working with the latest available prices; this way, even in case of a network interruption, vending machine functionality will not be compromised. The prices are shown in the main menu and are updated rather often, allowing to update the prices via the POST /api/prices API endpoint.

This design choice reflects a broader principle: the ESP32 is treated as an intermittently connected edge node, not as a permanently online thin client.

The presence of NVS caching, reconnect code for MQTT and QR generation at the device level enables degradation in multiple failure scenarios such as server unavailability, MQTT broker disconnect, and Wi-Fi connectivity issues, yet still complete the physical dispensing operation after a payment has been made.

7. Dose Counting and Physical Dispensing Control

The dispensing subsystem follows a contract that allows only two doses payment-wise at a time. The system gives two doses according to each confirmed order, and there is a screen counter of the remaining doses. The servo motor works when the spray button is pressed having some doses left.

When the counter reads zero, there is no further action possible. The relevant function is carried out by the Servo_controller.cpp program with the involvement of a state machine specified in the main.cpp program, which prohibits the driving of physical actuators during unpaid sessions. The servo-fragrance association is pre-specified (GPIO 18 plus three other GPIOs), while the spray button uses GPIO 19, 21, 26, and 27.

8. Development Challenges and Engineering Solutions

Challenge 1: On-device QR generation with memory restrictions.

Incorporating QR code payment method on an ESP32 requires a frame buffer and library. Street Aroma solves this problem with the help of qrcode library which utilizes RGB565 buffer allocated in PSRAM and goes on flushing the buffer conceived through LVGL library to the TFT. If PSRAM is not available, the frame buffer and QR buffer will compete for RAM in the internal SRAM of the ESP32.

Challenge 2: Verifying payments without revealing any secrets.

Payme merchant key is not accessed by the ESP32. It is done by the Flask server which processes the JSON-RPC webhook call and announces the result as a boolean via MQTT. Hence, a hacked device cannot produce false payment results; it may just subscribe to it.

Challenge 3: Managing network instability without freezing the UI.

MQTT problems appear via an error display, whilst the firmware attempts to communicate repeatedly every 3 seconds with no interruption to the LVGL event loop. The distinction between the network and the user experience is further established by the 30-second sync intervals for value changes and the NVS cache.

Challenge 4: Timeouts and cancellation races.

With 5 minutes allowed to make a payment with a 9-second countdown and the possibility to prolong it via button press, proper coordination between timers, MQTT callbacks, and button interrupts is required. The finite state machine implemented in main.cpp makes sure that a late confirmation from MQTT won't be accepted after cancellation.

9. Outcomes and Technical Significance

Using only a single ESP32 and a well-thought-out backend as seen in Street Aroma, you can create a high-quality vending machine. The device can generate QR codes, receive real-time payment notifications via the MQTT protocol, manage prices online while having a fall back to offline mode, and count dispenses accurately, all without the use of a mobile application, database, or payment SDK.

Firmware is modular (five source files + Globals.h), server, simple Flask web app with three libraries used, and hardware – ready-to-use components (ESP32 DevKit, ILI9488 TFT display, SG90 servos).

The most transferable aspect of this project is the design of its trust boundary, in which the payment authorizing logic is kept on the server, but the device needs only the bare minimum state to show the QR code, wait for MQTT verification, and actuate the servo. This design can be immediately applied to any edge device that requires digital payment capability.

10. Conclusion

Street Aroma is a scaled-down yet fully elaborated sample of edge IoT implementation.

Decisions taken by its creators, QR code generation based on PSRAM, payment verification through MQTT protocol, NVS caching of cost information, timings provided by state machine, and dosing done on servo level all look sound in themselves but jointly provide sound methodology of launching vending gadgets in the market.

A unique ESP32 firmware separates the module functioning from Flask and Payme and is the core thing in the whole architecture. It allows developers to make the device simple, inspectable, and effective while achieving the necessary real-time performance.

Abhinav Akula
DevOps Engineer and 3× Microsoft Azure Certified professional specializing in Azure cloud solutions, migration, deployment automation, and multi-cloud environments. He holds Microsoft certifications as an Azure Developer Associate and Azure Solutions Architect Expert, with expertise in building scalable, secure, and reliable cloud infrastructure.

Project Repository: github.com/myseringan/Street-Aroma-Vending

What Our Clients Say About Us

Client satisfaction is our ultimate goal. Here are some kind words of our precious clients they have used to express their satisfaction with our service.

Leadership That Leads Worldwide

With a physical presence in over 15 countries and a global footprint spanning 25+ countries, we are ready to serve you anywhere. Location, language, or culture is never a barrier, because our global team can work with you in your language. Our strong international team ensures seamless collaboration across borders We have a strong tech team, highly recognized in their domains, with extensive technical expertise.