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:
stefan1691
Date:
Sun Jun 21 12:42:38 2015 +0000
Revision:
16:b27c9d2b1a98
Parent:
15:fba25b7e63cd
Child:
17:8ee737f79492
Code bereinigt

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 16:b27c9d2b1a98 16 char *staticResources[2] [3] = { { "3/0/0", "Manufacturer", "mc-b" },
stefan1691 16:b27c9d2b1a98 17 { "3/0/1", "Model Number", "CoAP mbed device" },
stefan1691 16:b27c9d2b1a98 18 };
stefan1691 16:b27c9d2b1a98 19
bogdanm 2:7e489126fe7a 20 Serial pc(USBTX, USBRX); // tx, rx
terohoo 0:2edbfea18d23 21
bogdanm 2:7e489126fe7a 22 // ****************************************************************************
stefan1691 16:b27c9d2b1a98 23 // Server configuration
terohoo 0:2edbfea18d23 24 /* Change this IP address to that of your NanoService Platform installation */
stefan1691 16:b27c9d2b1a98 25 static const char* NSP_ADDRESS = "192.168.178.23"; /* Lokale Installation - leshan Server */
bogdanm 2:7e489126fe7a 26 static const int NSP_PORT = 5683;
stefan1691 14:54c3708f7c3c 27 char endpoint_name[] = "mbed-k64f-1234"; // aendern pro Node
bogdanm 2:7e489126fe7a 28 uint8_t ep_type[] = {"mbed_device"};
bogdanm 2:7e489126fe7a 29 uint8_t lifetime_ptr[] = {"1200"};
terohoo 0:2edbfea18d23 30
bogdanm 2:7e489126fe7a 31 // ****************************************************************************
bogdanm 2:7e489126fe7a 32 // Ethernet initialization
terohoo 0:2edbfea18d23 33
terohoo 0:2edbfea18d23 34 EthernetInterface eth;
terohoo 0:2edbfea18d23 35
bogdanm 2:7e489126fe7a 36 static void ethernet_init()
terohoo 0:2edbfea18d23 37 {
marcel1691 9:18f4959c2bac 38 //char mbed_uid[33]; // for creating unique name for the board
terohoo 0:2edbfea18d23 39
terohoo 0:2edbfea18d23 40 /* Initialize network */
terohoo 0:2edbfea18d23 41 eth.init();
marcel1691 9:18f4959c2bac 42
terohoo 0:2edbfea18d23 43 if(eth.connect(30000) == 0)
terohoo 0:2edbfea18d23 44 pc.printf("Connect OK\n\r");
terohoo 0:2edbfea18d23 45
marcel1691 9:18f4959c2bac 46 //mbed_interface_uid(mbed_uid); // -> Fehlt fuer K64F
marcel1691 9:18f4959c2bac 47 //strcpy( mbed_uid, "k64f-1234" );
marcel1691 9:18f4959c2bac 48 //mbed_uid[32] = '\0';
marcel1691 9:18f4959c2bac 49 //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 50
marcel1691 9:18f4959c2bac 51 printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 52
bogdanm 2:7e489126fe7a 53 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
bogdanm 2:7e489126fe7a 54 }
bogdanm 2:7e489126fe7a 55
bogdanm 2:7e489126fe7a 56 // ****************************************************************************
bogdanm 2:7e489126fe7a 57 // NSP initialization
bogdanm 2:7e489126fe7a 58
bogdanm 2:7e489126fe7a 59 UDPSocket server;
bogdanm 2:7e489126fe7a 60 Endpoint nsp;
bogdanm 2:7e489126fe7a 61
bogdanm 2:7e489126fe7a 62 static void nsp_init()
bogdanm 2:7e489126fe7a 63 {
terohoo 0:2edbfea18d23 64 server.init();
terohoo 0:2edbfea18d23 65 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 66
bogdanm 2:7e489126fe7a 67 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 68
bogdanm 2:7e489126fe7a 69 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 70 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 71
marcel1691 9:18f4959c2bac 72 printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 73 }
terohoo 0:2edbfea18d23 74
bogdanm 2:7e489126fe7a 75 // ****************************************************************************
bogdanm 2:7e489126fe7a 76 // Resource creation
terohoo 0:2edbfea18d23 77
bogdanm 2:7e489126fe7a 78 static int create_resources()
bogdanm 2:7e489126fe7a 79 {
bogdanm 2:7e489126fe7a 80 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 81 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 82
bogdanm 2:7e489126fe7a 83 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 84
terohoo 0:2edbfea18d23 85 /* Create resources */
bogdanm 2:7e489126fe7a 86 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 87 if(!resource_ptr)
terohoo 0:2edbfea18d23 88 return 0;
terohoo 0:2edbfea18d23 89 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 90
bogdanm 2:7e489126fe7a 91 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 92 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 93 {
bogdanm 2:7e489126fe7a 94 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 95 return 0;
terohoo 0:2edbfea18d23 96 }
terohoo 0:2edbfea18d23 97 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 98
bogdanm 2:7e489126fe7a 99 // Static resources
stefan1691 16:b27c9d2b1a98 100 nsdl_create_static_resource( resource_ptr, strlen(staticResources[0] [0]), (uint8_t*) staticResources[0] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 101 (uint8_t*) staticResources[0] [2], strlen(staticResources[0] [2]) );
stefan1691 16:b27c9d2b1a98 102 nsdl_create_static_resource( resource_ptr, strlen(staticResources[1] [0]), (uint8_t*) staticResources[1] [0], 0, 0,
stefan1691 16:b27c9d2b1a98 103 (uint8_t*) staticResources[1] [2], strlen(staticResources[1] [2]) );
terohoo 0:2edbfea18d23 104
bogdanm 2:7e489126fe7a 105 // Dynamic resources
stefan1691 15:fba25b7e63cd 106 led1.create( resource_ptr );
stefan1691 15:fba25b7e63cd 107 led2.create( resource_ptr );
stefan1691 15:fba25b7e63cd 108 led3.create( resource_ptr );
terohoo 0:2edbfea18d23 109
terohoo 0:2edbfea18d23 110 /* Register with NSP */
bogdanm 2:7e489126fe7a 111 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 112 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 113 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 114 else
terohoo 0:2edbfea18d23 115 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 116 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 117
bogdanm 2:7e489126fe7a 118 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 119 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 120 return 1;
terohoo 0:2edbfea18d23 121 }
terohoo 0:2edbfea18d23 122
bogdanm 2:7e489126fe7a 123 // ****************************************************************************
bogdanm 2:7e489126fe7a 124 // Program entry point
terohoo 0:2edbfea18d23 125
bogdanm 2:7e489126fe7a 126 int main()
terohoo 0:2edbfea18d23 127 {
marcel1691 9:18f4959c2bac 128 printf("mbed NanoService demo");
bogdanm 2:7e489126fe7a 129 NSDL_DEBUG("mbed NanoService Example App 0.1\n");
bogdanm 2:7e489126fe7a 130
bogdanm 2:7e489126fe7a 131 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 132 ethernet_init();
terohoo 1:e35d7f10999a 133
bogdanm 2:7e489126fe7a 134 // Initialize NSP node
bogdanm 2:7e489126fe7a 135 nsp_init();
bogdanm 2:7e489126fe7a 136
bogdanm 2:7e489126fe7a 137 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 138 nsdl_init();
bogdanm 2:7e489126fe7a 139
bogdanm 2:7e489126fe7a 140 // Create NSDL resources
bogdanm 2:7e489126fe7a 141 create_resources();
bogdanm 2:7e489126fe7a 142
bogdanm 2:7e489126fe7a 143 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 144 nsdl_event_loop();
terohoo 0:2edbfea18d23 145 }