Page 41 - Programming the Photon Getting Started With the Internet of Things
P. 41
-d access_token=123412341234 -d params=D0
The parameter set must be the pin number—either A0 to A5 or D0 to D7; the return value
will either be a 1 or −1.
AnalogRead will read an analog value of the pins labeled A0 to A5, which can be a
value between 0 and 4095 where 0 is LOW and 4095 is HIGH. Only the analog pins can
handle these values. Typically analog pins are used to read values from different types of
sensors. The API request is as follows:
POST /v1/devices/{DEVICE_ID}/analogread
# EXAMPLE REQUEST IN TERMINAL
# Core ID is 0123456789abcdef
# Your access token is 123412341234
curl https://api.spark.io/v1/devices/0123456789abcdef/analogread \
-d access_token=123412341234 -d params=A0
The return value will be between 0 and 4095 if successful and −1 if it fails to read the
value.
Running Tinker Alongside Your Scripts
If you have already played around with running your own firmware programs on the
Photon, then you soon realized that you cannot use the Tinker app while your code is
running. Well, now you can. Combine the following code with your firmware and flash it
to your Photon and you will be able to run your program while tinkering away:
int tinkerDigitalRead(String pin);
int tinkerDigitalWrite(String command);
int tinkerAnalogRead(String pin);
int tinkerAnalogWrite(String command);
//PUT YOUR VARIABLES HERE
void setup()
{
Spark.function("digitalread", tinkerDigitalRead);
Spark.function("digitalwrite", tinkerDigitalWrite);
Spark.function("analogread", tinkerAnalogRead);
Spark.function("analogwrite", tinkerAnalogWrite);
//PUT YOUR SETUP CODE HERE