Easily control you Particle (formerly Spark) Device with Ruby.
Assuming at this point you've followed Particle's Getting Started guides and connected your Core with the Particle Cloud.
Head over to the Particle Build IDE. In the Settings tab you can get your Access Token, and you can fetch your Device ID from the Cores tab. You'll need these both to authenticate your API calls, and the Device ID to direct them.
To use this gem, install it with gem install ruby_spark
or add this line to your Gemfile:
gem 'ruby_spark'
and install it with bundle install
Load:
require 'ruby_spark'
Configure:
# config/initializers/ruby_spark.rb
RubySpark.configuration do |config|
config.access_token = "spark_api_access_token"
config.timeout = 10.seconds # defaults to 30 seconds
config.organization = "particle" # if making requests on behalf of an organization
end
To instantiate a Core, you need to pass its device_id
. If you have your access_token
setup ahead of time using the config.access_token
then the second argument is optional.
core = RubySpark::Core.new("core_device_id")
# or
core = RubySpark::Core.new("core_device_id", "spark_api_access_token")
Fire away:
core.info #=> { info hash }
core.variable("something") #=> number (for now)
core.function("foo", "argument") #=> number
The tinker class provides digital_read
, digital_write
, analog_read
, and analog_write
for the default spark core code. This is the same interface as the tinker app.
core = RubySpark::Tinker.new("core_device_id")
# or
core = RubySpark::Tinker.new("core_device_id", "spark_api_access_token")
Fire away:
core.info #=> { info hash }
core.digital_write(3, "HIGH") #=> true or false
core.digital_read(5) #=> "HIGH" or "LOW"
Happily accepting contributions. To contribute, fork, develop, add some specs, and pull request.
Note about the specs. All API requests make use of the VCR gem. To contribute without exposing your Access Token and Core ID, run the specs with your real authentication, and then find-and-replace your Access Token and Core ID with fake values in the spec and any VCR cassettes.