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:
Tue Mar 24 14:49:32 2015 +0000
Revision:
13:3c205b19c540
Parent:
11:99178da0f3fa
Child:
14:54c3708f7c3c
CoAP Device Server Client

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcel1691 13:3c205b19c540 1 /** CoAP Device Server Client
marcel1691 13:3c205b19c540 2 Zeile 29: Node Name aendern und folgende Adresse aufrufen:
marcel1691 13:3c205b19c540 3 http://nsp.cloudapp.net:8083/, User/PW = demo
marcel1691 13:3c205b19c540 4 */
terohoo 0:2edbfea18d23 5 #include "mbed.h"
terohoo 0:2edbfea18d23 6 #include "EthernetInterface.h"
bogdanm 2:7e489126fe7a 7 #include "nsdl_support.h"
bogdanm 2:7e489126fe7a 8 #include "dbg.h"
bogdanm 2:7e489126fe7a 9 // Include various resources
marcel1691 9:18f4959c2bac 10 //#include "temperature.h"
bogdanm 2:7e489126fe7a 11 #include "light.h"
marcel1691 9:18f4959c2bac 12 //#include "gps.h"
marcel1691 9:18f4959c2bac 13 //#include "relay.h"
terohoo 0:2edbfea18d23 14
bogdanm 2:7e489126fe7a 15 Serial pc(USBTX, USBRX); // tx, rx
terohoo 0:2edbfea18d23 16
bogdanm 2:7e489126fe7a 17 // ****************************************************************************
bogdanm 2:7e489126fe7a 18 // Configuration section
bogdanm 2:7e489126fe7a 19
bogdanm 2:7e489126fe7a 20 // Ethernet configuration
terohoo 0:2edbfea18d23 21 /* Define this to enable DHCP, otherwise manual address configuration is used */
terohoo 0:2edbfea18d23 22 #define DHCP
terohoo 0:2edbfea18d23 23
bogdanm 2:7e489126fe7a 24 // NSP configuration
terohoo 0:2edbfea18d23 25 /* Change this IP address to that of your NanoService Platform installation */
marcel1691 11:99178da0f3fa 26 static const char* NSP_ADDRESS = "23.97.178.38"; /* demo NSP, web interface at http://nsp.cloudapp.net:8083/, User/PW = demo */
marcel1691 11:99178da0f3fa 27 //static const char* NSP_ADDRESS = "192.168.178.65"; /* Lokale Installation */
bogdanm 2:7e489126fe7a 28 static const int NSP_PORT = 5683;
marcel1691 9:18f4959c2bac 29 char endpoint_name[16] = "mbed-k64f-1234"; // aendern pro Node
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 */
bogdanm 2:7e489126fe7a 43 NSDL_DEBUG("DHCP in use\r\n");
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 9:18f4959c2bac 49 //mbed_interface_uid(mbed_uid); // -> Fehlt fuer K64F
marcel1691 9:18f4959c2bac 50 //strcpy( mbed_uid, "k64f-1234" );
marcel1691 9:18f4959c2bac 51 //mbed_uid[32] = '\0';
marcel1691 9:18f4959c2bac 52 //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name));
terohoo 0:2edbfea18d23 53
marcel1691 9:18f4959c2bac 54 printf("IP:%s", eth.getIPAddress());
terohoo 0:2edbfea18d23 55
bogdanm 2:7e489126fe7a 56 NSDL_DEBUG("IP Address:%s ", eth.getIPAddress());
bogdanm 2:7e489126fe7a 57 }
bogdanm 2:7e489126fe7a 58
bogdanm 2:7e489126fe7a 59 // ****************************************************************************
bogdanm 2:7e489126fe7a 60 // NSP initialization
bogdanm 2:7e489126fe7a 61
bogdanm 2:7e489126fe7a 62 UDPSocket server;
bogdanm 2:7e489126fe7a 63 Endpoint nsp;
bogdanm 2:7e489126fe7a 64
bogdanm 2:7e489126fe7a 65 static void nsp_init()
bogdanm 2:7e489126fe7a 66 {
terohoo 0:2edbfea18d23 67 server.init();
terohoo 0:2edbfea18d23 68 server.bind(NSP_PORT);
terohoo 0:2edbfea18d23 69
bogdanm 2:7e489126fe7a 70 nsp.set_address(NSP_ADDRESS, NSP_PORT);
bogdanm 2:7e489126fe7a 71
bogdanm 2:7e489126fe7a 72 NSDL_DEBUG("name: %s", endpoint_name);
bogdanm 2:7e489126fe7a 73 NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT);
terohoo 0:2edbfea18d23 74
marcel1691 9:18f4959c2bac 75 printf("EP name:%s\n", endpoint_name);
bogdanm 2:7e489126fe7a 76 }
terohoo 0:2edbfea18d23 77
bogdanm 2:7e489126fe7a 78 // ****************************************************************************
bogdanm 2:7e489126fe7a 79 // Resource creation
terohoo 0:2edbfea18d23 80
bogdanm 2:7e489126fe7a 81 static int create_resources()
bogdanm 2:7e489126fe7a 82 {
bogdanm 2:7e489126fe7a 83 sn_nsdl_resource_info_s *resource_ptr = NULL;
bogdanm 2:7e489126fe7a 84 sn_nsdl_ep_parameters_s *endpoint_ptr = NULL;
bogdanm 2:7e489126fe7a 85
bogdanm 2:7e489126fe7a 86 NSDL_DEBUG("Creating resources");
terohoo 0:2edbfea18d23 87
terohoo 0:2edbfea18d23 88 /* Create resources */
bogdanm 2:7e489126fe7a 89 resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 90 if(!resource_ptr)
terohoo 0:2edbfea18d23 91 return 0;
terohoo 0:2edbfea18d23 92 memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s));
terohoo 0:2edbfea18d23 93
bogdanm 2:7e489126fe7a 94 resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 95 if(!resource_ptr->resource_parameters_ptr)
terohoo 0:2edbfea18d23 96 {
bogdanm 2:7e489126fe7a 97 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 98 return 0;
terohoo 0:2edbfea18d23 99 }
terohoo 0:2edbfea18d23 100 memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s));
terohoo 0:2edbfea18d23 101
bogdanm 2:7e489126fe7a 102 // Static resources
marcel1691 10:57352d0c287c 103 nsdl_create_static_resource(resource_ptr, sizeof("dev/mfg")-1, (uint8_t*) "dev/mfg", 0, 0, (uint8_t*) "mc-b", sizeof("mc-b")-1);
marcel1691 10:57352d0c287c 104 nsdl_create_static_resource(resource_ptr, sizeof("dev/mdl")-1, (uint8_t*) "dev/mdl", 0, 0, (uint8_t*) "CoAP mbed device", sizeof("CoAP mbed device")-1);
terohoo 0:2edbfea18d23 105
bogdanm 2:7e489126fe7a 106 // Dynamic resources
marcel1691 9:18f4959c2bac 107 //create_temperature_resource(resource_ptr);
bogdanm 2:7e489126fe7a 108 create_light_resource(resource_ptr);
marcel1691 9:18f4959c2bac 109 //create_gps_resource(resource_ptr);
marcel1691 9:18f4959c2bac 110 //create_relay_resource(resource_ptr);
terohoo 0:2edbfea18d23 111
terohoo 0:2edbfea18d23 112 /* Register with NSP */
bogdanm 2:7e489126fe7a 113 endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr);
terohoo 0:2edbfea18d23 114 if(sn_nsdl_register_endpoint(endpoint_ptr) != 0)
terohoo 0:2edbfea18d23 115 pc.printf("NSP registering failed\r\n");
terohoo 0:2edbfea18d23 116 else
terohoo 0:2edbfea18d23 117 pc.printf("NSP registering OK\r\n");
bogdanm 2:7e489126fe7a 118 nsdl_clean_register_endpoint(&endpoint_ptr);
terohoo 0:2edbfea18d23 119
bogdanm 2:7e489126fe7a 120 nsdl_free(resource_ptr->resource_parameters_ptr);
bogdanm 2:7e489126fe7a 121 nsdl_free(resource_ptr);
terohoo 0:2edbfea18d23 122 return 1;
terohoo 0:2edbfea18d23 123 }
terohoo 0:2edbfea18d23 124
bogdanm 2:7e489126fe7a 125 // ****************************************************************************
bogdanm 2:7e489126fe7a 126 // Program entry point
terohoo 0:2edbfea18d23 127
bogdanm 2:7e489126fe7a 128 int main()
terohoo 0:2edbfea18d23 129 {
marcel1691 9:18f4959c2bac 130 printf("mbed NanoService demo");
bogdanm 2:7e489126fe7a 131 NSDL_DEBUG("mbed NanoService Example App 0.1\n");
bogdanm 2:7e489126fe7a 132
bogdanm 2:7e489126fe7a 133 // Initialize Ethernet interface first
bogdanm 2:7e489126fe7a 134 ethernet_init();
terohoo 1:e35d7f10999a 135
bogdanm 2:7e489126fe7a 136 // Initialize NSP node
bogdanm 2:7e489126fe7a 137 nsp_init();
bogdanm 2:7e489126fe7a 138
bogdanm 2:7e489126fe7a 139 // Initialize NSDL stack
bogdanm 2:7e489126fe7a 140 nsdl_init();
bogdanm 2:7e489126fe7a 141
bogdanm 2:7e489126fe7a 142 // Create NSDL resources
bogdanm 2:7e489126fe7a 143 create_resources();
bogdanm 2:7e489126fe7a 144
bogdanm 2:7e489126fe7a 145 // Run the NSDL event loop (never returns)
bogdanm 2:7e489126fe7a 146 nsdl_event_loop();
terohoo 0:2edbfea18d23 147 }