Skip to content

MQTT samples #1

@s-celles

Description

@s-celles

MQTT sample should be add in this repository.
subscriber should be able to display data in console or plot matplotlib graph or pyqtgraph.
see https://github.com/scls19fr/numpy-buffer/tree/master/samples
vispy may also be considered http://vispy.org/

If we want to publish SenseHat sensors state on several topics, this could help

# import mock
# sense = mock.Mock() # Mock SenseHat for testing

from collections import OrderedDict

# Define a dictionary for sensors
#   key: sensor name
#   value: a "callable" to get value of a sensor
d_sensors = OrderedDict([
    ('pixels', sense.get_pixels),
    ('humidity', sense.get_humidity),
    ('temperature', sense.get_temperature_from_humidity),
    ('temperature_from_pressure', sense.get_temperature_from_pressure),
    ('pressure', sense.get_pressure),
    ('orientation', sense.get_orientation),
    ('compass', sense.get_compass),
    ('gyroscope', sense.get_gyroscope),
    ('accelerometer', sense.get_accelerometer),
])

sensors = d_sensors.keys() # all sensors
#sensors = ['temperature', 'pressure']

for sensor in sensors:
    get_value = d_sensors[sensor] # get a "callable"
    print(sensor, get_value)

    data = {
        'ts': now.isoformat(),
        'd': {
            sensor: get_value()
        }
    }
    payload = json.dumps(data) # serialization
    cli.publish(topic='/sensors/SenseHat01/%s' % sensor, payload=payload, qos=0, retain=False)

    ...

We can (should?) also consider that we have only one topic per SenseHat and all sensors state are publish on the same topic so data (d) should be create accordingly.

d = {sensor: get_value() for sensor, get_value in d_sensors.items()}
data = {
    'ts': now.isoformat(),
    'd': d
}
payload = json.dumps(data) # serialization
cli.publish(topic='/sensors/SenseHat01', payload=payload, qos=0, retain=False)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions