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:
Wed Aug 26 06:58:27 2015 +0000
Revision:
19:7830976ac327
Parent:
18:17bdeb25e0a8
Child:
20:0db9b774aeb8
Fixer Endpoint Name durch Board + MAC ersetzt

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 19:7830976ac327 27 static const char* NSP_ADDRESS = "192.168.178.38"; /* Eclipse leshan Server */
bogdanm 2:7e489126fe7a 28 static const int NSP_PORT = 5683;
marcel1691 19:7830976ac327 29 char endpoint_name[64] = "mbed-k64f-"; // wird ergaenzt um MAC Adresse
bogdanm 2:7e489126fe7a 30 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 31 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 32
bogdanm 2:7e489126fe7a 33 // ****************************************************************************
bogdanm 2:7e489126fe7a 34 // Ethernet initialization
terohoo 0:2edbfea18d23 35
terohoo 0:2edbfea18d23 36 EthernetInterface eth;
terohoo 0:2edbfea18d23 37
bogdanm 2:7e489126fe7a 38 static void ethernet_init()
terohoo 0:2edbfea18d23 39 {
marcel1691 9:18f4959c2bac 40 //char mbed_uid[33]; // for creating unique name for the board
terohoo 0:2edbfea18d23 41
terohoo 0:2edbfea18d23 42 /* Initialize network */
terohoo 0:2edbfea18d23 43 eth.init();
marcel1691 9:18f4959c2bac 44
terohoo 0:2edbfea18d23 45 if(eth.connect(30000) == 0)
terohoo 0:2edbfea18d23 46 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 47
marcel1691 19:7830976ac327 48 // Board Name + MAC = EndPoint Name
marcel1691 19:7830976ac327 49 strcat( endpoint_name, eth.getMACAddress() );
terohoo 0:2edbfea18d23 50
marcel1691 19:7830976ac327 51 printf( "IP:%s", eth.getIPAddress() );
marcel1691 19:7830976ac327 52 printf( "NetMask is %s\r\n", eth.getNetworkMask() );
marcel1691 19:7830976ac327 53 printf( "Gateway Address is %s\r\n", eth.getGateway() );
marcel1691 19:7830976ac327 54 printf( "MAC Address is %s\r\n", eth.getMACAddress() );
marcel1691 19:7830976ac327 55 printf( "Endpoint is %s\r\n", endpoint_name );
marcel1691 19:7830976ac327 56
marcel1691 19:7830976ac327 57 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress() );
bogdanm 2:7e489126fe7a 58 }
bogdanm 2:7e489126fe7a 59
bogdanm 2:7e489126fe7a 60 // ****************************************************************************
bogdanm 2:7e489126fe7a 61 // NSP initialization
bogdanm 2:7e489126fe7a 62
bogdanm 2:7e489126fe7a 63 UDPSocket server;
bogdanm 2:7e489126fe7a 64 Endpoint nsp;
bogdanm 2:7e489126fe7a 65
bogdanm 2:7e489126fe7a 66 static void nsp_init()
bogdanm 2:7e489126fe7a 67 {
terohoo 0:2edbfea18d23 68 server.init();
terohoo 0:2edbfea18d23 69 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 70
bogdanm 2:7e489126fe7a 71 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 72
bogdanm 2:7e489126fe7a 73 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 74 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 75
marcel1691 9:18f4959c2bac 76 printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 77 }
terohoo 0:2edbfea18d23 78
bogdanm 2:7e489126fe7a 79 // ****************************************************************************
bogdanm 2:7e489126fe7a 80 // Resource creation
terohoo 0:2edbfea18d23 81
bogdanm 2:7e489126fe7a 82 static int create_resources()
bogdanm 2:7e489126fe7a 83 {
bogdanm 2:7e489126fe7a 84 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 85 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 86
bogdanm 2:7e489126fe7a 87 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 88
terohoo 0:2edbfea18d23 89 /* Create resources */
bogdanm 2:7e489126fe7a 90 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 91 if(!resource_ptr)
terohoo 0:2edbfea18d23 92 return 0;
terohoo 0:2edbfea18d23 93 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 94
bogdanm 2:7e489126fe7a 95 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 96 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 97 {
bogdanm 2:7e489126fe7a 98 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 99 return 0;
terohoo 0:2edbfea18d23 100 }
terohoo 0:2edbfea18d23 101 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 102
bogdanm 2:7e489126fe7a 103 // Static resources
stefan1691 16:b27c9d2b1a98 104 nsdl_create_static_resource( resource_ptr, strlen(staticResources[0] [0]), (uint8_t*) staticResources[0] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 105 (uint8_t*) staticResources[0] [2], strlen(staticResources[0] [2]) );
stefan1691 16:b27c9d2b1a98 106 nsdl_create_static_resource( resource_ptr, strlen(staticResources[1] [0]), (uint8_t*) staticResources[1] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 107 (uint8_t*) staticResources[1] [2], strlen(staticResources[1] [2]) );
stefan1691 17:8ee737f79492 108
stefan1691 17:8ee737f79492 109 nsdl_create_static_resource( resource_ptr, strlen(staticResources[2] [0]), (uint8_t*) staticResources[2] [0], 0, 0,
stefan1691 17:8ee737f79492 110 (uint8_t*) staticResources[2] [2], strlen(staticResources[2] [2]) );
stefan1691 17:8ee737f79492 111 nsdl_create_static_resource( resource_ptr, strlen(staticResources[3] [0]), (uint8_t*) staticResources[3] [0], 0, 0,
stefan1691 17:8ee737f79492 112 (uint8_t*) staticResources[3] [2], strlen(staticResources[3] [2]) );
terohoo 0:2edbfea18d23 113
bogdanm 2:7e489126fe7a 114 // Dynamic resources
stefan1691 15:fba25b7e63cd 115 led1.create( resource_ptr );
stefan1691 15:fba25b7e63cd 116 led2.create( resource_ptr );
stefan1691 15:fba25b7e63cd 117 led3.create( resource_ptr );
terohoo 0:2edbfea18d23 118
terohoo 0:2edbfea18d23 119 /* Register with NSP */
bogdanm 2:7e489126fe7a 120 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 121 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 122 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 123 else
terohoo 0:2edbfea18d23 124 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 125 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 126
bogdanm 2:7e489126fe7a 127 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 128 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 129 return 1;
terohoo 0:2edbfea18d23 130 }
terohoo 0:2edbfea18d23 131
bogdanm 2:7e489126fe7a 132 // ****************************************************************************
bogdanm 2:7e489126fe7a 133 // Program entry point
terohoo 0:2edbfea18d23 134
bogdanm 2:7e489126fe7a 135 int main()
terohoo 0:2edbfea18d23 136 {
marcel1691 9:18f4959c2bac 137 printf("mbed NanoService demo");
bogdanm 2:7e489126fe7a 138 NSDL_DEBUG("mbed NanoService Example App 0.1\n");
bogdanm 2:7e489126fe7a 139
bogdanm 2:7e489126fe7a 140 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 141 ethernet_init();
terohoo 1:e35d7f10999a 142
bogdanm 2:7e489126fe7a 143 // Initialize NSP node
bogdanm 2:7e489126fe7a 144 nsp_init();
bogdanm 2:7e489126fe7a 145
bogdanm 2:7e489126fe7a 146 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 147 nsdl_init();
bogdanm 2:7e489126fe7a 148
bogdanm 2:7e489126fe7a 149 // Create NSDL resources
bogdanm 2:7e489126fe7a 150 create_resources();
bogdanm 2:7e489126fe7a 151
bogdanm 2:7e489126fe7a 152 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 153 nsdl_event_loop();
terohoo 0:2edbfea18d23 154 }