sho hashimoto
/
sensor_http_post
posted data are here => http://shokai.mag.keio.ac.jp/sensor-storage/shokai/cds/recent
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "HTTPClient.h" 00003 #include <string> 00004 using namespace std; 00005 00006 Serial pc(USBTX, USBRX); 00007 00008 DigitalOut led1(LED1); // blink 00009 DigitalOut led2(LED2); // blink when http-post 00010 DigitalOut led3(p11); // represent ADC value 00011 00012 AnalogIn adc(p15); 00013 float ain; 00014 00015 HTTPClient http; // use DHCP 00016 00017 const string api_uri = "http://shokai.mag.keio.ac.jp/sensor-storage/shokai/cds/"; 00018 const string mbed_name = "shokai-mbed01"; 00019 const string sensor_name = "CdS"; 00020 00021 void blink_led(DigitalOut led, int num){ 00022 for(int i=0; i < num*2; i++){ 00023 led = !led; 00024 wait(0.1); 00025 } 00026 } 00027 00028 void post_sensor_value(){ 00029 blink_led(led2, 2); 00030 char query[256] = ""; 00031 char result[4096] = ""; // 4kb 00032 sprintf(query, "name=%s&%s=%f", mbed_name.c_str(), sensor_name.c_str(), (float)ain); 00033 pc.printf("post:%s => %s\n", query, api_uri.c_str()); 00034 http.post(api_uri.c_str(), query, result, 4096); 00035 pc.printf("%s\n", result); 00036 blink_led(led2, 2); 00037 } 00038 00039 int main(void){ 00040 led1 = 1; 00041 int count = 0; 00042 while(1) { 00043 ain = adc; 00044 pc.printf("sensor:%f\n", float(ain)); 00045 led1 = !led1; // blink LED 00046 if(ain > 0.1) led3 = 0; // represent CdS value 00047 else led3 = 1; 00048 if(count > 20){ 00049 post_sensor_value(); 00050 count = 0; 00051 } 00052 count++; 00053 wait(0.5); 00054 } 00055 }
Generated on Tue Jul 26 2022 05:37:47 by 1.7.2