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

Dependencies:   DxClinet EthernetInterface WebSocketClient mbed-rtos mbed

Fork of PwmOut_HelloWorld by mbed official

Files at this revision

API Documentation at this revision

Comitter:
komoritan
Date:
Sat Feb 14 00:29:02 2015 +0000
Parent:
0:50d2b9c62765
Child:
2:556ca5608855
Commit message:
First Version

Changed in this revision

DxClinet.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
Servo_DxDevice.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DxClinet.lib	Sat Feb 14 00:29:02 2015 +0000
@@ -0,0 +1,1 @@
+DxClinet#735163979ecf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Sat Feb 14 00:29:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#5887ae6c0c2c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Servo_DxDevice.lib	Sat Feb 14 00:29:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/PwmOut_HelloWorld/#50d2b9c62765
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Sat Feb 14 00:29:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#53d05ccff94a
--- a/main.cpp	Tue Feb 12 15:10:53 2013 +0000
+++ b/main.cpp	Sat Feb 14 00:29:02 2015 +0000
@@ -1,12 +1,146 @@
 #include "mbed.h"
+#include "EthernetInterface.h"
+#include "Websocket.h"
+#include "DxClient.h"
+
+#define PULSE_ON 0.0011 
+#define PULSE_OFF 0.0018 
+#define PULSE_NU 0.0015 
+
+#define GLUINSERVER "ws://xxx.yyy.zzz/dx"
+
+PwmOut servo(p21);
+AnalogIn noise(p15);
+
+DigitalOut  led_connect(LED1);
+DigitalOut  led_error(LED2);
+
+bool last_ctrl = false;
+bool bUpdate = false;
+
+extern "C" void mbed_mac_address(char *mac);
+
  
-PwmOut led(LED1);
+static bool device_get( dx_props *props)
+{
+    printf("device_get is called, but do nothing\n");
+    return false;
+}
+static bool device_set( dx_props *props)
+{
+    printf("device_set is called\n");
+    
+    if (props->numofprops>0) {
+        printf("%s: %s\n", props->props[0].name, props->props[0].b_val?"true":"false");
+    
+        bool ctrl = props->props[0].b_val;
+        if (last_ctrl != ctrl) {
+            if (ctrl) {
+                servo.pulsewidth(PULSE_ON); // servo position determined by a pulsewidth between 1-2ms
+                wait(0.5f);
+                servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
+            } else {
+                servo.pulsewidth(PULSE_OFF); // servo position determined by a pulsewidth between 1-2ms
+                wait(0.5f);
+                servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
+            }
+            last_ctrl = ctrl;
+            bUpdate = true;
+        }
+        return true;
+    }
+    return false;
+}
+
  
-int main() {
-    while(1) {
-        for(float p = 0.0f; p < 1.0f; p += 0.1f) {
-            led = p;
-            wait(0.1);
+int main() 
+{
+    // init servo
+    servo.period(0.020);          // servo requires a 20ms period
+    servo.pulsewidth(PULSE_NU); // servo position determined by a pulsewidth between 1-2ms
+
+    led_connect = 0;
+    led_error = 0;
+    
+    // ethernet initialize   
+    EthernetInterface ethernet;
+    ethernet.init();    // connect with DHCP
+    int ret_val = ethernet.connect();
+    if (0 == ret_val) {
+        printf("IP Address: %s\n", ethernet.getIPAddress());
+    } else {
+        error("ethernet failed to connect: %d.\n", ret_val);
+        led_error = 1;
+    }
+
+    //  get mac address for generationg deviceid
+    char mac[6];
+    char deviceid[32];
+    mbed_mac_address(mac);
+    sprintf( deviceid, "device-%02X-%02X-%02X-%02X-%02X-%02X", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5] );
+    printf( "deviceid: %s\n", deviceid );
+
+   
+    // connect to server
+    bool res; 
+    DxClient dxc(GLUINSERVER, deviceid, noise.read_u16());
+    
+    dxc.set_user("USER","PASS");
+    dxc.set_device_description("manipulator to control Swithes");
+    dxc.set_device_name("manipulator");
+    dxc.set_get_requset_handler( device_get );
+    dxc.set_set_requset_handler( device_set );
+    res = dxc.connect();
+    if (!res) {
+        led_error = 1;
+        return -1;
+    }
+    led_connect = 1;
+    
+    //  device registration
+    dx_props props;
+    
+    props.numofprops = 1;
+    props.props = (dx_prop*)malloc( sizeof(dx_prop) * props.numofprops );
+    sprintf( props.props[0].name, "manipulator" );
+    props.props[0].type = DX_BOOLEAN;
+    props.props[0].mode = DX_WRITEONLY;
+    props.props[0].direction = DX_UPDOWN;
+    props.props[0].b_val = false;
+    
+    res = dxc.register_device( &props );
+    if (!res) {
+        return -1;
+    }
+    
+    while(1){
+        //  send sensor values periodically
+        for(int i=0;i<0x7fffffff;i++) {
+    
+            // pool message from server for set and get request
+            dxc.handle_messages();
+            if (bUpdate) {
+                props.props[0].b_val = last_ctrl;
+                dxc.update_device(&props);
+                bUpdate = false;
+            }
+            wait(0.1f);
+    
+            // send keep alive message at a inverval        
+            if (i%30==0) {
+                dxc.keepalive_device();
+            }
         }
     }
-}
\ No newline at end of file
+    
+    res = dxc.deregister_device();
+
+    free( props.props );
+    
+    dxc.deregister_device();
+    dxc.close();
+    ethernet.disconnect();
+
+    while(true);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Sat Feb 14 00:29:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#bd07334df5b1
--- a/mbed.bld	Tue Feb 12 15:10:53 2013 +0000
+++ b/mbed.bld	Sat Feb 14 00:29:02 2015 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/0954ebd79f59
\ No newline at end of file
+http://mbed.org/users/mbed_official/code/mbed/builds/9327015d4013
\ No newline at end of file