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

Dependencies:   LM75B MMA7660 VodafoneUSBModem WebSocketClient mbed-rtos mbed

Committer:
samux
Date:
Fri Feb 08 13:11:17 2013 +0000
Revision:
1:89d8a05a8f4d
Parent:
0:c589d2050144
use app-board channel

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:c589d2050144 1 #include "mbed.h"
samux 0:c589d2050144 2 #include "VodafoneUSBModem.h"
samux 0:c589d2050144 3 #include "Websocket.h"
samux 0:c589d2050144 4 #include "LM75B.h"
samux 0:c589d2050144 5 #include "MMA7660.h"
samux 0:c589d2050144 6
samux 0:c589d2050144 7 // accelerometer
samux 0:c589d2050144 8 MMA7660 acc(p28, p27);
samux 0:c589d2050144 9
samux 0:c589d2050144 10 // temperature sensor
samux 0:c589d2050144 11 LM75B tmp(p28,p27);
samux 0:c589d2050144 12
samux 0:c589d2050144 13 Serial pc(USBTX, USBRX);
samux 0:c589d2050144 14
samux 0:c589d2050144 15 void test(void const*)
samux 0:c589d2050144 16 {
samux 0:c589d2050144 17 char json_str[100];
samux 0:c589d2050144 18
samux 0:c589d2050144 19 VodafoneUSBModem modem;
samux 1:89d8a05a8f4d 20
samux 1:89d8a05a8f4d 21 // See the output on http://sockets.mbed.org/app-board/viewer
samux 1:89d8a05a8f4d 22 Websocket ws("ws://sockets.mbed.org:443/ws/app-board/wo");
samux 0:c589d2050144 23
samux 0:c589d2050144 24
samux 0:c589d2050144 25 int ret = modem.connect("pp.vodafone.co.uk");
samux 0:c589d2050144 26 if(ret) {
samux 0:c589d2050144 27 printf("Could not connect\n");
samux 0:c589d2050144 28 return;
samux 0:c589d2050144 29 }
samux 0:c589d2050144 30
samux 0:c589d2050144 31 bool c = ws.connect();
samux 0:c589d2050144 32 printf("Connect result: %s\n", c?"OK":"Failed");
samux 0:c589d2050144 33
samux 0:c589d2050144 34 while(1) {
samux 1:89d8a05a8f4d 35 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());
samux 0:c589d2050144 36
samux 0:c589d2050144 37 // send str
samux 0:c589d2050144 38 ret = ws.send(json_str);
samux 0:c589d2050144 39
samux 0:c589d2050144 40 if(ret<0) {
samux 0:c589d2050144 41 printf("Timeout\n");
samux 0:c589d2050144 42 ws.close();
samux 0:c589d2050144 43 c = ws.connect();
samux 0:c589d2050144 44 printf("Connect result: %s\n", c?"OK":"Failed");
samux 0:c589d2050144 45 }
samux 0:c589d2050144 46
samux 0:c589d2050144 47 Thread::wait(100);
samux 0:c589d2050144 48 }
samux 0:c589d2050144 49
samux 0:c589d2050144 50 modem.disconnect();
samux 0:c589d2050144 51
samux 0:c589d2050144 52 while(1) {
samux 0:c589d2050144 53 }
samux 0:c589d2050144 54 }
samux 0:c589d2050144 55
samux 0:c589d2050144 56
samux 0:c589d2050144 57 int main()
samux 0:c589d2050144 58 {
samux 0:c589d2050144 59 pc.baud(921600);
samux 0:c589d2050144 60 Thread testTask(test, NULL, osPriorityNormal, 1024 * 4);
samux 0:c589d2050144 61 DigitalOut led(LED1);
samux 0:c589d2050144 62 while(1) {
samux 0:c589d2050144 63 led=!led;
samux 0:c589d2050144 64 Thread::wait(1000);
samux 0:c589d2050144 65 }
samux 0:c589d2050144 66
samux 0:c589d2050144 67 return 0;
samux 0:c589d2050144 68 }