1248 / microcoap Goto Github PK
View Code? Open in Web Editor NEWA small CoAP implementation for microcontrollers
License: MIT License
A small CoAP implementation for microcontrollers
License: MIT License
I tried the code on my arduino, everything works well but "digitalWrite(led, HIGH)" has no reply. I also tried digitalRead(pin), the value is LOW all the time. Is there something wrong with Arduino library? I'm using General esp8266 module.
Arduino due can not use Ethernet,So I changed the microcoap.ino like thie
/*
#define WLAN_SSID "ASUS" // cannot be longer than 32 characters!
#define WLAN_PASS "96881000"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define PORT 5683
EthernetUDP udp;
uint8_t packetbuf[256];
static uint8_t scratch_raw[32];
static coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
uint32_t ip;
bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!wifi.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); wifi.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); wifi.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); wifi.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); wifi.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); wifi.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
void InitHP() //init the color box
{
Wire.begin();
Wire.beginTransmission(byte(0x20)); //0x40 >> 1
Wire.write(byte(0x06));
Wire.write(byte(0x00));
Wire.write(byte(0x00));
Wire.endTransmission();
}
void WriteHP(uint16_t hp)
{
int data;
Wire.beginTransmission(byte(0x20)); //0x40 >> 1
data = 0xffff<<hp;
Wire.write(byte(0x02));
Wire.write(data & 0xff);
Wire.write((data & 0xff00) >> 8);
Wire.endTransmission();
}
void setup()
{
InitHP();
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println(F("\nInitializing..."));
if (!wifi.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}
// Optional SSID scan
// listSSIDResults();
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!wifi.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));
/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!wifi.checkDHCP())
{
delay(100); // ToDo: Insert a DHCP timeout!
}
/* Display the IP address DNS, Gateway, etc. */
while (! displayConnectionDetails()) {
delay(1000);}
Serial.println();
udp.begin(PORT);
coap_setup();
endpoint_setup();
}
void udp_send(const uint8_t *buf, int buflen)
{
udp.beginPacket(udp.remoteIP(), udp.remotePort());
while(buflen--)
udp.write(*buf++);
udp.endPacket();
}
void loop()
{
int sz;
int rc;
coap_packet_t pkt;
int i;
if ((sz = udp.parsePacket()) > 0)
{
udp.read(packetbuf, sizeof(packetbuf));
for (i=0;i<sz;i++)
{
Serial.print(packetbuf[i], HEX);
Serial.print(" ");
}
Serial.println("");
if (0 != (rc = coap_parse(&pkt, packetbuf, sz)))
{
Serial.print("Bad packet rc=");
Serial.println(rc, DEC);
}
else
{
size_t rsplen = sizeof(packetbuf);
coap_packet_t rsppkt;
coap_handle_req(&scratch_buf, &pkt, &rsppkt);
memset(packetbuf, 0, UDP_TX_PACKET_MAX_SIZE);
if (0 != (rc = coap_build(packetbuf, &rsplen, &rsppkt)))
{
Serial.print("coap_build failed rc=");
Serial.println(rc, DEC);
}
else
{
udp_send(packetbuf, rsplen);
}
}
}
}
It is working great to connect WIFI like this:
Initializing...
Attempting to connect to ASUS
Started AP/SSID scan
Connecting to ASUS...Waiting to connect...Connected!
Request DHCP
IP Addr: 192.168.50.141
Netmask: 255.255.255.0
Gateway: 192.168.50.1
DHCPsrv: 192.168.50.1
DNSserv: 192.168.50.1
BUT it does not work when i use libcoap client or firefox cooper.
always timeout
please help!
thanks
Hi, I'm interested in using this library on a windows machine. My dev board is the TI LaunchPad. I'm getting some include errors on netinet/in.h. Do I need cygwin or something to correct this?
we are using micro coap as server(to receive the message from client arduino and then to glow a LED) on one of the arduinos with ethernet shield.could someone help us to know what coap can be used as client( to detect the output of LDR and then check if there is any obstacle using IR sensor and send the message to server arduino) so that the two arduinos with ethernet shields can communicate with each other.
There is discussion of the UDP buffer size here: http://forum.arduino.cc/index.php?topic=103502.0
The solution is to #define it immediately after the include:
#include <EthernetUdp.h>
#define UDP_TX_PACKET_MAX_SIZE 860 //increase UDP size
Hey I am a newbie to microcoap. I wanted to implement this microcap code in arduino uno with ethernet shield. I have a few queries.
I know this is not the correct place to post this. Apologies for the same.
Dear Devels,
I am trying to code a lightweight implementation of CoAP using the MicroCoAP API, but before to start, I would like to ask the current API can handle both client and server services
Kind Regards
In the endpoints.c file, there are a lot of strncat calls.
For example: strncat(rsp, ">;", len);
The third parameter should be the max length to be appended from the string at the second parameter. So it should not be "len".
Also to avoid buffer overflow, I think it should be:
strncat(rsp, ">;", len-strlen(rsp)); //len is already the buffer size without \0
Is my understanding right?
I loaded microcoap on ARDUINO DUE. When I receive a packet from a CoAP client (mozilla firefox with Copper plugin), where I can find options number and his value?
I created a resource called Alice.
I see that inpkt->opts[0].num contains this binary value: 00111011. By referring to http://tools.ietf.org/pdf/rfc7252.pdf (section 3.1) I suppose that the first four bits (0011=3 in decimal) are the OPTION DELTA, the last four bits (1011=11 in decimal) are the OPTION LENGTH. By referring to the "Table 7: CoAP Option Numbers" of the previous CoAP document, I see that this option is Uri-Host (number 3), but I don' t know where are the 11 bytes of the OPTION VALUE. Where are them? Are my assumpions right?
I´m trying to send payloads bigger than 64 bytes.
Is there a way to achieve it?
I try to make Microcoap works on esp8266. And it works good! But only when tested with Copper in Firefox. I don't get request response when I try to access it with node-coap (https://github.com/mcollina/node-coap/). The node-coap response event doesn't fire.
I found a similar question with (probably) the same issue here: https://stackoverflow.com/questions/31369908/can-not-connect-to-coap-resource-on-arduino
Node-coap works with "coap://coap.me:5683/", so I guess the issue should be in Microcoap, or maybe some protocol implementation difference? Any ideas are welcome.
does this library provide support for DTLS ?
How the retries for CON messages are supported?
Hey this is cache again !! Successfully tested this code on the Arduino Mega and the Uno boards( after a lot of struggle initially ). Also ported this code to open picus , another C platfrom.
One problem I am having is that the creation of resources. I am able to create resources , but I want to know if I can send strings to those resource values rather than "1" / "0" .
Also I want to follow the LWM2M specs when creating the resources. Can anyone specify how to do so ?
Another question , is that can i use other clients to interface with this COAP stack ? I believe this COAP stack is a minified version. Please help !
Thanks in advance !
Cache
Is it true that the observe option is not yet implemented in the GET function? Thanks,
Roel
Hi, are block-wise transfers supported on this library?
The only related code I saw is
COAP_CONTENTTYPE_NONE = -1, // bodge to allow us not to send option block
on coap.h
Hi!!
Your source is simple and tidy, I'm checking to apply. But I have a part to comment.
If the delta value in the options section of coap is 13 or 14, the option type may be greater than 255. However, the 'len' type of the structure 'coap_option_t' is set to 'uint8_t', so it should be applied to uint16_t.
I think this type doesn't exceed 1 byte. Onem2m is defined with more than 256 values in iot platform. If you define it as 2 bytes, I think it can be extended.
thank you.
I am facing weird issue on ESP8266. outpkt->payload.p
value is changing after handling request.
Handler:
int handleGetDeviceInfo(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t high, uint8_t low) {
const char result[] = "12";
int r = coap_make_response(scratch, outpkt, (const uint8_t*)result, 2, high, low, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
Serial.print("In handler after make response : ");
Serial.print(outpkt->payload.p[0], DEC);
Serial.print(outpkt->payload.p[1], DEC);
return r;
}
This code prints: "In handler after make response: 4950"
coap_handle_req
in coap.cpp
, after match:
int r= ep->handler(scratch, inpkt, outpkt, inpkt->hdr.id[0], inpkt->hdr.id[1]);
Serial.print("After handler : ");
Serial.print(outpkt->payload.p[0], DEC);
Serial.println(outpkt->payload.p[1], DEC);
return r;
While this code prints: "After handler : 68134"
I don't what I am missing. Any help would be appreciated. Thanks.
Opps
Result of compiling microcoap.ino with Arduino IDE: 1.5.6-R2
The sketch uses 14,560 bytes (45%) of storage space programs. The maximum is 32,256 bytes.
Global variables use 2348 bytes (114%) dynamic memory, leaving -300 bytes for local variables. The maximum is 2048 bytes.
Placed a pull request #44 to ensure a more comprehensive content type list is shown.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.