Since IFTTT (If This Then That) arrived and before the Maker channel was introduced, I always wanted to create my own recipe, and connect to the ever growing list of applications and channels. Platforms like Particle were a perfect fit, and easy to test (there are TONS of recipes already). But then again, I wanted something with a very low power consumption footprint (under mA's) to play nicely in a mesh-like network more importantly to be addressable in my existing IPv6/6LoWPAN
network.
The Border Router that translates IPv6 to IPv4
The first thing we need is an IPv4/IPv6
router to translate our IPv6/6LoWPAN
request to IPv4
, as IFTTT doesn't currently have an IPv6
endpoint (at least none I am aware of).
One option already implemented in other past projects is to use a Border Router over tunslip6 and WrapSix to convert IPv6 addresses to IPv4. However for this project I wanted to do something simpler, without requiring a Raspberry Pi or other host just for this specific task.
My alternative is to use an IP64 Ethernet router as shown below. I had a previous prototype on which I'm actively working on (still not yet available). However the code and support is available in my GitHub repository.
In the meantime you can build your own pretty easily, I posted an article detailing the steps to build your own pretty easily.
The Ethernet router relies on DHCP
to get an IPv4 address, so you just need to connect to an router or switch with DHCP enabled, and wait for the device to join the network. The network joining process is signaled by the RGB LED blinking green and stopping when associated.
With the Router's IPv4 address you can access its web server and see the devices in the Router's network, and statistics like uptime, routes, etc.
The IFTTT Zolertia IoT button
Understanding how to create a trigger to the Maker Channel was the next step. The Maker channel has a good simulator to understand what HTTP request it expects, just fill in the fields.
In my case as I only wanted to capture the pressing of a button, I didn't want to send any values. Then again, for a later project it may be useful to send also values as the battery level, device ID or IP.
We create 3 files: the Makefile
, project-conf.h
and the ifttt_client.c
.
This is what the application's Makefile
(the one telling the compiler what to do) looks like:
CFLAGS+=-DPROJECT_CONF_H=\"project-conf.h\"
CONTIKI_PROJECT = client ifttt_client
all: $(CONTIKI_PROJECT)
MODULES += core/net/http-socket
WITH_IP64 = 1
CONTIKI = ../../../../..
include $(CONTIKI)/Makefile.include
The ifttt_client
(our application) it uses the http-socket
to handle the HTTP requests, with IP64 support.
In the project-conf.h
configuration file we define 2 strings: the name of the IFTTT event and our API key (as given by IFTTT). Alternatively we can choose between the 2.4GHz or 868/915MHz radio interface. This is actually the only file you will have to touch, the rest of the applications is shown for in case you want to go into the details.
#define HTTP_CLIENT_BUFFER_LEN 256
#define IFTTT_EVENT "button"
#define IFTTT_KEY "XXXXXX"
#define NETSTACK_CONF_RADIO cc2538_rf_driver
#define ANTENNA_SW_SELECT_DEF_CONF ANTENNA_SW_SELECT_2_4GHZ
Then in the ifttt_client.c
application we create our url_buffer
string to use in the POST
request as follows:
snprintf(url_buffer, HTTP_CLIENT_BUFFER_LEN,
"http://maker.ifttt.com/trigger/%s/with/key/%s",
IFTTT_EVENT, IFTTT_KEY);
Contiki had already a http-socket
library, implementing both GET
and POST
methods. The library is located at core/net/http-socket.
void http_socket_init(struct http_socket *s);
int http_socket_get(struct http_socket *s, const char *url,
int64_t pos, uint64_t length,
http_socket_callback_t callback,
void *callbackptr);
int http_socket_post(struct http_socket *s, const char *url,
const void *postdata,
uint16_t postdatalen,
const char *content_type,
http_socket_callback_t callback,
void *callbackptr);
int http_socket_close(struct http_socket *socket);
Building the application was pretty straightforward: create an http_socket
structure, start the http-socket
driver, then create a POST
request when the user button
is pressed as shown below:
static struct http_socket s;
http_socket_init(&s);
while(1) {
PROCESS_YIELD();
if((ev == sensors_event) && (data == &button_sensor)) {
if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) ==
BUTTON_SENSOR_PRESSED_LEVEL) {
leds_on(LEDS_GREEN);
printf("Button pressed! sending a POST to IFTTT\n");
http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL);
}
}
}
To compile the application type the following:
cd examples/zolertia/zoul/ip64-http-example/client
make ifttt_client.upload && make login
The result is shown below. The application is compiled and flashed to the RE-Mote
, then a debug console over USB is open... now press the user button
!
When pressing the user button, the HTTP request is sent to IFTTT maker channel. The IFTTT maker channel sends as reply the Congratulations! You've fired the button event
message, confirming the event has been triggered.
On the other end I created a recipe to post to a Slack channel whenever the RE-Mote
user button is pressed.
The message is quite simple, a link to GIFF.IN, a page with random infinite gifs
, the best content to procrastinate at work and annoy the boss.
There are a lot of new examples and recipes that could be created, take as an example the Amazon button and alike. Besides annoying the co-workers, without being restricted by batteries and having the option to go over a long range (kilometres even), the options are endless.
Comments
Please log in or sign up to comment.