Introduction
Design as a process involves- Understanding
- observation
- imagination
- analysis
- revision
- visualization
HTTP Request Methods
- GET - Requests data from a specified resource
- POST - Submits data to be processed to a specified resource
The GET Method
When you use get, you send a query string in the URL (exposing your names/values:/test/demo_form.asp?name1=value1&name2=value2
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests have length restrictions
- GET requests should be used only to retrieve data
Using your smartphone
- Download the smartphone app:
- Create an account with Spark
- Connect your Spark Core to your Wi-Fi network by connecting the core to your computer over USB
- Control your Core without writing any code by opening up Tinker on your phone:
- The app consists of 16 pins in vertical rows - 8 analog pins on the left, 8 digital pins on the right. These pins represent the 16 GPIO (General Purpose Input and Output) pins on the Spark Core, and are organized the same way.
To begin, tap any of the pins. A menu will pop up showing the functions that pin has available. Each pin can have up to four possible functions:- digitalWrite: Sets the pin to HIGH or LOW, which either connects it to 3.3V (the maximum voltage of the system) or to GND (ground). Pin D7 is connected to an on-board LED; if you set pin D7 to HIGH, the LED will turn on, and if you set it to LOW, it will turn off.
- analogWrite: Sets the pin to a value between 0 and 255, where 0 is the same as LOW and 255 is the same as HIGH. This is sort of like sending a voltage between 0 and 3.3V, but since this is a digital system, it uses a mechanism called Pulse Width Modulation, or PWM. You could use analogWrite to dim an LED, as an example.
- digitalRead: This will read the digital value of a pin, which can be read as either HIGH or LOW. If you were to connect the pin to 3.3V, it would read HIGH; if you connect it to GND, it would read LOW. Anywhere in between, it’ll probably read whichever one it’s closer to, but it gets dicey in the middle.
- analogRead: This will read the analog value of a pin, which is a value from 0 to 4095, where 0 is LOW (GND) and 4095 is HIGH (3.3V). All of the analog pins (A0 to A7) can handle this. analogRead is great for reading data from sensors.
- You can always get back to it by putting the Core in the factory reset mode
Hold down the MODE button, tap on the RESET button and wait for ten seconds to do a Factory Reset, where the Core is reprogrammed with the software that was installed on the Core in the factory (the Tinker application). The LED should turn white for three seconds and begin flashing quickly; when the LED switches to another color the Core has been reset.
Tinker API
The Tinker API requests can also be made from an application other than Tinker, so you can build your own web or mobile app around the Tinker firmware.digitalWrite
Sets the pin to HIGH or LOW, which either connects it to 3.3V (the maximum voltage of the system) or to GND (ground). Pin D7 is connected to an on-board LED; if you set pin D7 to HIGH, the LED will turn on, and if you set it to LOW, it will turn off.POST /v1/devices/{DEVICE_ID}/digitalwrite # EXAMPLE REQUEST IN TERMINAL # Core ID is 0123456789abcdef01234567 # Your access token is 1234123412341234123412341234123412341234 curl https://api.spark.io/v1/devices/0123456789abcdef01234567/digitalwrite \ -d access_token=1234123412341234123412341234123412341234 -d params=D0,HIGH
analogWrite
Sets the pin to a value between 0 and 255, where 0 is the same as LOW and 255 is the same as HIGH. This is sort of like sending a voltage between 0 and 3.3V, but since this is a digital system, it uses a mechanism called Pulse Width Modulation, or PWM. You could use analogWrite to dim an LED, as an example.POST /v1/devices/{DEVICE_ID}/analogwrite # EXAMPLE REQUEST IN TERMINAL # Core ID is 0123456789abcdef01234567 # Your access token is 1234123412341234123412341234123412341234 curl https://api.spark.io/v1/devices/0123456789abcdef01234567/analogwrite \ -d access_token=1234123412341234123412341234123412341234 -d params=A0,215
digitalRead
This will read the digital value of a pin, which can be read as either HIGH or LOW. If you were to connect the pin to 3.3V, it would read HIGH; if you connect it to GND, it would read LOW. Anywhere in between, it’ll probably read whichever one it’s closer to, but it gets dicey in the middle.POST /v1/devices/{DEVICE_ID}/digitalread # EXAMPLE REQUEST IN TERMINAL # Core ID is 0123456789abcdef01234567 # Your access token is 1234123412341234123412341234123412341234 curl https://api.spark.io/v1/devices/0123456789abcdef01234567/digitalread \ -d access_token=1234123412341234123412341234123412341234 -d params=D0
analogRead
This will read the analog value of a pin, which is a value from 0 to 4095, where 0 is LOW (GND) and 4095 is HIGH (3.3V). All of the analog pins (A0 to A7) can handle this. analogRead is great for reading data from sensors.POST /v1/devices/{DEVICE_ID}/analogread # EXAMPLE REQUEST IN TERMINAL # Core ID is 0123456789abcdef01234567 # Your access token is 1234123412341234123412341234123412341234 curl https://api.spark.io/v1/devices/0123456789abcdef01234567/analogread \ -d access_token=1234123412341234123412341234123412341234 -d params=A0
WEB IDE
Spark Build is an Integrated Development Environment, or IDE; that means that you can do software development in an easy-to-use application that run in your web browser.- The Web IDE can be found here.
- Login
- This is the screen for a new application:
On the top, there are three buttons:- Flash: Flashes the current code to the Spark Core. This initiates an over-the-air firmware update and loads the new software onto your Spark Core.
- Verify: This compiles your code without actually flashing it to the Core; if there are any errors in your code, they will be shown in the debug console on the bottom of the screen.
- Save: Saves any changes you’ve made to your code.
At the bottom, there are four buttons to navigate through the IDE:- Code: Shows a list of your firmware applications and lets you select which one to edit/flash.
- Docs: Brings you to the documentation for Spark.
- Cores: Shows a list of your Spark Cores, so you can choose which to flash, and get more information on each Core.
- Settings: Change your password, log out, or get your access token for API calls.
- Try it out:
int LED = D7; void setup() { pinMode(LED, OUTPUT); } void loop() { digitalWrite(LED, HIGH); delay(250); digitalWrite(LED, LOW); delay(250); }
- You can create programs with multiple files. This works just like in Arduino IDE where the main application is an "Arduino" file, and additional files/libraries are normal C++ source files and header files (which are not run through the pre-processor).
There are a few things to be aware of:- If you create additional files, the UI automatically creates both a header file (.h) and a source file (.cpp), and a #include statement for the header file. However you are free to delete or modify the line of code that we automatically insert.
Claiming Your Core
Once your Core is connected, it needs to be claimed in order to be associated with your account. This is what lets you control your Core and keeps anyone else from doing so. If you use the mobile app to set up your Core, it should claim it automatically. However if you connect your Core over USB, or if the claiming process is unsuccessful, you can claim it manually.- Open Cool Term
- Connect to the Core
- Type the letter i to get the id
You should see something like:# Example Core ID 55ff68064989495329092587
- Open up Spark Build
-
Click the ‘Cores’ icon.
- Click the button that says ‘Add a Core’
- Enter your ID in the text box.
Getting Help
The Spark Core is new, but there is an active community where you can find answers to many of your questions.Check out the Datasheet and the Firmware
Spark Core API
Every Spark Core has a URL, which can be used to GET variables, POST a function call, or PUT new firmware. The variables and functions that you have written in your firmware are exposed as subresources within the Spark Core.- List devices the currently authenticated user has access to.
GET /v1/devices
- Get basic information about the given Core, including the custom variables and functions it has exposed.
GET /v1/devices/{DEVICE_ID}
- Update the Core, including the display name or the firmware (either binary or source).
PUT /v1/devices/{DEVICE_ID}
- Request the current value of a variable exposed by the core
GET /v1/devices/{DEVICE_ID}/{VARIABLE}
- Call a function exposed by the core, with arguments passed in request body
POST /v1/devices/{DEVICE_ID}/{FUNCTION}
- // SYNTAX TO REGISTER A SPARK FUNCTION
Spark.function("funcKey", funcName);
- Spark.sleep() can be used to dramatically improve the battery life of a Spark-powered project by temporarily deactivating the Wi-Fi module, which is by far the biggest power draw.
Spark.sleep() can also be used to put the entire Core into a deep sleep mode.
The Core will automatically wake up and reestablish the WiFi connection after the specified number of seconds.Spark.sleep(int seconds); Spark.sleep(SLEEP_MODE_DEEP, int seconds);
Built in Libraries
Servo
This library allows a Spark Core to control RC (hobby) servo motors. Servos have integrated gears and a shaft that can be precisely controlled. Standard servos allow the shaft to be positioned at various angles, usually between 0 and 180 degrees. Continuous rotation servos allow the rotation of the shaft to be set to various speeds.// EXAMPLE CODE Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created int pos = 0; // variable to store the servo position void setup() { myservo.attach(A0); // attaches the servo on the A0 pin to the servo object } void loop() { for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees { // in steps of 1 degree myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees { myservo.write(pos); // tell servo to go to position in variable 'pos' delay(15); // waits 15ms for the servo to reach the position } }
RGB
This library allows the user to control the RGB LED on the front of the Spark Core.// EXAMPLE CODE // take control of the LED RGB.control(true); // red, green, blue, 0-255 RGB.color(0, 0, 0); // wait one second delay(1000); // resume normal operation RGB.control(false);
Check out some annotated examples