websocket over 3g using VodafoneUSBModem and app-board on-board sensors (acc - tmp)

Dependencies:   LM75B MMA7660 VodafoneUSBModem WebSocketClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "VodafoneUSBModem.h"
00003 #include "Websocket.h"
00004 #include "LM75B.h"
00005 #include "MMA7660.h"
00006 
00007 // accelerometer
00008 MMA7660 acc(p28, p27);
00009 
00010 // temperature sensor
00011 LM75B tmp(p28,p27);
00012 
00013 Serial pc(USBTX, USBRX);
00014 
00015 void test(void const*)
00016 {
00017     char json_str[100];
00018 
00019     VodafoneUSBModem modem;
00020     
00021     // See the output on http://sockets.mbed.org/app-board/viewer
00022     Websocket ws("ws://sockets.mbed.org:443/ws/app-board/wo");
00023     
00024 
00025     int ret = modem.connect("pp.vodafone.co.uk");
00026     if(ret) {
00027         printf("Could not connect\n");
00028         return;
00029     }
00030 
00031     bool c = ws.connect();
00032     printf("Connect result: %s\n", c?"OK":"Failed");
00033 
00034     while(1) {
00035         sprintf(json_str, "{\"id\":\"app_board_3g_EW2013\",\"ax\":%d,\"ay\":%d,\"az\":%d, \"tmp\":%d}", (int)(acc.x()*360), (int)(acc.y()*360), (int)(acc.z()*360), (int)tmp.read());
00036         
00037         // send str
00038         ret = ws.send(json_str);
00039         
00040         if(ret<0) {
00041             printf("Timeout\n");
00042             ws.close();
00043             c = ws.connect();
00044             printf("Connect result: %s\n", c?"OK":"Failed");
00045         }
00046         
00047         Thread::wait(100);
00048     }
00049 
00050     modem.disconnect();
00051 
00052     while(1) {
00053     }
00054 }
00055 
00056 
00057 int main()
00058 {
00059     pc.baud(921600);
00060     Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
00061     DigitalOut led(LED1);
00062     while(1) {
00063         led=!led;
00064         Thread::wait(1000);
00065     }
00066 
00067     return 0;
00068 }