mbed Weather Platform post to Pachube

Dependencies:   mbed NetServices

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "BMP085.h"
00003 #include "SHT.h"
00004 #include "WeatherMeters.h"
00005 //#include "I2CLCD.h"
00006 #include "EthernetNetIf.h"
00007 #include "HTTPClient.h"
00008 
00009 // Pachube API key
00010 #define apiKey "xxxxxxxxxxxxxxxxxxxxxx"
00011 // Pachube feed ID
00012 #define environmentID "0000"
00013 
00014 I2C i2c(p9, p10);
00015 
00016 BMP085 bmp085(i2c, BMP085_oss4);
00017 //I2CLCD i2clcd(i2c);
00018 SHT sht11(p12, p11, SHT_high); // sclock, data
00019 WeatherMeters wmeters(p21, p15, p22); // anemo, vane, rain
00020 
00021 DigitalOut led1(LED1), led2(LED2), led3(LED3);
00022 
00023 Serial pc(USBTX, USBRX);
00024 AnalogIn photo(p16);
00025 AnalogIn moist(p18);
00026 AnalogIn uv(p17);
00027 
00028 EthernetNetIf eth;
00029 HTTPClient client;
00030 
00031 float get_photo (AnalogIn &ain) {
00032     float f;
00033     
00034     f = ain * 5.0 / 1000; // A
00035     return f / 0.0000026; // lx
00036 }
00037 
00038 float get_moist (AnalogIn &ain) {
00039     float f;
00040     
00041     f = ain * 5.0; // V
00042     return f / ((3.3 - f) / 10.0); // k ohm
00043 }
00044 
00045 float get_uv (AnalogIn &ain) {
00046     float f;
00047     
00048     f = ain * 5.0 / 100000; // A
00049     return f / 0.000384; // mW/cm2
00050 }
00051 
00052 int main() {
00053     float p, t, h, b, a, v, r, m, u;
00054     Timer timer;
00055     
00056     led1 = 1;
00057 
00058     EthernetErr ethErr = eth.setup();
00059     if (ethErr) {
00060         printf("Error %d in setup.\n", ethErr);
00061         return -1;
00062     } else {
00063         led2 = 1;
00064     }
00065 
00066     timer.start();
00067     
00068     while(1) {
00069         led1 = 0;
00070 
00071         bmp085.update();
00072         p = bmp085.get_pressure();
00073         pc.printf("p:%6.2f hPa / t:%6.2f C\n", p, bmp085.get_temperature());
00074 
00075         sht11.update(SHT_high);
00076         t = sht11.get_temperature();
00077         h = sht11.get_humidity();
00078         pc.printf("t:%6.2f C / h:%6.2f %%\n", t, h);
00079 
00080         a = wmeters.get_windspeed();
00081         v = wmeters.get_windvane();
00082         r = wmeters.get_raingauge();
00083         pc.printf("a:%6.2f m/s / v:%6.2f / r:%6.2f mm\n", a, v, r);
00084 
00085         b = get_photo(photo);
00086         pc.printf("b:%6.2f lx\n", b);
00087         m = get_moist(moist);
00088         pc.printf("m:%6.2f k ohm\n", m);
00089         u = get_uv(uv);
00090         pc.printf("u:%6.2f mW/cm2\n", u);
00091 /*
00092         i2clcd.locate(0, 0);
00093         i2clcd.printf("%4.1f hPa", p);
00094         i2clcd.locate(0, 1);
00095         i2clcd.printf("%2.1f C / %2.1f %%", t, h);
00096 */
00097 
00098         {
00099             char uri[100], data[100];
00100             HTTPResult result;
00101             int response;
00102             HTTPText csvContent("text/csv");
00103 
00104             led3 = 1;
00105             sprintf(data, "%f,%f,%f,%f,%f,%f\r\n", p, t, h, a, v, r);
00106             client.setRequestHeader("X-PachubeApiKey", apiKey);
00107             csvContent.set(data);
00108             strcpy(uri, "http://api.pachube.com/v1/feeds/" environmentID ".csv?_method=put");
00109             result = client.post(uri, csvContent, NULL);
00110             response = client.getHTTPResponseCode();
00111             led3 = 0;
00112         }
00113 
00114         led1 = 1;
00115 
00116         while (timer.read() < 60);
00117         timer.reset();
00118     }
00119 }