![](/media/cache/group/ep_srxDLMd.webp.50x50_q85.jpg)
CoAP Device Server Client mit leshan Server
Dependencies: EthernetInterface mbed-rtos mbed nsdl_lib
Fork of COAPmbed by
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"
main.cpp@15:fba25b7e63cd, 2015-06-21 (annotated)
- Committer:
- stefan1691
- Date:
- Sun Jun 21 12:12:34 2015 +0000
- Revision:
- 15:fba25b7e63cd
- Parent:
- 14:54c3708f7c3c
- Child:
- 16:b27c9d2b1a98
CoAP Device Server Client mit leshan Server
Who changed what in which revision?
User | Revision | Line number | New 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" |
bogdanm | 2:7e489126fe7a | 8 | // Include various resources |
marcel1691 | 9:18f4959c2bac | 9 | //#include "temperature.h" |
bogdanm | 2:7e489126fe7a | 10 | #include "light.h" |
marcel1691 | 9:18f4959c2bac | 11 | //#include "gps.h" |
marcel1691 | 9:18f4959c2bac | 12 | //#include "relay.h" |
stefan1691 | 15:fba25b7e63cd | 13 | #include "PwmOutResource.h" |
stefan1691 | 15:fba25b7e63cd | 14 | |
stefan1691 | 15:fba25b7e63cd | 15 | PwmOutResource led1( D11, "led1", "10/0/0" ); |
stefan1691 | 15:fba25b7e63cd | 16 | PwmOutResource led2( D12, "led2", "10/0/1" ); |
stefan1691 | 15:fba25b7e63cd | 17 | PwmOutResource led3( D13, "led3", "10/0/2" ); |
terohoo | 0:2edbfea18d23 | 18 | |
bogdanm | 2:7e489126fe7a | 19 | Serial pc(USBTX, USBRX); // tx, rx |
terohoo | 0:2edbfea18d23 | 20 | |
bogdanm | 2:7e489126fe7a | 21 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 22 | // Configuration section |
bogdanm | 2:7e489126fe7a | 23 | |
bogdanm | 2:7e489126fe7a | 24 | // Ethernet configuration |
terohoo | 0:2edbfea18d23 | 25 | /* Define this to enable DHCP, otherwise manual address configuration is used */ |
terohoo | 0:2edbfea18d23 | 26 | #define DHCP |
terohoo | 0:2edbfea18d23 | 27 | |
bogdanm | 2:7e489126fe7a | 28 | // NSP configuration |
terohoo | 0:2edbfea18d23 | 29 | /* Change this IP address to that of your NanoService Platform installation */ |
stefan1691 | 15:fba25b7e63cd | 30 | static const char* NSP_ADDRESS = "192.168.178.23"; /* Lokale Installation - leshan Server (gepatcht) */ |
bogdanm | 2:7e489126fe7a | 31 | static const int NSP_PORT = 5683; |
stefan1691 | 14:54c3708f7c3c | 32 | char endpoint_name[] = "mbed-k64f-1234"; // aendern pro Node |
bogdanm | 2:7e489126fe7a | 33 | uint8_t ep_type[] = {"mbed_device"}; |
bogdanm | 2:7e489126fe7a | 34 | uint8_t lifetime_ptr[] = {"1200"}; |
terohoo | 0:2edbfea18d23 | 35 | |
bogdanm | 2:7e489126fe7a | 36 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 37 | // Ethernet initialization |
terohoo | 0:2edbfea18d23 | 38 | |
terohoo | 0:2edbfea18d23 | 39 | EthernetInterface eth; |
terohoo | 0:2edbfea18d23 | 40 | |
bogdanm | 2:7e489126fe7a | 41 | static void ethernet_init() |
terohoo | 0:2edbfea18d23 | 42 | { |
marcel1691 | 9:18f4959c2bac | 43 | //char mbed_uid[33]; // for creating unique name for the board |
terohoo | 0:2edbfea18d23 | 44 | |
terohoo | 0:2edbfea18d23 | 45 | /* Initialize network */ |
terohoo | 0:2edbfea18d23 | 46 | eth.init(); |
marcel1691 | 9:18f4959c2bac | 47 | |
terohoo | 0:2edbfea18d23 | 48 | if(eth.connect(30000) == 0) |
terohoo | 0:2edbfea18d23 | 49 | pc.printf("Connect OK\n\r"); |
terohoo | 0:2edbfea18d23 | 50 | |
marcel1691 | 9:18f4959c2bac | 51 | //mbed_interface_uid(mbed_uid); // -> Fehlt fuer K64F |
marcel1691 | 9:18f4959c2bac | 52 | //strcpy( mbed_uid, "k64f-1234" ); |
marcel1691 | 9:18f4959c2bac | 53 | //mbed_uid[32] = '\0'; |
marcel1691 | 9:18f4959c2bac | 54 | //strncat(endpoint_name, mbed_uid + 27, 15 - strlen(endpoint_name)); |
terohoo | 0:2edbfea18d23 | 55 | |
marcel1691 | 9:18f4959c2bac | 56 | printf("IP:%s", eth.getIPAddress()); |
terohoo | 0:2edbfea18d23 | 57 | |
bogdanm | 2:7e489126fe7a | 58 | NSDL_DEBUG("IP Address:%s ", eth.getIPAddress()); |
bogdanm | 2:7e489126fe7a | 59 | } |
bogdanm | 2:7e489126fe7a | 60 | |
bogdanm | 2:7e489126fe7a | 61 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 62 | // NSP initialization |
bogdanm | 2:7e489126fe7a | 63 | |
bogdanm | 2:7e489126fe7a | 64 | UDPSocket server; |
bogdanm | 2:7e489126fe7a | 65 | Endpoint nsp; |
bogdanm | 2:7e489126fe7a | 66 | |
bogdanm | 2:7e489126fe7a | 67 | static void nsp_init() |
bogdanm | 2:7e489126fe7a | 68 | { |
terohoo | 0:2edbfea18d23 | 69 | server.init(); |
terohoo | 0:2edbfea18d23 | 70 | server.bind(NSP_PORT); |
terohoo | 0:2edbfea18d23 | 71 | |
bogdanm | 2:7e489126fe7a | 72 | nsp.set_address(NSP_ADDRESS, NSP_PORT); |
bogdanm | 2:7e489126fe7a | 73 | |
bogdanm | 2:7e489126fe7a | 74 | NSDL_DEBUG("name: %s", endpoint_name); |
bogdanm | 2:7e489126fe7a | 75 | NSDL_DEBUG("NSP=%s - port %d\n", NSP_ADDRESS, NSP_PORT); |
terohoo | 0:2edbfea18d23 | 76 | |
marcel1691 | 9:18f4959c2bac | 77 | printf("EP name:%s\n", endpoint_name); |
bogdanm | 2:7e489126fe7a | 78 | } |
terohoo | 0:2edbfea18d23 | 79 | |
bogdanm | 2:7e489126fe7a | 80 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 81 | // Resource creation |
terohoo | 0:2edbfea18d23 | 82 | |
bogdanm | 2:7e489126fe7a | 83 | static int create_resources() |
bogdanm | 2:7e489126fe7a | 84 | { |
bogdanm | 2:7e489126fe7a | 85 | sn_nsdl_resource_info_s *resource_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 86 | sn_nsdl_ep_parameters_s *endpoint_ptr = NULL; |
bogdanm | 2:7e489126fe7a | 87 | |
bogdanm | 2:7e489126fe7a | 88 | NSDL_DEBUG("Creating resources"); |
terohoo | 0:2edbfea18d23 | 89 | |
terohoo | 0:2edbfea18d23 | 90 | /* Create resources */ |
bogdanm | 2:7e489126fe7a | 91 | resource_ptr = (sn_nsdl_resource_info_s*)nsdl_alloc(sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 92 | if(!resource_ptr) |
terohoo | 0:2edbfea18d23 | 93 | return 0; |
terohoo | 0:2edbfea18d23 | 94 | memset(resource_ptr, 0, sizeof(sn_nsdl_resource_info_s)); |
terohoo | 0:2edbfea18d23 | 95 | |
bogdanm | 2:7e489126fe7a | 96 | resource_ptr->resource_parameters_ptr = (sn_nsdl_resource_parameters_s*)nsdl_alloc(sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 97 | if(!resource_ptr->resource_parameters_ptr) |
terohoo | 0:2edbfea18d23 | 98 | { |
bogdanm | 2:7e489126fe7a | 99 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 100 | return 0; |
terohoo | 0:2edbfea18d23 | 101 | } |
terohoo | 0:2edbfea18d23 | 102 | memset(resource_ptr->resource_parameters_ptr, 0, sizeof(sn_nsdl_resource_parameters_s)); |
terohoo | 0:2edbfea18d23 | 103 | |
bogdanm | 2:7e489126fe7a | 104 | // Static resources |
stefan1691 | 15:fba25b7e63cd | 105 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/0")-1, (uint8_t*) "3/0/0", 0, 0, (uint8_t*) "mc-b", sizeof("mc-b")-1); |
stefan1691 | 15:fba25b7e63cd | 106 | nsdl_create_static_resource(resource_ptr, sizeof("3/0/1")-1, (uint8_t*) "3/0/1", 0, 0, (uint8_t*) "CoAP mbed device", sizeof("CoAP mbed device")-1); |
terohoo | 0:2edbfea18d23 | 107 | |
bogdanm | 2:7e489126fe7a | 108 | // Dynamic resources |
marcel1691 | 9:18f4959c2bac | 109 | //create_temperature_resource(resource_ptr); |
bogdanm | 2:7e489126fe7a | 110 | create_light_resource(resource_ptr); |
marcel1691 | 9:18f4959c2bac | 111 | //create_gps_resource(resource_ptr); |
marcel1691 | 9:18f4959c2bac | 112 | //create_relay_resource(resource_ptr); |
stefan1691 | 15:fba25b7e63cd | 113 | led1.create( resource_ptr ); |
stefan1691 | 15:fba25b7e63cd | 114 | led2.create( resource_ptr ); |
stefan1691 | 15:fba25b7e63cd | 115 | led3.create( resource_ptr ); |
terohoo | 0:2edbfea18d23 | 116 | |
terohoo | 0:2edbfea18d23 | 117 | /* Register with NSP */ |
bogdanm | 2:7e489126fe7a | 118 | endpoint_ptr = nsdl_init_register_endpoint(endpoint_ptr, (uint8_t*)endpoint_name, ep_type, lifetime_ptr); |
terohoo | 0:2edbfea18d23 | 119 | if(sn_nsdl_register_endpoint(endpoint_ptr) != 0) |
terohoo | 0:2edbfea18d23 | 120 | pc.printf("NSP registering failed\r\n"); |
terohoo | 0:2edbfea18d23 | 121 | else |
terohoo | 0:2edbfea18d23 | 122 | pc.printf("NSP registering OK\r\n"); |
bogdanm | 2:7e489126fe7a | 123 | nsdl_clean_register_endpoint(&endpoint_ptr); |
terohoo | 0:2edbfea18d23 | 124 | |
bogdanm | 2:7e489126fe7a | 125 | nsdl_free(resource_ptr->resource_parameters_ptr); |
bogdanm | 2:7e489126fe7a | 126 | nsdl_free(resource_ptr); |
terohoo | 0:2edbfea18d23 | 127 | return 1; |
terohoo | 0:2edbfea18d23 | 128 | } |
terohoo | 0:2edbfea18d23 | 129 | |
bogdanm | 2:7e489126fe7a | 130 | // **************************************************************************** |
bogdanm | 2:7e489126fe7a | 131 | // Program entry point |
terohoo | 0:2edbfea18d23 | 132 | |
bogdanm | 2:7e489126fe7a | 133 | int main() |
terohoo | 0:2edbfea18d23 | 134 | { |
marcel1691 | 9:18f4959c2bac | 135 | printf("mbed NanoService demo"); |
bogdanm | 2:7e489126fe7a | 136 | NSDL_DEBUG("mbed NanoService Example App 0.1\n"); |
bogdanm | 2:7e489126fe7a | 137 | |
bogdanm | 2:7e489126fe7a | 138 | // Initialize Ethernet interface first |
bogdanm | 2:7e489126fe7a | 139 | ethernet_init(); |
terohoo | 1:e35d7f10999a | 140 | |
bogdanm | 2:7e489126fe7a | 141 | // Initialize NSP node |
bogdanm | 2:7e489126fe7a | 142 | nsp_init(); |
bogdanm | 2:7e489126fe7a | 143 | |
bogdanm | 2:7e489126fe7a | 144 | // Initialize NSDL stack |
bogdanm | 2:7e489126fe7a | 145 | nsdl_init(); |
bogdanm | 2:7e489126fe7a | 146 | |
bogdanm | 2:7e489126fe7a | 147 | // Create NSDL resources |
bogdanm | 2:7e489126fe7a | 148 | create_resources(); |
bogdanm | 2:7e489126fe7a | 149 | |
bogdanm | 2:7e489126fe7a | 150 | // Run the NSDL event loop (never returns) |
bogdanm | 2:7e489126fe7a | 151 | nsdl_event_loop(); |
terohoo | 0:2edbfea18d23 | 152 | } |