Example #1
0
def main(args):
    print(vars(args))

    DEVICE_ID_FILE = os.path.join(args.cache, args.device, 'device')
    PROJECT_ID_FILE = os.path.join(args.cache, args.device, 'project')

    device_id = iotlib.get_device_id(
        args.endpoint + '/device', args.device, args.project, DEVICE_ID_FILE, proxies=args.proxy, metadata=dict(random=True)
    )
    project_id = iotlib.get_project_id(
        args.endpoint + '/project', args.project, PROJECT_ID_FILE, proxies=args.proxy
    )

    print(
        'device: <"{}"/{}>; project: <"{}"/{}>;'.format(
            args.device, device_id, args.project, project_id
        )
    )
    then = datetime.datetime.now()
    while True:
        try:
            dick = {
                'cpu_percent': iotlib.truncate(psutil.cpu_percent(), 4),
                'virtual_memory_percent': iotlib.truncate(psutil.virtual_memory().percent, 4),
                'disk_usage_percent': iotlib.truncate(psutil.disk_usage(os.path.abspath('/')).percent, 4),
            }
            cpu_freq = {'cpu_freq_' + k: iotlib.truncate(float(v), 4) for k, v in psutil.cpu_freq()._asdict().items() }
            dick.update(cpu_freq)
            print(repr(dick))
            success = iotlib.get_ping(args.endpoint + '/ping', proxies=args.proxy) and \
                iotlib.post_data(args.endpoint + '/raw_data', device_id, project_id, dick, proxies=args.proxy)
            print(
                'device: <"{}"/{}>; project: <"{}"/{}>; success: {}; uptime: <{}>'.format(
                    args.device, device_id, args.project, project_id, success,
                    datetime.datetime.now() - then
                )
            )
        except KeyboardInterrupt:
            print('ctrl + c detected, cached in {}'.format(args.cache))
            break
        except Exception as e:
            print('encountered an error')
            traceback.format_exc()
        time.sleep(args.sample)
Example #2
0
def main(args):
    print(vars(args))

    DEVICE_ID_FILE = os.path.join(args.cache, args.device, 'device')
    PROJECT_ID_FILE = os.path.join(args.cache, args.device, 'project')

    device_id = iotlib.get_device_id(args.endpoint + '/device',
                                     args.device,
                                     args.project,
                                     DEVICE_ID_FILE,
                                     proxies=args.proxy,
                                     metadata=dict(random=True))
    project_id = iotlib.get_project_id(args.endpoint + '/project',
                                       args.project,
                                       PROJECT_ID_FILE,
                                       proxies=args.proxy)

    print('device: <"{}"/{}>; project: <"{}"/{}>;'.format(
        args.device, device_id, args.project, project_id))
    then = datetime.datetime.now()
    while True:
        try:
            dick = {
                'capacity': 100 + random.randint(0, 69),
                'amps': 250 + random.randint(0, 69),
                'refridgerator': 500 + random.randint(0, 69),
                'temperature': 750 + random.randint(0, 69),
                'ac': 750 + random.randint(0, 69),
            }
            print(repr(dick))
            success = iotlib.get_ping(args.endpoint + '/ping', proxies=args.proxy) and \
                iotlib.post_data(args.endpoint + '/raw_data', device_id, project_id, dick, proxies=args.proxy)
            print(
                'device: <"{}"/{}>; project: <"{}"/{}>; success: {}; uptime: <{}>'
                .format(args.device, device_id, args.project, project_id,
                        success,
                        datetime.datetime.now() - then))
        except KeyboardInterrupt:
            print('ctrl + c detected, cached in {}'.format(args.cache))
            break
        except Exception as e:
            print('encountered an error')
            traceback.format_exc()
        time.sleep(args.sample)
Example #3
0
def main(args):
    print(vars(args))

    sensor_args = {
        '11': Adafruit_DHT.DHT11,
        '22': Adafruit_DHT.DHT22,
        '2302': Adafruit_DHT.AM2302
    }

    gpio_dht = '17'
    sensor_dht = '11'

    sensor = sensor_args[sensor_dht]
    pin = gpio_dht

    bus = smbus.SMBus(1)
    addr = 0x10

    #Write registers
    als_conf_0 = 0x00
    als_WH = 0x01
    als_WL = 0x02
    pow_sav = 0x03

    #Read registers
    als = 0x04
    white = 0x05
    interrupt = 0x06

    # These settings will provide the max range for the sensor (0-120Klx)
    # but at the lowest precision:
    #              LSB   MSB
    confValues = [0x00, 0x13]  # 1/8 gain, 25ms IT (Integration Time)
    #Reference data sheet Table 1 for configuration settings

    interrupt_high = [0x00, 0x00]  # Clear values
    #Reference data sheet Table 2 for High Threshold

    interrupt_low = [0x00, 0x00]  # Clear values
    #Reference data sheet Table 3 for Low Threshold

    power_save_mode = [0x00, 0x00]  # Clear values
    #Reference data sheet Table 4 for Power Saving Modes

    bus.write_i2c_block_data(addr, als_conf_0, confValues)
    bus.write_i2c_block_data(addr, als_WH, interrupt_high)
    bus.write_i2c_block_data(addr, als_WL, interrupt_low)
    bus.write_i2c_block_data(addr, pow_sav, power_save_mode)

    DEVICE_ID_FILE = os.path.join(args.cache, 'device')
    PROJECT_ID_FILE = os.path.join(args.cache, 'project')

    device_id = iotlib.get_device_id(args.endpoint + '/device',
                                     args.device,
                                     args.project,
                                     DEVICE_ID_FILE,
                                     proxies=args.proxy)
    project_id = iotlib.get_project_id(args.endpoint + '/project',
                                       args.project,
                                       PROJECT_ID_FILE,
                                       proxies=args.proxy)

    print('device: <"{}"/{}>; project: <"{}"/{}>;'.format(
        args.device, device_id, args.project, project_id))
    then = datetime.datetime.now()
    while True:
        try:
            humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
            temperature = temperature * 9 / 5.0 + 32  # Convert to farhenheit

            # Reading lux (luminosity)
            word = bus.read_word_data(addr, als)

            gain = 1.8432  #Gain for 1/8 gain & 25ms IT
            #Reference www.vishay.com/docs/84323/designingveml7700.pdf
            # 'Calculating the LUX Level'

            lux_val = round(word * gain, 1)  #Round value for presentation

            success = iotlib.get_ping(args.endpoint + '/ping', proxies=args.proxy) and \
                iotlib.post_data(args.endpoint + '/raw_data', device_id, project_id, dict(Temperature=temperature, Humidity=humidity, Lux=lux_val), proxies=args.proxy)
            print(
                'device: <"{}"/{}>; project: <"{}"/{}>; success: {}; uptime: <{}>'
                .format(args.device, device_id, args.project, project_id,
                        success,
                        datetime.datetime.now() - then))
        except KeyboardInterrupt:
            print('ctrl + c detected, cached in {}'.format(args.cache))
            break
        except Exception as e:
            print('encountered an error')
            traceback.format_exc()
        time.sleep(args.sample)