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 Feb 04 19:49:52 2015 +0000
Revision:
11:99178da0f3fa
Parent:
10:57352d0c287c
Child:
13:3c205b19c540
Abgestimmt mit IoTKit

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