mbed Weather Platform firmware http://mbed.org/users/okini3939/notebook/mbed-weather-platform-firmware/

Dependencies:   EthernetNetIf SDHCFileSystem I2CLEDDisp Agentbed NTPClient_NetServices mbed BMP085 HTTPClient ConfigFile I2CLCD

func.cpp

Committer:
okini3939
Date:
2011-01-13
Revision:
7:0d3484dc13a9
Parent:
6:060cb9725ce3
Child:
9:8c63e4f3edab

File content as of revision 7:0d3484dc13a9:

#include "mbed.h"
#include "ConfigFile.h"
#include "SDFileSystem.h"
#include "EthernetNetIf.h"
#include "NTPClient.h"
#include "HTTPClient.h"
#include "conf.h"

#define TIMEZONE 9
//#define NONBLOCKING
/** @file
 * @brief mbed Weather Platform
 */

#define STATION_URL "http://weather.sugakoubou.com/p"
#define TWITTER_URL "http://api.supertweet.net/1/statuses/update.xml"

extern Serial pc;
extern int seq;
extern char filename[];
extern Config conf;
extern DigitalOut led1, led2, led3, led4;

extern EthernetNetIf *eth; 
extern NTPClient *ntp;
extern HTTPClient *clientP, *clientT;
extern float pres, temp, humi, light, anemo, vane, rain, uv, moist, temp2;


void writefile (char *buf) {
    FILE *fp;

    led3 = 1;
    fp = fopen(filename, "a");
    if (fp) {
        fprintf(fp, buf);
        fclose(fp);
    } else {
        led2 = 0;
        conf.filetype = 0;
    }
    led3 = 0;
}

void weatherstations () {
    char post_data[200];
    HTTPText postContent("application/x-www-form-urlencoded");

    led3 = 1;
    strcpy(post_data, "fcd=");
    strcat(post_data, conf.stations_id);
    strcat(post_data, "&pin=");
    strcat(post_data, conf.stations_pin);
    sprintf(&post_data[strlen(post_data)], "&d0=%.2f&d1=%.2f&d2=%.2f&d3=%.2f&d4=%.2f&d5=%.2f&d6=%.2f&d7=%.2f&d8=%.2f&d9=%.2f", pres, temp, humi, anemo, vane, rain, light, uv, moist, temp2);
    postContent.puts(post_data);

#ifdef NONBLOCKING
    Net::poll();
    clientP->post(STATION_URL, postContent, NULL, &cb_clientP);
    Net::poll();
#else
    clientP->post(STATION_URL, postContent, NULL);
#endif
    led3 = 0;
}

void pachube (char *buf) {
    char uri[100];
    HTTPText csvContent("text/csv");

    led3 = 1;
    clientP->setRequestHeader("X-PachubeApiKey", conf.pachube_apikey);
    csvContent.set(buf);
    strcpy(uri, "http://api.pachube.com/v1/feeds/");
    strcat(uri, conf.pachube_feedid);
    strcat(uri, ".csv?_method=put");
#ifdef NONBLOCKING
    Net::poll();
    clientP->post(uri, csvContent, NULL, &cb_clientP);
    Net::poll();
#else
    clientP->post(uri, csvContent, NULL);
#endif
    led3 = 0;
}

void cb_clientP (HTTPResult status) {
    if (status != HTTP_OK) {
        pc.printf("Pachube failure (%d)\r\n", status);
//        pc.printf("Pachube failure (%d, %d)\r\n", status, clientP->getHTTPResponseCode());
    }
}

void twitstr (char *buf, int len) {
    int i, j;
    char c;
    float value;
    time_t sec = time(NULL);
    struct tm *tim = localtime(&sec);

    j = 0;
    for (i = 0; i < strlen(conf.twitter_mesg) && j < len; i ++) {
        c = conf.twitter_mesg[i];
        if (c == '%') {
            i ++;
            c = conf.twitter_mesg[i];
            switch (c) {
            case 'P':
                value = pres;
                break;
            case 'T':
                value = temp;
                break;
            case 'H':
                value = humi;
                break;
            case 'A':
                value = anemo;
                break;
            case 'V':
                value = vane;
                break;
            case 'R':
                value = rain;
                break;
            case 'L':
                value = light;
                break;
            case 'U':
                value = uv;
                break;

            case 'y':
                value = tim->tm_year + 1900;
                break;
            case 'm':
                value = tim->tm_mon;
                break;
            case 'd':
                value = tim->tm_mday;
                break;
            case 'h':
                value = tim->tm_hour;
                break;
            case 'i':
                value = tim->tm_min;
                break;
            case 's':
                value = tim->tm_sec;
                break;

            case '%':
                buf[j] = c;
                j ++;
                continue;
            default:
                continue;
            }

            if (c < 'a') {
                sprintf(&buf[j], "%.2f", value);
            } else {
                sprintf(&buf[j], "%02d", (int)value);
            }
            j = strlen(buf);
        } else {
            buf[j] = c;
            j ++;
        }
    }
    buf[j] = 0;
}

void twitter () {
    HTTPMap msg;
    char buf[170];

    led3 = 1;
    twitstr(buf, sizeof(buf));
    msg["status"] = buf;

    clientT->basicAuth(conf.twitter_user, conf.twitter_pwd);
#ifdef NONBLOCKING
    Net::poll();
    clientT->post(TWITTER_URL, msg, NULL, &cb_clientT); 
    Net::poll();
#else
    clientT->post(TWITTER_URL, msg, NULL); 
#endif
    led3 = 0;
}

void ntpdate () {
    ntp = new NTPClient;
    Host ntpserver(IpAddr(), 123, conf.ntpserver);

#ifdef NONBLOCKING
    Net::poll();
    ntp->setTime(ntpserver, &cb_settime);
    Net::poll();
#else
    ntp->setTime(ntpserver);
    time_t sec = time(NULL) + (60 * 60 * TIMEZONE);
    set_time(sec);
#endif
}

void cb_settime (NTPResult status) {
    if (status == NTP_OK) {
        time_t sec = time(NULL) + (60 * 60 * TIMEZONE);
        set_time(sec);
        pc.printf("Ntp success: %s\r\n", ctime(&sec));
    } else {
        pc.printf("Ntp failure (%d)\r\n", status);
    }
//    ntp->close();
}