CoAP Device Server Client mit leshan Server

Dependencies:   EthernetInterface mbed-rtos mbed nsdl_lib

Fork of COAPmbed by smd.iotkit2.ch

Sandbox Server

Eclipse stellt einen Testserver unter http://leshan.eclipse.org/ zur Verfügung. Das Programm verbindet sich mit diesem und ist unter mbed-k64f... erreichbar.

Installation lokale Version leshan

wget https://hudson.eclipse.org/leshan/job/leshan/lastSuccessfulBuild/artifact/leshan-server-demo.jar
java -jar ./leshan-server-demo.jar

cURL

Die Funktionen können mittels cURL oder Browser wie folgt getestet werden:

# Alle Clients abfragen (Antwort im JSON Format)
curl -X GET  http://localhost:8080/api/clients
                         
# Wert von LED2 abfragen (Antwort im text/plain Format)                      
curl -X GET  http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 
 
# Wert für LED2 setzen
curl -X GET -vvv http://localhost:8080/api/clients/mbed-k64f-1234/10/0/2 -H "Content-Type: text/plain" -d "10"
Committer:
marcel1691
Date:
Mon Aug 31 09:21:18 2015 +0000
Revision:
21:c11c5a72d8e0
Parent:
20:0db9b774aeb8
Child:
22:6d4c96af70e3
IP Adresse an /ch/open Workshoptage angepasst

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcel1691 13:3c205b19c540 1 /** CoAP Device Server Client
stefan1691 15:fba25b7e63cd 2 Zeile 29: Node Name aendern und auf der Gegenseite leshan Server Verwenden.
marcel1691 13:3c205b19c540 3 */
terohoo 0:2edbfea18d23 4 #include "mbed.h"
terohoo 0:2edbfea18d23 5 #include "EthernetInterface.h"
bogdanm 2:7e489126fe7a 6 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 7 #include "dbg.h"
stefan1691 16:b27c9d2b1a98 8
bogdanm 2:7e489126fe7a 9 // Include various resources
stefan1691 15:fba25b7e63cd 10 #include "PwmOutResource.h"
stefan1691 15:fba25b7e63cd 11
stefan1691 15:fba25b7e63cd 12 PwmOutResource led1( D11, "led1", "10/0/0" );
stefan1691 15:fba25b7e63cd 13 PwmOutResource led2( D12, "led2", "10/0/1" );
stefan1691 15:fba25b7e63cd 14 PwmOutResource led3( D13, "led3", "10/0/2" );
terohoo 0:2edbfea18d23 15
stefan1691 17:8ee737f79492 16 char *staticResources[4] [3] = { { "3/0/0", "Manufacturer", "mc-b" },
stefan1691 16:b27c9d2b1a98 17 { "3/0/1", "Model Number", "CoAP mbed device" },
stefan1691 17:8ee737f79492 18 { "5/0/3", "Firmware", "1" },
stefan1691 17:8ee737f79492 19 { "6/0/0", "GPS Informationen", "67,21" },
stefan1691 16:b27c9d2b1a98 20 };
stefan1691 16:b27c9d2b1a98 21
bogdanm 2:7e489126fe7a 22 Serial pc(USBTX, USBRX); // tx, rx
terohoo 0:2edbfea18d23 23
bogdanm 2:7e489126fe7a 24 // ****************************************************************************
stefan1691 16:b27c9d2b1a98 25 // Server configuration
terohoo 0:2edbfea18d23 26 /* Change this IP address to that of your NanoService Platform installation */
marcel1691 21:c11c5a72d8e0 27 //static const char* NSP_ADDRESS = "leshan.eclipse.org"; /* Eclipse leshan Server */
marcel1691 21:c11c5a72d8e0 28 static const char* NSP_ADDRESS = "192.168.200.14"; /* Eclipse leshan Server */
bogdanm 2:7e489126fe7a 29 static const int NSP_PORT = 5683;
marcel1691 19:7830976ac327 30 char endpoint_name[64] = "mbed-k64f-"; // wird ergaenzt um MAC Adresse
bogdanm 2:7e489126fe7a 31 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 32 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 33
bogdanm 2:7e489126fe7a 34 // ****************************************************************************
bogdanm 2:7e489126fe7a 35 // Ethernet initialization
terohoo 0:2edbfea18d23 36
terohoo 0:2edbfea18d23 37 EthernetInterface eth;
terohoo 0:2edbfea18d23 38
bogdanm 2:7e489126fe7a 39 static void ethernet_init()
terohoo 0:2edbfea18d23 40 {
marcel1691 9:18f4959c2bac 41 //char mbed_uid[33]; // for creating unique name for the board
terohoo 0:2edbfea18d23 42
terohoo 0:2edbfea18d23 43 /* Initialize network */
terohoo 0:2edbfea18d23 44 eth.init();
marcel1691 9:18f4959c2bac 45
terohoo 0:2edbfea18d23 46 if(eth.connect(30000) == 0)
terohoo 0:2edbfea18d23 47 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 48
marcel1691 19:7830976ac327 49 // Board Name + MAC = EndPoint Name
marcel1691 19:7830976ac327 50 strcat( endpoint_name, eth.getMACAddress() );
terohoo 0:2edbfea18d23 51
marcel1691 19:7830976ac327 52 printf( "IP:%s", eth.getIPAddress() );
marcel1691 19:7830976ac327 53 printf( "NetMask is %s\r\n", eth.getNetworkMask() );
marcel1691 19:7830976ac327 54 printf( "Gateway Address is %s\r\n", eth.getGateway() );
marcel1691 19:7830976ac327 55 printf( "MAC Address is %s\r\n", eth.getMACAddress() );
marcel1691 19:7830976ac327 56 printf( "Endpoint is %s\r\n", endpoint_name );
marcel1691 19:7830976ac327 57
marcel1691 19:7830976ac327 58 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress() );
bogdanm 2:7e489126fe7a 59 }
bogdanm 2:7e489126fe7a 60
bogdanm 2:7e489126fe7a 61 // ****************************************************************************
bogdanm 2:7e489126fe7a 62 // NSP initialization
bogdanm 2:7e489126fe7a 63
bogdanm 2:7e489126fe7a 64 UDPSocket server;
bogdanm 2:7e489126fe7a 65 Endpoint nsp;
bogdanm 2:7e489126fe7a 66
bogdanm 2:7e489126fe7a 67 static void nsp_init()
bogdanm 2:7e489126fe7a 68 {
terohoo 0:2edbfea18d23 69 server.init();
terohoo 0:2edbfea18d23 70 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 71
bogdanm 2:7e489126fe7a 72 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 73
bogdanm 2:7e489126fe7a 74 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 75 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 76
marcel1691 9:18f4959c2bac 77 printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 78 }
terohoo 0:2edbfea18d23 79
bogdanm 2:7e489126fe7a 80 // ****************************************************************************
bogdanm 2:7e489126fe7a 81 // Resource creation
terohoo 0:2edbfea18d23 82
bogdanm 2:7e489126fe7a 83 static int create_resources()
bogdanm 2:7e489126fe7a 84 {
bogdanm 2:7e489126fe7a 85 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 86 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 87
bogdanm 2:7e489126fe7a 88 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 89
terohoo 0:2edbfea18d23 90 /* Create resources */
bogdanm 2:7e489126fe7a 91 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 92 if(!resource_ptr)
terohoo 0:2edbfea18d23 93 return 0;
terohoo 0:2edbfea18d23 94 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 95
bogdanm 2:7e489126fe7a 96 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 97 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 98 {
bogdanm 2:7e489126fe7a 99 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 100 return 0;
terohoo 0:2edbfea18d23 101 }
terohoo 0:2edbfea18d23 102 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 103
bogdanm 2:7e489126fe7a 104 // Static resources
stefan1691 16:b27c9d2b1a98 105 nsdl_create_static_resource( resource_ptr, strlen(staticResources[0] [0]), (uint8_t*) staticResources[0] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 106 (uint8_t*) staticResources[0] [2], strlen(staticResources[0] [2]) );
stefan1691 16:b27c9d2b1a98 107 nsdl_create_static_resource( resource_ptr, strlen(staticResources[1] [0]), (uint8_t*) staticResources[1] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 108 (uint8_t*) staticResources[1] [2], strlen(staticResources[1] [2]) );
stefan1691 17:8ee737f79492 109
stefan1691 17:8ee737f79492 110 nsdl_create_static_resource( resource_ptr, strlen(staticResources[2] [0]), (uint8_t*) staticResources[2] [0], 0, 0,
stefan1691 17:8ee737f79492 111 (uint8_t*) staticResources[2] [2], strlen(staticResources[2] [2]) );
stefan1691 17:8ee737f79492 112 nsdl_create_static_resource( resource_ptr, strlen(staticResources[3] [0]), (uint8_t*) staticResources[3] [0], 0, 0,
stefan1691 17:8ee737f79492 113 (uint8_t*) staticResources[3] [2], strlen(staticResources[3] [2]) );
terohoo 0:2edbfea18d23 114
bogdanm 2:7e489126fe7a 115 // Dynamic resources
stefan1691 15:fba25b7e63cd 116 led1.create( resource_ptr );
stefan1691 15:fba25b7e63cd 117 led2.create( resource_ptr );
stefan1691 15:fba25b7e63cd 118 led3.create( resource_ptr );
terohoo 0:2edbfea18d23 119
terohoo 0:2edbfea18d23 120 /* Register with NSP */
bogdanm 2:7e489126fe7a 121 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 122 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 123 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 124 else
terohoo 0:2edbfea18d23 125 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 126 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 127
bogdanm 2:7e489126fe7a 128 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 129 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 130 return 1;
terohoo 0:2edbfea18d23 131 }
terohoo 0:2edbfea18d23 132
bogdanm 2:7e489126fe7a 133 // ****************************************************************************
bogdanm 2:7e489126fe7a 134 // Program entry point
terohoo 0:2edbfea18d23 135
bogdanm 2:7e489126fe7a 136 int main()
terohoo 0:2edbfea18d23 137 {
marcel1691 9:18f4959c2bac 138 printf("mbed NanoService demo");
bogdanm 2:7e489126fe7a 139 NSDL_DEBUG("mbed NanoService Example App 0.1\n");
bogdanm 2:7e489126fe7a 140
bogdanm 2:7e489126fe7a 141 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 142 ethernet_init();
terohoo 1:e35d7f10999a 143
bogdanm 2:7e489126fe7a 144 // Initialize NSP node
bogdanm 2:7e489126fe7a 145 nsp_init();
bogdanm 2:7e489126fe7a 146
bogdanm 2:7e489126fe7a 147 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 148 nsdl_init();
bogdanm 2:7e489126fe7a 149
bogdanm 2:7e489126fe7a 150 // Create NSDL resources
bogdanm 2:7e489126fe7a 151 create_resources();
bogdanm 2:7e489126fe7a 152
bogdanm 2:7e489126fe7a 153 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 154 nsdl_event_loop();
terohoo 0:2edbfea18d23 155 }