server_fan script

Central IoT server, fan manageer, and MQTT coordinator.

Script provides following functionalities:

  • Script manages a fan attached to a GPIO pin for cooling the system on the basis of the system temperature provided by the SoC.
  • Script acts as a MQTT coordinator utilizing local MQTT broker mosquitto for data exchange within the script as well as with outside environment.
  • Script communicates with cloud services ThingSpeak and Blynk.
  • Script publishes system temperature and fan status (running, idle) to all three platforms: local MQTT broker, ThingSpeak cloud, Blynk mobile application.
  • Script can receive commands from local MQTT broker and from Blynk mobile app in order to change its behaviour during runnig, e.g., turn on or off the fan, change fan trigger temperatures, etc.
server_fan.action_fan(command, value=None)[source]

Perform command for the fan.

Parameters:
  • command (str) – Action name to be realized.
  • value – Any value that the action should be realized with.
server_fan.action_script(command)[source]

Perform command for this script itself.

Parameters:command (str) – Received command to be realized: {"EXIT"}.
server_fan.blynk_publish_fan_limits()[source]

Publish fan temperature percentages to Blynk mobile application.

server_fan.blynk_publish_fan_percoff()[source]

Publish fan temperature percentage OFF to Blynk mobile application.

server_fan.blynk_publish_fan_percon()[source]

Publish fan temperature percentage ON to Blynk mobile application.

server_fan.blynk_publish_fan_status()[source]

Publish fan status to Blynk mobile application.

server_fan.cbBlynk_on_connect()[source]

Process actions when the script is connected to Blynk cloud.

server_fan.cbMqtt_on_connect(client, userdata, flags, rc)[source]

Process actions when the broker responds to a connection request.

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • flags (dict) – Response flags sent by the MQTT broker.
  • rc (int) – The connection result (result code).

See also

gbj_pythonlib_sw.mqtt._on_connect()
Description of callback arguments for proper utilizing.
server_fan.cbMqtt_on_disconnect(client, userdata, rc)[source]

Process actions when the client disconnects from the broker.

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • rc (int) – The connection result (result code).

See also

gbj_pythonlib_sw.mqtt._on_connect()
Description of callback arguments for proper utilizing.
server_fan.cbMqtt_on_message(client, userdata, message)[source]

Process actions when a non-filtered message has been received.

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • message (object) – An instance of MQTTMessage. This is a class with members topic, payload, qos, retain.

Notes

  • The topic that the client subscribes to and the message does not match an existing topic filter callback.
  • Use message_callback_add() to define a callback that will be called for specific topic filters. This function serves as fallback when none topic filter matched.
server_fan.cbMqtt_on_message_command(client, userdata, message)[source]

Process server command at receiving a message from the command topic(s).

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • message (object) – An instance of MQTTMessage. This is a class with members topic, payload, qos, retain.

Notes

  • The topic that the client subscribes to and the message match the topic filter for server commands.
server_fan.cbMqtt_on_message_data(client, userdata, message)[source]

Process server data send through a MQTT topic(s).

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • message (object) – An instance of MQTTMessage. This is a class with members topic, payload, qos, retain.
server_fan.cbMqtt_on_subscribe(client, userdata, mid, granted_qos)[source]

Process actions when the broker responds to a subscribe request.

Parameters:
  • client (object) – MQTT client instance for this callback.
  • userdata – The private user data.
  • mid (int) – The message ID from the subscribe request.
  • granted_qos (int) – The list of integers that give the QoS level the broker has granted for each of the different subscription requests.
server_fan.cbTimer_temp_measure(*arg, **kwargs)[source]

Measure current CPU temperature.

server_fan.cbTimer_temp_publish(*arg, **kwargs)[source]

Publish current CPU temperature.

server_fan.cbTimer_temp_triggers(*arg, **kwargs)[source]

Execute CPU temperature triggers.

server_fan.cbTimer_thingspeak(*arg, **kwargs)[source]

Publish to ThingSpeak.

server_fan.cbTrigger_fan(*args, **kwargs)[source]

Execute command for the fan.

server_fan.loop()[source]

Wait for keyboard or system exit.

server_fan.main()[source]

Fundamental control function.

server_fan.mqtt_message_log(message)[source]

Log receiving from a MQTT topic.

Parameters:message (str) – An instance of MQTTMessage. This is a class with members topic, payload, qos, retain.
Returns:Flag about present message payload.
Return type:bool

See also

gbj_pythonlib_sw.mqtt()
Module for MQTT processing.
server_fan.mqtt_publish_fan_limits()[source]

Publish fan temperature percentages to the MQTT status topic.

server_fan.mqtt_publish_fan_percoff()[source]

Publish fan temperature percentage OFF to the MQTT status topic.

server_fan.mqtt_publish_fan_percon()[source]

Publish fan temperature percentage ON to the MQTT status topic.

server_fan.mqtt_publish_fan_status()[source]

Publish fan status to the MQTT status topic.

server_fan.mqtt_publish_temp()[source]

Publish SoC temperature to a MQTT topic.

server_fan.setup()[source]

Global initialization.

server_fan.setup_blynk()[source]

Define Blynk parameters.

server_fan.setup_cmdline()[source]

Define command line arguments.

server_fan.setup_config()[source]

Define configuration file management.

server_fan.setup_filter()[source]

Define statistical smoothing and filtering.

server_fan.setup_logger()[source]

Configure logging facility.

server_fan.setup_mqtt()[source]

Define MQTT management.

server_fan.setup_mqtt_filters()[source]

Define MQTT topic filters and subscribe to them.

Notes

  • The function is called in on_connect callback function after successful connection to a MQTT broker.
server_fan.setup_pi()[source]

Define GPIO control.

Notes

  • Operational pin names are stored in the object as attributes.
  • Default fan percentage limits are stored in the object as attributes.
server_fan.setup_thingspeak()[source]

Define ThingSpeak management.

server_fan.setup_timers()[source]

Define dictionary of timers.

server_fan.setup_trigger()[source]

Define triggers for evaluating value limits.

server_fan.setup_trigger_fan(fan_perc_on=None, fan_perc_off=None)[source]

Define triggers for controlling fan by SoC temperature.

Parameters:
  • fan_perc_on (float) – Percentage of maximal temperature for turning fan on.
  • fan_perc_off (float) – Percentage of maximal temperature for turning fan off.
server_fan.thingspeak_publish(fan_status=False)[source]

Publish to ThingSpeak.

Parameters:fan_status (bool) – Flag determining whether current fan state should be published as a ThingSpeak channel status.

Notes

Data fields are published automatically.