Source code for pyiot.iot

"""
This module does the actual job of subscribing and publishing to MQTT topics,
use GET and POST method to transfer data using HTTP protocol on the request of
Action Server.
"""

import time
import json
import requests
import paho.mqtt.client as mqtt  # import the client1


[docs]def mqtt_subscribe_thread_start(arg_callback_func, arg_broker_url, arg_broker_port, arg_mqtt_topic, arg_mqtt_qos): """ This function is called in a new thread to subscribe to a MQTT topic. :param arg_callback_func: Callback function :type arg_callback_func: function :param arg_broker_url: Broker URL :type arg_broker_url: str :param arg_broker_port: Broker Port :type arg_broker_port: int :param arg_mqtt_topic: MQTT topic to subscribe :type arg_mqtt_topic: str :param arg_mqtt_qos: Quality of Service :type arg_mqtt_qos: int :return: Success flag :rtype: bool """ # pylint: disable=bare-except try: mqtt_client = mqtt.Client() mqtt_client.on_message = arg_callback_func mqtt_client.connect(arg_broker_url, arg_broker_port) mqtt_client.subscribe(arg_mqtt_topic, arg_mqtt_qos) time.sleep(1) # wait # mqtt_client.loop_forever() # starts a blocking infinite loop mqtt_client.loop_start() # starts a new thread return 0 except: return -1
# ----------------- MQTT PUB -------------------
[docs]def mqtt_publish(arg_broker_url, arg_broker_port, arg_mqtt_topic, arg_mqtt_message, arg_mqtt_qos): """ This function is called in new thread to publish data to a MQTT topic. :param arg_broker_url: Broker URL :type arg_broker_url: str :param arg_broker_port: Broker Port :type arg_broker_port: int :param arg_mqtt_topic: MQTT topic name :type arg_mqtt_topic: str :param arg_mqtt_message: Message to publish :type arg_mqtt_message: str, int :param arg_mqtt_qos: Quality of Service :type arg_mqtt_qos: int :return: Success flag :rtype: bool """ # pylint: disable=bare-except try: mqtt_client = mqtt.Client("mqtt_pub") mqtt_client.connect(arg_broker_url, arg_broker_port) mqtt_client.loop_start() print("Publishing message to topic", arg_mqtt_topic) mqtt_client.publish(arg_mqtt_topic, arg_mqtt_message, arg_mqtt_qos) time.sleep(0.1) # wait mqtt_client.loop_stop() # stop the loop return 0 except: return -1
# ----------------- HTTP POST -------------------
[docs]def http_post(spread_sheet_id, parameters): """ This function is called to push data to google spreadsheet using HTTP protocol. :param spread_sheet_id: Google Spreadsheet ID :type spread_sheet_id: str :param parameters: Parameters to push :type parameters: str, int :return: Success flag :rtype: bool """ # pylint: disable=bare-except try: response = requests.get("https://script.google.com/macros/s/" + spread_sheet_id + "/exec", params=json.loads(parameters)) return response.content except: return -1