You are on page 1of 20

Program the Photon to allow the user to read

temperature and humidity readings from sensors


connected to A0 and A1 over the internet. You
should also be able to control the LED connected
to D7 and an external LED connected to A6 over
the internet. D7 should receive “on” or “off”
commands, while A6 LED receives an int value
[0-4095] that controls the intensity. The system
diagram is shown in Figure 1.
Local user can access, monitor and control
inputs and outputs via the terminal window.
Remote client can can access, monitor and
control inputs and outputs via the terminal
window.

The system diagram is shown in Figure 1.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 1
Particle and Google Cloud Platform Integration
Taken as is from the link below:
https://docs.particle.io/tutorials/integrations/google-cloud-platform/

• Particle has teamed up with Google to create a 1st-class integration with Google Cloud Platform.
• Google Cloud Platform enables developers to build, test, and deploy applications on Google’s highly
scalable and reliable infrastructure.

Send device data from Particle into Google Cloud Platform with ease

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 2
HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 3
HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 4
https://cloud.google.com/products/calculator/

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 5
HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 6
Particle Variables & Functions
• As a part of supporting Internet of Things
applications, Particle allows users to register
Particle “variables” and Particle “functions” which Particle Server:
makes it possible to securely access and control In the Cloud
photons via HTTP requests.
• This also enables users to interface the Photon with
other platforms that support HTTP communication
such as Raspberry Pi interface with the photon.
• Send Photon’s available variables and functions to
Particle server through WiFi.
• Particle server registers Photon’s information Such
as the ID address & Access Token
• User queries the server for Photon’s variables, or
controls Photon’s output through HTTP requests Photon
such get or post using the URL + ID + Access Token
of the photon

7
Positing your Variables & Functions in the Particle Database

Device ID: aaaabbbbbbccccc


Variables: temp=24.5C, hum=50
Particle Server

Device ID: aaaabbbbbbccccc

Particle Server

Your variables are stored in the Server Database for Photon-2


Server Database for Photon-1
particle Server Database Device ID: aaaabbbbbbccccc
Device ID: xxxyyyyyyyzzzzzz
Variables: temp=30C, gas=200
Variables: temp=24.5C, hum=50
Functions: control_led(command)

8
Two ways to get the Particle Variables:
Your password
1. Via Remote Client Browser
Give me Variable temp from device ID aaaabbbbbbccccc &password
Example: URL
https://api.particle.io/v1/devices/aaaabbbbcccc/temp?access_token=your_access_token

Particle Server

Device ID: xxxyyyyyyyzzzzzz


Device ID: aaaabbbbbbccccc
Variables: temp=30C, gas=200
Variables: temp=24.5C, hum=50
Functions: control_led(command)

2. Via Terminal Window on User’s PC/Laptop.


Using Curl on Terminal Window of Client device: Type the following on Terminal Window:
curl https://api.particle.io/v1/devices/aaaabbbbcccc/temp?access_token=your_access_token

9
Posting Function command to the photon via the particle server
Post user command to make an action such act Turn On “ON” the LED on the photon via the particle server
Example on Web Browser on Client “ URL”
https://api.particle.io/v1/devices/xxxyyyyyyyzzzzzz/LED?access_token=your_access_token params=ON
Example on Terminal Window using Curl:
curl --data “params=on” https://api.particle.io/v1/devices/device_id/led?access_token=access_token
You expect to get acknowledgment like ok
Device ID: xxxyyyyyyyzzzzzz
Variables: temp=30C, gas=200
Functions: control_led(command)
Particle Server

Device ID: aaaabbbbbbccccc


Variables: temp=24.5C, hum=50

1. Via Remote Client Browser


Give me Variable: temp from device ID aaaabbbbbbccccc &password
Example: URL https://api.particle.io/v1/devices/aaaabbbbcccc/temp?access_token=your_access_token
2. Via Terminal Window on User’s PC/Laptop.
Using Curl on Terminal Window of Client device: Type the following on Terminal Window:
curl https://api.particle.io/v1/devices/aaaabbbbcccc/temp?access_token=your_access_token

10
HTTP Requests
• The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients and servers.
• HTTP works as a request-response protocol between a client and server. A web browser may be the client, and
an application on a computer that hosts a web site may be the server.
• Two commonly used methods for a request-response between a client and server are:
• GET - Requests data from a specified resource (e.g. request a webpage)
• Eg: To get the value of Temperature from remote Client web Browser:
https://api.particle.io/v1/devices/aaaabbbbcccc/temp?access_token=your_access_token
• POST - Submits data to be processed to a specified resource (e.g. submit an online survey)
• Eg: To Call a function we POST the value of Parameter Value such as ON/OFF to my Particle Server which
will physically affect the status of the GPIO on the Particle.
• https://api.particle.io/v1/devices/xxxyyyyyyyzzzzzz/LED?access_token=your_access_token params=ON
• The main difference between a GET request and a POST request is that the POST has data in message body i.e.
it has Parameters or Params in the URL, while the GET’s message body is empty.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
11
CuRL from the Terminal Window
• CuRL is a tool and a library (usable from many
languages) for client-side URL transfers,
supporting FTP, FTPS, HTTP, HTTPS, etc... .
• It can be accessed from the command prompt.
• For example, typing “curl
http://www.example.com” sends a GET request
for the homepage of an example website; to
which the response will be the HTML code of that
page.
• CuRL can also be used to send POST requests by
including data with the request.
• For example, typing “curl –d “hello world”
http://www.example.com” sends a POST request
to example.com with “hello world” in the
message body.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
12
Register a Particle Variable to get its value from remote client

• The format to register a particle variable:


Particle.variable(variable_online, local_variable);
variable_online: eg: temperature
the name of the variable at Particle’s server
local_variable: eg: temp_value Register the variable
the name given to the variable in your code Value of A0
it is possible to have the same name for both i.e. the temperature sensor

• Example:
Via ADC A0

Particle.variable("temperature", temp_value);

Update variable value

Convert the value into:


Volt
Then to C

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
13
• Connect the device, flash it and connect it then go to DOC
• Go to Reference tab and on the Firmware section select Particle.variable() option to get the
syntax for the CURL (Terminal Window) or remote web browser on client device.

Syntax (Generic):
curl "https://api.particle.io/v1/devices/MyDeviceID/MyVariable?access_token=MyAccessToken"
Example:
curl https://api.particle.io/v1/devices/0123456789abcdef/temperature?access_token=123412341234
You must change the device ID , variable name and the access token to access your own device.

14
GETting a Variable on the terminal Window: CuRL

• It is also possible to retrieve the registered variable by issuing a GET request through CuRL.
• By typing “curl https://api.particle.io/v1/devices/device_id/temperature?access_token=access_token”

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 15
GETting a Variable: Remote Client web Browser
• To get the value of a device variable from Particle’s server, the user sends a GET request which includes
the device ID, access token, and variable name.
• One way to do so is to type the following URL into a browser
• https://api.particle.io/v1/devices/device_id/temperature?access_token=access_token
• Particle server replies with information about the device, including information such as the current
variable value, last time the value was updated, and whether or not the device is currently online.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
16
Register a Particle Function

• The format to register a particle function:


Particle.function(function_online, local_function); Register the function
function_online: eg. led
the name of the function at Particle’s server
local_function: eg. ledToggle On the client browser
the name given to the function in your code or Curl on the
it is possible to have the same name for both terminal Window
• Example: Define what the function
Particle.function("led", ledToggle); In the code does

• Particle.functions always take a String as an argument or


paramter and return an integer. In this case, telling the function
"on" will turn the LED on and telling it "off" will turn the LED
off.
• Then, the function returns a value to us to let us know what
happened. In this case, it will return 1 for the LEDs turning on, 0
for the LEDs turning off, and -1 if we received an invalid
command.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
17
POSTing to a Function: CuRL
• POSTing to a Particle function (i.e. controlling a Photon online) can be easily done through CuRL by issuing a
POST request to the previously mentioned URL, but including the command.
• Recall that a Particle function takes a string command (input parameter) and returns an integer value.
• This is done by typing
“curl --data “params=on” https://api.particle.io/v1/devices/device_id/led?access_token=access_token”
Where params=on means that the value of the command input parameter defined in the Photon code is “on”.

You will see the function


output on the actual board
where the LED is ON/OFF.
Can check on browser from
the return value of the
function

OR:
curl https://api.particle.io/v1/devices/0123456789abcdef/led \-d access_token=123412341234 \-d "args=on"

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 18
Function Parameters Return Description

pinMode(pin, mode) pin: pin number you want to use None Configure digital pin to behave as
mode: INPUT, OUTPUT, an input or output
e.g. pinMode(D7,OUTPUT) INPUT_PULLUP
delay(ms) ms: an integer number of None Pauses program execution for
microseconds (ms) number of milliseconds
e.g. delay(500)
digitalWrite(pin, value) pin: pin number None If the pin has been configured as
value: HIGH=(2.4 - 3.3V) an output, its voltage will be set
e.g. digitalWrite(D7,HIGH) LOW=(0V - 0.5V) to the corresponding value
digitalRead(pin) pin: pin number int Reads the value from a specified
1/HIGH=(2-3.3V) digital pin that has been
e.g. digitalRead(D0) 0/LOW=(0-0.8V)
configured as input
analogWrite(pin, value) pin: pin number None Writes an analog output value
value: the duty cycle: between 0 (PWM wave) to a pin
e.g. analogWrite(DAC1,815) (always off) and 4095 (always on)
analogRead(pin) Pin: pin number int (0 to 4095) Reads the input value from the
specified analog pin
e.g. analogRead(A0)
Particle.variable(OL_var, LOC_var); OL_var: name of the variable as None Registers a particle variable at
registered at Particle’s server the particle server
e.g. Particle.variable("temperature",temp_value); LOC_var: name of the function in your
code
Particle.function(OL_fun, LOC_fun); OL_fun: name of the variable as int Registers a particle function at
registered at Particle’s server Can be used as status the particle server
e.g. Particle.function("led",ledToggle); LOC_fun: name of the function in code to indicate whether
your code the function execution
succeeded or failed
HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/ 19
Glossary
SMPS
Switched-Mode Power Supply
RF
Radio Frequency
SMT
Surface Mount Technology (often associated with SMD which is a surface mount device).
AP
Access Point
LED
Light-Emitting Diode
RGB LED
Red green and blue LEDs combined and diffused in one package.
USB
Universal Serial Bus
3V3
+3.3V; The regulated +3.3V supply rail. Also used to note a pin is only 3.3V tolerant.
RTC
Real Time Clock
OTA
Over The Air; describing how firmware is transferred to the device.

HTTPS://DOCS.PARTICLE.IO/DATASHEETS/PHOTON-DATASHEET/
20

You might also like