websocket over 3g using VodafoneUSBModem and app-board on-board sensors (acc - tmp)
Dependencies: LM75B MMA7660 VodafoneUSBModem WebSocketClient mbed-rtos mbed
main.cpp@0:c589d2050144, 2013-02-08 (annotated)
- Committer:
- samux
- Date:
- Fri Feb 08 12:34:40 2013 +0000
- Revision:
- 0:c589d2050144
- Child:
- 1:89d8a05a8f4d
websocket over 3g using VodafoneUSBModem and app-board on-board sensors (acc - tmp)
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:c589d2050144 | 20 | Websocket ws("ws://sockets.mbed.org:443/ws/sensors/wo"); |
samux | 0:c589d2050144 | 21 | |
samux | 0:c589d2050144 | 22 | |
samux | 0:c589d2050144 | 23 | int ret = modem.connect("pp.vodafone.co.uk"); |
samux | 0:c589d2050144 | 24 | if(ret) { |
samux | 0:c589d2050144 | 25 | printf("Could not connect\n"); |
samux | 0:c589d2050144 | 26 | return; |
samux | 0:c589d2050144 | 27 | } |
samux | 0:c589d2050144 | 28 | |
samux | 0:c589d2050144 | 29 | bool c = ws.connect(); |
samux | 0:c589d2050144 | 30 | printf("Connect result: %s\n", c?"OK":"Failed"); |
samux | 0:c589d2050144 | 31 | |
samux | 0:c589d2050144 | 32 | while(1) { |
samux | 0:c589d2050144 | 33 | sprintf(json_str, "{\"id\":\"3g_app_board_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 | 34 | |
samux | 0:c589d2050144 | 35 | // send str |
samux | 0:c589d2050144 | 36 | ret = ws.send(json_str); |
samux | 0:c589d2050144 | 37 | |
samux | 0:c589d2050144 | 38 | if(ret<0) { |
samux | 0:c589d2050144 | 39 | printf("Timeout\n"); |
samux | 0:c589d2050144 | 40 | ws.close(); |
samux | 0:c589d2050144 | 41 | c = ws.connect(); |
samux | 0:c589d2050144 | 42 | printf("Connect result: %s\n", c?"OK":"Failed"); |
samux | 0:c589d2050144 | 43 | } |
samux | 0:c589d2050144 | 44 | |
samux | 0:c589d2050144 | 45 | Thread::wait(100); |
samux | 0:c589d2050144 | 46 | } |
samux | 0:c589d2050144 | 47 | |
samux | 0:c589d2050144 | 48 | modem.disconnect(); |
samux | 0:c589d2050144 | 49 | |
samux | 0:c589d2050144 | 50 | while(1) { |
samux | 0:c589d2050144 | 51 | } |
samux | 0:c589d2050144 | 52 | } |
samux | 0:c589d2050144 | 53 | |
samux | 0:c589d2050144 | 54 | |
samux | 0:c589d2050144 | 55 | int main() |
samux | 0:c589d2050144 | 56 | { |
samux | 0:c589d2050144 | 57 | pc.baud(921600); |
samux | 0:c589d2050144 | 58 | Thread testTask(test, NULL, osPriorityNormal, 1024 * 4); |
samux | 0:c589d2050144 | 59 | DigitalOut led(LED1); |
samux | 0:c589d2050144 | 60 | while(1) { |
samux | 0:c589d2050144 | 61 | led=!led; |
samux | 0:c589d2050144 | 62 | Thread::wait(1000); |
samux | 0:c589d2050144 | 63 | } |
samux | 0:c589d2050144 | 64 | |
samux | 0:c589d2050144 | 65 | return 0; |
samux | 0:c589d2050144 | 66 | } |