Example #1
0
/********** MAIN PROGRAM ************************************************
 *
 * Control background light of a WS-2300 weather station
 * and writes the data to a log file.
 *
 * Just run the program without parameters for usage.
 *
 * It takes two parameters. The first is the log filename with path
 * The second is the config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open2300.conf-dist file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws2300;
	struct config_type config;

	if (argc < 2 || argc > 3)
	{
		print_usage();
	}			

	get_configuration(&config, argv[2]);

	ws2300 = open_weatherstation(config.serial_device_name);

   /* Get on or off */

	if (strcmp(argv[1],"on") == 0)
	{
		light(ws2300,1);
	}
	else if (strcmp(argv[1],"off") == 0)
	{
		light(ws2300,0);
	}
	else
	{
		print_usage();
	}

	close_weatherstation(ws2300);
	
	return (0);
}
Example #2
0
/********** MAIN PROGRAM ************************************************
 *
 * This program reads the max wind data from a WS2300
 * and writes the data to a log file.
 *
 * Log file format:
 * Timestamp Date Time Gust
 *
 * Just run the program without parameters for usage.
 *
 * It takes two parameters. The first is the log filename with path
 * The second is the config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open2300.conf-dist file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws2300;
	FILE *fileptr;
	struct config_type config;
	double tempfloat_max;
	struct timestamp time_max;
	
	get_configuration(&config, argv[2]);

	ws2300 = open_weatherstation(config.serial_device_name);

   /* Get log filename. */

	if (argc < 2 || argc > 3)
	{
		print_usage();
	}			

	fileptr = fopen(argv[1], "a+");
	if (fileptr == NULL)
	{
		printf("Cannot open file %s\n",argv[1]);
		exit(-1);
	}


	/* READ MAX WIND */

	//Get Windspeed max
	wind_minmax(ws2300, config.wind_speed_conv_factor, NULL,
	            &tempfloat_max, NULL, &time_max);
	
	fprintf(fileptr, "%02d/%02d/%04d %02d:%02d",
			time_max.month, time_max.day, time_max.year, time_max.hour, time_max.minute);
	fprintf(fileptr, " %.1f\n", tempfloat_max);

	
	/* RESET MAX WIND */
	
	wind_reset(ws2300, RESET_MAX);

	close_weatherstation(ws2300);
	
	fclose(fileptr);

	exit(0);
}
Example #3
0
int main(int argc, char *argv[]) {
	FILE *fileptr;
	unsigned char data[BUFSIZE];

	int start_adr, len;
	int block_len = 1000;
	int retries = 0;
	char* filename;

	if (argc != 2) {
		fprintf(stderr, "E: no dump file specified.\n");
		print_usage();
	}

	if (argc >= 3) {
		filename = argv[2];
	} else {
		// generate filename based on current time
		time_t t;
		struct tm *tm;
		t = time(NULL);
		tm = localtime(&t);
		if (tm == NULL) {
			perror("localtime");
			exit(EXIT_FAILURE);
		}
		filename = malloc(50);
		sprintf(filename, "tfa.dump.");
		strftime(filename+strlen(filename), sizeof(filename)-strlen(filename), "%Y%m%d.%H%M", tm);
	}

	// need root for (timing) portio
	if (geteuid() != 0) {
		fprintf(stderr, "E: this program needs root privileges to do direct port I/O.\n");
		exit(EXIT_FAILURE);
	}

	// Setup 
	open_weatherstation();

	// Setup file
	fileptr = fopen(filename, "w");
	if (fileptr == NULL) {
		printf("Cannot open file %s\n", filename);
		exit(EXIT_FAILURE);
	}

	// Start.
	start_adr = 0;
	len = 0x7FFF; // 1802*3; //(0x7ef4+259) - start_adr;
	printf("Dumping %d bytes to %s.\n", len, filename);
	memset(data, 0xAA, BUFSIZE);

	while (start_adr < len) {
		int got_len;
		int this_len = block_len;
		if (start_adr + block_len > len)
			this_len = len-start_adr;

		printf("   ... reading %d bytes beginning from %d\n", this_len, start_adr);

		nanodelay();
		eeprom_seek(start_adr);
		got_len = eeprom_read(data+start_adr, this_len);
		if (got_len != this_len) {
			if (got_len == -1 && retries < MAX_RETRIES) {
				retries++;
				fprintf(stderr, "W: eeprom ack failed, retrying read (retries left: %d).\n", MAX_RETRIES-retries);
				close_weatherstation();
				open_weatherstation();
				continue;
			}
			printf("   >>> got     %d bytes\n", got_len);
			fprintf(stderr, "E: got less than requested bytes, dump is probably unusable.\n");
			break;
		}

		start_adr += block_len;
		retries = 0;
	}

	fwrite(data, len, 1, fileptr);

	close_weatherstation();
	fclose(fileptr);
	return(0);
}
Example #4
0
/********** MAIN PROGRAM ************************************************
 *
 * Control background light of a WS-2300 weather station
 * and writes the data to a log file.
 *
 * Just run the program without parameters for usage.
 *
 * It takes two parameters. The first is the log filename with path
 * The second is the config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open2300.conf-dist file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws2300;
	struct config_type config;

	if (argc < 2 || argc > 3)
	{
		print_usage();
	}			

	get_configuration(&config, argv[2]);

	ws2300 = open_weatherstation(config.serial_device_name);

   /* Get on or off */

	if (strcmp(argv[1],"timax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		temperature_indoor_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"timin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		temperature_indoor_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"tiboth") == 0)
	{
		temperature_indoor_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"tomax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		temperature_outdoor_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"tomin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		temperature_outdoor_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"toboth") == 0)
	{
		temperature_outdoor_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"dpmax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		dewpoint_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"dpmin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		dewpoint_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"dpboth") == 0)
	{
		dewpoint_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"wcmax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		windchill_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"wcmin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		windchill_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"wcboth") == 0)
	{
		windchill_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"wmax") == 0)
	{
		wind_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"wmin") == 0)
	{
		wind_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"wboth") == 0)
	{
		wind_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"himax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		humidity_indoor_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"himin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		humidity_indoor_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"hiboth") == 0)
	{
		humidity_indoor_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"homax") == 0 || strcmp(argv[1],"dailymax") == 0)
	{
		humidity_outdoor_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"homin") == 0 || strcmp(argv[1],"dailymin") == 0)
	{
		humidity_outdoor_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"hoboth") == 0)
	{
		humidity_outdoor_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"pmax") == 0)
	{
		pressure_reset(ws2300, RESET_MAX);
	}
	if (strcmp(argv[1],"pmin") == 0)
	{
		pressure_reset(ws2300, RESET_MIN);
	}
	if (strcmp(argv[1],"pboth") == 0)
	{
		pressure_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	if (strcmp(argv[1],"r1max") == 0)
	{
		rain_1h_max_reset(ws2300);
	}
	if (strcmp(argv[1],"r24max") == 0)
	{
		rain_24h_max_reset(ws2300);
	}
	if (strcmp(argv[1],"r1") == 0)
	{
		rain_1h_reset(ws2300);
	}
	if (strcmp(argv[1],"r24") == 0)
	{
		rain_24h_reset(ws2300);
	}
	if (strcmp(argv[1],"rtotal") == 0)
	{
		rain_total_reset(ws2300);
	}

	close_weatherstation(ws2300);
	
	return (0);
}
/********** MAIN PROGRAM ************************************************
 *
 * This program reads the history records from a WS3600
 * weather station at a given record range
 * and prints the data to stdout and to a file.
 * Just run the program without parameters for usage.
 *
 * It uses the config file for device name.
 * Config file locations - see open3600.conf
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws;
	FILE *fileptr;
	unsigned char logline[3000] = "";
	unsigned char data[32768];
	long counter;
	char ch;
	char datestring[50];        //used to hold the date stamp for the log file
	time_t time_lastlog, time_tmp = 0, time_now;
	struct tm time_lastlog_tm, time_tm;
	int current_record, next_record, record_count;
	double temperature_in;
	double temperature_out;
	double dewpoint;
	double windchill;
	double pressure;
	int humidity_in;
	int humidity_out;
	double rain, current_rain;
	double prev_rain = 0;
	
	double windspeed;
	double windgust;
	double winddir_degrees;
	const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
	                           "S","SSW","SW","WSW","W","WNW","NW","NNW"};
  int minute;

	int temp_int1, temp_int2;
	int read_result;
	int i,j;

	if (argc < 2 || argc > 2)
	{
		print_usage();
	}	
	
	// Get serial port from config file

	get_configuration(&config, "");

    // Setup serial port

    // Get in-data and select mode.
	fileptr = fopen(argv[1], "ab+");
	if (fileptr == NULL)
	{
		printf("Cannot open file %s\n",argv[1]);
		exit(0);
	}
	
	fseek(fileptr, 1L, SEEK_END);
	counter = 60;
	do
	{
		counter++;
		if (fseek(fileptr, -counter, SEEK_END) < 0 )
			break;
		ch = getc(fileptr);
	} while (ch != '\n' && ch != '\r');
	if (fscanf(fileptr,"%4d%2d%2d%2d%2d", &temp_int1, &temp_int2,
	           &time_lastlog_tm.tm_mday, &time_lastlog_tm.tm_hour,
	           &time_lastlog_tm.tm_min) == 5)
	{
		time_lastlog_tm.tm_year = temp_int1 - 1900;
		time_lastlog_tm.tm_mon = temp_int2 - 1;	
		time_lastlog_tm.tm_sec = 0;
		time_lastlog_tm.tm_isdst = -1;
	}
	else
	{	//if no valid log we set the date to 1 Jan 1990 0:00
		time_lastlog_tm.tm_year = 1990; 
		time_lastlog_tm.tm_mon = 0;	
		time_lastlog_tm.tm_mday = 1;
		time_lastlog_tm.tm_hour = 0;
	  time_lastlog_tm.tm_min = 0;
		time_lastlog_tm.tm_sec = 0;
		time_lastlog_tm.tm_isdst = -1;
	}
	//printf("%4d,%2d,%2d,%2d,%2d\n", temp_int1, temp_int2,
	//           time_lastlog_tm.tm_mday, time_lastlog_tm.tm_hour,
	//           time_lastlog_tm.tm_min);
	
	time_lastlog = mktime(&time_lastlog_tm);
	i = 0;
	//time_now varialbe will be used to check correctness of time in history records
  time(&time_now);
  //add 10h margin for lower sensitivity of history time (history time may be max 10h higher than local computer time)
  time_now += 36000;
  do {
    ws = open_weatherstation(config.serial_device_name);
    read_result = read_safe(ws, HISTORY_BUFFER_ADR,0x7fff - HISTORY_BUFFER_ADR + 1,data, NULL);
    close_weatherstation(ws);
    i++;
    
    if (read_result != -1)
    {
      //check dates in history
      for (j = 0; j < HISTORY_REC_NO; j++)
      {
        read_history_record(data, j, &config,
  	                        &temperature_in,
  	                        &temperature_out,
  	                        &pressure,
  	                        &humidity_in,
  	                        &humidity_out,
  	                        &rain,
  	                        &windspeed,
  	                        &windgust,
  	                        &winddir_degrees,
  	                        &dewpoint,
  	                        &windchill,
                            &time_tm);
  
        if (time_tm.tm_min != 0xff)
        {
          time_tmp = mktime(&time_tm);
          if (time_tmp > time_now)
          {
            print_log(2,"wrong date in history");
            strftime(datestring,sizeof(datestring),"%Y%m%d%H%M%S %Y-%b-%d %H:%M:%S",
    		         &time_tm);
    		    print_log(2,datestring);
            
            break;
          }
        }
      }
    }
  } while (i < 4 && read_result == -1 && time_tmp <= time_now);		
  
  if (time_lastlog_tm.tm_year == 1990)
  {
    //find oldest record
    
    //go to the end of buffer (FF in minutes)
    next_record = 0;
    do
    {
      current_record = next_record;
      //printf("record1=%i\n",current_record);
      next_record = read_history_record(data, current_record, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);
    } while (time_tm.tm_min < 60);
    /*if (current_record == 0)
    {
      read_history_record(data, 1796, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);
    }*/
    
    //ommit FF's between end and begin of buffer (is only one FF byte for longer used station - with completely filled buffer)
    record_count = 0;
    do
    {
      //printf("record2=%i\n",current_record);
      current_record = next_record;
      next_record = read_history_record(data, current_record, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);
      record_count++;  
    } while (time_tm.tm_min >= 60 && record_count < HISTORY_REC_NO + 1);
      //first log record will have rain = 0 (we don't know previous state of rain counter)
      if (time_tm.tm_min < 60)
          prev_rain = rain;
    //we are at the beginning of buffer now!
  } else
  {
    //find oldest new record
    //go to the end of buffer (FF in minutes)
    next_record = 0;
    do
    {
      //printf("record2=%i\n",current_record);
      current_record = next_record;
      next_record = read_history_record(data, current_record, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);
    } while (time_tm.tm_min < 60 && next_record != 0);
    
    //ommit FF's between end and begin of buffer (is only one FF byte for longer used station - with completely filled buffer)
    do
    {
      //printf("record2=%i\n",current_record);
      current_record = next_record;
      next_record = read_history_record(data, current_record, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);
    } while (time_tm.tm_min >= 60 && next_record != 0);
    
    //find first new record
    record_count = 0;  
    do
    {
      current_record = next_record;
      //printf("record3=%i\n",current_record);
      next_record = read_history_record(data, next_record, &config,
	                        &temperature_in,
	                        &temperature_out,
	                        &pressure,
	                        &humidity_in,
	                        &humidity_out,
	                        &rain,
	                        &windspeed,
	                        &windgust,
	                        &winddir_degrees,
	                        &dewpoint,
	                        &windchill,
                          &time_tm);

      if (time_tm.tm_min < 60)
      {
        time_tmp = mktime(&time_tm);
      }
      
      if (time_tm.tm_min < 60 && time_tmp <= time_lastlog)
      {
        prev_rain = rain;
      }
      
      if (record_count == 0)
        prev_rain = rain;
        
      record_count++; 
      
    } while (time_tmp <= time_lastlog && record_count < HISTORY_REC_NO + 1);
    
    if (time_tmp <= time_lastlog && record_count == HISTORY_REC_NO + 1)
    {
      printf("\nNew records not found\n");
      close_weatherstation(ws);
      fclose(fileptr);
	    exit(0);
    }
  }
	
	//next_record = current_record;
	next_record = current_record;
	if (record_count < HISTORY_REC_NO + 1)
	{
  	do
  	{ 
  	  //printf("record4=%i\n",next_record);
  		next_record = read_history_record(data, next_record, &config,
  	                        &temperature_in,
  	                        &temperature_out,
  	                        &pressure,
  	                        &humidity_in,
  	                        &humidity_out,
  	                        &rain,
  	                        &windspeed,
  	                        &windgust,
  	                        &winddir_degrees,
  	                        &dewpoint,
  	                        &windchill,
                            &time_tm);
  		
  		strcpy(logline,"");
  		
  		// READ TEMPERATURE INDOOR
  		
  		sprintf(logline,"%s%.1f ", logline, temperature_in);
  		
  	
  		// READ TEMPERATURE OUTDOOR
  	
  		sprintf(logline,"%s%.1f ", logline, temperature_out);
  		
  		
  		// READ DEWPOINT
  	
  		sprintf(logline,"%s%.1f ", logline, dewpoint);
  		
  		
  		// READ RELATIVE HUMIDITY INDOOR
  	
  		sprintf(logline,"%s%d ", logline, humidity_in);	
  		
  		
  		// READ RELATIVE HUMIDITY OUTDOOR
  	
  		sprintf(logline,"%s%d ", logline, humidity_out);	 
  	
  	
  		// READ WIND SPEED AND DIRECTION aND WINDCHILL
  	
  		sprintf(logline,"%s%.1f ", logline, windspeed);
  		sprintf(logline,"%s%.1f %s ", logline, winddir_degrees, directions[(int)(winddir_degrees/22.5)]);
  		
  		// READ GUST
  		sprintf(logline,"%s%.2f ", logline, windgust);
  	
  	
  		// READ WINDCHILL
  		
  		sprintf(logline,"%s%.1f ", logline, windchill);
  	
  		
  		// READ RAIN 1H
  	
  		//	sprintf(logline,"%s%.2f ", logline,
  		//        rain_1h(ws, config.rain_conv_factor));
  		//	
  		
  		// READ RAIN 24H
  	
  		//
  		//sprintf(logline,"%s%.2f ", logline,
  		//        rain_24h(ws, config.rain_conv_factor));
  		//
  		
  		// READ RAIN IN INTERVAL
  		if (rain >= prev_rain)
        current_rain = rain - prev_rain;
      else
        current_rain = rain + 4096 - prev_rain;
  		sprintf(logline,"%s%.2f ", logline, current_rain * 0.518);
  		//sprintf(logline,"%s%.2f ", logline, rain);
  		prev_rain = rain;
  		
  		// READ ABSOLUTE PRESSURE
  	
  		sprintf(logline,"%s%.3f ", logline, pressure);
  		
  		
  	
  	
  		// GET DATE AND TIME FOR LOG FILE, PLACE BEFORE ALL DATA IN LOG LINE
  		
  		//	printf("time now: %d\n",time(&basictime));
  		//	time_lastrecord_tm.tm_hour=time_last.hour;
  
      minute = time_tm.tm_min;
  		if (minute < 60)
  		{
        mktime(&time_tm);                 //normalize time_lastlog_tm
  		
  		  strftime(datestring,sizeof(datestring),"%Y%m%d%H%M%S %Y-%b-%d %H:%M:%S",
  		         &time_tm);
  		
  		
  		  // Print out
  		  fseek(fileptr, 0L, SEEK_END);
  		  fprintf(fileptr,"%s %s\n",datestring, logline);
  		  fflush(NULL);
  		}
  	
  	} while (minute < 60);
  }

	// Goodbye and Goodnight
	//close_weatherstation(ws);
	fclose(fileptr);

	return(0);
}
Example #6
0
/********** MAIN PROGRAM ************************************************
 *
 * This program reads current weather data from a WS3600
 * and writes the data to a log file.
 *
 * Log file format:
 * Timestamp Date Time Ti To DP RHi RHo Wind Dir-degree Dir-text WC
 *              Rain1h Rain24h Rain-tot Rel-Press Tendency Forecast
 *
 * Just run the program without parameters for usage.
 *
 * It takes two parameters. The first is the log filename with path
 * The second is the config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open3600.conf file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws;
	FILE *fileptr;
	unsigned char logline[3000] = "";
	char datestring[50];        //used to hold the date stamp for the log file
	const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
	                           "S","SSW","SW","WSW","W","WNW","NW","NNW"};
	double winddir[6];
	char tendency[15];
	char forecast[15];
	time_t basictime;
	unsigned char data[32768];
	int read_result;
	int i;

	get_configuration(&config, argv[2]);

   /* Get log filename. */

	if (argc < 2 || argc > 3)
	{
		print_usage();
	}			

	fileptr = fopen(argv[1], "a+");
	if (fileptr == NULL)
	{
		printf("Cannot open file %s\n",argv[1]);
		exit(-1);
	}
  
  i = 0;
  do {
    ws = open_weatherstation(config.serial_device_name);
    read_result = read_safe(ws, 0, HISTORY_BUFFER_ADR,data, NULL);
    close_weatherstation(ws);
    i++;
  } while (i < 4 && read_result == -1);
  
  if (read_result == -1)
	{
    printf("\nError reading data\n");
	  exit(0);
  }

	/* READ TEMPERATURE INDOOR */
	
	sprintf(logline,"%s%.1f ", logline,
	        temperature_indoor(data));
	

	/* READ TEMPERATURE OUTDOOR */

	sprintf(logline,"%s%.1f ", logline,
	        temperature_outdoor(data));
	
	
	/* READ DEWPOINT */

	sprintf(logline,"%s%.1f ", logline,
	        dewpoint(data));
	
	
	/* READ RELATIVE HUMIDITY INDOOR */

	sprintf(logline,"%s%d ", logline, humidity_indoor(data));	
	
	
	/* READ RELATIVE HUMIDITY OUTDOOR */

	sprintf(logline,"%s%d ", logline, humidity_outdoor(data));	 


	/* READ WIND SPEED AND DIRECTION */

  sprintf(logline,"%s%.1f ", logline,
	       wind_current(data, winddir));
	sprintf(logline,"%s%.1f %s ", logline, winddir[0], directions[(int)(winddir[0]/22.5)]);


	/* READ WINDCHILL */
	
	sprintf(logline,"%s%.1f ", logline,
	        windchill(data));

	
	/* READ RAIN 1H */
	sprintf(logline,"%s%.2f ", logline,
	        rain_1h(data));
	
	/* READ RAIN 24H */

	sprintf(logline,"%s%.2f ", logline,
	        rain_24h(data));

  	// READ RAIN 1W

  sprintf(logline, "%s%.2f ", logline,
		rain_1w(data));
	        
	// READ RAIN 1M

  sprintf(logline, "%s%.2f ", logline,
		rain_1m(data));
	
	/* READ RAIN TOTAL */
	
	sprintf(logline,"%s%.2f ", logline,
	        rain_total(data));

	
	/* READ RELATIVE PRESSURE */

	sprintf(logline,"%s%.3f ", logline,
	        rel_pressure(data));
	

	/* READ TENDENCY AND FORECAST */
	
	tendency_forecast(data, tendency, forecast);
	sprintf(logline,"%s%s %s", logline, tendency, forecast);


	/* GET DATE AND TIME FOR LOG FILE, PLACE BEFORE ALL DATA IN LOG LINE */
	
	time(&basictime);
	strftime(datestring,sizeof(datestring),"%Y%m%d%H%M%S %Y-%b-%d %H:%M:%S",
	         localtime(&basictime));

	// Print out and leave

	// printf("%s %s\n",datestring, logline); //disabled to be used in cron job
	fprintf(fileptr,"%s %s\n",datestring, logline);

//	close_weatherstation(ws);
	
	fclose(fileptr);

	exit(0);
	
	return 0;
}
Example #7
0
/********** MAIN PROGRAM ************************************************
 *
 * This program reads current weather data from a WS2300
 * and writes the data to a log file.
 *
 * Log file format:
 * Timestamp Date Time Ti To DP RHi RHo Wind Dir-degree Dir-text WC
 *              Rain1h Rain24h Rain-tot Rel-Press Tendency Forecast
 *
 * Just run the program without parameters for usage.
 *
 * It takes two parameters. The first is the log filename with path
 * The second is the config file name with path
 * If this parameter is omitted the program will look at the default paths
 * See the open2300.conf-dist file for info
 *
 ***********************************************************************/
int main(int argc, char *argv[])
{
	WEATHERSTATION ws2300;
	FILE *fileptr;
	char tempstring[1000] = "";
	char logline[1000] = "";
    char urlline[3000] = "";
	char pachubeline[2000] = "";
	char datestring[50];        //used to hold the date stamp for the log file
	const char *directions[]= {"N","NNE","NE","ENE","E","ESE","SE","SSE",
	                           "S","SSW","SW","WSW","W","WNW","NW","NNW"};
	double winddir[6];
	int tempint;
	char tendency[15];
	char forecast[15];
	struct config_type config;
	time_t basictime;
	int dataFlag = 1;

	get_configuration(&config, argv[2]);

	ws2300 = open_weatherstation(config.serial_device_name);
	
	/* START WITH URL, ID AND PASSWORD */

	sprintf(urlline, "GET %s?ID=%s&PASSWORD=%s", WEATHER_UNDERGROUND_PATH,
	        config.weather_underground_id,config.weather_underground_password);

			
	/* GET DATE AND TIME FOR URL */
	
	time(&basictime);
	basictime = basictime - atof(config.timezone) * 60 * 60;
	strftime(datestring,sizeof(datestring),"&dateutc=%Y-%m-%d+%H%%3A%M%%3A%S",
	         localtime(&basictime));
	strcat(urlline, datestring);
	
	/* Get log filename. */

	if (argc < 2 || argc > 3)
	{
		print_usage();
	}

	fileptr = fopen(argv[1], "a+");
	if (fileptr == NULL)
	{
		printf("Cannot open file %s\n",argv[1]);
		exit(-1);
	}


	/* READ TEMPERATURE INDOOR */

	//sprintf(logline,"%.1f ", temperature_indoor(ws2300, config.temperature_conv));
	sprintf(tempstring,"%.1f", temperature_indoor(ws2300, config.temperature_conv)); 
	strcat(logline, tempstring);
	
	strcat(pachubeline,"2,");
	strcat(pachubeline,tempstring);
	
	double outdoor = humidity_outdoor(ws2300);
	double dew = dewpoint(ws2300, config.temperature_conv);

    if ( outdoor > 100) dataFlag = 0;
	if (dew < -10.0) dataFlag = 0;
	if (dew > 100.0) dataFlag = 0;
	
	/* READ TEMPERATURE OUTDOOR */

		//sprintf(tempstring,"%.1f ", temperature_outdoor(ws2300, config.temperature_conv));
		sprintf(tempstring,"%.1f", outdoor);
		strcat(logline, " ");
		strcat(logline, tempstring);
		
		if (dataFlag) {
			strcat(pachubeline,"\r\n4,");
			strcat(pachubeline,tempstring);
		
			strcat(urlline,"&tempf=");
			strcat(urlline, tempstring);
		}

		/* READ DEWPOINT */

		//sprintf(tempstring,"%.1f ", dewpoint(ws2300, config.temperature_conv));
		sprintf(tempstring,"%.1f", dew);
		strcat(logline, " ");
		strcat(logline, tempstring);
		if (dataFlag) {
			strcat(pachubeline,"\r\n5,");
			strcat(pachubeline,tempstring);
			
			strcat(urlline,"&dewptf=");
			strcat(urlline, tempstring);
		}
	
	/* READ RELATIVE HUMIDITY INDOOR */

	sprintf(tempstring,"%d", humidity_indoor(ws2300));	
	strcat(logline, " ");
	strcat(logline, tempstring);
	
	strcat(pachubeline,"\r\n3,");
	strcat(pachubeline,tempstring);

	/* READ RELATIVE HUMIDITY OUTDOOR */

	sprintf(tempstring,"%d", humidity_outdoor(ws2300));	 
	strcat(logline, " ");
	strcat(logline, tempstring);
	if (dataFlag) {
		strcat(pachubeline,"\r\n6,");
		strcat(pachubeline,tempstring);
			
		strcat(urlline,"&humidity=");
		strcat(urlline, tempstring);
	}
		
	/* READ WIND SPEED AND DIRECTION */

	sprintf(tempstring,"%.1f",
	       wind_all(ws2300, config.wind_speed_conv_factor, &tempint, winddir));
	
	strcat(logline, " ");
	strcat(logline, tempstring);
	if (dataFlag) {
		strcat(pachubeline,"\r\n7,");
		strcat(pachubeline,tempstring);
			
		strcat(urlline,"&windspeedmph=");
		strcat(urlline, tempstring);
	}
	
	sprintf(tempstring,"%.1f %s ", winddir[0], directions[tempint]);
	strcat(logline, tempstring);

	sprintf(tempstring,"%.1f", winddir[0]);
	strcat(pachubeline,"\r\n8,");
	strcat(pachubeline,tempstring);
			
	/* READ WIND GUST - miles/hour for Weather Underground */

	if (GUST)
	{
		tmpvalue = wind_minmax(ws2300, config.wind_speed_conv_factor, NULL, NULL, NULL, NULL);
		if (tmpvalue>100.0) tmpvalue=0.0;
		sprintf(tempstring, "%.1f",tmpvalue);
		strcat(logline, " ");
		strcat(logline, tempstring);
		
		strcat(pachubeline,"\r\n9,");
		strcat(pachubeline,tempstring);
	
		strcat(urlline,"&windgustmph=");
		strcat(urlline, tempstring);
		
		wind_reset(ws2300, RESET_MIN + RESET_MAX);
	}
	
	/* READ WINDCHILL */

	sprintf(tempstring,"%.1f ", windchill(ws2300, config.temperature_conv));
	strcat(logline, tempstring);

	/* READ RAIN 1H */

	sprintf(tempstring,"%.2f", rain_1h(ws2300, config.rain_conv_factor));
	strcat(logline, " ");
	strcat(logline, tempstring);
	
	strcat(pachubeline,"\r\n10,");
	strcat(pachubeline,tempstring);
	
	strcat(urlline,"&rainin=");
	strcat(urlline, tempstring);	

	/* READ RAIN 24H */

	sprintf(tempstring,"%.2f", rain_24h(ws2300, config.rain_conv_factor));
	strcat(logline, " ");
	strcat(logline, tempstring);
	
	strcat(pachubeline,"\r\n11,");
	strcat(pachubeline,tempstring);
	
	strcat(urlline,"&dailyrainin=");
	strcat(urlline, tempstring);	

	/* READ RAIN TOTAL */

	sprintf(tempstring,"%.2f ", rain_total(ws2300, config.rain_conv_factor));
	strcat(logline, tempstring);


	/* READ RELATIVE PRESSURE */

	sprintf(tempstring,"%.3f", rel_pressure(ws2300, config.pressure_conv_factor));
	strcat(logline, " ");
	strcat(logline, tempstring);
	
	strcat(pachubeline,"\r\n12,");
	strcat(pachubeline,tempstring);
	
	
	sprintf(tempstring, "%.3f", rel_pressure(ws2300, INCHES_HG) );
	strcat(urlline,"&baromin=");
	strcat(urlline, tempstring);

	/* READ TENDENCY AND FORECAST */

	tendency_forecast(ws2300, tendency, forecast);
	sprintf(tempstring,"%s %s ", tendency, forecast);
	strcat(logline, tempstring);

	/* GET DATE AND TIME FOR LOG FILE, PLACE BEFORE ALL DATA IN LOG LINE */

	//time(&basictime);
	strftime(datestring, sizeof(datestring), "%Y%m%d%H%M%S %Y-%b-%d %H:%M:%S",
	         localtime(&basictime));

	// Print out and leave

	// printf("%s %s\n",datestring, logline); //disabled to be used in cron job
	fprintf(fileptr, "%s %s\n", datestring, logline);
	
	fclose(fileptr);
	
	/* ADD SOFTWARE TYPE AND ACTION */
	sprintf(tempstring, "&softwaretype=open2300-%s&action=updateraw", VERSION);
	strcat(urlline, tempstring);
	
	sprintf(tempstring, " HTTP/1.0\r\nUser-Agent: open2300/%s\r\nAccept: */*\r\n"
	                   "Host: %s\r\nConnection: Keep-Alive\r\n\r\n",
	        VERSION, WEATHER_UNDERGROUND_BASEURL);
	strcat(urlline, tempstring);

	
	close_weatherstation(ws2300);
	
	if (DEBUG)
	{
		printf("%s\n",logline);
		printf("%s\n",urlline);
		printf("%s\n",pachubeline);
	}
	else
	{
	 	if (dataFlag) 
		{
			http_request_url(urlline);
		}
	}
	
	return(0);
}
Example #8
0
/********************************************************************
 * open_weatherstation, Windows version
 *
 * Input:   devicename (COM1, COM2 etc)
 * 
 * Returns: Handle to the weatherstation (type WEATHERSTATION)
 *
 ********************************************************************/
WEATHERSTATION open_weatherstation (char *device) {
  WEATHERSTATION ws;
  struct termios adtio;
  unsigned char buffer[BUFFER_SIZE];
  long i;
  print_log(1,"open_weatherstation");

  //calibrate nanodelay function
  microdelay_init(1);

  // Set up gpi pointer for direct register access
  setup_io();

  // Switch GPIO 7..11 to output mode

 /************************************************************************\
  * You are about to change the GPIO settings of your computer.          *
  * Mess this up and it will stop working!                               *
  * It might be a good idea to 'sync' before running this program        *
  * so at least you still have your code changes written to the SD-card! *
 \************************************************************************/

  // Set GPIO pins 7-11 to output
  for (g=7; g<=11; g++)
  {
    INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
    OUT_GPIO(g);
  }

	

	
	
  //Setup serial port
  if ((ws = open(device, O_RDWR | O_NOCTTY)) < 0)
  {
    printf("\nUnable to open serial device %s\n", device);
    exit(EXIT_FAILURE);
  }

  if ( flock(ws, LOCK_EX) < 0 ) {
    perror("\nSerial device is locked by other program\n");
    exit(EXIT_FAILURE);
  }
  //We want full control of what is set and simply reset the entire adtio struct
  memset(&adtio, 0, sizeof(adtio));
  // Serial control options
  adtio.c_cflag &= ~PARENB;      // No parity
  adtio.c_cflag &= ~CSTOPB;      // One stop bit
  adtio.c_cflag &= ~CSIZE;       // Character size mask
  adtio.c_cflag |= CS8;          // Character size 8 bits
  adtio.c_cflag |= CREAD;        // Enable Receiver
  //adtio.c_cflag &= ~CREAD;        // Disable Receiver
  adtio.c_cflag &= ~HUPCL;       // No "hangup"
  adtio.c_cflag &= ~CRTSCTS;     // No flowcontrol
  adtio.c_cflag |= CLOCAL;       // Ignore modem control lines

  // Baudrate, for newer systems
  cfsetispeed(&adtio, BAUDRATE);
  cfsetospeed(&adtio, BAUDRATE);	

  // Serial local options: adtio.c_lflag
  // Raw input = clear ICANON, ECHO, ECHOE, and ISIG
  // Disable misc other local features = clear FLUSHO, NOFLSH, TOSTOP, PENDIN, and IEXTEN
  // So we actually clear all flags in adtio.c_lflag
  adtio.c_lflag = 0;

  // Serial input options: adtio.c_iflag
  // Disable parity check = clear INPCK, PARMRK, and ISTRIP 
  // Disable software flow control = clear IXON, IXOFF, and IXANY
  // Disable any translation of CR and LF = clear INLCR, IGNCR, and ICRNL	
  // Ignore break condition on input = set IGNBRK
  // Ignore parity errors just in case = set IGNPAR;
  // So we can clear all flags except IGNBRK and IGNPAR
  adtio.c_iflag = IGNBRK|IGNPAR;

  // Serial output options
  // Raw output should disable all other output options
  adtio.c_oflag &= ~OPOST;

  adtio.c_cc[VTIME] = 10;		// timer 1s
  adtio.c_cc[VMIN] = 0;		// blocking read until 1 char

  if (tcsetattr(ws, TCSANOW, &adtio) < 0)
  {
	  printf("Unable to initialize serial device");
	  exit(0);
  }
  tcflush(ws, TCIOFLUSH);
  
  for (i = 0; i < 448; i++) {
    buffer[i] = 'U';
  }
  write(ws, buffer, 448);

  set_DTR(ws,0);
  set_RTS(ws,0);
  i = 0;
  do {
    sleep_short(10);
    i++;
  } while (i < INIT_WAIT && !get_DSR(ws));

  if (i == INIT_WAIT)
  {
    print_log(2,"Connection timeout 1");
    printf ("Connection timeout\n");
    close_weatherstation(ws);
    exit(0);
  }
  i = 0;
  do {
    sleep_short(10);
    i++;
  } while (i < INIT_WAIT && get_DSR(ws));

  if (i != INIT_WAIT) {
    set_RTS(ws,1);
    set_DTR(ws,1);
  } else {
    print_log(2,"Connection timeout 2");
    printf ("Connection timeout\n");
    close_weatherstation(ws);
    exit(0);
  }
  write(ws, buffer, 448);
  return ws;
}