Test C027 with Xively and Ethernet

Dependencies:   C027 EthernetInterface libxively mbed-rtos mbed HTTPClient

Fork of C027_Xively_Ethernet by Chau Vo

main.cpp

Committer:
olympux
Date:
2014-04-09
Revision:
3:259ac92c3ca8
Parent:
1:1c1802ec42a2
Child:
4:0bdab35cb164

File content as of revision 3:259ac92c3ca8:

#include "mbed.h"
#include "C027.h"
#include "EthernetInterface.h"
#include "Websocket.h"
 
 
void test(void const*)
{
    char msg[512] = {0};
 
    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n\r", eth.getIPAddress());
 
    Websocket ws("ws://sockets.mbed.org:443/ws/chauvo/wo");
    Timer t;
    t.start();
    
    bool c = ws.connect();
    printf("Connect result: %s\n", c?"OK":"Failed");
    
    for(int i = 0; i < 10000; i++) 
    {
        ws.connect();
        // create json string with acc/tmp data
        sprintf(msg, "Chau's demo loop: %d", i);
        ws.send(msg);    
        wait(0.5f);
        memset(msg, 0, 512);
        
        if (ws.read(msg))
        {
            printf("rcv: %s\r\n", msg);
        }
        else
        {
            printf("Loop %d ws.read() returns 0\n \t %s, line %d @ %6.2f seconds\n", i, __FILE__, __LINE__, t.read());
            wait(5.0f);
        }
        ws.close();
    }
    
    while(1) {
    }
}

int main()
{
    Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
    DigitalOut led(LED); // on rev A you should reasign the signal to A0
    while(1) {
        led=!led;
        Thread::wait(1000);
    }

    return 0;
}