mbeduino + Weatherduino Weather Stations post test

Dependencies:   mbed

main.cpp

Committer:
okini3939
Date:
2010-10-26
Revision:
0:10bcaa7c2253

File content as of revision 0:10bcaa7c2253:

#include "mbed.h"
#include "BMP085.h"
#include "SHT.h"
#include "WeatherMeters.h"
//#include "I2CLCD.h"
#include "KITTScanner.h"
#include "EthernetNetIf.h"
#include "HTTPClient.h"

// ステーションの登録ID
#define STATIONID "0000"
// ステーションのPIN
#define STATIONPIN "xxxxxxxxxxx"
// 投稿間隔(秒)
#define WAIT 300
// 
#define URI "http://weather.sugakoubou.com/post.php"


I2C i2c(p9, p10);

BMP085 bmp085(i2c, BMP085_oss4);
//I2CLCD i2clcd(i2c);
SHT sht11(p24, p12, SHT_high); // sclock, data
WeatherMeters wmeters(p30, p17, p21); // anemo, vane, rain

Serial pc(USBTX, USBRX);
AnalogIn photo(p18);
AnalogIn moist(p16);
AnalogIn uv(p15);

EthernetNetIf eth;
HTTPClient http;

KITTScanner kitt;

float get_photo (AnalogIn &ain) {
    float f;
    
    f = ain * 5.0 / 1000; // A
    return f / 0.0000026; // lx
}

float get_moist (AnalogIn &ain) {
    float f;
    
    f = ain * 5.0; // V
    return f / ((3.3 - f) / 10.0); // k ohm
}

float get_uv (AnalogIn &ain) {
    float f;
    
    f = ain * 5.0 / 100000; // A
    return f / 0.000384; // mW/cm2
}

void http_post (float *values, int num) {
    int i;
    char post_data[200];
    HTTPText post("application/x-www-form-urlencoded"), txt;
    HTTPResult r;
    
    strcpy(post_data, "fcd=" STATIONID "&pin=" STATIONPIN);
    for (i = 0; i < num; i ++) {
        sprintf(&post_data[strlen(post_data)], "&d%d=%.2f", i, values[i]);
    }
    printf("Post :%s\n", post_data); 
    post.puts(post_data);
    
    r = http.post(URI, post, &txt);
    if (r == HTTP_OK) {
        printf("Result :\"%s\"\n", txt.gets()); 
    } else {
        printf("Error %d \"%s\"\n", r, txt.gets());
    }
}

int main() {
    float p, t, h, b, a, v, r, m, u;
    float values[10];
    IpAddr ip;

    kitt.start();
    printf("Setting up...\n");
    EthernetErr ethErr = eth.setup();
    if (ethErr) {
        printf("Error %d in setup.\n", ethErr);
        return -1;
    }
    ip = eth.getIp();
    printf("Setup OK %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
    kitt.stop();

    while(1) {
        kitt.start();

        bmp085.update();
        p = bmp085.get_pressure();
        pc.printf("p:%6.2f hPa / t:%6.2f C\n", p, bmp085.get_temperature());

        sht11.update(SHT_high);
        t = sht11.get_temperature();
        h = sht11.get_humidity();
        pc.printf("t:%6.2f C / h:%6.2f %%\n", t, h);

        a = wmeters.get_windspeed();
        v = wmeters.get_windvane();
        r = wmeters.get_raingauge();
        pc.printf("a:%6.2f m/s / v:%6.2f / r:%6.2f mm\n", a, v, r);

        b = get_photo(photo);
        pc.printf("b:%6.2f lx\n", b);
        m = get_moist(moist);
        pc.printf("m:%6.2f k ohm\n", m);
        u = get_uv(uv);
        pc.printf("u:%6.2f mW/cm2\n", u);
/*
        i2clcd.locate(0, 0);
        i2clcd.printf("%4.1f hPa", p);
        i2clcd.locate(0, 1);
        i2clcd.printf("%2.1f C / %2.1f %%", t, h);
*/
        values[0] = p; values[9] = 0;
        values[1] = t; values[2] = h;
        values[3] = a; values[4] = v; values[5] = r;
        values[6] = b; values[7] = u; values[8] = u;
        http_post(values, 10);

        kitt.stop();
        wait(WAIT);
    }
}