Пример #1
0
def main():

    if app_config['restapi']:
        restapi = RestApi()
        restapi.run()

    # Retrieve previously stored baselines, if any (helps the compensation algorithm).
    has_baseline = False
    try:
        f_co2 = open('co2eq_baseline.txt', 'r')
        f_tvoc = open('tvoc_baseline.txt', 'r')

        co2_baseline = int(f_co2.read())
        tvoc_baseline = int(f_tvoc.read())
        #Use them to calibrate the sensor
        sgp30.set_indoor_air_quality_baseline(co2_baseline, tvoc_baseline)

        f_co2.close()
        f_tvoc.close()

        has_baseline = True
    except:
        print('Impossible to read SGP30 baselines!')

    #Store the time at which last baseline has been saved
    baseline_time = utime.time()

    if app_config['gcp']:
        # cloud connection
        jwt = create_jwt(google_cloud_config['project_id'],
                         jwt_config['private_key'], jwt_config['algorithm'],
                         jwt_config['token_ttl'])
        client = get_mqtt_client(google_cloud_config['project_id'],
                                 google_cloud_config['cloud_region'],
                                 google_cloud_config['registry_id'],
                                 config.google_cloud_config['device_id'], jwt)
        gc.collect()

    ts_send_gcp = 0
    ts_send_restapi = 0
    # acquiring and sending data
    while True:
        # acquiring data
        tvoc, eco2 = sgp30.indoor_air_quality
        timestamp = utime.time() + epoch_offset

        if tvoc > app_config['danger']:
            np[0] = (255, 0, 0)
            np.write()
            if app_config['audio']:
                warning_sound()
        elif tvoc > app_config['warning']:
            np[0] = (255, 255, 0)
            np.write()
            if app_config['audio']:
                warning_sound()
        else:
            np[0] = (0, 255, 0)
            np.write()

        #sending data to restapi
        if app_config['restapi']:
            if utime.time() - ts_send_restapi > app_config['ts_restapi']:
                restapi.tvoc = tvoc
                restapi.eco2 = eco2
                restapi.timestamp = timestamp
                ts_send_restapi = utime.time()

        #sending data to gcp
        if app_config['gcp']:
            if utime.time() - ts_send_gcp > app_config['ts_gcp']:
                message = {
                    "device_id": google_cloud_config['device_id'],
                    "tvoc": tvoc,
                    "eco2": eco2,
                    "timestamp": timestamp
                }
                print("Publishing message " + str(ujson.dumps(message)))
                mqtt_topic = '/devices/{}/{}'.format(
                    google_cloud_config['device_id'], 'events')
                client.publish(mqtt_topic.encode('utf-8'),
                               ujson.dumps(message).encode('utf-8'))
                gc.collect()
                client.check_msg()
                ts_send_gcp = utime.time()

        gc.collect()

        # Baselines should be saved after 12 hour the first
        # timen then every hour according to the doc.
        if (has_baseline and (utime.time() - baseline_time >= 3600)) \
            or ((not has_baseline) and (utime.time() - baseline_time >= 43200)):

            print('Saving baseline!')
            baseline_time = utime.time()

            try:
                f_co2 = open('co2eq_baseline.txt', 'w')
                f_tvoc = open('tvoc_baseline.txt', 'w')

                bl_co2, bl_tvoc = sgp30.indoor_air_quality_baseline
                f_co2.write(str(bl_co2))
                f_tvoc.write(str(bl_tvoc))

                f_co2.close()
                f_tvoc.close()

                has_baseline = True
            except:
                print('Impossible to write SGP30 baselines!')

        gc.collect()

        print("Going to sleep for about %s milliseconds!" %
              app_config["deepsleepms"])
        if app_config['restapi']:
            utime.sleep_ms(app_config["deepsleepms"])
        else:
            deepsleep(app_config["deepsleepms"])