trouch
Posts: 310
Joined: Fri Aug 03, 2012 7:24 pm
Location: France
Contact: Website

WebIOPi 0.6 now includes a CoAP stack with multicast support

Thu Mar 28, 2013 8:26 pm

For people who don't know, WebIOPi is a REST framework to remote control Raspberry Pi GPIO/I2C/SPI/...
WebIOPi 0.6 now include CoAP stack as an additional protocol to handle REST requests.

CoAP, for Constrained Application Protocol, is a protocol to access and make REST services with constrained devices.
It's currently in draft at IETF as a part of CoRE working group : http://tools.ietf.org/html/draft-ietf-core-coap-14

The Pi is enough powered to handle HTTP requests, but HTTP uses TCP which has its pro and cons.
Built on top of UDP, CoAP can decrease latency and allows multicast which is a premiere for the Pi !

The HTTP/TCP problem
Lets imagine you have many Pi on your network and you want to make the same action on all Pi.
With HTTP and TCP, you have to make a request and a connection to all Pi of your networks.
From your controller device, you can do it by making requests sequentially :

Code: Select all

request = ...
for ip in all_ips:                     # iterate over all IPs
    makeHTTPRequestThread(ip, request) # send the request to each IP
If you watch your controller network interface, you will see 5 packets with each devices you have.
If you have 10 Raspberry Pi, you will see 50 packets.
Then if each request take one second, you will have 10 seconds between the first and last Pi request.
You can of course use threads to make requests in parallel, but you can't create how many thread you want.

CoAP/UDP answer
With UDP multicast, devices listen on an IP used by all devices so they all receive UDP frames sent to that IP.
It means you can control all your Pis of your network with a single REST request in a single UDP frame :

Code: Select all

request = ...
makeCOAPRequest(multicastIP, request) # send the request to the multicast IP
If you watch your controller network interface, you will see a single packet sent.
That's it. For 1, 2, or thousands of Pi, a single packet is sent for all.
Depending on network topology, it even allows to synchronize actions on remote devices !

Client side libraries
Not only providing a CoAP stack, you can also found both Python and Java clients to use CoAP and/or HTTP :

Code: Select all

from webiopi.clients import *
from time import sleep

# Create a WebIOPi client
client = PiHttpClient("raspberrypi")   # HTTP only
#client = PiMixedClient("raspberrypi") # CoAP with HTTP fallback
#client = PiCoapClient("raspberrypi")  # CoAP only
#client = PiMulticastClient()          # CoAP Multicast

client.setCredentials("webiopi", "raspberry")

# RPi native GPIO
gpio = NativeGPIO(client)
gpio.setFunction(25, "out")
state = True

while True:
    # toggle digital state
    state = not state
    gpio.digitalWrite(25, state)
    sleep(1)
If you have a LED connected on GPIO 25 of all your Pis, they will blink synchronously.
To retrieve values, you send a single request and all Pis will answer you.

As you can see, PiMulticastClient does not requires any IP, because it knows the multicast IP used by WebIOPi.
So you can use the PiMulticastClient with a single Pi on your LAN if you don't want to care about its IP.

Links
WebIOPi includes many new features, see my blog post and the project page.
http://trouch.com/2013/03/27/webiopi-0- ... pi-to-iot/
https://code.google.com/p/webiopi/

WebIOPi - Raspberry Pi REST Framework to control your Pi from the web
http://store.raspberrypi.com/projects/webiopi
http://code.google.com/p/webiopi/
http://trouch.com

Return to “Networking and servers”