mbed Weather Platform post to Pachube
Dependencies: mbed NetServices
main.cpp@0:126d52039477, 2010-12-01 (annotated)
- Committer:
- okini3939
- Date:
- Wed Dec 01 15:13:50 2010 +0000
- Revision:
- 0:126d52039477
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
okini3939 | 0:126d52039477 | 1 | #include "mbed.h" |
okini3939 | 0:126d52039477 | 2 | #include "BMP085.h" |
okini3939 | 0:126d52039477 | 3 | #include "SHT.h" |
okini3939 | 0:126d52039477 | 4 | #include "WeatherMeters.h" |
okini3939 | 0:126d52039477 | 5 | //#include "I2CLCD.h" |
okini3939 | 0:126d52039477 | 6 | #include "EthernetNetIf.h" |
okini3939 | 0:126d52039477 | 7 | #include "HTTPClient.h" |
okini3939 | 0:126d52039477 | 8 | |
okini3939 | 0:126d52039477 | 9 | // Pachube API key |
okini3939 | 0:126d52039477 | 10 | #define apiKey "xxxxxxxxxxxxxxxxxxxxxx" |
okini3939 | 0:126d52039477 | 11 | // Pachube feed ID |
okini3939 | 0:126d52039477 | 12 | #define environmentID "0000" |
okini3939 | 0:126d52039477 | 13 | |
okini3939 | 0:126d52039477 | 14 | I2C i2c(p9, p10); |
okini3939 | 0:126d52039477 | 15 | |
okini3939 | 0:126d52039477 | 16 | BMP085 bmp085(i2c, BMP085_oss4); |
okini3939 | 0:126d52039477 | 17 | //I2CLCD i2clcd(i2c); |
okini3939 | 0:126d52039477 | 18 | SHT sht11(p12, p11, SHT_high); // sclock, data |
okini3939 | 0:126d52039477 | 19 | WeatherMeters wmeters(p21, p15, p22); // anemo, vane, rain |
okini3939 | 0:126d52039477 | 20 | |
okini3939 | 0:126d52039477 | 21 | DigitalOut led1(LED1), led2(LED2), led3(LED3); |
okini3939 | 0:126d52039477 | 22 | |
okini3939 | 0:126d52039477 | 23 | Serial pc(USBTX, USBRX); |
okini3939 | 0:126d52039477 | 24 | AnalogIn photo(p16); |
okini3939 | 0:126d52039477 | 25 | AnalogIn moist(p18); |
okini3939 | 0:126d52039477 | 26 | AnalogIn uv(p17); |
okini3939 | 0:126d52039477 | 27 | |
okini3939 | 0:126d52039477 | 28 | EthernetNetIf eth; |
okini3939 | 0:126d52039477 | 29 | HTTPClient client; |
okini3939 | 0:126d52039477 | 30 | |
okini3939 | 0:126d52039477 | 31 | float get_photo (AnalogIn &ain) { |
okini3939 | 0:126d52039477 | 32 | float f; |
okini3939 | 0:126d52039477 | 33 | |
okini3939 | 0:126d52039477 | 34 | f = ain * 5.0 / 1000; // A |
okini3939 | 0:126d52039477 | 35 | return f / 0.0000026; // lx |
okini3939 | 0:126d52039477 | 36 | } |
okini3939 | 0:126d52039477 | 37 | |
okini3939 | 0:126d52039477 | 38 | float get_moist (AnalogIn &ain) { |
okini3939 | 0:126d52039477 | 39 | float f; |
okini3939 | 0:126d52039477 | 40 | |
okini3939 | 0:126d52039477 | 41 | f = ain * 5.0; // V |
okini3939 | 0:126d52039477 | 42 | return f / ((3.3 - f) / 10.0); // k ohm |
okini3939 | 0:126d52039477 | 43 | } |
okini3939 | 0:126d52039477 | 44 | |
okini3939 | 0:126d52039477 | 45 | float get_uv (AnalogIn &ain) { |
okini3939 | 0:126d52039477 | 46 | float f; |
okini3939 | 0:126d52039477 | 47 | |
okini3939 | 0:126d52039477 | 48 | f = ain * 5.0 / 100000; // A |
okini3939 | 0:126d52039477 | 49 | return f / 0.000384; // mW/cm2 |
okini3939 | 0:126d52039477 | 50 | } |
okini3939 | 0:126d52039477 | 51 | |
okini3939 | 0:126d52039477 | 52 | int main() { |
okini3939 | 0:126d52039477 | 53 | float p, t, h, b, a, v, r, m, u; |
okini3939 | 0:126d52039477 | 54 | Timer timer; |
okini3939 | 0:126d52039477 | 55 | |
okini3939 | 0:126d52039477 | 56 | led1 = 1; |
okini3939 | 0:126d52039477 | 57 | |
okini3939 | 0:126d52039477 | 58 | EthernetErr ethErr = eth.setup(); |
okini3939 | 0:126d52039477 | 59 | if (ethErr) { |
okini3939 | 0:126d52039477 | 60 | printf("Error %d in setup.\n", ethErr); |
okini3939 | 0:126d52039477 | 61 | return -1; |
okini3939 | 0:126d52039477 | 62 | } else { |
okini3939 | 0:126d52039477 | 63 | led2 = 1; |
okini3939 | 0:126d52039477 | 64 | } |
okini3939 | 0:126d52039477 | 65 | |
okini3939 | 0:126d52039477 | 66 | timer.start(); |
okini3939 | 0:126d52039477 | 67 | |
okini3939 | 0:126d52039477 | 68 | while(1) { |
okini3939 | 0:126d52039477 | 69 | led1 = 0; |
okini3939 | 0:126d52039477 | 70 | |
okini3939 | 0:126d52039477 | 71 | bmp085.update(); |
okini3939 | 0:126d52039477 | 72 | p = bmp085.get_pressure(); |
okini3939 | 0:126d52039477 | 73 | pc.printf("p:%6.2f hPa / t:%6.2f C\n", p, bmp085.get_temperature()); |
okini3939 | 0:126d52039477 | 74 | |
okini3939 | 0:126d52039477 | 75 | sht11.update(SHT_high); |
okini3939 | 0:126d52039477 | 76 | t = sht11.get_temperature(); |
okini3939 | 0:126d52039477 | 77 | h = sht11.get_humidity(); |
okini3939 | 0:126d52039477 | 78 | pc.printf("t:%6.2f C / h:%6.2f %%\n", t, h); |
okini3939 | 0:126d52039477 | 79 | |
okini3939 | 0:126d52039477 | 80 | a = wmeters.get_windspeed(); |
okini3939 | 0:126d52039477 | 81 | v = wmeters.get_windvane(); |
okini3939 | 0:126d52039477 | 82 | r = wmeters.get_raingauge(); |
okini3939 | 0:126d52039477 | 83 | pc.printf("a:%6.2f m/s / v:%6.2f / r:%6.2f mm\n", a, v, r); |
okini3939 | 0:126d52039477 | 84 | |
okini3939 | 0:126d52039477 | 85 | b = get_photo(photo); |
okini3939 | 0:126d52039477 | 86 | pc.printf("b:%6.2f lx\n", b); |
okini3939 | 0:126d52039477 | 87 | m = get_moist(moist); |
okini3939 | 0:126d52039477 | 88 | pc.printf("m:%6.2f k ohm\n", m); |
okini3939 | 0:126d52039477 | 89 | u = get_uv(uv); |
okini3939 | 0:126d52039477 | 90 | pc.printf("u:%6.2f mW/cm2\n", u); |
okini3939 | 0:126d52039477 | 91 | /* |
okini3939 | 0:126d52039477 | 92 | i2clcd.locate(0, 0); |
okini3939 | 0:126d52039477 | 93 | i2clcd.printf("%4.1f hPa", p); |
okini3939 | 0:126d52039477 | 94 | i2clcd.locate(0, 1); |
okini3939 | 0:126d52039477 | 95 | i2clcd.printf("%2.1f C / %2.1f %%", t, h); |
okini3939 | 0:126d52039477 | 96 | */ |
okini3939 | 0:126d52039477 | 97 | |
okini3939 | 0:126d52039477 | 98 | { |
okini3939 | 0:126d52039477 | 99 | char uri[100], data[100]; |
okini3939 | 0:126d52039477 | 100 | HTTPResult result; |
okini3939 | 0:126d52039477 | 101 | int response; |
okini3939 | 0:126d52039477 | 102 | HTTPText csvContent("text/csv"); |
okini3939 | 0:126d52039477 | 103 | |
okini3939 | 0:126d52039477 | 104 | led3 = 1; |
okini3939 | 0:126d52039477 | 105 | sprintf(data, "%f,%f,%f,%f,%f,%f\r\n", p, t, h, a, v, r); |
okini3939 | 0:126d52039477 | 106 | client.setRequestHeader("X-PachubeApiKey", apiKey); |
okini3939 | 0:126d52039477 | 107 | csvContent.set(data); |
okini3939 | 0:126d52039477 | 108 | strcpy(uri, "http://api.pachube.com/v1/feeds/" environmentID ".csv?_method=put"); |
okini3939 | 0:126d52039477 | 109 | result = client.post(uri, csvContent, NULL); |
okini3939 | 0:126d52039477 | 110 | response = client.getHTTPResponseCode(); |
okini3939 | 0:126d52039477 | 111 | led3 = 0; |
okini3939 | 0:126d52039477 | 112 | } |
okini3939 | 0:126d52039477 | 113 | |
okini3939 | 0:126d52039477 | 114 | led1 = 1; |
okini3939 | 0:126d52039477 | 115 | |
okini3939 | 0:126d52039477 | 116 | while (timer.read() < 60); |
okini3939 | 0:126d52039477 | 117 | timer.reset(); |
okini3939 | 0:126d52039477 | 118 | } |
okini3939 | 0:126d52039477 | 119 | } |