ESP32 MQTT IoT Communication
Loading...
Searching...
No Matches
mqtt_sub Namespace Reference

Subscribes to mqtt. More...

Functions

tuple[device_writer.DeviceWriter, list[device_writer.SensorWriter]] json_parse_device (dict json_dict)
 Parses a JSON Dict into a tuple of device and the sensors connected.
 
device_writer.ReadingsWriter json_parse_reading (dict json_dict)
 @briefParses a reading JSON object.
 
list parse_json (dict json_dict)
 Parses a JSON Dctionary into a list of Objects.
 
None extract_from_json (dict json_dict, int indent=0)
 Extracts the data from a JSON Object.
 
mqtt_client connect_mqtt ()
 Connects a Client to the MQTT broker.
 
 subscribe (mqtt_client client, sqlite3.Connection con)
 Subscribes to a topic on the MQTT broker.
 
None main ()
 Connects a client to the broker and subscribes to a given topic.
 

Variables

str BROKER = 'rpi.local'
 MQTT Broker Ip address.
 
int PORT = 1883
 Port that the broker is running on.
 
list MQTT_TOPIC = [("esp32/id", 2), ("esp32/walk", 2)]
 (MQTT topic, QoS level) tuple
 
str DB_PATH = "db/iot.db"
 Path to the.
 
str client_id = f'subscribe-{random.randint(0, 100)}'
 Generate a Client ID with the subscribe prefix.
 
dict devices = {}
 (dev_address, device.Device) MAC address to object dictionary
 
dict sensors = {}
 (sensor_address, device.Sensor) Sensor Address to object dictionary
 

Detailed Description

Subscribes to mqtt.

Function Documentation

◆ connect_mqtt()

mqtt_client mqtt_sub.connect_mqtt ( )

Connects a Client to the MQTT broker.

Returns
MQTT Client object
Connects a Client to the MQTT broker

◆ extract_from_json()

None mqtt_sub.extract_from_json ( dict  json_dict,
int   indent = 0 
)

Extracts the data from a JSON Object.

Parameters
json_dictA JSON object in the form of a dict object
indentNumber of spaces for printing nested JSON Objects (optional)
Extracts the data from a JSON Object

◆ json_parse_device()

tuple[device_writer.DeviceWriter, list[device_writer.SensorWriter]] mqtt_sub.json_parse_device ( dict  json_dict)

Parses a JSON Dict into a tuple of device and the sensors connected.

Parameters
json_dictJSON Object in the form of a dictionary
Returns
(Device Object, List of Sensor Objects)
Parses a JSON Dict into a tuple of device and the sensors connected

◆ json_parse_reading()

device_writer.ReadingsWriter mqtt_sub.json_parse_reading ( dict  json_dict)

@briefParses a reading JSON object.

Parameters
json_dictJSON object as a dictionary
Returns
ReadingsWriter Object
Parses a reading JSON object.

◆ main()

None mqtt_sub.main ( )

Connects a client to the broker and subscribes to a given topic.

Connects a client to the broker and subscribes to a given topic

◆ parse_json()

list mqtt_sub.parse_json ( dict  json_dict)

Parses a JSON Dctionary into a list of Objects.

Parameters
json_dictJSON Object in the form of a dictionary
Returns
list List of objects (Device/Sensor/SensorReadings)
Parses a JSON Dctionary into a list of Objects

◆ subscribe()

mqtt_sub.subscribe ( mqtt_client  client,
sqlite3.Connection  con 
)

Subscribes to a topic on the MQTT broker.

Parameters
clientMQTT Client
conSqlite3 Database connection
Subscribes to a topic on the MQTT broker