Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of coap-example by
Diff: source/main.cpp
- Revision:
- 1:0615912bf8ec
- Parent:
- 0:0681e205d0e9
- Child:
- 2:6704302e21c7
--- a/source/main.cpp Wed Feb 15 21:18:58 2017 +0100
+++ b/source/main.cpp Thu Jun 28 14:11:10 2018 +0000
@@ -22,12 +22,31 @@
#include "sn_coap_protocol.h"
#include "sn_coap_header.h"
+
+
+//EDIT Martin Zeitler
+//Definieren der Netzwerkparameter
+
+#define TARGET_IP "192.168.0.230"
+//#define TARGET_IP "coap.me"
+#define TARGET_PORT 5683
+/*
+Serial pc(USBTX, USBRX);
+char c;
+bool endless = true;
+*/
+//ENIT END
+
+
+
UDPSocket socket; // Socket to talk CoAP over
+
Thread recvfromThread; // Thread to receive messages over CoAP
struct coap_s* coapHandle;
coap_version_e coapVersion = COAP_VERSION_1;
+
// CoAP HAL
void* coap_malloc(uint16_t size) {
return malloc(size);
@@ -48,7 +67,12 @@
return 0;
}
+
+
+
+
// Main function for the recvfrom thread
+
void recvfromMain() {
SocketAddress addr;
uint8_t* recv_buffer = (uint8_t*)malloc(1280); // Suggested is to keep packet size under 1280 bytes
@@ -71,6 +95,7 @@
printf("\tpayload_len: %d\n", parsed->payload_len);
printf("\tpayload: %s\n", payload.c_str());
printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr);
+ printf("\toptions_list_ptr: %p\n", parsed->options_list_ptr);
}
free(recv_buffer);
@@ -78,26 +103,35 @@
printf("UDPSocket::recvfrom failed, error code %d. Shutting down receive thread.\n", ret);
}
+
+
+
int main() {
+ //Netzwerkkonfiguration
NetworkInterface *network = easy_connect(true);
if (!network) {
printf("Cannot connect to the network, see serial output");
return 1;
}
-
printf("Connected to the network. Opening a socket...\n");
-
// Open a socket on the network interface
socket.open(network);
+
// Initialize the CoAP protocol handle, pointing to local implementations on malloc/free/tx/rx functions
coapHandle = sn_coap_protocol_init(&coap_malloc, &coap_free, &coap_tx_cb, &coap_rx_cb);
+
// UDPSocket::recvfrom is blocking, so run it in a separate RTOS thread
recvfromThread.start(&recvfromMain);
+
+
+/*
+ while(endless){
+*/
// Path to the resource we want to retrieve
- const char* coap_uri_path = "/hello";
+ const char* coap_uri_path = "1214";
// See ns_coap_header.h
sn_coap_hdr_s *coap_res_ptr = (sn_coap_hdr_s*)calloc(sizeof(sn_coap_hdr_s), 1);
@@ -126,11 +160,30 @@
// }
// printf("\n");
- int scount = socket.sendto("coap.me", 5683, message_ptr, message_len);
- printf("Sent %d bytes to coap://coap.me:5683\n", scount);
+ int scount = socket.sendto(TARGET_IP, TARGET_PORT, message_ptr, message_len);
+ printf("Sent %d bytes to %s:%d \n", scount,TARGET_IP, TARGET_PORT);
free(coap_res_ptr);
free(message_ptr);
-
+
Thread::wait(osWaitForever);
+
+
+
+/*
+ printf("Type /'t/' to send again, press /'f/' to stop");
+ char c = pc.getc();
+
+ if(c == 't')
+ {
+ endless = true;
+ }
+ if(c == 'f')
+ {
+ endless = false;
+ }
+ }
+*/
+
+
}
