mbedデバイスから直接gluinサーバに接続するサンプルです。サーボモータをGluinに接続します。メモリの関係から現在、Ethernet専用になっています。

Dependencies:   DxClinet EthernetInterface WebSocketClient mbed-rtos mbed

Fork of PwmOut_HelloWorld by mbed official

Committer:
MACRUM
Date:
Sat Feb 14 07:21:47 2015 +0000
Revision:
3:2e3db15882d8
Parent:
1:200a677fb4c5
Fixed link error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:50d2b9c62765 1 #include "mbed.h"
komoritan 1:200a677fb4c5 2 #include "EthernetInterface.h"
komoritan 1:200a677fb4c5 3 #include "Websocket.h"
komoritan 1:200a677fb4c5 4 #include "DxClient.h"
komoritan 1:200a677fb4c5 5
komoritan 1:200a677fb4c5 6 #define PULSE_ON 0.0011
komoritan 1:200a677fb4c5 7 #define PULSE_OFF 0.0018
komoritan 1:200a677fb4c5 8 #define PULSE_NU 0.0015
komoritan 1:200a677fb4c5 9
komoritan 1:200a677fb4c5 10 #define GLUINSERVER "ws://xxx.yyy.zzz/dx"
komoritan 1:200a677fb4c5 11
komoritan 1:200a677fb4c5 12 PwmOut servo(p21);
komoritan 1:200a677fb4c5 13 AnalogIn noise(p15);
komoritan 1:200a677fb4c5 14
komoritan 1:200a677fb4c5 15 DigitalOut led_connect(LED1);
komoritan 1:200a677fb4c5 16 DigitalOut led_error(LED2);
komoritan 1:200a677fb4c5 17
komoritan 1:200a677fb4c5 18 bool last_ctrl = false;
komoritan 1:200a677fb4c5 19 bool bUpdate = false;
komoritan 1:200a677fb4c5 20
komoritan 1:200a677fb4c5 21 extern "C" void mbed_mac_address(char *mac);
komoritan 1:200a677fb4c5 22
mbed_official 0:50d2b9c62765 23
komoritan 1:200a677fb4c5 24 static bool device_get( dx_props *props)
komoritan 1:200a677fb4c5 25 {
komoritan 1:200a677fb4c5 26 printf("device_get is called, but do nothing\n");
komoritan 1:200a677fb4c5 27 return false;
komoritan 1:200a677fb4c5 28 }
komoritan 1:200a677fb4c5 29 static bool device_set( dx_props *props)
komoritan 1:200a677fb4c5 30 {
komoritan 1:200a677fb4c5 31 printf("device_set is called\n");
komoritan 1:200a677fb4c5 32
komoritan 1:200a677fb4c5 33 if (props->numofprops>0) {
komoritan 1:200a677fb4c5 34 printf("%s: %s\n", props->props[0].name, props->props[0].b_val?"true":"false");
komoritan 1:200a677fb4c5 35
komoritan 1:200a677fb4c5 36 bool ctrl = props->props[0].b_val;
komoritan 1:200a677fb4c5 37 if (last_ctrl != ctrl) {
komoritan 1:200a677fb4c5 38 if (ctrl) {
komoritan 1:200a677fb4c5 39 servo.pulsewidth(PULSE_ON); // servo position determined by a pulsewidth between 1-2ms
komoritan 1:200a677fb4c5 40 wait(0.5f);
komoritan 1:200a677fb4c5 41 servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
komoritan 1:200a677fb4c5 42 } else {
komoritan 1:200a677fb4c5 43 servo.pulsewidth(PULSE_OFF); // servo position determined by a pulsewidth between 1-2ms
komoritan 1:200a677fb4c5 44 wait(0.5f);
komoritan 1:200a677fb4c5 45 servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
komoritan 1:200a677fb4c5 46 }
komoritan 1:200a677fb4c5 47 last_ctrl = ctrl;
komoritan 1:200a677fb4c5 48 bUpdate = true;
komoritan 1:200a677fb4c5 49 }
komoritan 1:200a677fb4c5 50 return true;
komoritan 1:200a677fb4c5 51 }
komoritan 1:200a677fb4c5 52 return false;
komoritan 1:200a677fb4c5 53 }
komoritan 1:200a677fb4c5 54
mbed_official 0:50d2b9c62765 55
komoritan 1:200a677fb4c5 56 int main()
komoritan 1:200a677fb4c5 57 {
komoritan 1:200a677fb4c5 58 // init servo
komoritan 1:200a677fb4c5 59 servo.period(0.020); // servo requires a 20ms period
komoritan 1:200a677fb4c5 60 servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
komoritan 1:200a677fb4c5 61
komoritan 1:200a677fb4c5 62 led_connect = 0;
komoritan 1:200a677fb4c5 63 led_error = 0;
komoritan 1:200a677fb4c5 64
komoritan 1:200a677fb4c5 65 // ethernet initialize
komoritan 1:200a677fb4c5 66 EthernetInterface ethernet;
komoritan 1:200a677fb4c5 67 ethernet.init(); // connect with DHCP
komoritan 1:200a677fb4c5 68 int ret_val = ethernet.connect();
komoritan 1:200a677fb4c5 69 if (0 == ret_val) {
komoritan 1:200a677fb4c5 70 printf("IP Address: %s\n", ethernet.getIPAddress());
komoritan 1:200a677fb4c5 71 } else {
komoritan 1:200a677fb4c5 72 error("ethernet failed to connect: %d.\n", ret_val);
komoritan 1:200a677fb4c5 73 led_error = 1;
komoritan 1:200a677fb4c5 74 }
komoritan 1:200a677fb4c5 75
komoritan 1:200a677fb4c5 76 // get mac address for generationg deviceid
komoritan 1:200a677fb4c5 77 char mac[6];
komoritan 1:200a677fb4c5 78 char deviceid[32];
komoritan 1:200a677fb4c5 79 mbed_mac_address(mac);
komoritan 1:200a677fb4c5 80 sprintf( deviceid, "device-%02X-%02X-%02X-%02X-%02X-%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5] );
komoritan 1:200a677fb4c5 81 printf( "deviceid: %s\n", deviceid );
komoritan 1:200a677fb4c5 82
komoritan 1:200a677fb4c5 83
komoritan 1:200a677fb4c5 84 // connect to server
komoritan 1:200a677fb4c5 85 bool res;
komoritan 1:200a677fb4c5 86 DxClient dxc(GLUINSERVER, deviceid, noise.read_u16());
komoritan 1:200a677fb4c5 87
komoritan 1:200a677fb4c5 88 dxc.set_user("USER","PASS");
komoritan 1:200a677fb4c5 89 dxc.set_device_description("manipulator to control Swithes");
komoritan 1:200a677fb4c5 90 dxc.set_device_name("manipulator");
komoritan 1:200a677fb4c5 91 dxc.set_get_requset_handler( device_get );
komoritan 1:200a677fb4c5 92 dxc.set_set_requset_handler( device_set );
komoritan 1:200a677fb4c5 93 res = dxc.connect();
komoritan 1:200a677fb4c5 94 if (!res) {
komoritan 1:200a677fb4c5 95 led_error = 1;
komoritan 1:200a677fb4c5 96 return -1;
komoritan 1:200a677fb4c5 97 }
komoritan 1:200a677fb4c5 98 led_connect = 1;
komoritan 1:200a677fb4c5 99
komoritan 1:200a677fb4c5 100 // device registration
komoritan 1:200a677fb4c5 101 dx_props props;
komoritan 1:200a677fb4c5 102
komoritan 1:200a677fb4c5 103 props.numofprops = 1;
komoritan 1:200a677fb4c5 104 props.props = (dx_prop*)malloc( sizeof(dx_prop) * props.numofprops );
komoritan 1:200a677fb4c5 105 sprintf( props.props[0].name, "manipulator" );
komoritan 1:200a677fb4c5 106 props.props[0].type = DX_BOOLEAN;
komoritan 1:200a677fb4c5 107 props.props[0].mode = DX_WRITEONLY;
komoritan 1:200a677fb4c5 108 props.props[0].direction = DX_UPDOWN;
komoritan 1:200a677fb4c5 109 props.props[0].b_val = false;
komoritan 1:200a677fb4c5 110
komoritan 1:200a677fb4c5 111 res = dxc.register_device( &props );
komoritan 1:200a677fb4c5 112 if (!res) {
komoritan 1:200a677fb4c5 113 return -1;
komoritan 1:200a677fb4c5 114 }
komoritan 1:200a677fb4c5 115
komoritan 1:200a677fb4c5 116 while(1){
komoritan 1:200a677fb4c5 117 // send sensor values periodically
komoritan 1:200a677fb4c5 118 for(int i=0;i<0x7fffffff;i++) {
komoritan 1:200a677fb4c5 119
komoritan 1:200a677fb4c5 120 // pool message from server for set and get request
komoritan 1:200a677fb4c5 121 dxc.handle_messages();
komoritan 1:200a677fb4c5 122 if (bUpdate) {
komoritan 1:200a677fb4c5 123 props.props[0].b_val = last_ctrl;
komoritan 1:200a677fb4c5 124 dxc.update_device(&props);
komoritan 1:200a677fb4c5 125 bUpdate = false;
komoritan 1:200a677fb4c5 126 }
komoritan 1:200a677fb4c5 127 wait(0.1f);
komoritan 1:200a677fb4c5 128
komoritan 1:200a677fb4c5 129 // send keep alive message at a inverval
komoritan 1:200a677fb4c5 130 if (i%30==0) {
komoritan 1:200a677fb4c5 131 dxc.keepalive_device();
komoritan 1:200a677fb4c5 132 }
mbed_official 0:50d2b9c62765 133 }
mbed_official 0:50d2b9c62765 134 }
komoritan 1:200a677fb4c5 135
komoritan 1:200a677fb4c5 136 res = dxc.deregister_device();
komoritan 1:200a677fb4c5 137
komoritan 1:200a677fb4c5 138 free( props.props );
komoritan 1:200a677fb4c5 139
komoritan 1:200a677fb4c5 140 dxc.deregister_device();
komoritan 1:200a677fb4c5 141 dxc.close();
komoritan 1:200a677fb4c5 142 ethernet.disconnect();
komoritan 1:200a677fb4c5 143
komoritan 1:200a677fb4c5 144 while(true);
komoritan 1:200a677fb4c5 145 }
komoritan 1:200a677fb4c5 146