Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.
/ conatra Public archive

Sinatra-style syntax for the Internet of Things (CoAP/CoRE, Arduino)

License

Notifications You must be signed in to change notification settings

valpackett/conatra

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

conatra unlicense

A C/C++ library for implementing CoAP / CoRE on microcontrollers, featuring Sinatra-style syntax!

  • CoAP is the Constrained Application Protocol, which is basically simplified and very compact binary HTTP over UDP.
  • CoRE is the Constrained RESTful Environments Link Format, which is basically the HTTP Link header as a response, not as a header.

This allows you to create RESTful interfaces on IoT (Internet of Things) devices.

In the box:

  • <conatra.h> // a Sinatra-style DSL for the microcoap library (using C preprocessor dark magic)
  • <EtherCard+coap.h> // boilerplate for connecting the EtherCard library (driver for the very popular ENC28J60 Ethernet module) with the microcoap library // supports multicast!
  • <WiFi+coap.h> // boilerplate for connecting the Arduino WiFi library (for the WiFi Shield or the ESP8266) with the microcoap library

If you use a different networking module, please send a pull request with boilerplate necessary for supporting it!

Usage

Arduino

  1. Install the microcoap library into the Arduino IDE;
  2. Remove main-posix.c and endpoints.c from the libraries/microcoap folder, because the IDE tries to compile all the things for no good reason;
  3. Install the library for your network module (EtherCard) into the Arduino IDE;
  4. Install conatra into the Arduino IDE;
  5. Use all of that stuff in your sketches like this:
#include <EtherCard.h>
#include <coap.h>
#include <EtherCard+coap.h>

byte Ethernet::buffer[400];
static uint8_t mymac[] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 };

void setup(void) {
  if (ether.begin(sizeof Ethernet::buffer, mymac, 4) == 0)
    Serial.println(F("Ethernet failed"));
  if (!ether.dhcpSetup())
    Serial.println(F("DHCP failed"));
  ether.printIp("My IP: ", ether.myip);
  coap_ethercard_begin();
  coap_ethercard_begin_multicast();
}

void loop(void) {
  ether.packetLoop(ether.packetReceive());
  delay(50);
}

// We define our endpoints in the ROUTES macro using the ROUTE macro:
// ROUTE(name, method, url, CoRE parameters, handler body)
//
// The name is used in C identifiers and doesn't
// really matter, just has to be unique.
//
// If you don't want to list the endpoint in CoRE links:
// ROUTE_HIDDEN(name, method, url, handler body)
//
// For responding, use one of the following macros:
// CONTENT, NOT_FOUND, BAD_REQUEST, CHANGED

#define ROUTES \
ROUTE(hello, COAP_METHOD_GET, URL("tests", "hello"), ";if=\"test\"", { \
  char response[3] = "Hi"; \
  CONTENT(COAP_CONTENTTYPE_TEXT_PLAIN, response); \
})

#include <conatra.h>

Here's how you would interact over this example (using the coap-cli tool):

$ coap get coap://192.168.1.13/.well-known/core
</tests/hello>;if="test"
$ coap get coap://192.168.1.13/tests/hello
Hi

For a full example, see the examples/iotweather/iotweather.ino sketch.

Contributing

Please feel free to submit pull requests! Bugfixes and simple non-breaking improvements will be accepted without any questions :-)

By participating in this project you agree to follow the Contributor Code of Conduct.

License

This is free and unencumbered software released into the public domain.
For more information, please refer to the UNLICENSE file or unlicense.org.

About

Sinatra-style syntax for the Internet of Things (CoAP/CoRE, Arduino)

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages