Posts

Showing posts with the label IoT

How to fake mqtt messages by using Python

Catch  import paho.mqtt.client as mqtt from auto_everything.io import IO io_ = IO() def on_connect (client, userdata, flags, rc) : print( "Connected with result code " +str(rc)) client.subscribe( "#" ) def on_message (client, userdata, msg) : text = msg.payload.decode( "utf-8" ) print(text) io_.append( "data.txt" , "\n" + text) client = mqtt.Client() client.on_connect = on_connect client.on_message = on_message while True : try : client.connect( "192.168.201.81" , 1883 , 60 ) client.loop_forever() except Exception as e: print(e) Replay import paho.mqtt.client as mqtt import json from time import sleep from auto_everything.io import IO io_ = IO() import socket socket.setdefaulttimeout( 1000 ) data_list = io_.read( "data.txt" ).split( "\n" ) index = 0 length = len(data_list) client = mqtt.Client() client.connect( "192.16...

How can we use MQTT protocol to send messages over micropython(esp32) board?

Concepts Basically, you got two actions available. One is publish . Another is subscribe . If we want to send some message out to others, we need to use publish . If we want to just receive messages from others, we use subscribe . publish  from umqtt.simple import MQTTClient c = MQTTClient( "clients/a" , SERVICE_IP) c.connect() c.publish( b"a_topic" , b"hello" ) c.disconnect() subscribe import time from umqtt.simple import MQTTClient # Received messages from subscriptions will be delivered to this callback def callback (topic, msg) : print((topic, msg)) c = MQTTClient( "clients/b" , SERVICE_IP) c.set_callback(callback) c.connect() c.subscribe( b"a_topic" ) while True : if True : # Blocking wait for message c.wait_msg() else : # Non-blocking wait for message c.check_msg() # Then need to sleep to avoid 100% CPU usage (in a real # app other useful actions woul...

What is MQTT

MQTT Message Queuing Telemetry Transport is a lightweight, publish-subscribe network protocol that transports messages between devices. The protocol usually runs over TCP/IP.  In my opinion, it’s a protocol that was widely used in IoT(Internet of Things) . MQTT clients Normally, it’s a bunch of micro-controllers that have WiFi access. Like ESP32, ESP8866. MQTT broker It’s the service that runs on servers(or clouds). Many big companies offer such a platform that allows your MQTT clients to connect. It’s a middle software that transfers your data between MQTT clients. ​