Beispiel #1
0
void Device::print_readings(unsigned int num, std::string filename)
{
        //char temp[128];
        std::stringstream ss;
        if(num == readings.size())
                ss << "  Printing all the " << num << " readings:" << std::endl;
        else
                ss << "  Printing the latest " << num << " readings:" << std::endl;
        if(filename.empty())
                printf("%s", ss.str().c_str());
        else
                log(ss.str(), filename, true, false);

        std::map<time_t,int>::iterator it = readings.end();
        for(int i = 1; i < num+1; i++) {
                char buff[20];
                time_t time = 0;
                if(it != readings.begin())
                        time = (--it)->first;
                else
                        continue;
                format_time(time, buff);
                ss.str(std::string()); // clear the stream
                ss << "      " << buff << "\t" << get_reading(time) << std::endl;
                if(filename.empty())
                        printf("%s", ss.str().c_str());
                else
                        log(ss.str(), filename, true, false);
        }
}
/*
   update the state of the sensor
*/
void AP_CollisionAvoidance_PulsedLightLRF::update(void)
{
    if (get_reading(state.distance_cm)) {
        // update range_valid state based on distance measured
        update_status();
    } else {
        set_status(CollisionAvoidance::CollisionAvoidance_NoData);
    }
}
/* 
   update the state of the sensor
*/
void AP_RangeFinder_MaxsonarI2CXL::update(void)
{
    if (get_reading(state.distance_cm)) {
        // update range_valid state based on distance measured
        update_status();
    } else {
        set_status(RangeFinder::RangeFinder_NoData);
    }
}
/* 
   update the state of the sensor
*/
void AP_RangeFinder_MaxsonarSerialLV::update(void)
{
    if (get_reading(state.distance_cm)) {
        // update range_valid state based on distance measured
        last_reading_ms = AP_HAL::millis();
        update_status();
    } else if (AP_HAL::millis() - last_reading_ms > 500) {
        set_status(RangeFinder::RangeFinder_NoData);
    }
}
/* 
   update the state of the sensor
*/
void AP_RangeFinder_LightWareSerial::update(void)
{
    if (get_reading(state.distance_cm)) {
        // update range_valid state based on distance measured
        state.last_reading_ms = AP_HAL::millis();
        update_status();
    } else if (AP_HAL::millis() - state.last_reading_ms > 200) {
        set_status(RangeFinder::RangeFinder_NoData);
    }
}
/* 
   detect if a Maxbotix rangefinder is connected. We'll detect by
   trying to take a reading on I2C. If we get a result the sensor is
   there.
*/
bool AP_RangeFinder_MaxsonarI2CXL::detect(RangeFinder &_ranger, uint8_t instance)
{
    if (!start_reading()) {
        return false;
    }
    // give time for the sensor to process the request
    hal.scheduler->delay(50);
    uint16_t reading_cm;
    return get_reading(reading_cm);
}
/*
   detect if a PulsedLight rangefinder is connected. We'll detect by
   trying to take a reading on I2C. If we get a result the sensor is
   there.
*/
bool AP_CollisionAvoidance_PulsedLightLRF::detect(CollisionAvoidance &_ranger, uint8_t instance)
{
    if (!start_reading()) {
        return false;
    }
    // give time for the sensor to process the request
    hal.scheduler->delay(50);
    uint16_t reading_cm;
    return get_reading(reading_cm);
}
/* 
   update the state of the sensor
*/
void AP_RangeFinder_Benewake::update(void)
{
    bool signal_ok;
    if (get_reading(state.distance_cm, signal_ok)) {
        // update range_valid state based on distance measured
        last_reading_ms = AP_HAL::millis();
        if (signal_ok) {
            update_status();
        } else {
            // if signal is weak set status to out-of-range
            set_status(RangeFinder::RangeFinder_OutOfRangeHigh);
        }
    } else if (AP_HAL::millis() - last_reading_ms > 200) {
        set_status(RangeFinder::RangeFinder_NoData);
    }
}
/*
   update the state of the sensor
*/
void AP_RangeFinder_BLPing::update(void)
{
    if (uart == nullptr) {
        return;
    }

    const uint32_t now = AP_HAL::millis();

    if (get_reading(state.distance_cm)) {
        // update range_valid state based on distance measured
        state.last_reading_ms = now;
        update_status();
    } else if (now - state.last_reading_ms > BLPING_TIMEOUT_MS) {
        set_status(RangeFinder::RangeFinder_NoData);

        // initialise sensor if no distances recently
        if (now - last_init_ms > BLPING_INIT_RATE_MS) {
            last_init_ms = now;
            init_sensor();
        }
    }
}
static int32_t get_lux(void)
{
	return  alscode2lux(get_reading());
}
int main(int argc, char *argv[])
{
    char packet[] = {0x00, 0x00, 0x00};

    float temp, hum;

    int error_count = 0;

    int count = 1;

    GError *error = NULL;
    GOptionContext *context;

    context = g_option_context_new ("- collect temperature and humidity readings");
    g_option_context_add_main_entries (context, entries, NULL);
    if (!g_option_context_parse (context, &argc, &argv, &error))
    {
      g_print ("option parsing failed: %s\n", error->message);
      exit (1);
    }
    g_option_context_free(context);
    set_debug(debug);
    hid_device *handle;
    gboolean found = search_for_device();

    if(!found) {
        g_printerr("Sensor not found, aborting.\n");
        exit(-1);
    }
    
    // Open the device using the VID and PID
    handle = open_lascar();
    if(handle == NULL) {
      g_printerr("Error opening sensor.\n");
      exit(-1);
    }
    
    SoupSession *session = NULL;
    int channel;
    gchar *channel_name;
    gchar *room;
    GString *uri, *body;
    
    /* parse config file */
    GKeyFile *gkf = g_key_file_new();
    g_key_file_load_from_data_dirs(gkf,"templogger.conf",NULL,G_KEY_FILE_NONE,&error);
    if(error!=NULL) {
        g_printerr("Can't load configuration file, not uploading to server.\n");
        g_key_file_free(gkf);
    }
    else {
      gchar *url = g_key_file_get_string(gkf,"influx","url",NULL);
      channel = g_key_file_get_integer(gkf,"channel","channel_num",NULL);
      channel_name = g_key_file_get_string(gkf,"channel","channel_name",NULL);
      room = g_key_file_get_string(gkf,"channel","room",NULL);
      int port = g_key_file_get_integer(gkf,"influx","port",NULL);
      gchar *db = g_key_file_get_string(gkf,"influx","database",NULL);
      gchar *username = g_key_file_get_string(gkf,"influx","username",NULL);
      gchar *password = g_key_file_get_string(gkf,"influx","password",NULL);
      g_key_file_free(gkf);
    
      /* open session */
    
      session = soup_session_new();
      uri = g_string_new("http://");
      g_string_append_printf(uri,"%s:%d/write?db=%s&u=%s&p=%s",url,port,db,username,password);

      g_message(uri->str);
      body = g_string_new("");
	  g_print("Uploading as channel %d, name %s, room %s",channel, channel_name, room);
    }
    
    FILE *logfile = NULL;
    if(log_local) {
        logfile = fopen("log.txt","a");
    }
    
    const int upload_freq = floor(UPLOAD_TIME/SLEEP_TIME);
    
    while(1) {
        int ret = get_reading(handle, packet, &temp, &hum, TRUE);
        if(ret >= 0) {
            gint64 t = 1000*g_get_real_time();
            
            if(log_local) {
              fprintf(logfile,"%" G_GINT64_FORMAT "\t%.1f\t%.1f\n",t, temp, hum);
              fflush(logfile);
            }
            
            if(debug)
              g_print("%" G_GINT64_FORMAT "\t%.1f\t%.1f\n",t, temp, hum);
            
            if(session && (count % upload_freq == 0)) {
            SoupRequestHTTP *request = soup_session_request_http(session,"POST",uri->str,NULL);
            SoupMessage *message = soup_request_http_get_message(request);
            g_string_append_printf(body,"temp,channel=%d,channel_name=%s,room=%s",channel,channel_name,room);
            g_string_append_printf(body," value=%.1f %" G_GINT64_FORMAT,temp,t);
            g_string_append_printf(body,"\n");
            g_string_append_printf(body,"hum,channel=%d,channel_name=%s,room=%s",channel,channel_name,room);
            g_string_append_printf(body," value=%.1f %" G_GINT64_FORMAT,hum,t);
            g_string_append_printf(body,"\n");
            
            if(debug)
              g_message(body->str);
            
            if(!testing) {
                soup_message_set_request(message,"application/binary",SOUP_MEMORY_COPY,body->str,body->len);
                guint session_status = soup_session_send_message(session,message);
                if(session_status == 204) { /* message was received */
                    //g_print("received status %d\n",session_status);
                    g_string_erase(body,0,-1); /* clear the string */
                }
                else {
                  g_print("no connection to server");
                }
                /* otherwise, keep it and attempt to resend next time */
            }
            g_object_unref(message);
            }
            count ++;

            /* reset the error count on successful read */
            error_count = 0;
        } else if(error_count > MAX_ERRORS) {
            g_printerr("Too many errors to continue\n");
            exit(-1);
        } else {
            error_count++;
        }
        if(testing && count>60)
          break;
        g_usleep(SLEEP_TIME*1000000);
    }
    if(session) {
      g_string_free(uri,TRUE);
      g_string_free(body,TRUE);
      g_object_unref(session);
    }
    hid_close(handle);
    hid_exit();
    
    return 0;
}
/* 
   update the state of the sensor
*/
void AP_RangeFinder_PulsedLightLRF::update(void)
{
    state.healthy = get_reading(state.distance_cm);
}