[Answered ]-Show MQTT data in django template

1👍

It’s the wrong way, but if it helps someone…

from django.shortcuts import render
import paho.mqtt.client as mqtt

import json

valor_mqtt = 0

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))

client.subscribe("mhub/hr")


def on_message(client, userdata, msg):
  global valor_mqtt
  valor_mqtt = (msg.payload)
  print(valor_mqtt)

def print_on_m(request):
  global valor_mqtt
  message = str(valor_mqtt)
  return render(request, 'home/index.html',{'context':message})



client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message


client.connect("mqtt.eclipseprojects.io", 1883, 60)

I’m saying it’s the wrong way because it doesn’t update the value in the template(.html) in real time when you get a message

👤Rod

Leave a comment