Thread: newbie needs help with code

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    3

    newbie needs help with code

    hi all,
    i am using open2300 for my weather station, and i want to send to hamweather.net, not wunderground. here is my code:
    Code:
    /*  open2300 - wu2300.c
     *  
     *  Version 1.10
     *  
     *  Control WS2300 weather station
     *  
     *  Copyright 2004-2005, Kenneth Lavrsen
     *  This program is published under the GNU General Public license
     */
    
    #define DEBUG 0  // wu2300 stops writing to standard out if setting this to 0
    
    #include "rw2300.h"
    
    /********** MAIN PROGRAM ************************************************
     *
     * This program reads all current weather data from a WS2300
     * and sends it to Weather Underground.
     *
     * It takes one parameter which 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;
    	unsigned char urlline[3000] = "";
    	char datestring[50];        //used to hold the date stamp for the log file
    	double tempfloat;
    	time_t basictime;
    
    	get_configuration(&config, argv[1]);
    
    	ws2300 = open_weatherstation(config.serial_device_name);
    
    	
    	/* START WITH URL, ID AND PASSWORD */
    
    	sprintf(urlline, "http://%s%s?ID=%s&PASSWORD=%s",
    			WEATHER_UNDERGROUND_BASEURL,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));
    	sprintf(urlline, "%s%s", urlline, datestring);
    
    
    	/* READ TEMPERATURE OUTDOOR - deg F for Weather Underground */
    
    	sprintf(urlline, "%s&tempf=%.2f", urlline,
    	        temperature_outdoor(ws2300, FAHRENHEIT) );
    
    
    	/* READ DEWPOINT - deg F for Weather Underground*/
    	
    	sprintf(urlline, "%s&dewptf=%.2f", urlline, dewpoint(ws2300, FAHRENHEIT) );
    
    
    	/* READ RELATIVE HUMIDITY OUTDOOR */
    
    	sprintf(urlline, "%s&humidity=%d", urlline, humidity_outdoor(ws2300) );
    
    
    	/* READ WIND SPEED AND DIRECTION - miles/hour for Weather Underground */
    
    	sprintf(urlline,"%s&windspeedmph=%.2f", urlline,
    	        wind_current(ws2300, MILES_PER_HOUR, &tempfloat) );
    	sprintf(urlline,"%s&winddir=%.1f",	urlline, tempfloat);
    
    
    	/* READ RAIN 1H - inches for Weather Underground */
    	
    	sprintf(urlline,"%s&rainin=%.2f", urlline, rain_1h(ws2300, INCHES) );
    
    
    	/* READ RAIN 24H - inches for Weather Underground */
    
    	sprintf(urlline,"%s&dailyrainin=%.2f", urlline, rain_24h(ws2300, INCHES) );
    
    
    	/* READ RELATIVE PRESSURE - Inches of Hg for Weather Underground */
    
    	sprintf(urlline,"%s&baromin=%.3f",urlline,
    	        rel_pressure(ws2300, INCHES_HG) );
    
    
    	/* ADD SOFTWARE TYPE AND ACTION */
    	sprintf(urlline,"%s&softwaretype=%s%s&action=updateraw",urlline,
    	        WEATHER_UNDERGROUND_SOFTWARETYPE,VERSION);
    
    
    	/* SEND DATA TO WEATHER UNDERGROUND AS HTTP REQUEST */
    	/* or print the URL if DEBUG is enabled in the top of this file */
    
    	close_weatherstation(ws2300);
    
    	if (DEBUG)
    	{
    		printf("%s\n",urlline);
    	}
    	else
    	{
    		http_request_url(urlline);
    	}
    	
    	return(0);
    }
    and i need it to send this:
    Code:
    http://www.hamweather.net/weatherstations/pwsupdate.php?
    ID=STATIONID&PASSWORD=password
    &dateutc=2006-05-03+03%3A04%3A00&winddir=107&windspeedmph=12.5
    &win dgustmph=12.5&tempf=47.30&rainin=0.00&baromin=29.26&dewptf=41.13
    &humidity=79 &weather=%20&clouds=SKC&softwaretype=&action=updateraw
    **modedit: line folded for display only - it's only one line **
    maybe someone in here can help me out

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Send us the "rw2300.h".
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > get_configuration(&config, argv[1]);
    Did you run the program with a command line parameter?
    Does this function return a result if the file doesn't exist or contains bad data?

    > WEATHER_UNDERGROUND_BASEURL
    I'm assuming this is
    #define WEATHER_UNDERGROUND_BASEURL "somesite"
    in your header file. Did you change it?

    > sprintf(urlline, "%s%s", urlline, datestring);
    This may produce undefined results since you use the same array for both output, and as an input.
    In this case, as simple
    strcat( urlline, datestring);
    would suffice.

    More complex cases should sprintf() to a temporary buffer all the things you want to append, then call strcat as shown.

    > and i need it to send this:
    So is this actually what gets printed if you turn on DEBUG?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    Code:
    /* open2300 - rw2300.h
     * Include file for the open2300 read and write functions
     * including the data conversion functions
     * version 1.10
     */
     
    #ifndef _INCLUDE_RW2300_H_
    #define _INCLUDE_RW2300_H_ 
    
    #ifdef WIN32
    #include "win2300.h"
    #endif
    
    #ifndef WIN32
    #include "linux2300.h"
    #endif
    
    #include <string.h>
    #include <fcntl.h>
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <math.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    #define MAXRETRIES          50
    #define MAXWINDRETRIES      20
    #define WRITENIB            0x42
    #define SETBIT              0x12
    #define UNSETBIT            0x32
    #define WRITEACK            0x10
    #define SETACK              0x04
    #define UNSETACK            0x0C
    #define RESET_MIN           0x01
    #define RESET_MAX           0x02
    
    #define METERS_PER_SECOND   1.0
    #define KILOMETERS_PER_HOUR 3.6
    #define MILES_PER_HOUR      2.23693629
    #define CELCIUS             0
    #define FAHRENHEIT          1
    #define MILLIMETERS         1
    #define INCHES              25.4
    #define HECTOPASCAL         1.0
    #define MILLIBARS           1.0
    #define INCHES_HG           33.8638864
                
    
    /* ONLY EDIT THESE IF WEATHER UNDERGROUND CHANGES URL */
    #define WEATHER_UNDERGROUND_BASEURL "weatherstation.wunderground.com"
    #define WEATHER_UNDERGROUND_PATH "/weatherstation/updateweatherstation.php"
    
    #define WEATHER_UNDERGROUND_SOFTWARETYPE   "open2300%20v"
    
    #define MAX_APRS_HOSTS	6
    
    typedef struct {
    	char name[50];
    	int port;
    } hostdata;
    
    struct config_type
    {
    	char   serial_device_name[50];
    	char   citizen_weather_id[30];
    	char   citizen_weather_latitude[20];
    	char   citizen_weather_longitude[20];
    	hostdata aprs_host[MAX_APRS_HOSTS]; // max 6 possible aprs hosts 1 primary and 5 alternate
    	int    num_hosts;					// total defined hosts
    	char   weather_underground_id[30];
    	char   weather_underground_password[50];
    	char   timezone[6];                //not integer because of half hour time zones
    	double wind_speed_conv_factor;     //from m/s to km/h or miles/hour
    	int    temperature_conv;           //0=Celcius, 1=Fahrenheit
    	double rain_conv_factor;           //from mm to inch
    	double pressure_conv_factor;       //from hPa (=millibar) to mmHg
    	char   mysql_host[50];             //Either localhost, IP address or hostname
    	char   mysql_user[25];
    	char   mysql_passwd[25];
    	char   mysql_database[30];
    	int    mysql_port;                 //0 works for local connection
    	char   pgsql_connect[128];
    	char   pgsql_table[25];
    	char   pgsql_station[25];
    };
    
    struct timestamp
    {
    	int minute;
    	int hour;
    	int day;
    	int month;
    	int year;
    };
    
    
    /* Weather data functions */
    
    double temperature_indoor(WEATHERSTATION ws2300, int temperature_conv);
    
    void temperature_indoor_minmax(WEATHERSTATION ws2300,
                                   int temperature_conv,
                                   double *temp_min,
                                   double *temp_max,
                                   struct timestamp *time_min,
                                   struct timestamp *time_max);
    
    int temperature_indoor_reset(WEATHERSTATION ws2300, char minmax);
    
    double temperature_outdoor(WEATHERSTATION ws2300, int temperature_conv);
    
    void temperature_outdoor_minmax(WEATHERSTATION ws2300,
                                    int temperature_conv,
                                    double *temp_min,
                                    double *temp_max,
                                    struct timestamp *time_min,
                                    struct timestamp *time_max);
    
    int temperature_outdoor_reset(WEATHERSTATION ws2300, char minmax);
    
    double dewpoint(WEATHERSTATION ws2300, int temperature_conv);
    
    void dewpoint_minmax(WEATHERSTATION ws2300,
    					int temperature_conv,
    					double *dp_min,
    					double *dp_max,
    					struct timestamp *time_min,
    					struct timestamp *time_max);
    					
    int dewpoint_reset(WEATHERSTATION ws2300, char minmax);
    
    int humidity_indoor(WEATHERSTATION ws2300);
    
    int humidity_indoor_all(WEATHERSTATION ws2300,
    					int *hum_min,
    					int *hum_max,
    					struct timestamp *time_min,
    					struct timestamp *time_max);
    
    int humidity_indoor_reset(WEATHERSTATION ws2300, char minmax);
    
    int humidity_outdoor(WEATHERSTATION ws2300);
    
    int humidity_outdoor_all(WEATHERSTATION ws2300,
    					int *hum_min,
    					int *hum_max,
    					struct timestamp *time_min,
    					struct timestamp *time_max);
    
    int humidity_outdoor_reset(WEATHERSTATION ws2300, char minmax);
    
    double wind_current(WEATHERSTATION ws2300,
                        double wind_speed_conv_factor,
                        double *winddir);
    
    double wind_all(WEATHERSTATION ws2300,
                    double wind_speed_conv_factor,
                    int *winddir_index,
                    double *winddir);
    
    double wind_minmax(WEATHERSTATION ws2300,
                     double wind_speed_conv_factor,
                     double *wind_min,
                     double *wind_max,
                     struct timestamp *time_min,
                     struct timestamp *time_max);
                     
    int wind_reset(WEATHERSTATION ws2300, char minmax);
    
    double windchill(WEATHERSTATION ws2300, int temperature_conv);
    
    void windchill_minmax(WEATHERSTATION ws2300,
                          int temperature_conv,
                          double *wc_min,
                          double *wc_max,
                          struct timestamp *time_min,
                          struct timestamp *time_max);
                          
    int windchill_reset(WEATHERSTATION ws2300, char minmax);
    
    double rain_1h(WEATHERSTATION ws2300, double rain_conv_factor);
    
    double rain_1h_all(WEATHERSTATION ws2300,
                       double rain_conv_factor,
                       double *rain_max,
                       struct timestamp *time_max);
    
    double rain_24h(WEATHERSTATION ws2300, double rain_conv_factor);
    
    int rain_1h_max_reset(WEATHERSTATION ws2300);
    
    int rain_1h_reset(WEATHERSTATION ws2300);
    
    double rain_24h_all(WEATHERSTATION ws2300,
                       double rain_conv_factor,
                       double *rain_max,
                       struct timestamp *time_max);
    
    int rain_24h_max_reset(WEATHERSTATION ws2300);
    
    int rain_24h_reset(WEATHERSTATION ws2300);
    
    double rain_total(WEATHERSTATION ws2300, double rain_conv_factor);
    
    double rain_total_all(WEATHERSTATION ws2300,
                       double rain_conv_factor,
                       struct timestamp *time_max);
    
    int rain_total_reset(WEATHERSTATION ws2300);
    
    double rel_pressure(WEATHERSTATION ws2300, double pressure_conv_factor);
    
    void rel_pressure_minmax(WEATHERSTATION ws2300,
                             double pressure_conv_factor,
                             double *pres_min,
                             double *pres_max,
                             struct timestamp *time_min,
                             struct timestamp *time_max);
    
    double abs_pressure(WEATHERSTATION ws2300, double pressure_conv_factor);
    
    void abs_pressure_minmax(WEATHERSTATION ws2300,
                             double pressure_conv_factor,
                             double *pres_min,
                             double *pres_max,
                             struct timestamp *time_min,
                             struct timestamp *time_max);
    
    int pressure_reset(WEATHERSTATION ws2300, char minmax);
    
    double pressure_correction(WEATHERSTATION ws2300, double pressure_conv_factor);
    
    void tendency_forecast(WEATHERSTATION ws2300, char *tendency, char *forecast);
    
    int read_history_info(WEATHERSTATION ws2300, int *interval, int *countdown,
                          struct timestamp *time_last, int *no_records);
    
    int read_history_record(WEATHERSTATION ws2300,
                            int record,
                            struct config_type *config,
                            double *temperature_indoor,
                            double *temperature_outdoor,
                            double *pressure,
                            int *humidity_indoor,
                            int *humidity_outdoor,
                            double *raincount,
                            double *windspeed,
                            double *winddir_degrees,
                            double *dewpoint,
                            double *windchill);
                            
    void light(WEATHERSTATION ws2300, int control);
    
    
    /* Generic functions */
    
    void read_error_exit(void);
    
    void write_error_exit(void);
    
    void print_usage(void);
    
    int get_configuration(struct config_type *, char *path);
    
    WEATHERSTATION open_weatherstation(char *device);
    
    void close_weatherstation(WEATHERSTATION ws);
    
    void address_encoder(int address_in, unsigned char *address_out);
    
    void data_encoder(int number, unsigned char encode_constant,
    				  unsigned char *data_in, unsigned char *data_out);
    
    unsigned char numberof_encoder(int number);
    
    unsigned char command_check0123(unsigned char *command, int sequence);
    
    unsigned char command_check4(int number);
    
    unsigned char data_checksum(unsigned char *data, int number);
    
    int initialize(WEATHERSTATION ws2300);
    
    void reset_06(WEATHERSTATION ws2300);
    
    int read_data(WEATHERSTATION ws2300, int address, int number,
    			  unsigned char *readdata, unsigned char *commanddata);
    
    int write_data(WEATHERSTATION ws2300, int address, int number,
    			   unsigned char encode_constant, unsigned char *writedata,
    			   unsigned char *commanddata);
    
    int read_safe(WEATHERSTATION ws2300, int address, int number,
    			  unsigned char *readdata, unsigned char *commanddata);
    			  
    int write_safe(WEATHERSTATION ws2300, int address, int number,
    			   unsigned char encode_constant, unsigned char *writedata,
    			   unsigned char *commanddata);
    
    
    /* Platform dependent functions */
    int read_device(WEATHERSTATION serdevice, unsigned char *buffer, int size);
    int write_device(WEATHERSTATION serdevice, unsigned char *buffer, int size);
    void sleep_short(int milliseconds);
    void sleep_long(int seconds);
    int http_request_url(char *urlline);
    int citizen_weather_send(struct config_type *config, char *datastring);
    
    #endif /* _INCLUDE_RW2300_H_ */

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Code:
    /* ONLY EDIT THESE IF WEATHER UNDERGROUND CHANGES URL */
    #define WEATHER_UNDERGROUND_BASEURL "weatherstation.wunderground.com"
    #define WEATHER_UNDERGROUND_PATH "/weatherstation/updateweatherstation.php"
    You see?
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Registered User
    Join Date
    Jul 2006
    Posts
    3
    i see, but i dont think the url is the only problem, i think the sending format is different

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    If you know the format and it can be set by header constants you can do it. But if it needs manipulation of functions it can't be done with this header since you don't have it functions body.
    Code:
    #define MAXRETRIES          50
    #define MAXWINDRETRIES      20
    #define WRITENIB            0x42
    #define SETBIT              0x12
    #define UNSETBIT            0x32
    #define WRITEACK            0x10
    #define SETACK              0x04
    #define UNSETACK            0x0C
    #define RESET_MIN           0x01
    #define RESET_MAX           0x02
    
    #define METERS_PER_SECOND   1.0
    #define KILOMETERS_PER_HOUR 3.6
    #define MILES_PER_HOUR      2.23693629
    #define CELCIUS             0
    #define FAHRENHEIT          1
    #define MILLIMETERS         1
    #define INCHES              25.4
    #define HECTOPASCAL         1.0
    #define MILLIBARS           1.0
    #define INCHES_HG           33.8638864
    Last edited by siavoshkc; 07-23-2006 at 08:57 PM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  3. newbie reading code
    By franziss in forum C++ Programming
    Replies: 9
    Last Post: 08-25-2005, 12:18 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM