项目作者: adafruit

项目描述 :
A Node.js Adafruit IO Node.js Client, Local Server, & io.adafruit.com TLS Tunnel.
高级语言: JavaScript
项目地址: git://github.com/adafruit/adafruit-io-node.git
创建时间: 2014-12-15T16:46:43Z
项目社区:https://github.com/adafruit/adafruit-io-node

开源协议:MIT License

下载


Adafruit IO Build Status

A Node.js Adafruit IO Client, Local Server, & io.adafruit.com TLS Tunnel.

Installation

Make sure you have Node.js v4.0.0 or higher installed on your computer.

  1. $ node -v
  2. v4.0.0

Install forever and adafruit-io on your computer using npm.

  1. $ npm install -g forever adafruit-io

You can also optionally install forever-service if you are using a Linux distro, and would like to run the server as a service.

  1. $ npm install -g forever-service

Table of Contents

Help

View the available sub commands for adafruit-io:

  1. $ adafruit-io help
  2. Usage: adafruit-io <command>
  3. Commands:
  4. server Adafruit IO local server
  5. client Adafruit IO client
  6. tunnel TLS tunnel to io.adafruit.com
  7. help Show help
  8. version Show version info
  9. completion generate bash completion script

The three main commands available are service, tunnel, and help.

Server

The server included in this package is an open source API compatible version of
Adafruit IO that you can run locally. Future additions to io.adafruit.com will
allow you to sync your local data with the hosted service.

Usage

The tunnel commands will always be prefixed with adafruit-io server, and you can always get more information
about the available commands by running adafruit-io server help.

Configuration

First, you will need to configure the server with a username and key that
you will expect when clients make requests to the server. Let’s look at the help
for adafruit-io server config help.

  1. $ adafruit-io server config help
  2. Usage: adafruit-io server config [options]
  3. Commands:
  4. help Show help
  5. Options:
  6. -p, --port Server port [default: "8080"]
  7. -u, --username Local Adafruit IO Username [required]
  8. -k, --key Local Adafruit IO Key [required]

The --username and --key you set here will be used by the server to authenticate requests from
your devices. You can set them to any value, but make sure the Adafruit IO Key is not easily
guessable.

  1. $ adafruit-io server config --username test_user --key efoih3r8hsfdfh1r31rhsdhf
  2. [info] Server settings saved

To start the server daemon, you can run the following command on the default port of 8080

  1. $ adafruit-io server start
  2. ▄▄
  3. ▄████
  4. ▄███████
  5. █████████▌
  6. ███████████
  7. ████████████▌
  8. ███████████████▄ ████████████▌
  9. █████████████████████▀▀█████ ▄▄▄▄▄▄▄
  10. ▐██████████████████ █████████████████▄▄
  11. ▀█████████ ▀▀███ ██████████████████████
  12. █████████▄▄ ▐████▀ ▐█████████████▀
  13. ▀▀███████████████▄▄█████████████▀
  14. ▄███████ ██ ▀████████████▀
  15. ███████▀ ▄████ ▐█████▄
  16. █████████████████▄▄██████▄
  17. ███████████████████████████
  18. ██████████████ ▐████████████▌
  19. ▐██████████▀▀ ▀███████████▌
  20. █████▀▀ ▀█████████▌
  21. ▀██████
  22. ▀███
  23. ----------------------------------------------------------------------
  24. adafruit io
  25. ----------------------------------------------------------------------
  26. [info] server ready at http://todd.local:8080/
  27. [info] documentation is available at http://todd.local:8080/api/docs

The server will stay up until you shut down your computer, or stop the server.
To stop the server, run the following command:

  1. $ adafruit-io server stop
  2. [info] stopping server...

If you would like to run the server on a different port, you can use the -p option
to set the port you wish to use. Note: As as a general rule processes running
without root privileges cannot bind to ports below 1024. Use a port > 1024, or run
the server using elevated privileges via sudo, but this is not recommended.

TLS Tunnel

This is a TLS/SSL tunnel for securely connecting HTTP and MQTT clients to io.adafruit.com.
Instead of pointing your MQTT or HTTP client at io.adafruit.com, use the IP address
or hostname of the computer you are running the tunnel on.

HTTP

Requests to port 8888 on the Pi will be tunneled to HTTPS port 443 on io.adafruit.com.

MQTT

Connections to port 1883 on the Pi will be tunneled to MQTTS port 8883 on io.adafruit.com.

Security Considerations

The purpose of this service is to add an encryption layer, specifically TLS, to your adafruit IO messages in transit across the Internet. Not all IoT radio modules have a TLS stack and if you sent a message to adafruit.io with that device, it would be unencrypted at the application layer. For example, the message would be encrypted by the radio at the WiFi layer, but would be unencrypted at the Ethernet layer as it went from your router to the Internet.

The danger of unencrypted application messages are two-fold. The first is that your message may be modified in transit but more importantly, your message can be read by any server that routes your traffic. It’s like sending a postcard. If you made a adafruit.io connected garage door, then a lot of people (and machines) would know when the door opened and closed.

Therefore, this service protects your data from prying eyes on the Internet.

However, this service runs as an unauthenticated service on your network. This is by design so that your IoT device can make a connection to it. But it also means that anything on your network can talk to this service. Your adafruit.io credentials are not stored on this gateway service, but on your IoT device. So an attacker would still need to know your adafuit.io credentials to post to adafruit.io.

Lastly, this service does not protect cellular modules. If you have something like a Adafruit FONA, then it makes an Internet connection directly through the cellular system and unless there is a TLS stack on the module, than most likely it’s not end-to-end encrypted.

Usage

The tunnel commands will always be prefixed with adafruit-io tunnel, and you can always get more information
about the available commands by running adafruit-io tunnel help.

  1. $ adafruit-io tunnel help
  2. Usage: adafruit-io tunnel <command> [options]
  3. Commands:
  4. install Install tunnel service (linux only)
  5. remove Remove tunnel service (linux only)
  6. start Start tunnel daemon
  7. restart Restart tunnel daemon
  8. stop Stop tunnel daemon
  9. help Show help
  10. Options:
  11. -h, --http HTTP port [default: "8888"]
  12. -m, --mqtt MQTT port [default: "1883"]

To start the tunnel daemon, you can run the following command on the default port of 8888:

  1. $ adafruit-io tunnel start
  2. ▄▄
  3. ▄████
  4. ▄███████
  5. █████████▌
  6. ███████████
  7. ████████████▌
  8. ███████████████▄ ████████████▌
  9. █████████████████████▀▀█████ ▄▄▄▄▄▄▄
  10. ▐██████████████████ █████████████████▄▄
  11. ▀█████████ ▀▀███ ██████████████████████
  12. █████████▄▄ ▐████▀ ▐█████████████▀
  13. ▀▀███████████████▄▄█████████████▀
  14. ▄███████ ██ ▀████████████▀
  15. ███████▀ ▄████ ▐█████▄
  16. █████████████████▄▄██████▄
  17. ███████████████████████████
  18. ██████████████ ▐████████████▌
  19. ▐██████████▀▀ ▀███████████▌
  20. █████▀▀ ▀█████████▌
  21. ▀██████
  22. ▀███
  23. ----------------------------------------------------------------------
  24. adafruit io
  25. ----------------------------------------------------------------------
  26. [info] io.adafruit.com HTTPS tunnel running: http://todd.local:8888/
  27. [info] io.adafruit.com MQTTS tunnel running: mqtt://todd.local:1883

The tunnel will stay up until you shut down your computer, or stop the tunnel.
To stop the tunnel daemon, run the following command:

  1. $ adafruit-io tunnel stop
  2. [info] stopping tunnel...

If you would like to run the tunnel on a different ports, you can use the --http & --mqtt options
to set the ports you wish to use. Note: As as a general rule processes running
without root privileges cannot bind to ports below 1024. Use a port > 1024, or run
the tunnel using elevated privileges via sudo, but this is not recommended.

Client

A CLI client for use with io.adafruit.com or the local Adafruit IO server included in this package.

Usage

The client commands will always be prefixed with adafruit-io client, and you can append help to any
command to get more info about that command.

Authentication

  1. $ adafruit-io client config help
  2. Usage: adafruit-io client config [options]
  3. Commands:
  4. help Show help
  5. Options:
  6. -h, --host Server hostname [default: "io.adafruit.com"]
  7. -p, --port Server port [default: "80"]
  8. -u, --username Adafruit IO Username [required]
  9. -k, --key Adafruit IO Key [required]

You can then use your Adafruit IO username and key to authenticate yourself.

  1. $ adafruit-io client config --username testing_username --key xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
  2. [info] Client settings saved

Feeds

Feeds are the core of the Adafruit IO system. The feed holds metadata about data that gets pushed, and you will
have one feed for each type of data you send to the system. You can have separate feeds for each
sensor in a project, or you can use one feed to contain JSON encoded data for all of your sensors.

  1. $ adafruit-io client feeds help
  2. Usage: adafruit-io client feeds <action>
  3. Actions:
  4. all All feeds for current user
  5. create Create a new Feed
  6. destroy Delete an existing Feed
  7. get Get feed by ID, Feed key, or Feed Name
  8. update Update properties of an existing Feed
  9. replace Replace an existing Feed
  10. watch Listen for new values
  11. help Show help

Example: List all feeds for your user

  1. $ adafruit-io client feeds all
  2. [info] Success
  3. [ { id: 1000,
  4. key: 'frontdoor',
  5. name: 'frontdoor',
  6. description: '',
  7. unit_type: null,
  8. unit_symbol: null,
  9. last_value: 'open',
  10. status: 'offline',
  11. visibility: 'private',
  12. enabled: true,
  13. license: null,
  14. group_id: null,
  15. created_at: '2015-08-28T17:13:12.516Z',
  16. updated_at: '2015-09-21T17:20:04.630Z' } ]

Some commands will expect paramaters, so you can also view help on each command. Let’s
take a look at the parameters expected by update.

  1. $ adafruit-io client feeds update help
  2. Usage: adafruit-io client feeds update <id> [options]
  3. Parameters:
  4. id ID, key, or name of feed to use
  5. help Show help
  6. Options:
  7. -j, --json JSON output

Example: Update frontdoor feed name to be Front

  1. $ adafruit-io client feeds update frontdoor
  2. ? feed.name (optional): Front
  3. ? feed.key (optional):
  4. ? feed.description (optional):
  5. ? feed.unit_type (optional):
  6. ? feed.unit_symbol (optional):
  7. ? feed.visibility (optional):
  8. ? feed.license (optional):
  9. ? feed.group_id (optional):
  10. [info] Success
  11. { id: 1000,
  12. key: 'frontdoor',
  13. name: 'Front',
  14. description: '',
  15. unit_type: '',
  16. unit_symbol: '',
  17. last_value: 'open',
  18. status: 'offline',
  19. visibility: 'private',
  20. enabled: true,
  21. license: null,
  22. group_id: null,
  23. created_at: '2015-08-28T17:13:12.516Z',
  24. updated_at: '2015-09-21T21:12:37.958Z' }

You can also listen for realtime changes to your feeds by using the watch command.

Example: Listen for changes to the frontdoor feed

  1. $ adafruit-io client feeds watch door
  2. [info] Feeds -> Front
  3. { id: 1000,
  4. key: 'frontdoor',
  5. name: 'Front',
  6. description: '',
  7. unit_type: '',
  8. unit_symbol: '',
  9. last_value: 'closed',
  10. status: 'offline',
  11. visibility: 'private',
  12. user_id: 5,
  13. created_at: '2015-08-28T17:13:12.516Z',
  14. updated_at: '2015-09-21T21:12:37.958Z',
  15. mode: null,
  16. enabled: true,
  17. fixed: false,
  18. last_value_at: '2015-09-21T15:10:34.048Z',
  19. license: null,
  20. group_id: null,
  21. feed_alias: null,
  22. recorded: 'just now',
  23. last_geo: { lat: null, lon: null, ele: null }
  24. }

All operations can also output raw JSON so they can be piped to tools like jq
for processing incoming data.

Example: Get all feeds as JSON, and pipe the output to jq
to extract the feed names.

  1. $ adafruit-io client feeds all --json | jq '.[] | .name' --raw-output
  2. Front
  3. temperature
  4. battery
  5. fan
  6. door

Example: Watch the door feed for changes, output new values as JSON,
and pipe the output to jq.

  1. $ adafruit-io client feeds watch door --json | jq .last_value --raw-output
  2. open
  3. closed
  4. open

Data

Data represents the actual stored data sent to Adafruit IO feeds.

  1. $ adafruit-io client data help
  2. Usage: adafruit-io client data <action>
  3. Actions:
  4. all All data for current feed
  5. create Create new Data
  6. send Create new Data and Feed
  7. receive Receive data?
  8. previous Previous Data in Queue
  9. next Next Data in Queue
  10. last Last Data in Queue
  11. destroy Delete existing Data
  12. get Returns data based on ID
  13. update Update properties of existing Data
  14. replace Replace existing Data
  15. watch Listen for new values
  16. help Show help

Let’s look at the all operation as an example.

  1. $ adafruit-io client data all help
  2. Usage: adafruit-io client data all <feed_id>
  3. Parameters:
  4. feed_id ID, key, or name of feed
  5. help Show help

Example: View all feed data

  1. $ adafruit-io client data all door
  2. [info] Success
  3. [ { id: 122222,
  4. value: 'open',
  5. position: null,
  6. feed_id: 1000,
  7. group_id: null,
  8. expiration: null,
  9. lat: null,
  10. lon: null,
  11. ele: null,
  12. completed_at: null,
  13. created_at: '2015-09-21T15:10:34.048Z',
  14. updated_at: '2015-09-21T15:10:34.048Z',
  15. created_epoch: 1442848234.04861 } ]

Groups

Groups allow you to update and retrieve multiple feeds with one request.

  1. $ adafruit-io client groups help
  2. Usage: adafruit-io client groups <action>
  3. Actions:
  4. all All groups for current user
  5. create Create a new Group
  6. destroy Delete an existing Group
  7. get Returns Group based on ID
  8. update Update properties of an existing Group
  9. replace Replace an existing Group
  10. watch Listen for new values
  11. help Show help

Example: List all groups for your user

  1. $ adafruit-io client groups all
  2. [info] Success
  3. [ { id: 5,
  4. name: 'weather',
  5. key: 'weather',
  6. description: null,
  7. source: null,
  8. properties: null,
  9. source_keys: null,
  10. created_at: '2015-07-14T22:41:48.898Z',
  11. updated_at: '2015-07-14T22:41:48.898Z',
  12. feeds: [ [Object], [Object] ] } ]

Example: Get the weather group as JSON and parse the feed names with jq

  1. $ adafruit-io client groups get weather --json | jq '.feeds[] | .name' --raw-output
  2. humidity
  3. temp

License

Copyright (c) 2015 Adafruit Industries. Licensed under the MIT license.

Adafruit invests time and resources providing this open source code. Please support Adafruit and open-source hardware by purchasing products from Adafruit.