#  Demo to show a simple MQTT subscriber to BayEOS Gateway bridge
#  Oliver Archner
#  29.07.2015

from bayeosgatewayclient import BayEOSWriter, BayEOSSender
import paho.mqtt.client as mqtt

# BayEOS sender
sender = BayEOSSender(
    url='http://bayconf.bayceer.uni-bayreuth.de/gateway/frame/saveFlat',
    user='******',
    password='******')
sender.start()

# BayEOS writer
writer = BayEOSWriter()


# MQTT connect and subscribe
def on_connect(client, userdata, rc):
    print("Connected with result code:{0}".format(rc))
    # Subscribe everything
    client.subscribe("#")


# MQTT receive and save message
def on_message(client, userdata, msg):
    # Expects a payload in csv format like '1.0,2.0,3.0,4.0'
    try:
        lst = msg.payload.split(',')
        writer.save(values=[float(i) for i in lst], origin=str(msg.topic))
Example #2
0
"""Creates an example writer and sender using threads."""
from time import sleep
from bayeosgatewayclient import BayEOSWriter, BayEOSSender
import logging
import tempfile
from os import path

PATH = path.join(tempfile.gettempdir(), 'bayeos-device')
BACKUP_PATH = path.join(tempfile.gettempdir(), 'bayeos-device-backup')
NAME = 'Python-Thread-WithLogging'

URL = 'http://bayconf.bayceer.uni-bayreuth.de/gateway/frame/saveFlat'

writer = BayEOSWriter(PATH, max_time=10, log_level=logging.DEBUG)
writer.save_msg('Writer was started.')

sender = BayEOSSender(PATH,
                      NAME,
                      URL,
                      backup_path=BACKUP_PATH,
                      log_level=logging.DEBUG)
sender.start()

nr = 0
while True:
    writer.save([nr, 3, 20.5])
    #writer.flush()
    nr += 1
    sleep(5)
"""Reads BayEOS options from a config file."""

from bayeosgatewayclient import BayEOSWriter, BayEOSSender, bayeos_confparser

config = bayeos_confparser('config')
print config

writer = BayEOSWriter(path=config['path'], max_chunk=config['max_chunk'])
sender = BayEOSSender(path=config['path'])
Example #4
0
from sht21 import SHT21
from mcp3424 import MCP3424
from gpio import GPIO

# gpio pins
ADDR_PINS = [11, 12, 13, 15, 16, 18]  # GPIO 17, 18, 27, 22, 23, 24
DATA_PIN = 24  # GPIO 8
EN_PIN = 26  # GPIO 7

# configuration for BayEOSWriter and BayEOSSender
PATH = '/tmp/raspberrypi/'
NAME = 'RaspberryPi'
URL = 'http://bayconf.bayceer.uni-bayreuth.de/gateway/frame/saveFlat'

# instantiate objects of BayEOSWriter and BayEOSSender
writer = BayEOSWriter(PATH)
sender = BayEOSSender(PATH, NAME, URL)

# initialize GPIO Board on Raspberry Pi
gpio = GPIO(ADDR_PINS, EN_PIN, DATA_PIN)

# initialize I2C Bus with sensors
try:
    i2c = I2C()
    sht21 = SHT21(1)
    mcp3424 = MCP3424(i2c.get_smbus())
except IOError as err:
    sys.stderr.write(
        'I2C Connection Error: ' + str(err) +
        '. This must be run as root. Did you use the right device number?')
Example #5
0
"""Creates an example writer and flushes it before max chunk size is reached."""

from time import sleep
from bayeosgatewayclient import BayEOSWriter

PATH = '/tmp/bayeos-device1/'
writer = BayEOSWriter(PATH, max_chunk=1000)
writer.save_msg('Writer was started.', origin='Python-Writer-Flush-Example')
flush = 0

while True:
    #print 'adding frame\n'
    writer.save(values=[2.1, 3, 20.5],
                value_type=0x02,
                offset=2,
                origin='Python-Writer-Flush-Example')
    flush += 1
    if flush % 5 == 0:  # flush writer every 5 seconds
        writer.save_msg('Writer was flushed.',
                        origin='Python-Writer-Flush-Example')
        writer.flush()
        print 'flushed writer'
        flush = 0
    sleep(1)
from bayeosgatewayclient import BayEOSWriter, BayEOSSender, bayeos_argparser

# Fetch input arguments
args = bayeos_argparser('This is the text to appear on the command line.')

WRITER_SLEEP = float(args.writer_sleep)
MAX_CHUNK = float(args.max_chunk)

NAME = args.name + '-WS' + str(WRITER_SLEEP) + '-M' + str(MAX_CHUNK)
PATH = args.path + '/' + NAME + '/'
if args.url:
    URL = args.url
else:
    URL = 'http://bayconf.bayceer.uni-bayreuth.de/gateway/frame/saveFlat'

print 'name to appear in Gateway is', NAME
print 'max-chunk is', MAX_CHUNK, 'byte'
print 'writer sleep time is', WRITER_SLEEP, 'sec'
print 'path to store writer files is', PATH

# init writer and sender
writer = BayEOSWriter(PATH, MAX_CHUNK)
writer.save_msg('Writer was started.')

sender = BayEOSSender(PATH, NAME, URL, 'import', 'import')
sender.start()

# start measurement
while True:
    writer.save([time()], value_type=0x21)
    sleep(WRITER_SLEEP)