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:
Sun Jan 18 15:41:27 2015 +0000
Revision:
9:18f4959c2bac
Parent:
8:cdf868ccae03
Child:
10:57352d0c287c
CoAP Client Beispiel

Who changed what in which revision?

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