Demo entry 6786825



Submitted by anonymous on Apr 04, 2019 at 13:10
Language: Python 3. Code size: 4.3 kB.

# ***************************************************************************
# Copyright (c) 2017, 2019 IBM Corporation and other Contributors.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# ***************************************************************************

import sys
import argparse
import wiotp.sdk
import RPi.GPIO as GPIO
import lightsensor
import gassensor
import time
import distancesensor

    import ibmiotf.device
except ImportError:
    # This part is only required to run the sample from within the samples
    # directory when the module itself is not installed.
    # If you have the module installed, just use "import ibmiotf.device"
    import os
    import inspect

    cmd_subfolder = os.path.realpath(
        os.path.abspath(os.path.join(os.path.split(inspect.getfile(inspect.currentframe()))[0], "../../src"))
    if cmd_subfolder not in sys.path:
        sys.path.insert(0, cmd_subfolder)
    import ibmiotf.device

def commandProcessor(cmd):
    print("Command received: %s" %

authMethod = None

# Initialize the properties we need
parser = argparse.ArgumentParser()

# Primary Options
parser.add_argument("-o", "--organization", required=False, default="beide0")
parser.add_argument("-T", "--typeId", required=False, default="LichtSensor")
parser.add_argument("-I", "--deviceId", required=False, default="lichtsensor1")
parser.add_argument("-t", "--token", required=False, default="Mn3JR?*rO*zZxAAsVH", help="authentication token")
parser.add_argument("-c", "--cfg", required=False, default=None, help="configuration file")
parser.add_argument("-E", "--event", required=False, default="event", help="type of event to send")
    "-N", "--nummsgs", required=False, type=int, default=1, help="send this many messages before disconnecting"
parser.add_argument("-D", "--delay", required=False, type=float, default=1, help="number of seconds between msgs")
args, unknown = parser.parse_known_args()

if args.token:
    authMethod = "token"

# Initialize the device client.

    if args.cfg is not None:
        deviceOptions = wiotp.sdk.device.parseConfigFile(args.cfg)
        deviceOptions = {
            "identity": {"orgId": args.organization, "typeId": args.typeId, "deviceId": args.deviceId},
            "auth": {"token": args.token},
    deviceCli = wiotp.sdk.device.DeviceClient(deviceOptions)
    deviceCli.commandCallback = commandProcessor
except Exception as e:
    print("Caught exception connecting device: %s" % str(e))

# Connect and send datapoint(s) into the cloud


GPIO.setup(2, GPIO.IN)
GPIO.setup(3, GPIO.IN)

GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.IN)

i = 0

lightvalue = ""
gasvalue = ""

def light_value():
    global lightvalue
    input_lightvalue = GPIO.input(2)
    lightvalue += str(input_lightvalue)
    if len(lightvalue) >= 5:
        data_light = lightsensor.send_latest_lightdata(lightvalue)
        json["night"] = data_light

def gas_value():
    global gasvalue
    input_gasvalue = GPIO.input(3)
    gasvalue += str(input_gasvalue)
    if len(gasvalue) >= 5:
        data_gas = gassensor.send_latest_gasdata(gasvalue)
        json["gas"] = data_gas

while True:
    i += 1
    json = {}

    # For the light sensor
    # For the gas_sensor
    # For the distance sensor

    json["distance"] = distancesensor.food_percentage()


    # Send to IBM Watson IoT Platform
    if i % 5 == 0:
        success = deviceCli.publishEvent(args.event, "json", json, qos=0)
        if not success:
            print("Not connected to IoTF")
        lightvalue = lightvalue[-1:-6]
        gasvalue = gasvalue[-1:-6]

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).