Skip to content

Instantly share code, notes, and snippets.

@vigevenoj
Last active February 27, 2017 08:13
Show Gist options
  • Save vigevenoj/d0944d83abbf231ad642 to your computer and use it in GitHub Desktop.
Save vigevenoj/d0944d83abbf231ad642 to your computer and use it in GitHub Desktop.
bmp085 -> beagleboneblack -> mqtt -> ruby -> influxdb -> graphs!
#!/opt/rh/ruby193/root/usr/bin/ruby
# -*- encoding: utf-8 -*-
#
# mqtt & influxdb sample...
#
# libraries
# $ gem install mqtt
# $ gem install influxdb
#
require 'rubygems'
require 'mqtt'
require 'json'
require 'logger'
require 'influxdb'
# configure
mqtt_params = {
:remote_host => 'localhost',
:remote_port => 8883,
:username => 'username',
:password => 'password',
:ssl => true
}
influxdb_params = {
:host => 'localhost',
:username => 'username',
:password => 'password',
}
database = 'database'
STDOUT.sync = true
log = Logger.new(STDOUT)
#log = Logger.new('mqtt-influx.log', 10, 1048576)
log.level = Logger::INFO
influxdb = InfluxDB::Client.new database,
username: influxdb_params[:username],
password: influxdb_params[:password]
def handle_message topic, message
@log.info("received mqtt message : topic=#{topic}, message=#{message}")
# Split the topic string into tags.
# We know the first element is "sensors" so we can strip that,
# but we want the rest (address, location, and type, by our convention)
tags = topic.split("/", 2)[1].split("/")
json = JSON.parse(message)
# validate that the type matches the type in the message as well
# compare tags[2] to json['_type']
@log.info("#{json['_type']} vs #{tags[2]}")
# if json['_type'] == tags[2]
# Then connect to influxdb and write this point
data = {
series: json['_type'],
tags: { adress: tags[0], location: tags[1] },
values: json
}
influxdb.write_point(json['_type'], data, 's')
end
begin
MQTT::Client.connect(mqtt_params) do |c|
log.info("connection start...")
c.get('sensors/#') do |topic, message|
handle_message topic, message
end
end
rescue Exception => e
log.error(e)
end
#!/usr/bin/python
import sys
import paho.mqtt.client as paho
import paho.mqtt.publish as publish
import time
import json
import ssl
from decimal import Decimal
# format of json message to mqtt:
# { 'type' : 'temperature', 'value' : value, 'units' : 'C', 'timestamp' : current timestamp gmt }
temperature_sensor = "/sys/bus/i2c/drivers/bmp085/1-0077/temp0_input"
pressure_sensor = "/sys/bus/i2c/drivers/bmp085/1-0077/pressure0_input"
broker = "sharkbaitextraordinaire.com"
port = 8883
topic = "sensors/hancock/temperature"
with open(temperature_sensor) as file:
data = file.read()
value = Decimal(data)/10
message = dict(_type="temperature", val=str(value), units="C", timestamp=time.time() )
text = json.dumps(message)
client = paho.Client()
client.username_pw_set("username", "password")
client.tls_set('/home/debian/mqtt_ca.crt', tls_version=ssl.PROTOCOL_TLSv1)
client.connect("sharkbaitextraordinaire.com", 8883, 60)
client.publish(topic, text)
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment