Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT samples #1

Open
s-celles opened this issue Oct 16, 2015 · 0 comments
Open

MQTT samples #1

s-celles opened this issue Oct 16, 2015 · 0 comments

Comments

@s-celles
Copy link
Member

s-celles commented Oct 16, 2015

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant