ch8_mbed_websocket_client to sockets.mbed.org

Dependencies:   EthernetInterface HTTPD WebSocketClient mbed-rpc mbed-rtos-for-old mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "HTTPD.h"
00004 #include "Websocket.h"
00005 
00006 
00007 
00008 EthernetInterface *eth;
00009 HTTPD *httpd;
00010 
00011 
00012 Serial pc(USBTX, USBRX);
00013 InterruptIn probe(p8);
00014 LocalFileSystem local("local");
00015 DigitalOut led1(LED1), led2(LED2),led3(LED3);
00016 Timer timer;
00017 Ticker ticker;
00018 
00019 uint32_t low_time = 0;
00020 //uint32_t low_time = 0;
00021 
00022 void down();
00023 void up();
00024 
00025 void tick()
00026 {
00027     pc.printf("%d us, %f %%\r\n", low_time, low_time / (30.0 * 1000000));
00028     low_time = 0;
00029    
00030 }
00031  
00032 void down()
00033 {
00034     probe.rise(&up);
00035     led1 = 0;
00036     timer.start();
00037 }
00038 
00039 void up()
00040 {
00041     led1 = 1;
00042     timer.stop();
00043     probe.fall(&down);
00044     
00045     low_time += timer.read_us();
00046     timer.reset();
00047 }
00048 
00049 void callback_api(int id) {
00050     int i, n;
00051     char low_time_buf[256];
00052     char buf[256];
00053     
00054     sprintf( low_time_buf, "%f", low_time / (30.0 * 1000000));
00055     strcpy(buf, "{"); 
00056     strcat(buf, "success: true ,");
00057     strcat(buf, "data: {");
00058     strcat(buf, "lowpulseoccupancytime: ");
00059     strcat(buf, low_time_buf);
00060     strcat(buf, "}");
00061     strcat(buf, "}");
00062 
00063     n = strlen(buf);
00064     i = httpd->receive(id, &buf[n], sizeof(buf) - n);
00065     if (i < 0) return;
00066     i += n;
00067     buf[i] = 0;
00068     printf("API %d %s\r\n", id, buf);
00069     httpd->send(id, buf, i, "Content-Type: text/plain\r\n");
00070 }
00071 
00072 int main() {
00073     pc.baud(115200);
00074     pc.printf("Dust Sensor test\r\n");
00075     probe.fall(&down);
00076     ticker.attach(tick, 30);
00077     
00078     printf("HTTP Server...\r\n");
00079 
00080     eth = new EthernetInterface;
00081     eth->init("192.168.21.23", "255.255.255.0", "192.168.21.2" );
00082    // eth->init();
00083 
00084     if (eth->connect()) return -1;
00085     led3 = 1;
00086     
00087     
00088     printf("Connected to websocket server. \r\n");
00089     
00090     
00091     printf("IP Address is %s\r\n", eth->getIPAddress());
00092     httpd = new HTTPD;
00093     httpd->attach("/1/mbed/lpc1768/sensor/dust/sen12291p", &callback_api);
00094     httpd->attach("/", "/local/");
00095     httpd->start(80);
00096     printf("httpd ready\r\n");
00097     led2 = 1;
00098      
00099     Websocket ws("ws://sockets.mbed.org/ws/mbedschool/viewer");
00100   // Websocket ws("ws://192.168.21.123:8888");
00101     
00102     while (!ws.connect());
00103    // ws.send("echo-protocol");
00104     while (1) {
00105 
00106         char low_time_buf[256];
00107         char buf[256];   
00108         sprintf( low_time_buf, "%f", low_time / (30.0 * 1000000));
00109 
00110         strcpy(buf, "{"); 
00111         strcat(buf, "\"lowpulseoccupancytime\":");
00112         strcat(buf, low_time_buf);
00113         strcat(buf, "}");
00114         ws.send(buf);
00115        
00116         
00117         wait(1.0);
00118     } 
00119     
00120 }