Internet of Toys (An IOT stack for Toys & Devices)
Clone or download
Pull request Compare This branch is 12 commits behind sparkslabs:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
Arduino
Python
.gitignore
README.md
card_notes.md
web_api.md

README.md

IOToy - Internet of Toys

(and other Devices)

This is where a collection of tools for creating and working with toys connected to each other as an Internet of Toys. The following pieces are in the process of being packaged up to go in here:

  • A collection of pieces allowing serial control (wired or BT) and introspection of an arduino device.
  • A mapping of that to a python proxy
  • A mapping of that python proxy to a RESTful JSON based service.
  • Automatic advertisement of that service on a local mDNS interface - with the service name derived from the arduino API
  • A python API for "importing" IOT devices. In particular treating devices like python packages.
  • A python API for then mapping the RESTful interface back to python linguistic rules.

The idea being that you can build your own little robots or sensors. Once you've done that being able to do the minimum to allow it to be controlled using high level python code over a local network connection doing something like this:

from iotoy.local import robot

ON = 1
robot.forward()
robot.led = ON
if robot.sensor >255:
   robot.forward()

print robot.sensor.__doc__

At present there isn't a (complete) robot example (Boo!) - though this is coming. However, the testhost is fully implemented, meaning you can plug in a device to the server host. Then on that machine do this:

michael@home:~/Development/iotoy/Python/examples$ ./webhost_for_arduino_device.py

That the communicates with the device, finds out what it does and create the RESTful web service. You can then do this:

michael@wizard:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from iotoy.local import testhosttiny
>>> testhosttiny
<iotoy.discovery.testhosttiny object at 0x7f7add452350>
>>> help(testhosttiny)
{ Screen shows help from the introspected device }
>>> pprint.pprint(testhosttiny.devinfo)
{u'attrs': {u'drive_forward_time_ms': {u'help': u'How long to move forward',
                                       u'href': u'/drive_forward_time_ms',
                                       u'type': u'int'},
            u'ratio': {u'help': u'Sample float attribute',
                       u'href': u'/ratio',
                       u'type': u'float'},
            u'some_flag': {u'help': u'Sample bool attribute',
                           u'href': u'/some_flag',
                           u'type': u'bool'},
            u'str_id': {u'help': u'Sample str attribute',
                        u'href': u'/str_id',
                        u'type': u'str'},
            u'turn_time_ms': {u'help': u'How long to turn',
                              u'href': u'/turn_time_ms',
                              u'type': u'int'}},
 u'devicename': u'testhosttiny',
 u'funcs': {u'barecommand': {u'help': u'test, basic command, no arg/result',
                             u'href': u'/barecommand',
                             u'type': 'iotoy.org/types/function',
                             u'value': {u'name': u'barecommand',
                                        u'spec': {u'args': [],
                                                  u'result': []}}},
            u'no_arg_result_T': {u'help': u'test, one arg, generic type',
                                 u'href': u'/no_arg_result_T',
                                 u'type': 'iotoy.org/types/function',
                                 u'value': {u'name': u'no_arg_result_T',
                                            u'spec': {u'args': [],
                                                      u'result': [[u'result',
                                                                   u'T']]}}},
            u'no_arg_result_bool': {u'help': u'test, one arg, boolean',
                                    u'href': u'/no_arg_result_bool',
                                    u'type': 'iotoy.org/types/function',
                                    u'value': {u'name': u'no_arg_result_bool',
                                               u'spec': {u'args': [],
                                                         u'result': [[u'result',
                                                                      u'bool']]}}},
            u'no_arg_result_float': {u'help': u'test, one arg, float',
                                     u'href': u'/no_arg_result_float',
                                     u'type': 'iotoy.org/types/function',
                                     u'value': {u'name': u'no_arg_result_float',
                                                u'spec': {u'args': [],
                                                          u'result': [[u'result',
                                                                       u'float']]}}},
            u'no_arg_result_int': {u'help': u'test, one arg, integer',
                                   u'href': u'/no_arg_result_int',
                                   u'type': 'iotoy.org/types/function',
                                   u'value': {u'name': u'no_arg_result_int',
                                              u'spec': {u'args': [],
                                                        u'result': [[u'result',
                                                                     u'int']]}}},
            u'no_arg_result_str': {u'help': u'test, one arg, string',
                                   u'href': u'/no_arg_result_str',
                                   u'type': 'iotoy.org/types/function',
                                   u'value': {u'name': u'no_arg_result_str',
                                              u'spec': {u'args': [],
                                                        u'result': [[u'result',
                                                                     u'str']]}}},
            u'one_arg_T': {u'help': u'test, one arg, generic type',
                           u'href': u'/one_arg_T',
                           u'type': 'iotoy.org/types/function',
                           u'value': {u'name': u'one_arg_T',
                                      u'spec': {u'args': [[u'attr', u'T']],
                                                u'result': []}}},
            u'one_arg_bool': {u'help': u'test, one arg, boolean',
                              u'href': u'/one_arg_bool',
                              u'type': 'iotoy.org/types/function',
                              u'value': {u'name': u'one_arg_bool',
                                         u'spec': {u'args': [[u'myarg',
                                                              u'bool']],
                                                   u'result': []}}},
            u'one_arg_float': {u'help': u'test, one arg, float',
                               u'href': u'/one_arg_float',
                               u'type': 'iotoy.org/types/function',
                               u'value': {u'name': u'one_arg_float',
                                          u'spec': {u'args': [[u'myarg',
                                                               u'float']],
                                                    u'result': []}}},
            u'one_arg_int': {u'help': u'test, one arg, integer',
                             u'href': u'/one_arg_int',
                             u'type': 'iotoy.org/types/function',
                             u'value': {u'name': u'one_arg_int',
                                        u'spec': {u'args': [[u'myarg',
                                                             u'int']],
                                                  u'result': []}}},
            u'one_arg_int_result_int': {u'help': u'test, one arg, one result, both ints',
                                        u'href': u'/one_arg_int_result_int',
                                        u'type': 'iotoy.org/types/function',
                                        u'value': {u'name': u'one_arg_int_result_int',
                                                   u'spec': {u'args': [[u'myarg',
                                                                        u'int']],
                                                             u'result': [[u'result',
                                                                          u'int']]}}},
            u'one_arg_str': {u'help': u'test, one arg, string',
                             u'href': u'/one_arg_str',
                             u'type': 'iotoy.org/types/function',
                             u'value': {u'name': u'one_arg_str',
                                        u'spec': {u'args': [[u'myarg',
                                                             u'str']],
                                                  u'result': []}}}}}

This API is entirely derived from the device itself. Various parts of automation and similar are coming.

Linguistic mapping of REST

The last thing that this project seeks to do is to implement something RPC/SOAP like. Instead, this project works on the following assumptions.

In python:

>>> x = module.SomeThing()
>>> x
<module.SomeThing object at 0xa11c44c>

In this it should be clear that in python you don't actually ever represent with the actual hardware or data - just representations, and interact with them via names. In particular, SomeThing() creates a resource, which has a given representation. We can give that representation a name - x.

From then on, we can do things like:

>>> x.name = "Frank"
>>> x.name
'Frank'

In this case, x.__setattr__("name", "Frank") sets a given name to map to a given representation of bytes. Similarly the second like performs x.__getattr__("name").

From this view, you can have the following mappings:

  • x maps can be viewed as mapping to a web server
  • .name can map to a given resource - eg /names
  • x.__setattr__("name", "Frank") is akin to PUT /name HTTP/1.0 "Frank"
  • x.__getattr__("name") is akin to GET /name HTTP/1.0
  • Perhaps less obviously, module.SomeThing() maps:
    • module to a web server
    • SomeThing to a resource - most obviously /SomeThing
    • module.SomeThing() maps to POST /SomeThing HTTP/1.0 empty body

License

You may use this code under the Apache 2 License. You may additionally use this code under the GPL version 2, but ask for any/all patches back to be provided under the apache 2 license (for simplicity's sake)

Original implementation ©copyright 2013 BBC.

Initial release 2014 by BBC Research & Development

Contact

Michael Sparks

twitter: @sparks_rd Website: http://www.sparkslabs.com/michael/ email: sparks DOT m AT gmail