Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop clearing request's memory #8

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
all:
gcc -Wall -o coap endpoints.c main-posix.c coap.c -DDEBUG
gcc -Wall -o coap main-posix.c coap.c -DDEBUG

clean:
rm -f coap
50 changes: 15 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,21 @@
microcoap
=========
# Connecting sensors to the cloud

A tiny CoAP server for microcontrollers.
See http://tools.ietf.org/html/draft-ietf-core-coap-18
I've connected a [temperature & humidity sensor][dht], a photoresistor and a LED to an [Ethernet shield][eth] plugged onto my Arduino Uno.

Endpoint handlers are defined in endpoints.c
![Schematic](https://github.com/mbialon/microcoap/blob/master/doc/schematic.png)

* Arduino demo (Mega + Ethernet shield, LED + 220R on pin 6, PUT "0" or "1" to /light)
* POSIX (OS X/Linux) demo
* GET/PUT/POST
* No retries
* Piggybacked ACK only
I've used a [CoAP][coap] [library][microcoap] and I've created an [Arduino][arduino] [sketch][sketch].
It connects to the internet and sends sensor readings to the [cloud][teo].

I may view them in the application or on a dashboard.

For linux/OSX

make
./coap

For Arduino

open microcoap.ino

To test, use libcoap

./coap-client -v 100 -m get coap://127.0.0.1/.well-known/core
./coap-client -v 100 -m get coap://127.0.0.1/light
./coap-client -e "1" -m put coap://127.0.0.1/light
./coap-client -e "0" -m put coap://127.0.0.1/light

Or use copper (Firefox plugin)

coap://127.0.0.1

Arduino problem
===============

Arduino, by default, has a UDP transmit buffer of 24 bytes. This is too small
for some endpoints and will result in an error.
![Dashboard](https://github.com/mbialon/microcoap/blob/master/doc/dashboard.png)

[coap]: http://coap.technology
[arduino]: http://arduino.cc
[teo]: https://telemetria-online.pl/en/#app
[eth]: http://www.dx.com/p/ethernet-shield-with-wiznet-w5100-ethernet-chip-tf-slot-118061#.VYU-tWBVuHo
[dht]: http://www.dx.com/p/keyes-dht22-fr4-temperature-humidity-sensor-module-for-arduino-red-white-300285#.VYVPbmBVuHo
[microcoap]: https://github.com/1248/microcoap
[sketch]: https://github.com/mbialon/microcoap/blob/master/microcoap.ino
[repo]: https://github.com/mbialon/microcoap
35 changes: 30 additions & 5 deletions coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
#include <stddef.h>
#include "coap.h"

extern void endpoint_setup(void);
extern const coap_endpoint_t endpoints[];
//extern void endpoint_setup(void);
//extern const coap_endpoint_t endpoints[];

#ifdef DEBUG
void coap_dumpHeader(coap_header_t *hdr)
Expand Down Expand Up @@ -360,6 +360,18 @@ void coap_option_nibble(uint8_t value, uint8_t *nibble)
}
}

int coap_add_option(coap_packet_t *pkt, uint8_t num, const uint8_t *p, size_t len)
{
if (pkt->numopts == MAXOPT) {
return 1;
}
coap_option_t *opt = &pkt->opts[pkt->numopts++];
opt->num = num;
opt->buf.p = p;
opt->buf.len = len;
return 0;
}

int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, const coap_buffer_t* tok, coap_responsecode_t rspcode, coap_content_type_t content_type)
{
pkt->hdr.ver = 0x01;
Expand Down Expand Up @@ -389,14 +401,27 @@ int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint
return 0;
}

int coap_make_request(coap_packet_t *pkt)
{
int mid = rand();
pkt->hdr.ver = 0x01;
pkt->hdr.t = COAP_TYPE_CON;
pkt->hdr.tkl = 0;
pkt->hdr.code = 2;
pkt->hdr.id[0] = mid & 0xFF;
pkt->hdr.id[1] = mid >> 8;
pkt->numopts = 0;
pkt->payload.len = 0;
return 0;
}

// FIXME, if this looked in the table at the path before the method then
// it could more easily return 405 errors
int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt)
int coap_handle_req(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt)
{
const coap_option_t *opt;
uint8_t count;
int i;
const coap_endpoint_t *ep = endpoints;

while(NULL != ep->handler)
{
Expand Down Expand Up @@ -427,5 +452,5 @@ int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_

void coap_setup(void)
{
srand(analogRead(0));
}

9 changes: 7 additions & 2 deletions coap.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ typedef enum
COAP_CONTENTTYPE_NONE = -1, // bodge to allow us not to send option block
COAP_CONTENTTYPE_TEXT_PLAIN = 0,
COAP_CONTENTTYPE_APPLICATION_LINKFORMAT = 40,
COAP_CONTENTTYPE_OCTET_STREAM = 42
} coap_content_type_t;

///////////////////////
Expand Down Expand Up @@ -152,10 +153,14 @@ const coap_option_t *coap_findOptions(const coap_packet_t *pkt, uint8_t num, uin
int coap_build(uint8_t *buf, size_t *buflen, const coap_packet_t *pkt);
void coap_dump(const uint8_t *buf, size_t buflen, bool bare);
int coap_make_response(coap_rw_buffer_t *scratch, coap_packet_t *pkt, const uint8_t *content, size_t content_len, uint8_t msgid_hi, uint8_t msgid_lo, const coap_buffer_t* tok, coap_responsecode_t rspcode, coap_content_type_t content_type);
int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt);
int coap_handle_req(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt);
void coap_option_nibble(uint8_t value, uint8_t *nibble);
void coap_setup(void);
void endpoint_setup(void);

//void endpoint_setup(void);

int coap_make_request(coap_packet_t *pkt);
int coap_add_option(coap_packet_t *pkt, uint8_t num, const uint8_t *p, size_t len);

#ifdef __cplusplus
}
Expand Down
Binary file added doc/dashboard.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/schematic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 0 additions & 113 deletions endpoints.c

This file was deleted.

71 changes: 0 additions & 71 deletions main-posix.c

This file was deleted.

Loading