2

I need to make a simple proof of concept, having an Arduino or ESP32 with a couple of LEDs than can be turned on/off via browser.

There are a lot of examples about having a small server in the Arduino and accessing it via browser. However for my project I want to use an MQTT broker.

The part where the Arduino updates the LED when a value is changed in the MQTT server is done and works well.

However what I need now it a way of posting a message to the MQTT server just by accessing a website.

Something like: if you open the following link the LED will turn ON: www.mymqttserverdomain.com?LED1=1

Bence Kaulics
  • 7,843
  • 8
  • 42
  • 90
Cincinnatus
  • 141
  • 1
  • 2

2 Answers2

2

Assuming your broker supports MQTT over WebSockets, just run a MQTT client in the webpage and have it send the messages directly.

There is a Paho JavaScript client and nearly all brokers support WebSockets these days

hardillb
  • 12,813
  • 1
  • 21
  • 34
0

You can, but you must use port 1883 to connect your client with a broker. Here I uploaded a Python code for that

from flask import Flask

import paho.mqtt.client as mqtt

app = Flask(name)

def on_message(client, userdata, message) : print("Received message: ", str(message.payload.decode("utf-8","ignore")))

mqttBroker = "broker.emqx.io"

client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, "Webpage") client.on_message = on_message client.connect(mqttBroker)

client.subscribe("abc") client.loop_forever()

@app.route('/LED1/<msg>') def send(msg): if msg == '1': client.publish("abc", "ON") elif msg == '0': client.publish("abc", "OFF") else: pass return "ok"

if name == 'main': app.run(debug=True)

Note - To turn on your LED1 You have to use this link "<your_gateway>/LED1/1"

hardillb
  • 12,813
  • 1
  • 21
  • 34